更新 PluginManager.cs

This commit is contained in:
PrefacedCorg
2026-04-08 12:41:56 +08:00
parent 302ef307fe
commit ea55eb1738
+33 -48
View File
@@ -2,8 +2,10 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using Weikio.PluginFramework;
using Weikio.PluginFramework.Abstractions;
using Weikio.PluginFramework.Catalogs;
namespace Ink_Canvas.Plugins namespace Ink_Canvas.Plugins
{ {
@@ -15,7 +17,6 @@ namespace Ink_Canvas.Plugins
public string FilePath { get; set; } = ""; public string FilePath { get; set; } = "";
public bool IsLoaded { get; set; } public bool IsLoaded { get; set; }
public IPlugin? Instance { get; set; } public IPlugin? Instance { get; set; }
public Assembly? Assembly { get; set; }
public Exception? LoadError { get; set; } public Exception? LoadError { get; set; }
} }
@@ -27,6 +28,7 @@ namespace Ink_Canvas.Plugins
private readonly List<PluginInfo> _plugins = new(); private readonly List<PluginInfo> _plugins = new();
private readonly Dictionary<Type, object> _services = new(); private readonly Dictionary<Type, object> _services = new();
private string _pluginsDirectory; private string _pluginsDirectory;
private FolderPluginCatalog? _catalog;
public IReadOnlyList<PluginInfo> Plugins => _plugins.AsReadOnly(); public IReadOnlyList<PluginInfo> Plugins => _plugins.AsReadOnly();
public event EventHandler<PluginInfo>? PluginLoaded; public event EventHandler<PluginInfo>? PluginLoaded;
@@ -48,11 +50,6 @@ namespace Ink_Canvas.Plugins
} }
public async Task LoadAllAsync() public async Task LoadAllAsync()
{
await Task.Run(() => LoadAll());
}
private void LoadAll()
{ {
if (!Directory.Exists(_pluginsDirectory)) if (!Directory.Exists(_pluginsDirectory))
{ {
@@ -61,70 +58,58 @@ namespace Ink_Canvas.Plugins
return; return;
} }
var dllFiles = Directory.GetFiles(_pluginsDirectory, "*.dll")
.Where(f => !f.EndsWith("InkCanvasForClass.dll") &&
!f.EndsWith("Weikio.PluginFramework.dll"));
foreach (var dll in dllFiles)
{
LoadPlugin(dll);
}
Log($"Plugin loading complete. Loaded {_plugins.Count} plugins.");
}
private void LoadPlugin(string dllPath)
{
if (!File.Exists(dllPath))
{
Log($"Plugin file not found: {dllPath}");
return;
}
try try
{ {
var assembly = Assembly.LoadFrom(dllPath); var options = new FolderPluginCatalogOptions
var pluginTypes = assembly.GetTypes() {
.Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract); SearchPatterns = new[] { "*.dll" },
IncludeSubfolders = false
};
foreach (var pluginType in pluginTypes) _catalog = new FolderPluginCatalog(_pluginsDirectory, options);
await _catalog.Initialize();
var weikioPlugins = _catalog.GetPlugins();
foreach (var weikioPlugin in weikioPlugins)
{ {
try try
{ {
var plugin = (IPlugin)Activator.CreateInstance(pluginType)!; var pluginType = weikioPlugin.GetType();
var instance = Activator.CreateInstance(pluginType) as IPlugin;
if (instance == null)
{
Log($"Failed to create instance of plugin from {weikioPlugin.AssemblyPath}");
continue;
}
var info = new PluginInfo var info = new PluginInfo
{ {
Id = plugin.Id, Id = instance.Id,
Name = plugin.Name, Name = instance.Name,
Version = plugin.Version, Version = instance.Version,
FilePath = dllPath, FilePath = weikioPlugin.AssemblyPath ?? "",
IsLoaded = true, IsLoaded = true,
Instance = plugin, Instance = instance
Assembly = assembly
}; };
plugin.Initialize(this); instance.Initialize(this);
_plugins.Add(info); _plugins.Add(info);
PluginLoaded?.Invoke(this, info); PluginLoaded?.Invoke(this, info);
Log($"Plugin loaded: {info.Name} v{info.Version}"); Log($"Plugin loaded: {info.Name} v{info.Version}");
} }
catch (Exception ex) catch (Exception ex)
{ {
LogError($"Failed to create plugin instance from {dllPath}", ex); LogError($"Failed to load plugin from {weikioPlugin.AssemblyPath}", ex);
} }
} }
Log($"Plugin loading complete. Loaded {_plugins.Count} plugins.");
} }
catch (Exception ex) catch (Exception ex)
{ {
LogError($"Failed to load plugin from {dllPath}", ex); LogError("Failed to initialize plugin catalog", ex);
var info = new PluginInfo
{
FilePath = dllPath,
LoadError = ex
};
_plugins.Add(info);
} }
} }