-
Notifications
You must be signed in to change notification settings - Fork 5
/
PluginLoader.cs
149 lines (123 loc) · 4.98 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
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.ComponentModel;
using SimplePlugins.Exceptions;
using System.Collections.Generic;
using SimplePlugins;
using SimplePlugins.Loaders;
namespace SimplePlugins.Loaders
{
[Serializable]
public class PluginLoader : PluginLoaderBase
{
protected const string PYTHON_PROXY_PLUGIN_FILE_NAME = "SimplePlugins.IronPython.dll";
Assembly _loadedPluginAssembly;
public PluginLoader()
: base()
{
}
protected override PluginInfo OnGetInfo(string pluginFileName)
{
PluginInfo.PluginTypes pluginType = PluginInfo.DeterminePluginType(pluginFileName);
AssemblyName assName = null;
if (pluginType == PluginInfo.PluginTypes.PythonScript)
{
string pythonProxyPluginPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
pythonProxyPluginPath = Path.Combine(pythonProxyPluginPath, PluginLoader.PYTHON_PROXY_PLUGIN_FILE_NAME);
assName = AssemblyName.GetAssemblyName(pythonProxyPluginPath);
}
else
assName = AssemblyName.GetAssemblyName(pluginFileName);
_loadedPluginAssembly = AppDomain.CurrentDomain.Load(assName);
Type[] types = _loadedPluginAssembly.GetTypes();
Type pluginBaseType = typeof(PluginBase);
foreach (Type t in types)
{
if (t.IsSubclassOf(pluginBaseType))
{
PluginBase pi = null;
pi = (PluginBase)Activator.CreateInstance(t, pluginFileName);
PluginInfo info = new PluginInfo(pi);
return info;
}
}
return null;
}
protected override PluginInfoList OnProbe(string pluginFolderPath, SearchOption scanDeapth)
{
PluginInfoList list = new PluginInfoList();
string[] exes = Directory.GetFiles(pluginFolderPath, "*.exe", scanDeapth);
string[] dlls = Directory.GetFiles(pluginFolderPath, "*.dll", scanDeapth);
string[] pys = Directory.GetFiles(pluginFolderPath, "*.py", scanDeapth);
List<string> files = new List<string>();
files.AddRange(exes);
files.AddRange(dlls);
files.AddRange(pys);
foreach (string file in files)
{
try
{
PluginInfo info = this.OnGetInfo(file);
if (info != null)
list.Add(info);
}
catch (Exception) { }
}
return list;
}
protected override PluginInfo OnLoad(string pluginFileName)
{
if (File.Exists(pluginFileName))
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
PluginInfo info = this.OnGetInfo(pluginFileName);
if (info == null)
throw new InvalidPluginFileException(pluginFileName);
else
{
this.LoadedPlugin = (PluginBase)Activator.CreateInstance(_loadedPluginAssembly.GetType(info.PluginTypeName), pluginFileName);
return info;
}
}
else
throw new FileNotFoundException("Specified assembly file '" + pluginFileName + "' does not exist.", pluginFileName);
}
protected override PluginParameters OnExecute(PluginParameters args)
{
return this.LoadedPlugin.Main(args);
}
protected override void OnAbort()
{
if (this.LoadedPlugin != null)
this.LoadedPlugin.OnAbort();
}
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly asm in loadedAssemblies)
{
if (asm.FullName.Equals(args.Name))
return asm;
}
AssemblyName asmInfo = new AssemblyName(args.Name);
for (int i = 0; i < this.AssemblyResolutionPaths.Count; i++)
{
string asmFilePath = Path.Combine(this.AssemblyResolutionPaths[i], asmInfo.Name + ".dll");
if (File.Exists(asmFilePath))
return Assembly.LoadFrom(asmFilePath);
else
{
asmFilePath = Path.Combine(this.AssemblyResolutionPaths[i], asmInfo.Name + ".exe");
if (File.Exists(asmFilePath))
return Assembly.LoadFrom(asmFilePath);
}
}
return null;
}
}
}