-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPluginManager.cs
More file actions
351 lines (304 loc) · 12.7 KB
/
PluginManager.cs
File metadata and controls
351 lines (304 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
using Avalonia.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
namespace ShowWrite
{
public interface IPlugin
{
string Name { get; }
string Version { get; }
string Description { get; }
string Author { get; }
void Initialize();
void OnLoad();
void OnUnload();
}
public interface IBottomToolbarPlugin : IPlugin
{
List<PluginToolbarButton> GetToolbarButtons();
void UpdateToolbarButtons();
void SetRefreshToolbarCallback(Action callback);
}
public interface IPluginWindow : IPlugin
{
void SetPluginOverlay(Border overlay);
void SetToolbarVisibilityCallback(Action<bool> callback);
void OnPluginActivated();
void OnPluginDeactivated();
void OnCameraFrame(Avalonia.Media.Imaging.Bitmap? bitmap);
void SetShowResultWindowCallback(Action<string, bool, List<(string Text, Action Callback)>>? callback);
void SetCancelCallback(Action callback);
void SetShowNotificationCallback(Action<string, int>? callback);
}
public class PluginToolbarButton
{
public string IconPath { get; set; } = string.Empty;
public string Label { get; set; } = string.Empty;
public Action? OnClick { get; set; }
public int Order { get; set; } = 100;
public bool IsEnabled { get; set; } = true;
}
public class PluginInfo
{
public string Name { get; set; } = string.Empty;
public string Version { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public string Author { get; set; } = string.Empty;
public string FilePath { get; set; } = string.Empty;
public bool IsLoaded { get; set; }
public bool IsEnabled { get; set; }
public IPlugin? PluginInstance { get; set; }
public Assembly? Assembly { get; set; }
}
public class PluginManager
{
private static PluginManager? _instance;
public static PluginManager Instance => _instance ??= new PluginManager();
private readonly List<PluginInfo> _plugins = new();
private readonly string _pluginsPath;
public IReadOnlyList<PluginInfo> Plugins => _plugins.AsReadOnly();
private PluginManager()
{
_pluginsPath = Config.GetPluginsPath();
}
public void LoadPlugins()
{
if (!Directory.Exists(_pluginsPath))
{
Directory.CreateDirectory(_pluginsPath);
return;
}
var config = Config.Load();
var enabledPlugins = config.EnabledPlugins;
var dllFiles = Directory.GetFiles(_pluginsPath, "*.dll", SearchOption.AllDirectories);
Console.WriteLine($"扫描插件路径: {_pluginsPath}");
Console.WriteLine($"找到 {dllFiles.Length} 个 DLL 文件");
foreach (var dllFile in dllFiles)
{
try
{
Console.WriteLine($"正在加载插件文件: {Path.GetFileName(dllFile)}");
var pluginContext = new PluginLoadContext(dllFile);
var assembly = pluginContext.LoadFromAssemblyPath(dllFile);
var pluginTypes = assembly.GetTypes()
.Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract)
.ToList();
Console.WriteLine($" 找到 {pluginTypes.Count} 个插件类型");
foreach (var pluginType in pluginTypes)
{
IPlugin? pluginInstance = null;
try
{
pluginInstance = (IPlugin?)Activator.CreateInstance(pluginType);
}
catch (Exception ex)
{
Console.WriteLine($"创建插件实例失败: {ex.Message}");
Console.WriteLine($" 堆栈跟踪: {ex.StackTrace}");
if (ex.InnerException != null)
{
Console.WriteLine($" 内部异常: {ex.InnerException.Message}");
Console.WriteLine($" 内部异常堆栈: {ex.InnerException.StackTrace}");
}
continue;
}
if (pluginInstance == null) continue;
var pluginName = pluginInstance.Name;
var isEnabled = enabledPlugins.Contains(pluginName);
Console.WriteLine($" 插件名称: {pluginName}, 已启用: {isEnabled}");
var pluginInfo = new PluginInfo
{
Name = pluginName,
Version = pluginInstance.Version,
Description = pluginInstance.Description,
Author = pluginInstance.Author,
FilePath = dllFile,
IsEnabled = isEnabled,
PluginInstance = pluginInstance,
Assembly = assembly
};
if (isEnabled)
{
try
{
pluginInstance.Initialize();
pluginInstance.OnLoad();
pluginInfo.IsLoaded = true;
Console.WriteLine($" 插件 {pluginName} 加载成功");
}
catch (Exception ex)
{
Console.WriteLine($"插件 {pluginName} 加载失败: {ex.Message}");
Console.WriteLine($" 堆栈跟踪: {ex.StackTrace}");
}
}
else
{
Console.WriteLine($" 插件 {pluginName} 未启用,正在自动启用...");
try
{
pluginInstance.Initialize();
pluginInstance.OnLoad();
pluginInfo.IsEnabled = true;
pluginInfo.IsLoaded = true;
enabledPlugins.Add(pluginName);
config.Save();
Console.WriteLine($" 插件 {pluginName} 已自动启用并加载成功");
}
catch (Exception ex)
{
Console.WriteLine($"插件 {pluginName} 自动启用失败: {ex.Message}");
Console.WriteLine($" 堆栈跟踪: {ex.StackTrace}");
}
}
_plugins.Add(pluginInfo);
}
}
catch (Exception ex)
{
Console.WriteLine($"加载插件文件 {dllFile} 失败: {ex.Message}");
Console.WriteLine($" 堆栈跟踪: {ex.StackTrace}");
}
}
}
public void EnablePlugin(string pluginName)
{
var plugin = _plugins.FirstOrDefault(p => p.Name == pluginName);
if (plugin == null || plugin.IsEnabled) return;
try
{
plugin.PluginInstance?.Initialize();
plugin.PluginInstance?.OnLoad();
plugin.IsEnabled = true;
plugin.IsLoaded = true;
var config = Config.Load();
if (!config.EnabledPlugins.Contains(pluginName))
{
config.EnabledPlugins.Add(pluginName);
config.Save();
}
}
catch (Exception ex)
{
Console.WriteLine($"启用插件 {pluginName} 失败: {ex.Message}");
}
}
public void DisablePlugin(string pluginName)
{
var plugin = _plugins.FirstOrDefault(p => p.Name == pluginName);
if (plugin == null || !plugin.IsEnabled) return;
try
{
plugin.PluginInstance?.OnUnload();
plugin.IsEnabled = false;
plugin.IsLoaded = false;
var config = Config.Load();
config.EnabledPlugins.Remove(pluginName);
config.Save();
}
catch (Exception ex)
{
Console.WriteLine($"禁用插件 {pluginName} 失败: {ex.Message}");
}
}
public void ReloadPlugin(string pluginName)
{
var plugin = _plugins.FirstOrDefault(p => p.Name == pluginName);
if (plugin == null) return;
if (plugin.IsEnabled)
{
DisablePlugin(pluginName);
}
if (plugin.Assembly != null)
{
try
{
var pluginContext = new PluginLoadContext(plugin.FilePath);
var assembly = pluginContext.LoadFromAssemblyPath(plugin.FilePath);
var pluginTypes = assembly.GetTypes()
.Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract)
.ToList();
if (pluginTypes.Count > 0)
{
var pluginInstance = (IPlugin?)Activator.CreateInstance(pluginTypes[0]);
if (pluginInstance != null)
{
plugin.PluginInstance = pluginInstance;
plugin.Assembly = assembly;
plugin.Name = pluginInstance.Name;
plugin.Version = pluginInstance.Version;
plugin.Description = pluginInstance.Description;
plugin.Author = pluginInstance.Author;
}
}
}
catch (Exception ex)
{
Console.WriteLine($"重新加载插件 {pluginName} 失败: {ex.Message}");
}
}
}
public PluginInfo? GetPlugin(string pluginName)
{
return _plugins.FirstOrDefault(p => p.Name == pluginName);
}
public List<PluginToolbarButton> GetBottomToolbarButtons()
{
var buttons = new List<PluginToolbarButton>();
foreach (var plugin in _plugins.Where(p => p.IsEnabled && p.IsLoaded))
{
if (plugin.PluginInstance is IBottomToolbarPlugin bottomToolbarPlugin)
{
try
{
var pluginButtons = bottomToolbarPlugin.GetToolbarButtons();
buttons.AddRange(pluginButtons);
}
catch (Exception ex)
{
Console.WriteLine($"获取插件 {plugin.Name} 的工具栏按钮失败: {ex.Message}");
}
}
}
return buttons.OrderBy(b => b.Order).ToList();
}
public void RefreshToolbarButtons()
{
foreach (var plugin in _plugins.Where(p => p.IsEnabled && p.IsLoaded))
{
if (plugin.PluginInstance is IBottomToolbarPlugin bottomToolbarPlugin)
{
try
{
bottomToolbarPlugin.UpdateToolbarButtons();
}
catch (Exception ex)
{
Console.WriteLine($"刷新插件 {plugin.Name} 的工具栏按钮失败: {ex.Message}");
}
}
}
}
}
public class PluginLoadContext : AssemblyLoadContext
{
private readonly AssemblyDependencyResolver _resolver;
public PluginLoadContext(string pluginPath) : base(isCollectible: true)
{
_resolver = new AssemblyDependencyResolver(pluginPath);
}
protected override Assembly? Load(AssemblyName assemblyName)
{
var assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
if (assemblyPath != null)
{
return LoadFromAssemblyPath(assemblyPath);
}
return null;
}
}
}