|
4 | 4 | using System.IO;
|
5 | 5 | using System.Linq;
|
6 | 6 | using System.Reflection;
|
| 7 | +using System.Runtime.CompilerServices; |
| 8 | +using System.Runtime.Loader; |
7 | 9 | using TrProtocol;
|
8 | 10 |
|
9 | 11 | namespace MultiSEngine.Core
|
10 | 12 | {
|
11 |
| - public abstract class MSEPlugin |
| 13 | + public interface IMSEPlugin |
12 | 14 | {
|
13 |
| - public abstract string Name { get; } |
14 |
| - public abstract string Description { get; } |
15 |
| - public abstract string Author { get; } |
16 |
| - public abstract Version Version { get; } |
17 |
| - public abstract void Initialize(); |
18 |
| - public abstract void Dispose(); |
19 |
| - public virtual PacketSerializer Serializer => Net.ClientSerializer; |
| 15 | + public string Name { get; } |
| 16 | + public string Description { get; } |
| 17 | + public string Author { get; } |
| 18 | + public Version Version { get; } |
| 19 | + public void Initialize(); |
| 20 | + public void Dispose(); |
| 21 | + public PacketSerializer Serializer => Net.ClientSerializer; |
20 | 22 | }
|
21 | 23 | public class PluginSystem
|
22 | 24 | {
|
23 | 25 | public static readonly string PluginPath = Path.Combine(Environment.CurrentDirectory, "Plugins");
|
24 |
| - public static readonly List<MSEPlugin> PluginList = new(); |
25 |
| - [AutoInit("Loading all plugins.", $"Plugin(s) loaded.")] |
| 26 | + public static readonly List<IMSEPlugin> PluginList = new(); |
| 27 | + private static PluginHost<IMSEPlugin> _pluginHost; |
| 28 | + [AutoInit("Loading all plugins.")] |
26 | 29 | internal static void Load()
|
27 | 30 | {
|
28 | 31 | if (!Directory.Exists(PluginPath))
|
29 | 32 | Directory.CreateDirectory(PluginPath);
|
30 |
| - Directory.GetFiles(PluginPath, "*.dll").ForEach(p => |
31 |
| - { |
32 |
| - Assembly plugin = Assembly.LoadFile(p); |
33 |
| - if (plugin.GetTypes().Where(t => t.BaseType == typeof(MSEPlugin))?.ToArray() is { Length: > 0 } instances) |
34 |
| - { |
35 |
| - instances.ForEach(instance => |
36 |
| - { |
37 |
| - try |
38 |
| - { |
39 |
| - var pluginInstance = Activator.CreateInstance(instance) as MSEPlugin; |
40 |
| - pluginInstance.Initialize(); |
41 |
| - Logs.Success($"- Loaded plugin: {pluginInstance.Name} <{pluginInstance.Author}> V{pluginInstance.Version}"); |
42 |
| - PluginList.Add(pluginInstance); |
43 |
| - } |
44 |
| - catch (Exception ex) |
45 |
| - { |
46 |
| - Logs.Warn($"Failed to load plugin: {p}{Environment.NewLine}{ex}"); |
47 |
| - } |
48 |
| - }); |
49 |
| - } |
50 |
| - }); |
| 33 | + _pluginHost ??= new PluginHost<IMSEPlugin>(); |
| 34 | + _pluginHost.LoadPlugins(PluginPath, plugin => Logs.Success($"- Loaded plugin: {plugin.Name} <{plugin.Author}> V{plugin.Version}")); |
| 35 | + |
| 36 | + Logs.Info($"{PluginList.Count} Plugin(s) loaded."); |
51 | 37 | }
|
52 | 38 | internal static void Unload()
|
53 | 39 | {
|
54 | 40 | PluginList.ForEach(p =>
|
55 | 41 | {
|
56 | 42 | p.Dispose();
|
57 |
| - Logs.Text($"Plugin: {p.Name} disposed."); |
| 43 | + Logs.Info($"- Disposed plugin: {p.Name}"); |
58 | 44 | });
|
59 | 45 | PluginList.Clear();
|
| 46 | + _pluginHost.Unload(); |
| 47 | + _pluginHost = null; |
| 48 | + } |
| 49 | + public static void Reload() |
| 50 | + { |
| 51 | + Unload(); |
| 52 | + Load(); |
| 53 | + } |
| 54 | + #region 插件加载类 |
| 55 | + class PluginHost<TPlugin> where TPlugin : IMSEPlugin |
| 56 | + { |
| 57 | + private AssemblyLoadContext _pluginAssemblyLoadingContext; |
| 58 | + |
| 59 | + public PluginHost() |
| 60 | + { |
| 61 | + _pluginAssemblyLoadingContext = new AssemblyLoadContext("PluginAssemblyContext", isCollectible: true); |
| 62 | + } |
| 63 | + public static void RegisterPlugin(TPlugin plugin, Action<IMSEPlugin> registerCallback = null) |
| 64 | + { |
| 65 | + if (PluginList.Contains(plugin)) |
| 66 | + Logs.Warn($"Plugin:{plugin.Name} already loaded."); |
| 67 | + else |
| 68 | + { |
| 69 | + try |
| 70 | + { |
| 71 | + plugin.Initialize(); |
| 72 | + PluginList.Add(plugin); |
| 73 | + registerCallback?.Invoke(plugin); |
| 74 | + } |
| 75 | + catch (Exception ex) |
| 76 | + { |
| 77 | + Logs.Warn($"Failed to initialize plugin: {plugin.Name}{Environment.NewLine}{ex}"); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + public void LoadPlugins(string pluginPath, Action<IMSEPlugin> registerCallback = null) |
| 82 | + { |
| 83 | + LoadPlugins(FindAssemliesWithPlugins(pluginPath), registerCallback); |
| 84 | + } |
| 85 | + public void LoadPlugins(IReadOnlyCollection<string> assembliesWithPlugins, Action<IMSEPlugin> registerCallback = null) |
| 86 | + { |
| 87 | + foreach (var assemblyPath in assembliesWithPlugins) |
| 88 | + { |
| 89 | + var assembly = _pluginAssemblyLoadingContext.LoadFromAssemblyPath(assemblyPath); |
| 90 | + var validPluginTypes = GetPluginTypes(assembly); |
| 91 | + foreach (var pluginType in validPluginTypes) |
| 92 | + { |
| 93 | + var pluginInstance = (TPlugin)Activator.CreateInstance(pluginType); |
| 94 | + RegisterPlugin(pluginInstance, registerCallback); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + /// <summary> |
| 99 | + /// 寻找存在指定类型的程序集路径 |
| 100 | + /// </summary> |
| 101 | + /// <param name="path">路径</param> |
| 102 | + /// <returns></returns> |
| 103 | + public static IReadOnlyCollection<string> FindAssemliesWithPlugins(string path) |
| 104 | + { |
| 105 | + var assemblies = Directory.GetFiles(path, "*.dll"); |
| 106 | + var assemblyPluginInfos = new List<string>(); |
| 107 | + var pluginFinderAssemblyContext = new AssemblyLoadContext(name: "PluginFinderAssemblyContext", isCollectible: true); |
| 108 | + foreach (var assemblyPath in assemblies) |
| 109 | + { |
| 110 | + var assembly = pluginFinderAssemblyContext.LoadFromAssemblyPath(assemblyPath); |
| 111 | + if (GetPluginTypes(assembly).Any()) |
| 112 | + assemblyPluginInfos.Add(assembly.Location); |
| 113 | + } |
| 114 | + pluginFinderAssemblyContext.Unload(); |
| 115 | + return assemblyPluginInfos; |
| 116 | + } |
| 117 | + public static IReadOnlyCollection<Type> GetPluginTypes(Assembly assembly) |
| 118 | + { |
| 119 | + return assembly.GetTypes() |
| 120 | + .Where(type => |
| 121 | + !type.IsAbstract && |
| 122 | + typeof(TPlugin).IsAssignableFrom(type)) |
| 123 | + .ToArray(); |
| 124 | + } |
| 125 | + public void Unload() |
| 126 | + { |
| 127 | + _pluginAssemblyLoadingContext.Unload(); |
| 128 | + _pluginAssemblyLoadingContext = null; |
| 129 | + } |
60 | 130 | }
|
| 131 | + #endregion |
61 | 132 | }
|
62 | 133 | }
|
0 commit comments