Skip to content

Commit

Permalink
修复 删除插件错误
Browse files Browse the repository at this point in the history
下载 下载在线插件
  • Loading branch information
MakesYT committed Aug 14, 2024
1 parent 5c98112 commit d865d12
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 31 deletions.
11 changes: 11 additions & 0 deletions Core/SDKs/Services/Plugin/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ public static void Init()
Load();
}

public static void EnablePluginByInfo(PluginInfo pluginInfoEx)
{
PluginManager.EnablePlugin.Add(pluginInfoEx.ToPlgString(),
new Plugin(pluginInfoEx));
ConfigManger.Config.EnabledPluginInfos.Add(pluginInfoEx);
ConfigManger.Save();
pluginInfoEx.IsEnabled = true;
// Items.ResetBindings();
CustomScenarioManger.ReCheck(true);
}

public static void Reload()
{
AllPluginInfos.Clear();
Expand Down
30 changes: 27 additions & 3 deletions Core/ViewModel/Pages/MarketPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.ObjectModel;
using System.Drawing;
using System.IO.Compression;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
Expand Down Expand Up @@ -57,7 +58,11 @@ public bool InLocal {
get{
return PluginManager.AllPluginInfos.Any(x=>x.NameSign==NameSign);
}}


public void Upadate()
{
OnPropertyChanged(nameof(InLocal));
}
public string ToPlgString() => $"{Id}_{AuthorId}_{NameSign}";

public override string ToString()
Expand Down Expand Up @@ -102,9 +107,28 @@ private async Task LoadPlugins()
}

[RelayCommand]
private void OpenPlugin(OnlinePluginInfo plugin)
private async Task DownloadPlugin(OnlinePluginInfo plugin)
{

var streamAsync =await _httpClient.GetStreamAsync($"https://www.ncserver.top:5111/api/plugin/download/1/{plugin.Id}/{plugin.LastVersionId}");
Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "temp"));
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "temp",$"{plugin}.zip");
using (var fs = new FileStream(path, FileMode.Create))
{
await streamAsync.CopyToAsync(fs);
}

var zipArchive = ZipFile.Open(path,ZipArchiveMode.Read);
zipArchive.ExtractToDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugins",plugin.ToPlgString()));
zipArchive.Dispose();
File.Delete(path);
PluginManager.Reload();
var pluginInfoEx = PluginManager.AllPluginInfos.FirstOrDefault(e=>e.ToPlgString()==plugin.ToPlgString());
if (pluginInfoEx is null)
{
return;
}
PluginManager.EnablePluginByInfo(pluginInfoEx);
plugin.Upadate();
}

}
55 changes: 28 additions & 27 deletions Core/ViewModel/Pages/plugin/PluginManagerPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void Delete(PluginInfo pluginInfoEx)
CloseButtonText = "取消",
PrimaryAction = () =>
{
Switch(pluginInfoEx);
UnloadPlugin(pluginInfoEx);
if (!pluginInfoEx.UnloadFailed)
{
var pluginsDirectoryInfo = new DirectoryInfo($"{AppDomain.CurrentDomain.BaseDirectory}plugins{Path.DirectorySeparatorChar}{pluginInfoEx.ToPlgString()}");
Expand All @@ -68,40 +68,41 @@ public void Switch(PluginInfo pluginInfoEx)
{
//卸载插件

Plugin.UnloadByPluginInfo(pluginInfoEx.ToPlgString(), out var weakReference);
PluginManager.EnablePlugin.Remove(pluginInfoEx.ToPlgString());
ConfigManger.Config.EnabledPluginInfos.RemoveAll(e => e.ToPlgString()==pluginInfoEx.ToPlgString());
ConfigManger.Save();
pluginInfoEx.IsEnabled = false;

for (int i = 0; i < 15; i++)
{
GC.Collect(2, GCCollectionMode.Aggressive);
GC.WaitForPendingFinalizers();
Task.Delay(10).Wait();
}
if (weakReference.IsAlive)
{
pluginInfoEx.UnloadFailed = true;
}

// Items.ResetBindings();
CustomScenarioManger.LoadAll();
UnloadPlugin(pluginInfoEx);
}
else
{
//加载插件
//Plugin.NewPlugin(pluginInfoEx.Path, out var weakReference);
PluginManager.EnablePlugin.Add(pluginInfoEx.ToPlgString(),
new Plugin(pluginInfoEx));
ConfigManger.Config.EnabledPluginInfos.Add(pluginInfoEx);
ConfigManger.Save();
pluginInfoEx.IsEnabled = true;
// Items.ResetBindings();
CustomScenarioManger.ReCheck(true);
PluginManager.EnablePluginByInfo(pluginInfoEx);
}
}

private static void UnloadPlugin(PluginInfo pluginInfoEx)
{
Plugin.UnloadByPluginInfo(pluginInfoEx.ToPlgString(), out var weakReference);
PluginManager.EnablePlugin.Remove(pluginInfoEx.ToPlgString());
ConfigManger.Config.EnabledPluginInfos.RemoveAll(e => e.ToPlgString()==pluginInfoEx.ToPlgString());
ConfigManger.Save();
pluginInfoEx.IsEnabled = false;

for (int i = 0; i < 15; i++)
{
GC.Collect(2, GCCollectionMode.Aggressive);
GC.WaitForPendingFinalizers();
Task.Delay(10).Wait();
}
if (weakReference.IsAlive)
{
pluginInfoEx.UnloadFailed = true;
}

// Items.ResetBindings();
CustomScenarioManger.LoadAll();
}



[RelayCommand]
public void ToPluginSettingPage(PluginInfo pluginInfoEx)
{
Expand Down
2 changes: 1 addition & 1 deletion KitopiaAvalonia/Pages/MarketPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<StackPanel Grid.Column="0" Margin="5,0,5,0" Spacing="5" Grid.ColumnSpan="2"
Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
<Button IsEnabled="{Binding !InLocal}"
Command="{Binding Path=$parent[ItemsControl].((pages:MarketPageViewModel)DataContext).OpenPluginCommand}"
Command="{Binding Path=$parent[ItemsControl].((pages:MarketPageViewModel)DataContext).DownloadPluginCommand}"
CommandParameter="{Binding}">
<Button.Styles>
<Style Selector="Button[IsEnabled=True]">
Expand Down

0 comments on commit d865d12

Please sign in to comment.