1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using RomCheater.Core; |
6 |
using System.Reflection; |
7 |
using System.IO; |
8 |
|
9 |
namespace RomCheater.PluginFramework.Core |
10 |
{ |
11 |
|
12 |
#region Static plugin defintions |
13 |
public interface IBasePluginDefinition : IPluginName, IPluginDescription, IPluginID, IToString { } |
14 |
internal class BasePluginDefinition : IBasePluginDefinition |
15 |
{ |
16 |
public BasePluginDefinition() : this(string.Empty) { } |
17 |
public BasePluginDefinition(string name) : this(name, string.Empty) { } |
18 |
public BasePluginDefinition(Guid guid) : this(string.Empty, guid) { } |
19 |
public BasePluginDefinition(string name, string decription) : this(name, decription, new Guid()) { } |
20 |
public BasePluginDefinition(string name, Guid guid) : this(name, string.Empty, guid) { } |
21 |
public BasePluginDefinition(string name, string decription, Guid guid) { this.Name = name; this.Description = decription; this.ID = guid; } |
22 |
#region IBasePluginDefinition Members |
23 |
private string _Name; |
24 |
public string Name |
25 |
{ |
26 |
get { return _Name; } |
27 |
protected set { _Name = value; } |
28 |
} |
29 |
private string _Description; |
30 |
public string Description |
31 |
{ |
32 |
get { return _Description; } |
33 |
protected set { _Description = value; } |
34 |
} |
35 |
private Guid _ID; |
36 |
public Guid ID |
37 |
{ |
38 |
get { return _ID; } |
39 |
protected set { _ID = value; } |
40 |
} |
41 |
public override string ToString() |
42 |
{ |
43 |
return string.Format("{0} [{1}]", Name, ID.ToString()); |
44 |
} |
45 |
#endregion |
46 |
} |
47 |
|
48 |
|
49 |
public enum PluginType |
50 |
{ |
51 |
Config, |
52 |
Input, |
53 |
Window, |
54 |
UserControl, |
55 |
Unknown = int.MaxValue, |
56 |
} |
57 |
|
58 |
public enum PluginNames |
59 |
{ |
60 |
#region Config Plugins |
61 |
GenericConfig, |
62 |
#endregion |
63 |
#region Input Plugins |
64 |
NullInputPlugin, |
65 |
#endregion |
66 |
#region Windows Plugins |
67 |
NullWindowPlugin, |
68 |
#endregion |
69 |
|
70 |
#region userControl Plugins |
71 |
CheatCodeConverterPlugin, |
72 |
EmuMemoryMapPlugin, |
73 |
RVACheatListPlugin, |
74 |
ScratchPadPlugin, |
75 |
#endregion |
76 |
|
77 |
Unknown = int.MaxValue, |
78 |
} |
79 |
|
80 |
#region Plugin Impelementation Classes |
81 |
public static class PluginCollection |
82 |
{ |
83 |
#region ConfigPlugins |
84 |
internal static class ConfigPlugins |
85 |
{ |
86 |
private const PluginType PluginType = Core.PluginType.Config; |
87 |
#region GenericConfig |
88 |
internal class GenericConfig |
89 |
{ |
90 |
private const PluginNames PluginName = Core.PluginNames.GenericConfig; |
91 |
const string dll = @"plugins\RomCheater.CorePlugins.dll"; |
92 |
private static BasePluginDefinition bpd = null; |
93 |
static GenericConfig() |
94 |
{ |
95 |
// this is where we would load the plugin |
96 |
FileInfo fi = new FileInfo(dll); |
97 |
Assembly asm = Assembly.LoadFile(fi.FullName); |
98 |
List<Type> types = new List<Type>(asm.GetTypes()); |
99 |
foreach (Type type in types) |
100 |
{ |
101 |
if (type.Name == "GenericConfig") |
102 |
{ |
103 |
ConstructorInfo ci = null; |
104 |
ci = type.GetConstructor(new Type[] { }); |
105 |
if (ci == null) |
106 |
{ |
107 |
throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name)); |
108 |
} |
109 |
else |
110 |
{ |
111 |
object o = ci.Invoke(new object[] { }); |
112 |
IConfigPlugin c = (IConfigPlugin)o; |
113 |
if (c == null) |
114 |
{ |
115 |
throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name)); |
116 |
} |
117 |
else |
118 |
{ |
119 |
bpd = new BasePluginDefinition(c.Name, c.Description, c.ID); |
120 |
} |
121 |
} |
122 |
break; |
123 |
} |
124 |
else |
125 |
{ |
126 |
continue; |
127 |
} |
128 |
} |
129 |
} |
130 |
public static IBasePluginDefinition Plugin { get { return (bpd as IBasePluginDefinition); } } |
131 |
} |
132 |
#endregion |
133 |
} |
134 |
#endregion |
135 |
#region InputPlugins |
136 |
internal static class InputPlugins |
137 |
{ |
138 |
private const PluginType PluginType = Core.PluginType.Input; |
139 |
#region NullInputPlugin |
140 |
internal class NullInputPlugin |
141 |
{ |
142 |
private const PluginNames PluginName = Core.PluginNames.NullInputPlugin; |
143 |
const string dll = @"RomCheater.PluginFramework.dll"; |
144 |
private static BasePluginDefinition bpd = null; |
145 |
static NullInputPlugin() |
146 |
{ |
147 |
// this is where we would load the plugin |
148 |
FileInfo fi = new FileInfo(dll); |
149 |
Assembly asm = Assembly.LoadFile(fi.FullName); |
150 |
List<Type> types = new List<Type>(asm.GetTypes()); |
151 |
foreach (Type type in types) |
152 |
{ |
153 |
if (type.Name == "NullInputPlugin") |
154 |
{ |
155 |
ConstructorInfo ci = null; |
156 |
ci = type.GetConstructor(new Type[] { }); |
157 |
if (ci == null) |
158 |
{ |
159 |
throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name)); |
160 |
} |
161 |
else |
162 |
{ |
163 |
object o = ci.Invoke(new object[] { }); |
164 |
IInputPlugin c = (IInputPlugin)o; |
165 |
if (c == null) |
166 |
{ |
167 |
throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name)); |
168 |
} |
169 |
else |
170 |
{ |
171 |
bpd = new BasePluginDefinition(c.Name, c.Description, c.ID); |
172 |
} |
173 |
} |
174 |
break; |
175 |
} |
176 |
else |
177 |
{ |
178 |
continue; |
179 |
} |
180 |
} |
181 |
} |
182 |
public static IBasePluginDefinition Plugin { get { return (bpd as IBasePluginDefinition); } } |
183 |
} |
184 |
#endregion |
185 |
} |
186 |
#endregion |
187 |
#region WindowPlugins |
188 |
internal static class WindowPlugins |
189 |
{ |
190 |
private const PluginType PluginType = Core.PluginType.Window; |
191 |
#region NullWindowPlugin |
192 |
internal class NullWindowPlugin |
193 |
{ |
194 |
private const PluginNames PluginName = Core.PluginNames.NullWindowPlugin; |
195 |
const string dll = @"RomCheater.PluginFramework.dll"; |
196 |
private static BasePluginDefinition bpd = null; |
197 |
static NullWindowPlugin() |
198 |
{ |
199 |
// this is where we would load the plugin |
200 |
FileInfo fi = new FileInfo(dll); |
201 |
Assembly asm = Assembly.LoadFile(fi.FullName); |
202 |
List<Type> types = new List<Type>(asm.GetTypes()); |
203 |
foreach (Type type in types) |
204 |
{ |
205 |
if (type.Name == "NullWindowPlugin") |
206 |
{ |
207 |
ConstructorInfo ci = null; |
208 |
ci = type.GetConstructor(new Type[] { }); |
209 |
if (ci == null) |
210 |
{ |
211 |
throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name)); |
212 |
} |
213 |
else |
214 |
{ |
215 |
object o = ci.Invoke(new object[] { }); |
216 |
IWindowPlugin c = (IWindowPlugin)o; |
217 |
if (c == null) |
218 |
{ |
219 |
throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name)); |
220 |
} |
221 |
else |
222 |
{ |
223 |
bpd = new BasePluginDefinition(c.Name, c.Description, c.ID); |
224 |
} |
225 |
} |
226 |
break; |
227 |
} |
228 |
else |
229 |
{ |
230 |
continue; |
231 |
} |
232 |
} |
233 |
} |
234 |
public static IBasePluginDefinition Plugin { get { return (bpd as IBasePluginDefinition); } } |
235 |
} |
236 |
#endregion |
237 |
} |
238 |
#endregion |
239 |
#region UserControlPlugins |
240 |
internal static class UserControlPlugins |
241 |
{ |
242 |
private const PluginType PluginType = Core.PluginType.UserControl; |
243 |
#region CheatCodePlugion |
244 |
internal class CheatCodePlugin |
245 |
{ |
246 |
private const PluginNames PluginName = Core.PluginNames.CheatCodeConverterPlugin; |
247 |
const string dll = @"plugins\RomCheater.CheatPlugin.dll"; |
248 |
private static BasePluginDefinition bpd = null; |
249 |
static CheatCodePlugin() |
250 |
{ |
251 |
// this is where we would load the plugin |
252 |
FileInfo fi = new FileInfo(dll); |
253 |
Assembly asm = Assembly.LoadFile(fi.FullName); |
254 |
List<Type> types = new List<Type>(asm.GetTypes()); |
255 |
foreach (Type type in types) |
256 |
{ |
257 |
if (type.Name == "CheatCodePlugin") |
258 |
{ |
259 |
ConstructorInfo ci = null; |
260 |
ci = type.GetConstructor(new Type[] { }); |
261 |
if (ci == null) |
262 |
{ |
263 |
throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name)); |
264 |
} |
265 |
else |
266 |
{ |
267 |
object o = ci.Invoke(new object[] { }); |
268 |
IUserControlPlugin c = (IUserControlPlugin)o; |
269 |
if (c == null) |
270 |
{ |
271 |
throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name)); |
272 |
} |
273 |
else |
274 |
{ |
275 |
bpd = new BasePluginDefinition(c.Name, c.Description, c.ID); |
276 |
} |
277 |
} |
278 |
break; |
279 |
} |
280 |
else |
281 |
{ |
282 |
continue; |
283 |
} |
284 |
} |
285 |
} |
286 |
public static IBasePluginDefinition Plugin { get { return (bpd as IBasePluginDefinition); } } |
287 |
} |
288 |
#endregion |
289 |
#region EmuMMAPPlugin |
290 |
internal class EmuMMAPPlugin |
291 |
{ |
292 |
private const PluginNames PluginName = Core.PluginNames.EmuMemoryMapPlugin; |
293 |
const string dll = @"plugins\RomCheater.EmuMMAPPlugin.dll"; |
294 |
private static BasePluginDefinition bpd = null; |
295 |
static EmuMMAPPlugin() |
296 |
{ |
297 |
// this is where we would load the plugin |
298 |
FileInfo fi = new FileInfo(dll); |
299 |
Assembly asm = Assembly.LoadFile(fi.FullName); |
300 |
List<Type> types = new List<Type>(asm.GetTypes()); |
301 |
foreach (Type type in types) |
302 |
{ |
303 |
if (type.Name == "EmuMMAPPlugin") |
304 |
{ |
305 |
ConstructorInfo ci = null; |
306 |
ci = type.GetConstructor(new Type[] { }); |
307 |
if (ci == null) |
308 |
{ |
309 |
throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name)); |
310 |
} |
311 |
else |
312 |
{ |
313 |
object o = ci.Invoke(new object[] { }); |
314 |
IUserControlPlugin c = (IUserControlPlugin)o; |
315 |
if (c == null) |
316 |
{ |
317 |
throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name)); |
318 |
} |
319 |
else |
320 |
{ |
321 |
bpd = new BasePluginDefinition(c.Name, c.Description, c.ID); |
322 |
} |
323 |
} |
324 |
break; |
325 |
} |
326 |
else |
327 |
{ |
328 |
continue; |
329 |
} |
330 |
} |
331 |
} |
332 |
public static IBasePluginDefinition Plugin { get { return (bpd as IBasePluginDefinition); } } |
333 |
} |
334 |
#endregion |
335 |
#region RVACalculatorPlugin |
336 |
internal class RVACalculatorPlugin |
337 |
{ |
338 |
private const PluginNames PluginName = Core.PluginNames.RVACheatListPlugin; |
339 |
const string dll = @"plugins\RomCheater.RVACheatList.dll"; |
340 |
private static BasePluginDefinition bpd = null; |
341 |
static RVACalculatorPlugin() |
342 |
{ |
343 |
// this is where we would load the plugin |
344 |
FileInfo fi = new FileInfo(dll); |
345 |
Assembly asm = Assembly.LoadFile(fi.FullName); |
346 |
List<Type> types = new List<Type>(asm.GetTypes()); |
347 |
foreach (Type type in types) |
348 |
{ |
349 |
if (type.Name == "RVACheatListPlugin") |
350 |
{ |
351 |
ConstructorInfo ci = null; |
352 |
ci = type.GetConstructor(new Type[] { }); |
353 |
if (ci == null) |
354 |
{ |
355 |
throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name)); |
356 |
} |
357 |
else |
358 |
{ |
359 |
object o = ci.Invoke(new object[] { }); |
360 |
IUserControlPlugin c = (IUserControlPlugin)o; |
361 |
if (c == null) |
362 |
{ |
363 |
throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name)); |
364 |
} |
365 |
else |
366 |
{ |
367 |
bpd = new BasePluginDefinition(c.Name, c.Description, c.ID); |
368 |
} |
369 |
} |
370 |
break; |
371 |
} |
372 |
else |
373 |
{ |
374 |
continue; |
375 |
} |
376 |
} |
377 |
} |
378 |
public static IBasePluginDefinition Plugin { get { return (bpd as IBasePluginDefinition); } } |
379 |
} |
380 |
#endregion |
381 |
#region ScratchPadPlugin |
382 |
internal class ScratchPadPlugin |
383 |
{ |
384 |
private const PluginNames PluginName = Core.PluginNames.ScratchPadPlugin; |
385 |
const string dll = @"plugins\RomCheater.ScratchPad.dll"; |
386 |
private static BasePluginDefinition bpd = null; |
387 |
static ScratchPadPlugin() |
388 |
{ |
389 |
// this is where we would load the plugin |
390 |
FileInfo fi = new FileInfo(dll); |
391 |
Assembly asm = Assembly.LoadFile(fi.FullName); |
392 |
List<Type> types = new List<Type>(asm.GetTypes()); |
393 |
foreach (Type type in types) |
394 |
{ |
395 |
if (type.Name == "ScratchPadPlugin") |
396 |
{ |
397 |
ConstructorInfo ci = null; |
398 |
ci = type.GetConstructor(new Type[] { }); |
399 |
if (ci == null) |
400 |
{ |
401 |
throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name)); |
402 |
} |
403 |
else |
404 |
{ |
405 |
object o = ci.Invoke(new object[] { }); |
406 |
IUserControlPlugin c = (IUserControlPlugin)o; |
407 |
if (c == null) |
408 |
{ |
409 |
throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name)); |
410 |
} |
411 |
else |
412 |
{ |
413 |
bpd = new BasePluginDefinition(c.Name, c.Description, c.ID); |
414 |
} |
415 |
} |
416 |
break; |
417 |
} |
418 |
else |
419 |
{ |
420 |
continue; |
421 |
} |
422 |
} |
423 |
} |
424 |
public static IBasePluginDefinition Plugin { get { return (bpd as IBasePluginDefinition); } } |
425 |
} |
426 |
#endregion |
427 |
} |
428 |
#endregion |
429 |
#region UnkownPlugin Support |
430 |
internal static class UnknownPlugins |
431 |
{ |
432 |
private const PluginType PluginType = Core.PluginType.Unknown; |
433 |
#region UnknownPlugin |
434 |
internal class UnknownPlugin |
435 |
{ |
436 |
private const PluginNames PluginName = Core.PluginNames.Unknown; |
437 |
private static List<IBasePluginDefinition> bpds = new List<IBasePluginDefinition>(); |
438 |
static UnknownPlugin() |
439 |
{ |
440 |
bpds = new List<IBasePluginDefinition>(); |
441 |
PluginLoader loader = new PluginLoader(); |
442 |
loader.LoadPlugins(true); |
443 |
var config_plugins = loader.LoadedConfigPlugins; |
444 |
var input_plugins = loader.LoadedInputPlugins; |
445 |
var window_plugins = loader.LoadedWindowPlugins; |
446 |
var user_plugins = loader.LoadedUserControlPlugins; |
447 |
|
448 |
config_plugins.ForEach(s => bpds.Add(new BasePluginDefinition(s.Name, s.Description, s.ID))); |
449 |
input_plugins.ForEach(s => bpds.Add(new BasePluginDefinition(s.Name, s.Description, s.ID))); |
450 |
window_plugins.ForEach(s => bpds.Add(new BasePluginDefinition(s.Name, s.Description, s.ID))); |
451 |
user_plugins.ForEach(s => bpds.Add(new BasePluginDefinition(s.Name, s.Description, s.ID))); |
452 |
|
453 |
} |
454 |
public static List<IBasePluginDefinition> Plugin { get { return (bpds as List<IBasePluginDefinition>); } } |
455 |
} |
456 |
#endregion |
457 |
} |
458 |
#endregion |
459 |
|
460 |
#region Name/Guid Collection |
461 |
public static IBasePluginDefinition GetPluginByName(PluginNames plugin_name) |
462 |
{ |
463 |
return GetPluginsByName(plugin_name).FirstOrDefault(); |
464 |
} |
465 |
public static List<IBasePluginDefinition> GetPluginsByName(PluginNames plugin_name) |
466 |
{ |
467 |
Type t = typeof(PluginCollection); |
468 |
List<Type> types = new List<Type>(t.GetNestedTypes(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)); |
469 |
|
470 |
var plugin_type = PluginType.Unknown; |
471 |
Type found_type = null; |
472 |
foreach (var type in types) |
473 |
{ |
474 |
var field = type.GetField("PluginType", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); |
475 |
plugin_type = (PluginType)field.GetValue(null); |
476 |
List<Type> sub_types = new List<Type>(type.GetNestedTypes(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)); |
477 |
foreach (var sub_type in sub_types) |
478 |
{ |
479 |
var sub_field = sub_type.GetField("PluginName", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); |
480 |
PluginNames o = (PluginNames)sub_field.GetValue(null); |
481 |
if (o == plugin_name) |
482 |
{ |
483 |
found_type = sub_type; |
484 |
break; |
485 |
} |
486 |
} |
487 |
} |
488 |
if (found_type != null) |
489 |
{ |
490 |
ConstructorInfo ci = found_type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, System.Type.DefaultBinder, System.Type.EmptyTypes, null); |
491 |
if (ci == null) |
492 |
{ |
493 |
throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", found_type.Name)); |
494 |
} |
495 |
else |
496 |
{ |
497 |
object o = ci.Invoke(new object[] { }); |
498 |
var plugin_prop = found_type.GetProperty("Plugin", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); |
499 |
|
500 |
IBasePluginDefinition bpd = null; |
501 |
object prop_value = plugin_prop.GetValue(o, null); |
502 |
bpd = (prop_value as IBasePluginDefinition); |
503 |
if (bpd != null) |
504 |
{ |
505 |
return new List<IBasePluginDefinition>() { bpd }; |
506 |
} |
507 |
else |
508 |
{ |
509 |
List<IBasePluginDefinition> bpds = new List<IBasePluginDefinition>(); |
510 |
bpds = (prop_value as List<IBasePluginDefinition>); |
511 |
bpds.TrimExcess(); |
512 |
return bpds; |
513 |
} |
514 |
} |
515 |
} |
516 |
throw new PluginNotFoundException(string.Format("Could not find a plugin by name with value: '{0}'", plugin_name.ToString())); |
517 |
} |
518 |
public static List<IBasePluginDefinition> GetPluginsByType(PluginType plugin_type) |
519 |
{ |
520 |
List<IBasePluginDefinition> plugins = new List<IBasePluginDefinition>(); |
521 |
|
522 |
Type found_type = null; |
523 |
Type t = typeof(PluginCollection); |
524 |
List<Type> types = new List<Type>(t.GetNestedTypes(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)); |
525 |
foreach (var type in types) |
526 |
{ |
527 |
var field = type.GetField("PluginType", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); |
528 |
var o = (PluginType)field.GetValue(null); |
529 |
if (o == plugin_type) |
530 |
{ |
531 |
found_type = type; |
532 |
break; |
533 |
} |
534 |
} |
535 |
if (found_type != null) |
536 |
{ |
537 |
List<Type> sub_types = new List<Type>(found_type.GetNestedTypes(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)); |
538 |
foreach (var sub_type in sub_types) |
539 |
{ |
540 |
var sub_field = sub_type.GetField("PluginName", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); |
541 |
PluginNames o = (PluginNames)sub_field.GetValue(null); |
542 |
var bpds = GetPluginsByName(o); |
543 |
if (bpds != null) |
544 |
{ |
545 |
if (bpds.Count == 0) |
546 |
{ |
547 |
throw new ArgumentOutOfRangeException(string.Format("Found pluginby name: '{0}' but no plugin data was returned", o.ToString())); |
548 |
} |
549 |
foreach (var bpd in bpds) |
550 |
{ |
551 |
plugins.Add(bpd); |
552 |
} |
553 |
} |
554 |
else |
555 |
{ |
556 |
throw new NullReferenceException(string.Format("Found pluginby name: '{0}' but the returned plugin data was null", o.ToString())); |
557 |
} |
558 |
} |
559 |
} |
560 |
plugins.TrimExcess(); |
561 |
return plugins; |
562 |
} |
563 |
#endregion |
564 |
|
565 |
} |
566 |
#endregion |
567 |
|
568 |
#endregion |
569 |
} |