Skip to content

Commit f0e74a2

Browse files
committed
Fix issue that plugin manager will save settings for the uninstalled and settings-removed plugins
1 parent 0e4cf57 commit f0e74a2

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using Flow.Launcher.Plugin.SharedCommands;
1515
using System.Text.Json;
1616
using Flow.Launcher.Core.Resource;
17-
using Flow.Launcher.Infrastructure.Storage;
1817

1918
namespace Flow.Launcher.Core.Plugin
2019
{
@@ -548,10 +547,17 @@ internal static void UninstallPlugin(PluginMetadata plugin, bool removePluginFro
548547
var assemblyLoader = new PluginAssemblyLoader(plugin.ExecuteFilePath);
549548
var assembly = assemblyLoader.LoadAssemblyAndDependencies();
550549
var assemblyName = assembly.GetName().Name;
551-
var directoryPath = Path.Combine(DataLocation.DataDirectory(), JsonStorage<object>.DirectoryName, Constant.Plugins, assemblyName);
552-
if (Directory.Exists(directoryPath))
550+
551+
// if user want to remove the plugin settings, we cannot call save method for the plugin json storage instance of this plugin
552+
// so we need to remove it from the api instance
553+
var method = API.GetType().GetMethod("RemovePluginSettings");
554+
var pluginJsonStorage = method?.Invoke(API, new object[] { assemblyName });
555+
556+
// if there exists a json storage for current plugin, we need to delete the directory path
557+
if (pluginJsonStorage != null)
553558
{
554-
Directory.Delete(directoryPath, true);
559+
var deleteMethod = pluginJsonStorage.GetType().GetMethod("DeleteDirectory");
560+
deleteMethod?.Invoke(pluginJsonStorage, null);
555561
}
556562
}
557563

Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33

44
namespace Flow.Launcher.Infrastructure.Storage
55
{
6-
public class PluginJsonStorage<T> :JsonStorage<T> where T : new()
6+
public class PluginJsonStorage<T> : JsonStorage<T> where T : new()
77
{
8+
// Use assembly name to check which plugin is using this storage
9+
public readonly string AssemblyName;
10+
811
public PluginJsonStorage()
912
{
1013
// C# related, add python related below
1114
var dataType = typeof(T);
12-
var assemblyName = dataType.Assembly.GetName().Name;
13-
DirectoryPath = Path.Combine(DataLocation.DataDirectory(), DirectoryName, Constant.Plugins, assemblyName);
15+
AssemblyName = dataType.Assembly.GetName().Name;
16+
DirectoryPath = Path.Combine(DataLocation.DataDirectory(), DirectoryName, Constant.Plugins, AssemblyName);
1417
Helper.ValidateDirectory(DirectoryPath);
1518

1619
FilePath = Path.Combine(DirectoryPath, $"{dataType.Name}{FileSuffix}");
@@ -20,6 +23,13 @@ public PluginJsonStorage(T data) : this()
2023
{
2124
Data = data;
2225
}
26+
27+
public void DeleteDirectory()
28+
{
29+
if (Directory.Exists(DirectoryPath))
30+
{
31+
Directory.Delete(DirectoryPath, true);
32+
}
33+
}
2334
}
2435
}
25-

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,23 @@ public void LogException(string className, string message, Exception e,
189189

190190
private readonly ConcurrentDictionary<Type, object> _pluginJsonStorages = new();
191191

192+
public object RemovePluginSettings(string assemblyName)
193+
{
194+
foreach (var keyValuePair in _pluginJsonStorages)
195+
{
196+
var key = keyValuePair.Key;
197+
var value = keyValuePair.Value;
198+
var name = value.GetType().GetField("AssemblyName")?.GetValue(value)?.ToString();
199+
if (name == assemblyName)
200+
{
201+
_pluginJsonStorages.Remove(key, out var pluginJsonStorage);
202+
return pluginJsonStorage;
203+
}
204+
}
205+
206+
return null;
207+
}
208+
192209
/// <summary>
193210
/// Save plugin settings.
194211
/// </summary>

0 commit comments

Comments
 (0)