1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using RomCheater.PluginFramework.Interfaces; |
6 |
using RomCheater.Logging; |
7 |
using System.IO; |
8 |
using System.Reflection; |
9 |
|
10 |
namespace RomCheater.PluginFramework.Core |
11 |
{ |
12 |
public class PluginLoader : IPluginLoader |
13 |
{ |
14 |
public PluginLoader() |
15 |
{ |
16 |
LoadedConfigPlugins = new List<IConfigPlugin>(); |
17 |
LoadedInputPlugins = new List<IInputPlugin>(); |
18 |
LoadedWindowPlugins = new List<IWindowPlugin>(); |
19 |
} |
20 |
#region IPluginLoader Members |
21 |
public void LoadPlugins() { LoadPlugins(false); } |
22 |
public void LoadPlugins(bool silent) |
23 |
{ |
24 |
try |
25 |
{ |
26 |
if (!silent) |
27 |
logger.Info.WriteLine("Loading Plugins..."); |
28 |
|
29 |
string PluginPath = string.Format(@"{0}\Plugins", typeof(PluginLoader).Assembly.Location.Replace(@"\RomCheater.PluginFramework.dll", "")); |
30 |
if (!silent) |
31 |
logger.Debug.WriteLine("Plugins Path: {0}", PluginPath); |
32 |
List<string> dlls = new List<string>(Directory.GetFiles(PluginPath, "*.dll")); |
33 |
if (!silent) |
34 |
logger.Debug.WriteLine(" Found: {0} plugin dlls", dlls.Count); |
35 |
foreach (string dll in dlls) |
36 |
{ |
37 |
FileInfo fi = new FileInfo(dll); |
38 |
if (!silent) |
39 |
logger.Debug.WriteLine(" plugin[{0}]: {1}", dlls.IndexOf(dll), fi.Name); |
40 |
GetConfigPluginsFromDll(fi); |
41 |
GetInputPluginsFromDll(fi); |
42 |
GetWindowPluginsFromDll(fi); |
43 |
} |
44 |
|
45 |
if (!silent) |
46 |
logger.Info.WriteLine(" Loaded {0} config plugins", LoadedConfigPlugins.Count); |
47 |
if (!silent) |
48 |
logger.Info.WriteLine(" Loaded {0} input plugins", LoadedInputPlugins.Count); |
49 |
if (!silent) |
50 |
logger.Info.WriteLine(" Loaded {0} window plugins", LoadedWindowPlugins.Count); |
51 |
if (!silent) |
52 |
logger.Info.WriteLine("Plugins Loaded."); |
53 |
} |
54 |
catch (ReflectionTypeLoadException ex) |
55 |
{ |
56 |
StringBuilder builder = new StringBuilder(); |
57 |
if (ex.LoaderExceptions.Count() > 0) |
58 |
{ |
59 |
foreach (Exception c in ex.LoaderExceptions) |
60 |
{ |
61 |
builder.AppendLine(c.ToString()); |
62 |
} |
63 |
} |
64 |
if (!silent) |
65 |
logger.Error.WriteLine("Failed to load one or more plugins{0}Possible Reason:{0}{1}", System.Environment.NewLine, builder.ToString()); |
66 |
} |
67 |
catch (Exception ex) |
68 |
{ |
69 |
if (!silent) |
70 |
logger.Error.WriteLine("Failed to load one or more plugins{0}Possible Reason:{0}{1}", System.Environment.NewLine, ex.ToString()); |
71 |
} |
72 |
} |
73 |
public List<IConfigPlugin> LoadedConfigPlugins { get; private set; } |
74 |
public List<IInputPlugin> LoadedInputPlugins { get; private set; } |
75 |
public List<IWindowPlugin> LoadedWindowPlugins { get; private set; } |
76 |
|
77 |
public IConfigPlugin GetConfigPlugin(string t) |
78 |
{ |
79 |
foreach (IConfigPlugin c in LoadedConfigPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } } |
80 |
return GetGenericConfigPlugin(); |
81 |
} |
82 |
private IConfigPlugin GetGenericConfigPlugin() |
83 |
{ |
84 |
foreach (IConfigPlugin c in LoadedConfigPlugins) { if (c.ID.ToString().ToLower() == "478e225b-c3e8-9280-57ca-384b884fc4cc".ToLower()) { return c; } } |
85 |
return null; |
86 |
} |
87 |
public IInputPlugin GetInputPlugin(string t) |
88 |
{ |
89 |
foreach (IInputPlugin c in LoadedInputPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } } |
90 |
return GetGenericInputPlugin(); |
91 |
} |
92 |
private IInputPlugin GetGenericInputPlugin() |
93 |
{ |
94 |
foreach (IInputPlugin c in LoadedInputPlugins) { } |
95 |
return null; |
96 |
} |
97 |
public IWindowPlugin GetWindowPlugin(string t) |
98 |
{ |
99 |
foreach (IWindowPlugin c in LoadedWindowPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } } |
100 |
return GetGenericWindowPlugin(); |
101 |
} |
102 |
private IWindowPlugin GetGenericWindowPlugin() |
103 |
{ |
104 |
foreach (IWindowPlugin c in LoadedWindowPlugins) { } |
105 |
return null; |
106 |
} |
107 |
#endregion |
108 |
|
109 |
private void GetConfigPluginsFromDll(FileInfo dll) |
110 |
{ |
111 |
logger.Debug.WriteLine(" Getting Config plugins contained in {0}", dll.Name); |
112 |
Assembly asm = Assembly.LoadFile(dll.FullName); |
113 |
List<Type> types = new List<Type>(asm.GetTypes()); |
114 |
foreach (Type type in types) |
115 |
{ |
116 |
if (type.BaseType == typeof(ConfigPlugin)) |
117 |
{ |
118 |
ConstructorInfo ci = null; |
119 |
ci = type.GetConstructor(new Type[] { }); |
120 |
if (ci == null) |
121 |
{ |
122 |
throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name)); |
123 |
} |
124 |
else |
125 |
{ |
126 |
object o = ci.Invoke(new object[] { }); |
127 |
IConfigPlugin c = (IConfigPlugin)o; |
128 |
if (c == null) |
129 |
{ |
130 |
throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name)); |
131 |
} |
132 |
else |
133 |
{ |
134 |
logger.Debug.WriteLine(" Loaded Config Plugin [name={0}] from {1}", c.Name, dll.Name); |
135 |
LoadedConfigPlugins.Add(c); |
136 |
} |
137 |
} |
138 |
} |
139 |
} |
140 |
} |
141 |
private void GetInputPluginsFromDll(FileInfo dll) |
142 |
{ |
143 |
logger.Debug.WriteLine(" Getting Input plugins contained in {0}", dll.Name); |
144 |
Assembly asm = Assembly.LoadFile(dll.FullName); |
145 |
List<Type> types = new List<Type>(asm.GetTypes()); |
146 |
foreach (Type type in types) |
147 |
{ |
148 |
if (type.BaseType == typeof(InputPlugin)) |
149 |
{ |
150 |
ConstructorInfo ci = null; |
151 |
ci = type.GetConstructor(new Type[] { }); |
152 |
if (ci == null) |
153 |
{ |
154 |
throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name)); |
155 |
} |
156 |
else |
157 |
{ |
158 |
object o = ci.Invoke(new object[] { }); |
159 |
IInputPlugin c = (IInputPlugin)o; |
160 |
if (c == null) |
161 |
{ |
162 |
throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name)); |
163 |
} |
164 |
else |
165 |
{ |
166 |
logger.Debug.WriteLine(" Loaded Input Plugin [name={0}] from {1}", c.Name, dll.Name); |
167 |
LoadedInputPlugins.Add(c); |
168 |
} |
169 |
} |
170 |
} |
171 |
} |
172 |
} |
173 |
private void GetWindowPluginsFromDll(FileInfo dll) |
174 |
{ |
175 |
logger.Debug.WriteLine(" Getting Window plugins contained in {0}", dll.Name); |
176 |
Assembly asm = Assembly.LoadFile(dll.FullName); |
177 |
List<Type> types = new List<Type>(asm.GetTypes()); |
178 |
foreach (Type type in types) |
179 |
{ |
180 |
if (type.BaseType == typeof(WindowPlugin)) |
181 |
{ |
182 |
ConstructorInfo ci = null; |
183 |
ci = type.GetConstructor(new Type[] { }); |
184 |
if (ci == null) |
185 |
{ |
186 |
throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name)); |
187 |
} |
188 |
else |
189 |
{ |
190 |
object o = ci.Invoke(new object[] { }); |
191 |
IWindowPlugin c = (IWindowPlugin)o; |
192 |
if (c == null) |
193 |
{ |
194 |
throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name)); |
195 |
} |
196 |
else |
197 |
{ |
198 |
logger.Debug.WriteLine(" Loaded Window Plugin [name={0}] from {1}", c.Name, dll.Name); |
199 |
LoadedWindowPlugins.Add(c); |
200 |
} |
201 |
} |
202 |
} |
203 |
} |
204 |
} |
205 |
} |
206 |
} |