forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginManager.cs
More file actions
173 lines (150 loc) · 6 KB
/
Copy pathPluginManager.cs
File metadata and controls
173 lines (150 loc) · 6 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using L2dotNET.Logging.Abstraction;
namespace L2dotNET.Plugins
{
public class PluginManager
{
private static readonly ILog Log = LogProvider.GetCurrentClassLogger();
public List<object> Plugins { get; } = new List<object>();
private string _currentPath;
private static volatile PluginManager instance;
private static readonly object syncRoot = new object();
public static PluginManager Instance
{
get
{
if (instance != null)
return instance;
lock (syncRoot)
{
if (instance == null)
instance = new PluginManager();
}
return instance;
}
}
public void Initialize(GameServer server)
{
LoadPlugins();
ExecuteStartup(server);
EnablePlugins(server);
}
internal void LoadPlugins()
{
// Default it is the directory we are executing, and below.
string pluginDirectoryPaths = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
pluginDirectoryPaths = @"./Plugins/";//Config.GetProperty("PluginDirectory", pluginDirectoryPaths);
foreach (string dirPath in pluginDirectoryPaths.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
{
if (dirPath == null) continue;
string pluginDirectory = Path.GetFullPath(dirPath);
_currentPath = pluginDirectory;
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += MyResolveEventHandler;
List<string> pluginPaths = new List<string>();
pluginPaths.AddRange(Directory.GetFiles(pluginDirectory, "*.dll", SearchOption.AllDirectories));
pluginPaths.AddRange(Directory.GetFiles(pluginDirectory, "*.exe", SearchOption.AllDirectories));
foreach (string pluginPath in pluginPaths)
{
Assembly newAssembly = Assembly.LoadFile(pluginPath);
Type[] types = newAssembly.GetExportedTypes();
foreach (Type type in types)
{
try
{
// If no PluginAttribute and does not implement IPlugin interface, not a valid plugin
if (!type.IsDefined(typeof(PluginAttribute), true) && !typeof(IPlugin).IsAssignableFrom(type)) continue;
if (type.IsDefined(typeof(PluginAttribute), true))
{
PluginAttribute pluginAttribute = Attribute.GetCustomAttribute(type, typeof(PluginAttribute), true) as PluginAttribute;
if (pluginAttribute != null)
{
// if (!Config.GetProperty(pluginAttribute.PluginName + ".Enabled", true)) continue;
}
}
ConstructorInfo ctor = type.GetConstructor(Type.EmptyTypes);
if (ctor == null)
continue;
object plugin = ctor.Invoke(null);
Plugins.Add(plugin);
}
catch (Exception ex)
{
Log.WarnFormat($"Failed loading plugin type {type} as a plugin.");
Log.DebugException("Plugin loader caught exception, but is moving on.", ex);
}
}
}
}
}
private Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
{
if (_currentPath == null) return null;
try
{
AssemblyName name = new AssemblyName(args.Name);
string assemblyPath = _currentPath + "\\" + name.Name + ".dll";
return Assembly.LoadFile(assemblyPath);
}
catch (Exception)
{
try
{
AssemblyName name = new AssemblyName(args.Name);
string assemblyPath = _currentPath + "\\" + name.Name + ".exe";
return Assembly.LoadFile(assemblyPath);
}
catch (Exception)
{
return Assembly.LoadFile(args.Name + ".dll");
}
}
}
internal void ExecuteStartup(GameServer server)
{
foreach (IStartup startupClass in Plugins.OfType<IStartup>())
{
try
{
startupClass.Configure(server);
}
catch (Exception ex)
{
Log.WarnException("Execute Startup class failed", ex);
}
}
}
internal void EnablePlugins(GameServer server)
{
foreach (IPlugin enablingPlugin in Plugins.ToArray().OfType<IPlugin>())
{
try
{
enablingPlugin.OnEnable(new PluginContext(server, this));
}
catch (Exception ex)
{
Log.WarnException("On enable plugin", ex);
}
}
}
internal void DisablePlugins()
{
foreach (IPlugin enablingPlugin in Plugins.OfType<IPlugin>())
{
try
{
enablingPlugin.OnDisable();
}
catch (Exception ex)
{
Log.WarnException("On disable plugin", ex);
}
}
}
}
}