From 73a80ef47696be9040996baf0e7c34d9334df07f Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 2 Aug 2022 21:17:53 +0200 Subject: [PATCH] Plugins - Don't load incompatible plugins --- .../Interfaces/IPluginManagementService.cs | 4 ++-- .../Services/PluginManagementService.cs | 7 +++++-- .../Screens/Plugins/PluginSettingsViewModel.cs | 15 +++++++++------ .../Screens/Settings/Tabs/PluginsTabViewModel.cs | 8 +++++++- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/Artemis.Core/Services/Interfaces/IPluginManagementService.cs b/src/Artemis.Core/Services/Interfaces/IPluginManagementService.cs index ddd61d6cd..337f0dbd8 100644 --- a/src/Artemis.Core/Services/Interfaces/IPluginManagementService.cs +++ b/src/Artemis.Core/Services/Interfaces/IPluginManagementService.cs @@ -37,7 +37,7 @@ namespace Artemis.Core.Services /// Loads the plugin located in the provided /// /// The directory where the plugin is located - Plugin LoadPlugin(DirectoryInfo directory); + Plugin? LoadPlugin(DirectoryInfo directory); /// /// Enables the provided @@ -68,7 +68,7 @@ namespace Artemis.Core.Services /// /// The full path to the ZIP file that contains the plugin /// The resulting plugin - Plugin ImportPlugin(string fileName); + Plugin? ImportPlugin(string fileName); /// /// Unloads and permanently removes the provided plugin diff --git a/src/Artemis.Core/Services/PluginManagementService.cs b/src/Artemis.Core/Services/PluginManagementService.cs index 782fa1108..00f01d3ab 100644 --- a/src/Artemis.Core/Services/PluginManagementService.cs +++ b/src/Artemis.Core/Services/PluginManagementService.cs @@ -292,7 +292,7 @@ namespace Artemis.Core.Services } } - public Plugin LoadPlugin(DirectoryInfo directory) + public Plugin? LoadPlugin(DirectoryInfo directory) { _logger.Verbose("Loading plugin from {directory}", directory.FullName); @@ -304,6 +304,9 @@ namespace Artemis.Core.Services // PluginInfo contains the ID which we need to move on PluginInfo pluginInfo = CoreJson.DeserializeObject(File.ReadAllText(metadataFile))!; + if (!pluginInfo.IsCompatible) + return null; + if (pluginInfo.Guid == Constants.CorePluginInfo.Guid) throw new ArtemisPluginException($"Plugin {pluginInfo} cannot use reserved GUID {pluginInfo.Guid}"); @@ -531,7 +534,7 @@ namespace Artemis.Core.Services OnPluginDisabled(new PluginEventArgs(plugin)); } - public Plugin ImportPlugin(string fileName) + public Plugin? ImportPlugin(string fileName) { DirectoryInfo pluginDirectory = new(Constants.PluginsFolder); diff --git a/src/Artemis.UI/Screens/Plugins/PluginSettingsViewModel.cs b/src/Artemis.UI/Screens/Plugins/PluginSettingsViewModel.cs index 6a7c6c9fb..932961620 100644 --- a/src/Artemis.UI/Screens/Plugins/PluginSettingsViewModel.cs +++ b/src/Artemis.UI/Screens/Plugins/PluginSettingsViewModel.cs @@ -13,7 +13,7 @@ namespace Artemis.UI.Screens.Plugins; public class PluginSettingsViewModel : ActivatableViewModelBase { - private Plugin _plugin; + private readonly Plugin _plugin; private readonly IPluginManagementService _pluginManagementService; private readonly INotificationService _notificationService; @@ -41,10 +41,13 @@ public class PluginSettingsViewModel : ActivatableViewModelBase bool wasEnabled = _plugin.IsEnabled; await Task.Run(() => _pluginManagementService.UnloadPlugin(_plugin)); - _plugin = _pluginManagementService.LoadPlugin(_plugin.Directory); - if (wasEnabled) - await Task.Run(() => _pluginManagementService.EnablePlugin(_plugin, true, true)); - - _notificationService.CreateNotification().WithTitle("Reloaded plugin.").Show(); + Plugin? plugin = _pluginManagementService.LoadPlugin(_plugin.Directory); + if (plugin != null && wasEnabled) + { + await Task.Run(() => _pluginManagementService.EnablePlugin(plugin, true, true)); + _notificationService.CreateNotification().WithTitle("Reloaded plugin.").Show(); + } + else if (plugin == null) + _notificationService.CreateNotification().WithTitle("Failed to load plugin after unloading it.").Show(); } } \ No newline at end of file diff --git a/src/Artemis.UI/Screens/Settings/Tabs/PluginsTabViewModel.cs b/src/Artemis.UI/Screens/Settings/Tabs/PluginsTabViewModel.cs index 99463667f..11c7d7870 100644 --- a/src/Artemis.UI/Screens/Settings/Tabs/PluginsTabViewModel.cs +++ b/src/Artemis.UI/Screens/Settings/Tabs/PluginsTabViewModel.cs @@ -82,7 +82,13 @@ namespace Artemis.UI.Screens.Settings try { - Plugin plugin = _pluginManagementService.ImportPlugin(files[0]); + Plugin? plugin = _pluginManagementService.ImportPlugin(files[0]); + if (plugin == null) + { + await _windowService.ShowConfirmContentDialog("Failed to import plugin", "Make sure it is compatible with the current platform.", "Close", null); + return; + } + SearchPluginInput = plugin.Info.Name; // Wait for the VM to be created asynchronously (it would be better to respond to some event here)