/[RomCheater]/trunk/RomCheater.PluginFramework/Core/PluginCollectionTypes.cs
ViewVC logotype

Annotation of /trunk/RomCheater.PluginFramework/Core/PluginCollectionTypes.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 714 - (hide annotations) (download)
Tue Jun 18 10:44:55 2013 UTC (8 years, 3 months ago) by william
File size: 25873 byte(s)
+ add support for using the PluginLoader to get unknown plugin types (ones we haven't explicitly defined)

1 william 713 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     RVACalculatorPlugin,
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.RVACalculatorPlugin;
339     const string dll = @"plugins\RomCheater.RVACalculator.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 == "RVACalculatorPlugin")
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 = @"RomCheater.PluginFramework.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 william 714 #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 william 713
448 william 714 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 william 713
453 william 714 }
454     public static List<IBasePluginDefinition> Plugin { get { return (bpds as List<IBasePluginDefinition>); } }
455     }
456     #endregion
457     }
458     #endregion
459    
460 william 713 #region Name/Guid Collection
461 william 714 public static List<IBasePluginDefinition> GetPluginByName(PluginNames plugin_name)
462 william 713 {
463     Type t = typeof(PluginCollection);
464     List<Type> types = new List<Type>(t.GetNestedTypes(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public));
465    
466     var plugin_type = PluginType.Unknown;
467     Type found_type = null;
468     foreach (var type in types)
469     {
470     var field = type.GetField("PluginType", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
471     plugin_type = (PluginType)field.GetValue(null);
472     List<Type> sub_types = new List<Type>(type.GetNestedTypes(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public));
473     foreach (var sub_type in sub_types)
474     {
475     var sub_field = sub_type.GetField("PluginName", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
476     PluginNames o = (PluginNames)sub_field.GetValue(null);
477     if (o == plugin_name)
478     {
479     found_type = sub_type;
480     break;
481     }
482     }
483     }
484     if (found_type != null)
485     {
486     ConstructorInfo ci = found_type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, System.Type.DefaultBinder, System.Type.EmptyTypes, null);
487     if (ci == null)
488     {
489     throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", found_type.Name));
490     }
491     else
492     {
493     object o = ci.Invoke(new object[] { });
494     var plugin_prop = found_type.GetProperty("Plugin", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
495    
496     IBasePluginDefinition bpd = null;
497     object prop_value = plugin_prop.GetValue(o, null);
498     bpd = (prop_value as IBasePluginDefinition);
499     if (bpd != null)
500     {
501 william 714 return new List<IBasePluginDefinition>() { bpd };
502 william 713 }
503 william 714 else
504     {
505     List<IBasePluginDefinition> bpds = new List<IBasePluginDefinition>();
506     bpds = (prop_value as List<IBasePluginDefinition>);
507     bpds.TrimExcess();
508     return bpds;
509     }
510 william 713 }
511     }
512     throw new PluginNotFoundException(string.Format("Could not find a plugin by name with value: '{0}'", plugin_name.ToString()));
513     }
514     public static List<IBasePluginDefinition> GetPluginsByType(PluginType plugin_type)
515     {
516     List<IBasePluginDefinition> plugins = new List<IBasePluginDefinition>();
517    
518     Type found_type = null;
519     Type t = typeof(PluginCollection);
520     List<Type> types = new List<Type>(t.GetNestedTypes(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public));
521     foreach (var type in types)
522     {
523     var field = type.GetField("PluginType", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
524     var o = (PluginType)field.GetValue(null);
525     if (o == plugin_type)
526     {
527     found_type = type;
528     break;
529     }
530     }
531     if (found_type != null)
532     {
533     List<Type> sub_types = new List<Type>(found_type.GetNestedTypes(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public));
534     foreach (var sub_type in sub_types)
535     {
536     var sub_field = sub_type.GetField("PluginName", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
537     PluginNames o = (PluginNames)sub_field.GetValue(null);
538 william 714 var bpds = GetPluginByName(o);
539     if (bpds != null)
540 william 713 {
541 william 714 if (bpds.Count == 0)
542     {
543     throw new ArgumentOutOfRangeException(string.Format("Found pluginby name: '{0}' but no plugin data was returned", o.ToString()));
544     }
545     foreach (var bpd in bpds)
546     {
547     plugins.Add(bpd);
548     }
549 william 713 }
550     else
551     {
552     throw new NullReferenceException(string.Format("Found pluginby name: '{0}' but the returned plugin data was null", o.ToString()));
553     }
554     }
555     }
556     plugins.TrimExcess();
557     return plugins;
558     }
559     #endregion
560    
561     }
562     #endregion
563    
564     #endregion
565     }

  ViewVC Help
Powered by ViewVC 1.1.22