mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-31 01:42:02 +00:00
Plugins - Added removal of plugins
Plugins - Added updating plugins at runtime
This commit is contained in:
parent
af4bf79936
commit
3c29c3aeeb
@ -178,12 +178,15 @@ namespace Artemis.Core
|
|||||||
{
|
{
|
||||||
foreach (PluginFeature feature in Features)
|
foreach (PluginFeature feature in Features)
|
||||||
feature.Dispose();
|
feature.Dispose();
|
||||||
|
SetEnabled(false);
|
||||||
|
|
||||||
Kernel?.Dispose();
|
Kernel?.Dispose();
|
||||||
PluginLoader?.Dispose();
|
PluginLoader?.Dispose();
|
||||||
|
|
||||||
|
GC.Collect();
|
||||||
|
GC.WaitForPendingFinalizers();
|
||||||
|
|
||||||
_features.Clear();
|
_features.Clear();
|
||||||
SetEnabled(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -70,6 +70,12 @@ namespace Artemis.Core.Services
|
|||||||
/// <returns>The resulting plugin</returns>
|
/// <returns>The resulting plugin</returns>
|
||||||
Plugin ImportPlugin(string fileName);
|
Plugin ImportPlugin(string fileName);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Unloads and permanently removes the provided plugin
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="plugin">The plugin to remove</param>
|
||||||
|
void RemovePlugin(Plugin plugin);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Enables the provided plugin feature
|
/// Enables the provided plugin feature
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -5,6 +5,7 @@ using System.IO;
|
|||||||
using System.IO.Compression;
|
using System.IO.Compression;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Runtime.Loader;
|
||||||
using Artemis.Core.DeviceProviders;
|
using Artemis.Core.DeviceProviders;
|
||||||
using Artemis.Core.Ninject;
|
using Artemis.Core.Ninject;
|
||||||
using Artemis.Storage.Entities.Plugins;
|
using Artemis.Storage.Entities.Plugins;
|
||||||
@ -275,6 +276,7 @@ namespace Artemis.Core.Services
|
|||||||
plugin.PluginLoader = PluginLoader.CreateFromAssemblyFile(mainFile!, configure =>
|
plugin.PluginLoader = PluginLoader.CreateFromAssemblyFile(mainFile!, configure =>
|
||||||
{
|
{
|
||||||
configure.IsUnloadable = true;
|
configure.IsUnloadable = true;
|
||||||
|
configure.LoadInMemory = true;
|
||||||
configure.PreferSharedTypes = true;
|
configure.PreferSharedTypes = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -430,7 +432,6 @@ namespace Artemis.Core.Services
|
|||||||
OnPluginDisabled(new PluginEventArgs(plugin));
|
OnPluginDisabled(new PluginEventArgs(plugin));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public Plugin ImportPlugin(string fileName)
|
public Plugin ImportPlugin(string fileName)
|
||||||
{
|
{
|
||||||
DirectoryInfo pluginDirectory = new(Path.Combine(Constants.DataFolder, "plugins"));
|
DirectoryInfo pluginDirectory = new(Path.Combine(Constants.DataFolder, "plugins"));
|
||||||
@ -449,7 +450,16 @@ namespace Artemis.Core.Services
|
|||||||
|
|
||||||
Plugin? existing = _plugins.FirstOrDefault(p => p.Guid == pluginInfo.Guid);
|
Plugin? existing = _plugins.FirstOrDefault(p => p.Guid == pluginInfo.Guid);
|
||||||
if (existing != null)
|
if (existing != null)
|
||||||
throw new ArtemisPluginException($"A plugin with the same GUID is already loaded: {existing.Info}");
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
RemovePlugin(existing);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
throw new ArtemisPluginException("A plugin with the same GUID is already loaded, failed to remove old version", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
string targetDirectory = pluginInfo.Main.Split(".dll")[0].Replace("/", "").Replace("\\", "");
|
string targetDirectory = pluginInfo.Main.Split(".dll")[0].Replace("/", "").Replace("\\", "");
|
||||||
string uniqueTargetDirectory = targetDirectory;
|
string uniqueTargetDirectory = targetDirectory;
|
||||||
@ -464,19 +474,38 @@ namespace Artemis.Core.Services
|
|||||||
|
|
||||||
// Extract everything in the same archive directory to the unique plugin directory
|
// Extract everything in the same archive directory to the unique plugin directory
|
||||||
DirectoryInfo directoryInfo = new(Path.Combine(pluginDirectory.FullName, uniqueTargetDirectory));
|
DirectoryInfo directoryInfo = new(Path.Combine(pluginDirectory.FullName, uniqueTargetDirectory));
|
||||||
Directory.CreateDirectory(directoryInfo.FullName);
|
Utilities.CreateAccessibleDirectory(directoryInfo.FullName);
|
||||||
string metaDataDirectory = metaDataFileEntry.FullName.Replace(metaDataFileEntry.Name, "");
|
string metaDataDirectory = metaDataFileEntry.FullName.Replace(metaDataFileEntry.Name, "");
|
||||||
foreach (ZipArchiveEntry zipArchiveEntry in archive.Entries)
|
foreach (ZipArchiveEntry zipArchiveEntry in archive.Entries)
|
||||||
|
{
|
||||||
if (zipArchiveEntry.FullName.StartsWith(metaDataDirectory))
|
if (zipArchiveEntry.FullName.StartsWith(metaDataDirectory))
|
||||||
{
|
{
|
||||||
string target = Path.Combine(directoryInfo.FullName, zipArchiveEntry.FullName.Remove(0, metaDataDirectory.Length));
|
string target = Path.Combine(directoryInfo.FullName, zipArchiveEntry.FullName.Remove(0, metaDataDirectory.Length));
|
||||||
zipArchiveEntry.ExtractToFile(target);
|
// Create folders
|
||||||
|
if (zipArchiveEntry.FullName.EndsWith("/"))
|
||||||
|
Utilities.CreateAccessibleDirectory(Path.GetDirectoryName(target)!);
|
||||||
|
// Extract files
|
||||||
|
else
|
||||||
|
zipArchiveEntry.ExtractToFile(target);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Load the newly extracted plugin and return the result
|
// Load the newly extracted plugin and return the result
|
||||||
return LoadPlugin(directoryInfo);
|
return LoadPlugin(directoryInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void RemovePlugin(Plugin plugin)
|
||||||
|
{
|
||||||
|
DirectoryInfo directory = plugin.Directory;
|
||||||
|
lock (_plugins)
|
||||||
|
{
|
||||||
|
if (_plugins.Contains(plugin))
|
||||||
|
UnloadPlugin(plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
directory.Delete(true);
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Features
|
#region Features
|
||||||
|
|||||||
@ -59,12 +59,7 @@ namespace Artemis.UI.Screens.Settings.Tabs.Plugins
|
|||||||
{
|
{
|
||||||
Items.Clear();
|
Items.Clear();
|
||||||
await Task.Delay(200);
|
await Task.Delay(200);
|
||||||
_instances = _pluginManagementService.GetAllPlugins()
|
GetPluginInstances();
|
||||||
.Select(p => _settingsVmFactory.CreatePluginSettingsViewModel(p))
|
|
||||||
.OrderBy(i => i.Plugin.Info.Name)
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
UpdatePluginSearch();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
base.OnActivate();
|
base.OnActivate();
|
||||||
@ -80,14 +75,21 @@ namespace Artemis.UI.Screens.Settings.Tabs.Plugins
|
|||||||
{
|
{
|
||||||
Plugin plugin = _pluginManagementService.ImportPlugin(dialog.FileName);
|
Plugin plugin = _pluginManagementService.ImportPlugin(dialog.FileName);
|
||||||
|
|
||||||
_instances = _pluginManagementService.GetAllPlugins()
|
GetPluginInstances();
|
||||||
.Select(p => _settingsVmFactory.CreatePluginSettingsViewModel(p))
|
|
||||||
.OrderBy(i => i.Plugin.Info.Name)
|
|
||||||
.ToList();
|
|
||||||
SearchPluginInput = plugin.Info.Name;
|
SearchPluginInput = plugin.Info.Name;
|
||||||
|
|
||||||
_messageService.ShowMessage($"Imported plugin: {plugin.Info.Name}");
|
_messageService.ShowMessage($"Imported plugin: {plugin.Info.Name}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void GetPluginInstances()
|
||||||
|
{
|
||||||
|
_instances = _pluginManagementService.GetAllPlugins()
|
||||||
|
.Select(p => _settingsVmFactory.CreatePluginSettingsViewModel(p))
|
||||||
|
.OrderBy(i => i.Plugin.Info.Name)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
UpdatePluginSearch();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -53,15 +53,28 @@
|
|||||||
</Grid>
|
</Grid>
|
||||||
|
|
||||||
|
|
||||||
<Button Grid.Row="1"
|
<StackPanel Grid.Row="1"
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
|
VerticalAlignment="Bottom"
|
||||||
|
Orientation="Horizontal">
|
||||||
|
<Button
|
||||||
VerticalAlignment="Bottom"
|
VerticalAlignment="Bottom"
|
||||||
Style="{StaticResource MaterialDesignOutlinedButton}"
|
Style="{StaticResource MaterialDesignOutlinedButton}"
|
||||||
ToolTip="Open the plugins settings window"
|
ToolTip="Open the plugins settings window"
|
||||||
Margin="4"
|
Margin="4"
|
||||||
Command="{s:Action OpenSettings}">
|
Command="{s:Action OpenSettings}">
|
||||||
SETTINGS
|
SETTINGS
|
||||||
</Button>
|
</Button>
|
||||||
|
<Button
|
||||||
|
VerticalAlignment="Bottom"
|
||||||
|
Style="{StaticResource MaterialDesignOutlinedButton}"
|
||||||
|
ToolTip="Remove plugin"
|
||||||
|
Margin="4"
|
||||||
|
Command="{s:Action Remove}">
|
||||||
|
<materialDesign:PackIcon Kind="DeleteForever" />
|
||||||
|
</Button>
|
||||||
|
</StackPanel>
|
||||||
|
|
||||||
|
|
||||||
<CheckBox Grid.Row="1"
|
<CheckBox Grid.Row="1"
|
||||||
Grid.Column="1"
|
Grid.Column="1"
|
||||||
@ -70,7 +83,13 @@
|
|||||||
Margin="8"
|
Margin="8"
|
||||||
Visibility="{Binding Enabling, Converter={x:Static s:BoolToVisibilityConverter.InverseInstance}, Mode=OneWay}"
|
Visibility="{Binding Enabling, Converter={x:Static s:BoolToVisibilityConverter.InverseInstance}, Mode=OneWay}"
|
||||||
Style="{StaticResource MaterialDesignCheckBox}" IsChecked="{Binding IsEnabled}">
|
Style="{StaticResource MaterialDesignCheckBox}" IsChecked="{Binding IsEnabled}">
|
||||||
Plugin enabled
|
<StackPanel Orientation="Horizontal">
|
||||||
|
<TextBlock>Plugin enabled</TextBlock>
|
||||||
|
<materialDesign:PackIcon Kind="ShieldHalfFull"
|
||||||
|
Margin="5 0 0 0"
|
||||||
|
ToolTip="Plugin requires admin rights"
|
||||||
|
Visibility="{Binding Plugin.Info.RequiresAdmin, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}"/>
|
||||||
|
</StackPanel>
|
||||||
</CheckBox>
|
</CheckBox>
|
||||||
|
|
||||||
<ProgressBar Grid.Row="1"
|
<ProgressBar Grid.Row="1"
|
||||||
|
|||||||
@ -19,6 +19,7 @@ namespace Artemis.UI.Screens.Settings.Tabs.Plugins
|
|||||||
private readonly IDialogService _dialogService;
|
private readonly IDialogService _dialogService;
|
||||||
private readonly IPluginManagementService _pluginManagementService;
|
private readonly IPluginManagementService _pluginManagementService;
|
||||||
private readonly ISettingsVmFactory _settingsVmFactory;
|
private readonly ISettingsVmFactory _settingsVmFactory;
|
||||||
|
private readonly ICoreService _coreService;
|
||||||
private readonly IMessageService _messageService;
|
private readonly IMessageService _messageService;
|
||||||
private readonly IWindowManager _windowManager;
|
private readonly IWindowManager _windowManager;
|
||||||
private bool _enabling;
|
private bool _enabling;
|
||||||
@ -26,6 +27,7 @@ namespace Artemis.UI.Screens.Settings.Tabs.Plugins
|
|||||||
|
|
||||||
public PluginSettingsViewModel(Plugin plugin,
|
public PluginSettingsViewModel(Plugin plugin,
|
||||||
ISettingsVmFactory settingsVmFactory,
|
ISettingsVmFactory settingsVmFactory,
|
||||||
|
ICoreService coreService,
|
||||||
IWindowManager windowManager,
|
IWindowManager windowManager,
|
||||||
IDialogService dialogService,
|
IDialogService dialogService,
|
||||||
IPluginManagementService pluginManagementService,
|
IPluginManagementService pluginManagementService,
|
||||||
@ -34,6 +36,7 @@ namespace Artemis.UI.Screens.Settings.Tabs.Plugins
|
|||||||
Plugin = plugin;
|
Plugin = plugin;
|
||||||
|
|
||||||
_settingsVmFactory = settingsVmFactory;
|
_settingsVmFactory = settingsVmFactory;
|
||||||
|
_coreService = coreService;
|
||||||
_windowManager = windowManager;
|
_windowManager = windowManager;
|
||||||
_dialogService = dialogService;
|
_dialogService = dialogService;
|
||||||
_pluginManagementService = pluginManagementService;
|
_pluginManagementService = pluginManagementService;
|
||||||
@ -82,6 +85,24 @@ namespace Artemis.UI.Screens.Settings.Tabs.Plugins
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task Remove()
|
||||||
|
{
|
||||||
|
bool confirmed = await _dialogService.ShowConfirmDialog("Delete plugin", "Are you sure you want to delete this plugin?");
|
||||||
|
if (!confirmed)
|
||||||
|
return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_pluginManagementService.RemovePlugin(Plugin);
|
||||||
|
((PluginSettingsTabViewModel) Parent).GetPluginInstances();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_dialogService.ShowExceptionDialog("Failed to remove plugin", e);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void ShowLogsFolder()
|
public void ShowLogsFolder()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -124,6 +145,18 @@ namespace Artemis.UI.Screens.Settings.Tabs.Plugins
|
|||||||
{
|
{
|
||||||
Enabling = true;
|
Enabling = true;
|
||||||
|
|
||||||
|
if (Plugin.Info.RequiresAdmin && !_coreService.IsElevated)
|
||||||
|
{
|
||||||
|
bool confirmed = await _dialogService.ShowConfirmDialog("Enable plugin", "This plugin requires admin rights, are you sure you want to enable it?");
|
||||||
|
if (!confirmed)
|
||||||
|
{
|
||||||
|
Enabling = false;
|
||||||
|
NotifyOfPropertyChange(nameof(IsEnabled));
|
||||||
|
NotifyOfPropertyChange(nameof(CanOpenSettings));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await Task.Run(() => _pluginManagementService.EnablePlugin(Plugin, true));
|
await Task.Run(() => _pluginManagementService.EnablePlugin(Plugin, true));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user