Skip to content

Commit

Permalink
🔧 Fix: 增加对插件列表的操作锁
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynesshely committed Dec 3, 2022
1 parent 96df406 commit 902b8eb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
40 changes: 24 additions & 16 deletions Services/PluginsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ internal static void Execute(string msg, IPEndPoint endPoint)

internal static readonly Queue<Plugin> pluginsToDelete = new();

internal static readonly object PluginsListOperationLock = new();

/// <summary>
/// 持续检查并移除
/// </summary>
Expand Down Expand Up @@ -236,14 +238,17 @@ internal static void KeepCheckAndRemoveOrDelete()
while (pluginsToRemoveFromDB.Count > 0)
{
Plugin pg = pluginsToRemoveFromDB.Dequeue();
Program.PluginsList.Plugins.RemoveAt(
Program.PluginsList.Plugins.FindIndex(
x =>
{
if (x.InstallPath != null)
return x.InstallPath.Equals(pg.InstallPath);
else return false;
}));
lock (PluginsListOperationLock)
{
Program.PluginsList.Plugins.RemoveAt(
Program.PluginsList.Plugins.FindIndex(
x =>
{
if (x.InstallPath != null)
return x.InstallPath.Equals(pg.InstallPath);
else return false;
}));
}
}
}
Expand All @@ -253,14 +258,17 @@ internal static void KeepCheckAndRemoveOrDelete()
while (pluginsToDelete.Count > 0)
{
Plugin pg = pluginsToDelete.Dequeue();
Program.PluginsList.Plugins.RemoveAt(
Program.PluginsList.Plugins.FindIndex(
x =>
{
if (x.InstallPath != null)
return x.InstallPath.Equals(pg.InstallPath);
else return false;
}));
lock (PluginsListOperationLock)
{
Program.PluginsList.Plugins.RemoveAt(
Program.PluginsList.Plugins.FindIndex(
x =>
{
if (x.InstallPath != null)
return x.InstallPath.Equals(pg.InstallPath);
else return false;
}));
}
string pgfiledir = Path.GetFullPath(
$"{Program.Config.App.LocalPluginsFileDirectory}/" +
$"{pg.PluginDetails.PublisherName}_{pg.PluginDetails.AuthorName}/" +
Expand Down
35 changes: 20 additions & 15 deletions ViewModels/Pages/RepoPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,29 @@ internal void RefreshPlugins(object _)
//LiteDatabase? pgdb = Program.PluginsDataBase;

PluginBars.Clear();
foreach (var item in Program.PluginsList.Plugins)
lock (PluginsManager.PluginsListOperationLock)
{
try
foreach (var item in Program.PluginsList.Plugins)
{
Plugin plugin = new()
try
{
InstallPath = item.InstallPath,
PluginDetails = JsonSerializer.Deserialize<PluginStruct>(
File.ReadAllText(Path.GetFullPath($"{item.InstallPath}/PluginStruct.json"))),
RequiredLoaderStruct = JsonSerializer.Deserialize<LoaderStruct>(
File.ReadAllText(Path.GetFullPath($"{item.InstallPath}/LoaderStruct.json"))),
InstalledDevices = new()
};
PluginBars.Add(new(plugin, ref pluginBars));
}
catch (Exception ex)
{
Log.Error("In RefreshPlugins()", ex);
Plugin plugin = new()
{
InstallPath = item.InstallPath,
PluginDetails = JsonSerializer.Deserialize<PluginStruct>(
File.ReadAllText(
Path.GetFullPath($"{item.InstallPath}/PluginStruct.json"))),
RequiredLoaderStruct = JsonSerializer.Deserialize<LoaderStruct>(
File.ReadAllText(
Path.GetFullPath($"{item.InstallPath}/LoaderStruct.json"))),
InstalledDevices = new()
};
PluginBars.Add(new(plugin, ref pluginBars));
}
catch (Exception ex)
{
Log.Error("In RefreshPlugins()", ex);
}
}
}
}
Expand Down

0 comments on commit 902b8eb

Please sign in to comment.