From 3185bda824ab19a132703d77dbf7c6844dd0a2a8 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sat, 5 Apr 2025 11:31:24 +0800 Subject: [PATCH] Use IRemovable interface instead of reflection --- Flow.Launcher.Core/Plugin/PluginManager.cs | 9 ++++----- Flow.Launcher.Core/Storage/IRemovable.cs | 19 +++++++++++++++++++ Flow.Launcher/PublicAPIInstance.cs | 7 ++++--- 3 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 Flow.Launcher.Core/Storage/IRemovable.cs diff --git a/Flow.Launcher.Core/Plugin/PluginManager.cs b/Flow.Launcher.Core/Plugin/PluginManager.cs index c167a5b21..03ddcb13d 100644 --- a/Flow.Launcher.Core/Plugin/PluginManager.cs +++ b/Flow.Launcher.Core/Plugin/PluginManager.cs @@ -13,6 +13,7 @@ using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; using Flow.Launcher.Plugin.SharedCommands; +using IRemovable = Flow.Launcher.Core.Storage.IRemovable; using ISavable = Flow.Launcher.Plugin.ISavable; namespace Flow.Launcher.Core.Plugin @@ -588,12 +589,10 @@ namespace Flow.Launcher.Core.Plugin if (removePluginSettings) { // For dotnet plugins, we need to remove their PluginJsonStorage and PluginBinaryStorage instances - if (AllowedLanguage.IsDotNet(plugin.Language)) + if (AllowedLanguage.IsDotNet(plugin.Language) && API is IRemovable removable) { - var method = API.GetType().GetMethod("RemovePluginSettings"); - method?.Invoke(API, new object[] { plugin.AssemblyName }); - var method1 = API.GetType().GetMethod("RemovePluginCache"); - method1?.Invoke(API, new object[] { plugin.PluginCacheDirectoryPath }); + removable.RemovePluginSettings(plugin.AssemblyName); + removable.RemovePluginCaches(plugin.PluginCacheDirectoryPath); } try diff --git a/Flow.Launcher.Core/Storage/IRemovable.cs b/Flow.Launcher.Core/Storage/IRemovable.cs new file mode 100644 index 000000000..fc34395e0 --- /dev/null +++ b/Flow.Launcher.Core/Storage/IRemovable.cs @@ -0,0 +1,19 @@ +namespace Flow.Launcher.Core.Storage; + +/// +/// Remove storage instances from instance +/// +public interface IRemovable +{ + /// + /// Remove all instances of one plugin + /// + /// + public void RemovePluginSettings(string assemblyName); + + /// + /// Remove all instances of one plugin + /// + /// + public void RemovePluginCaches(string cacheDirectory); +} diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 50052f1c9..b34006272 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -14,6 +14,8 @@ using CommunityToolkit.Mvvm.DependencyInjection; using Squirrel; using Flow.Launcher.Core; using Flow.Launcher.Core.Plugin; +using Flow.Launcher.Core.Resource; +using Flow.Launcher.Core.Storage; using Flow.Launcher.Helper; using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.Http; @@ -27,11 +29,10 @@ using Flow.Launcher.Plugin.SharedModels; using Flow.Launcher.Plugin.SharedCommands; using Flow.Launcher.ViewModel; using JetBrains.Annotations; -using Flow.Launcher.Core.Resource; namespace Flow.Launcher { - public class PublicAPIInstance : IPublicAPI + public class PublicAPIInstance : IPublicAPI, IRemovable { private readonly Settings _settings; private readonly Internationalization _translater; @@ -348,7 +349,7 @@ namespace Flow.Launcher private readonly ConcurrentDictionary<(string, string, Type), object> _pluginBinaryStorages = new(); - public void RemovePluginCache(string cacheDirectory) + public void RemovePluginCaches(string cacheDirectory) { foreach (var keyValuePair in _pluginBinaryStorages) {