Revert "更新 PluginManager.cs"

This reverts commit ea55eb1738.
This commit is contained in:
PrefacedCorg
2026-04-08 12:43:55 +08:00
parent ea55eb1738
commit 77cf91e802
+48 -33
View File
@@ -2,10 +2,8 @@ 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
{ {
@@ -17,6 +15,7 @@ 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; }
} }
@@ -28,7 +27,6 @@ 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;
@@ -50,6 +48,11 @@ 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))
{ {
@@ -58,58 +61,70 @@ 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 options = new FolderPluginCatalogOptions var assembly = Assembly.LoadFrom(dllPath);
{ var pluginTypes = assembly.GetTypes()
SearchPatterns = new[] { "*.dll" }, .Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);
IncludeSubfolders = false
};
_catalog = new FolderPluginCatalog(_pluginsDirectory, options); foreach (var pluginType in pluginTypes)
await _catalog.Initialize();
var weikioPlugins = _catalog.GetPlugins();
foreach (var weikioPlugin in weikioPlugins)
{ {
try try
{ {
var pluginType = weikioPlugin.GetType(); var plugin = (IPlugin)Activator.CreateInstance(pluginType)!;
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 = instance.Id, Id = plugin.Id,
Name = instance.Name, Name = plugin.Name,
Version = instance.Version, Version = plugin.Version,
FilePath = weikioPlugin.AssemblyPath ?? "", FilePath = dllPath,
IsLoaded = true, IsLoaded = true,
Instance = instance Instance = plugin,
Assembly = assembly
}; };
instance.Initialize(this); plugin.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 load plugin from {weikioPlugin.AssemblyPath}", ex); LogError($"Failed to create plugin instance from {dllPath}", ex);
} }
} }
Log($"Plugin loading complete. Loaded {_plugins.Count} plugins.");
} }
catch (Exception ex) catch (Exception ex)
{ {
LogError("Failed to initialize plugin catalog", ex); LogError($"Failed to load plugin from {dllPath}", ex);
var info = new PluginInfo
{
FilePath = dllPath,
LoadError = ex
};
_plugins.Add(info);
} }
} }