From cd0e23983f20285e6911e51dd859585c253828dc Mon Sep 17 00:00:00 2001 From: MakesYT <2696703792@qq.com> Date: Tue, 27 Aug 2024 18:32:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E5=90=8E=E6=83=85=E6=99=AF=E7=BC=BA=E5=A4=B1?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E5=8D=B4=E6=8F=90=E7=A4=BA=E6=9C=AA=E5=90=AF?= =?UTF-8?q?=E7=94=A8=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomScenarioLoadFromJsonException.cs | 1 + .../CustomScenario/CustomScenarioManger.cs | 5 +- Core/SDKs/Services/Plugin/PluginManager.cs | 70 ++++++++++++++----- .../plugin/PluginManagerPageViewModel.cs | 26 +------ KitopiaEx/manifest.json | 2 +- 5 files changed, 59 insertions(+), 45 deletions(-) diff --git a/Core/SDKs/CustomScenario/CustomScenarioLoadFromJsonException.cs b/Core/SDKs/CustomScenario/CustomScenarioLoadFromJsonException.cs index 8931afb..64ab39a 100644 --- a/Core/SDKs/CustomScenario/CustomScenarioLoadFromJsonException.cs +++ b/Core/SDKs/CustomScenario/CustomScenarioLoadFromJsonException.cs @@ -6,6 +6,7 @@ public enum CustomScenarioLoadFromJsonFailedType { 插件未找到, 插件未启用, + 插件正在被卸载需要重启, 方法未找到, 类未找到 } diff --git a/Core/SDKs/CustomScenario/CustomScenarioManger.cs b/Core/SDKs/CustomScenario/CustomScenarioManger.cs index 62cfff3..0b3e45e 100644 --- a/Core/SDKs/CustomScenario/CustomScenarioManger.cs +++ b/Core/SDKs/CustomScenario/CustomScenarioManger.cs @@ -63,11 +63,11 @@ public static void Init() { Load(fileInfo); } - + Log.Debug($"加载情景信息完成共{CustomScenarios.Count}情景被识别"); WeakReferenceMessenger.Default.Send("Kitopia_SoftwareStarted", "CustomScenarioTrigger"); } - public static void LoadAll() + public static void Reload() { CustomScenarios.Clear(); var info = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "customScenarios"); @@ -75,6 +75,7 @@ public static void LoadAll() { Load(fileInfo); } + Log.Debug($"重载情景信息完成共{CustomScenarios.Count}情景被识别"); } public static void Load(FileInfo fileInfo) diff --git a/Core/SDKs/Services/Plugin/PluginManager.cs b/Core/SDKs/Services/Plugin/PluginManager.cs index f1f9c45..d0c6468 100644 --- a/Core/SDKs/Services/Plugin/PluginManager.cs +++ b/Core/SDKs/Services/Plugin/PluginManager.cs @@ -93,7 +93,7 @@ public static void EnablePluginByInfo(PluginInfo pluginInfoEx) CustomScenarioManger.ReCheck(true); } - public static void UnloadPlugin(PluginInfo pluginInfoEx) + public static void UnloadPlugin(PluginInfo pluginInfoEx,bool reloadPluginAndCustomScenarion=true) { Plugin.UnloadByPluginInfo(pluginInfoEx.ToPlgString(), out var weakReference); PluginManager.EnablePlugin.Remove(pluginInfoEx.ToPlgString()); @@ -111,12 +111,15 @@ public static void UnloadPlugin(PluginInfo pluginInfoEx) if (weakReference.IsAlive) { pluginInfoEx.UnloadFailed = true; - File.Create( - $"{AppDomain.CurrentDomain.BaseDirectory}plugins{Path.DirectorySeparatorChar}{pluginInfoEx.ToPlgString()}{Path.DirectorySeparatorChar}.unload"); } // Items.ResetBindings(); - CustomScenarioManger.LoadAll(); + if (reloadPluginAndCustomScenarion) + { + Reload(); + CustomScenarioManger.Reload(); + } + } public static void Reload() @@ -127,6 +130,7 @@ public static void Reload() } AllPluginInfos.Clear(); Load(); + } public enum VersionCheckResult { @@ -264,18 +268,7 @@ public static void Load(bool init=false) continue; } - if (File.Exists($"{directoryInfo.FullName}{Path.DirectorySeparatorChar}.unload")) - { - if (init) - { - File.Delete($"{directoryInfo.FullName}{Path.DirectorySeparatorChar}.unload"); - } - else - { - continue; - } - - } + if (File.Exists($"{directoryInfo.FullName}{Path.DirectorySeparatorChar}manifest.json")) { var readAllText = File.ReadAllText($"{directoryInfo.FullName}{Path.DirectorySeparatorChar}manifest.json"); @@ -315,6 +308,7 @@ public static void Load(bool init=false) { DownloadPluginOnline(serialize).Wait(); } + Log.Debug($"加载插件{serialize.Name}信息成功"); if (ConfigManger.Config.EnabledPluginInfos.Any(e => e.ToPlgString()==serialize.ToPlgString())) { serialize.IsEnabled = true; @@ -327,7 +321,48 @@ public static void Load(bool init=false) } } - + Log.Debug($"加载插件信息完成共{AllPluginInfos.Count}插件被识别"); + } + + public static void DeletePlugin(string pluginSignName) + { + DeletePlugin(AllPluginInfos.FirstOrDefault(e=>e.NameSign==pluginSignName)); + } + public static void DeletePlugin(PluginInfo pluginInfoEx) + { + var dialog = new DialogContent() + { + Title = $"删除{pluginInfoEx.Name}?", + Content = "是否确定删除?\n他真的会丢失很久很久(不可恢复)", + PrimaryButtonText = "确定", + CloseButtonText = "取消", + PrimaryAction = () => + { + DeletePluginWithoutCheck(pluginInfoEx); + } + }; + ((IContentDialog)ServiceManager.Services!.GetService(typeof(IContentDialog))!).ShowDialogAsync(null, + dialog); + } + + public static void DeletePluginWithoutCheck(PluginInfo pluginInfoEx) + { + Log.Debug($"删除插件{pluginInfoEx.Name}"); + PluginManager.UnloadPlugin(pluginInfoEx,false); + if (!pluginInfoEx.UnloadFailed) + { + var pluginsDirectoryInfo = new DirectoryInfo($"{AppDomain.CurrentDomain.BaseDirectory}plugins{Path.DirectorySeparatorChar}{pluginInfoEx.ToPlgString()}"); + pluginsDirectoryInfo.Delete(true); + Task.Run(PluginManager.Reload); + } + else + { + File.Create( + $"{AppDomain.CurrentDomain.BaseDirectory}plugins{Path.DirectorySeparatorChar}{pluginInfoEx.ToPlgString()}{Path.DirectorySeparatorChar}.remove"); + Task.Run(PluginManager.Reload); + } + Reload(); + CustomScenarioManger.Reload(); } public static async Task GetOnlinePluginInfo(int id) @@ -424,6 +459,7 @@ private static async Task DownloadPlugin(int id,object versionId,string pl { try { + Log.Debug( $"从服务器下载插件{plugin}(ID:{id})版本{versionId}"); var streamAsync =await _httpClient.GetStreamAsync($"https://www.ncserver.top:5111/api/plugin/download/1/{id}/{versionId}"); Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "temp")); var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "temp",$"{plugin}.zip"); diff --git a/Core/ViewModel/Pages/plugin/PluginManagerPageViewModel.cs b/Core/ViewModel/Pages/plugin/PluginManagerPageViewModel.cs index 36a67a0..117fa55 100644 --- a/Core/ViewModel/Pages/plugin/PluginManagerPageViewModel.cs +++ b/Core/ViewModel/Pages/plugin/PluginManagerPageViewModel.cs @@ -54,31 +54,7 @@ private void RestartApp() [RelayCommand] private void Delete(PluginInfo pluginInfoEx) { - var dialog = new DialogContent() - { - Title = $"删除{pluginInfoEx.Name}?", - Content = "是否确定删除?\n他真的会丢失很久很久(不可恢复)", - PrimaryButtonText = "确定", - CloseButtonText = "取消", - PrimaryAction = () => - { - PluginManager.UnloadPlugin(pluginInfoEx); - if (!pluginInfoEx.UnloadFailed) - { - var pluginsDirectoryInfo = new DirectoryInfo($"{AppDomain.CurrentDomain.BaseDirectory}plugins{Path.DirectorySeparatorChar}{pluginInfoEx.ToPlgString()}"); - pluginsDirectoryInfo.Delete(true); - Task.Run(PluginManager.Reload); - } - else - { - File.Create( - $"{AppDomain.CurrentDomain.BaseDirectory}plugins{Path.DirectorySeparatorChar}{pluginInfoEx.ToPlgString()}{Path.DirectorySeparatorChar}.remove"); - Task.Run(PluginManager.Reload); - } - } - }; - ((IContentDialog)ServiceManager.Services!.GetService(typeof(IContentDialog))!).ShowDialogAsync(null, - dialog); + PluginManager.DeletePlugin(pluginInfoEx); } diff --git a/KitopiaEx/manifest.json b/KitopiaEx/manifest.json index 24125a1..d53dd6e 100644 --- a/KitopiaEx/manifest.json +++ b/KitopiaEx/manifest.json @@ -3,7 +3,7 @@ "AuthorName": "Kitopia", "AuthorId" : 7, "Name": "Kitopia拓展", - "NameSign" : "KitopiaEx", + "NameSign" : "kitopiaex", "IsPublic": true, "Version": "1.0.0", "VersionId": 1,