forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginLoader.cs
281 lines (232 loc) · 9.49 KB
/
PluginLoader.cs
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
using log4net;
using MissionPlanner.Utilities;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using MissionPlanner.Controls;
namespace MissionPlanner.Plugin
{
public class PluginLoader
{
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
//List of disabled plugins (as dll file names)
public static List<String> DisabledPluginNames = new List<String>();
// Plugin enable/disable settings changed not loaded but enabled plugins will not shown
public static bool bRestartRequired = false;
public static List<Plugin> LoadingPlugins = new List<Plugin>();
public static List<Plugin> Plugins = new List<Plugin>();
public static Dictionary<string, string[]> filecache = new Dictionary<string, string[]>();
static Assembly LoadFromSameFolder(object sender, ResolveEventArgs args)
{
if (args.RequestingAssembly == null)
return null;
// check install folder
string folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (filecache.ContainsKey(folderPath))
{
}
else
{
string[] search1 = Directory.GetFiles(folderPath, "*.dll",
SearchOption.AllDirectories);
filecache[folderPath] = search1;
}
foreach (var file in filecache[folderPath].Where(a => a.ToLower().Contains(new AssemblyName(args.Name).Name.ToLower() + ".dll")))
{
try
{
Assembly assembly = Assembly.LoadFrom(file);
if (assembly.FullName == args.Name)
return assembly;
}
catch { }
}
// check local directory
folderPath = Path.GetDirectoryName(args.RequestingAssembly.Location);
if (filecache.ContainsKey(folderPath))
{
}
else
{
string[] search1 = Directory.GetFiles(folderPath, "*.dll",
SearchOption.AllDirectories);
filecache[folderPath] = search1;
}
foreach (var file in filecache[folderPath].Where(a => a.ToLower().Contains(new AssemblyName(args.Name).Name.ToLower() + ".dll")))
{
try
{
Assembly assembly = Assembly.LoadFrom(file);
if (assembly.FullName == args.Name)
return assembly;
}
catch { }
}
log.Info("LoadFromSameFolder " + args.RequestingAssembly + "-> " + args.Name);
return null;
}
public static void Load(String file)
{
if (!File.Exists(file) || !file.EndsWith(".dll", true, null) ||
file.ToLower().Contains("microsoft.") ||
file.ToLower().Contains("system.") ||
file.ToLower().Contains("missionplanner.grid.dll") ||
file.ToLower().Contains("usbserialforandroid")
)
return;
//Check if it is disabled (moved out from the previous IF, to make it loggable)
if (DisabledPluginNames.Contains(Path.GetFileName(file).ToLower()))
{
log.InfoFormat("Plugin {0} is disabled in config.xml", Path.GetFileName(file));
return;
}
// file exists in the install directory, so skip trying to load it as a plugin
if (File.Exists(file) && File.Exists(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) +
Path.DirectorySeparatorChar + Path.GetFileName(file)))
return;
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += new ResolveEventHandler(LoadFromSameFolder);
Assembly asm = null;
DateTime startDateTime = DateTime.Now;
try
{
asm = Assembly.LoadFile(file);
log.Info("Plugin Load " + file);
}
catch (Exception)
{
// unable to load
return;
}
InitPlugin(asm, file);
log.InfoFormat("Plugin Load {0} time {1} s", file, (DateTime.Now - startDateTime).TotalSeconds);
}
public static void InitPlugin(Assembly asm, string pluginfilename)
{
if (asm == null)
return;
Type pluginInfo = null;
try
{
Type[] types = asm.GetTypes();
Type type = typeof(MissionPlanner.Plugin.Plugin);
foreach (var t in types)
if (type.IsAssignableFrom((Type) t))
{
pluginInfo = t;
break;
}
if (pluginInfo != null)
{
Object o = Activator.CreateInstance(pluginInfo, BindingFlags.Default, null, null,
CultureInfo.CurrentUICulture);
Plugin plugin = (Plugin) o;
plugin.Assembly = asm;
plugin.Host = new PluginHost();
plugin.FileName = Path.GetFileName(pluginfilename);
if (plugin.Init())
{
log.InfoFormat("Plugin Init {0} {1} by {2}", plugin.Name, plugin.Version, plugin.Author);
lock (LoadingPlugins)
{
LoadingPlugins.Add(plugin);
}
}
}
}
catch (Exception ex)
{
log.Error("Failed to load plugin " + asm.FullName, ex);
}
}
public static void LoadAll()
{
string path = Settings.GetRunningDirectory() + "plugins" +
Path.DirectorySeparatorChar;
log.Info("Plugin path: "+path);
if (!Directory.Exists(path))
return;
// cs plugins are background compiled, and loaded in the ui thread
Task.Run(() =>
{
String[] csFiles = Directory.GetFiles(path, "*.cs");
foreach (var csFile in csFiles)
{
log.Info("Plugin: " + csFile);
//Check if it is disabled (moved out from the previous IF, to make it loggable)
if (DisabledPluginNames.Contains(Path.GetFileName(csFile).ToLower()))
{
log.InfoFormat("Plugin {0} is disabled in config.xml", Path.GetFileName(csFile));
continue;
}
try
{
//csharp 5 max
// create a compiler
var compiler = CodeGen.CreateCompiler();
// get all the compiler parameters
var parms = CodeGen.CreateCompilerParameters();
// compile the code into an assembly
var results = CodeGen.CompileCodeFile(compiler, parms, csFile);
InitPlugin(results?.CompiledAssembly, Path.GetFileName(csFile));
if (results?.CompiledAssembly != null)
continue;
}
catch (Exception ex)
{
log.Error(ex);
}
try
{
// csharp 8
var ans = CodeGenRoslyn.BuildCode(csFile);
InitPlugin(ans, Path.GetFileName(csFile));
continue;
}
catch (Exception ex)
{
log.Error(ex);
}
}
MainV2.instance.BeginInvokeIfRequired(() =>
{
PluginInit();
});
});
String[] files = Directory.GetFiles(path, "*.dll");
foreach (var s in files)
Load(Path.Combine(Environment.CurrentDirectory, s));
PluginInit();
}
private static void PluginInit()
{
List<Plugin> LoadingSnapshot;
lock (LoadingPlugins)
{
LoadingSnapshot = LoadingPlugins.ToList();
LoadingPlugins.Clear();
}
foreach (var p in LoadingSnapshot)
{
try
{
if (p.Loaded())
{
lock (Plugins)
{
Plugins.Add(p);
}
}
}
catch (Exception ex)
{
log.Error(ex);
}
}
}
}
}