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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 86 - (hide annotations) (download)
Wed May 9 19:41:06 2012 UTC (8 years, 11 months ago) by william
File size: 7370 byte(s)
+ add support to set plugins in user settings

1 william 83 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 william 84 using System.IO;
8 william 85 using System.Reflection;
9 william 83
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    
22     public void LoadPlugins()
23     {
24 william 85 try
25     {
26     logger.Info.WriteLine("Loading Plugins...");
27 william 84
28 william 85 string PluginPath = string.Format(@"{0}\Plugins", typeof(PluginLoader).Assembly.Location.Replace(@"\RomCheater.PluginFramework.dll", ""));
29     logger.Debug.WriteLine("Plugins Path: {0}", PluginPath);
30     List<string> dlls = new List<string>(Directory.GetFiles(PluginPath, "*.dll"));
31     logger.Debug.WriteLine(" Found: {0} plugin dlls", dlls.Count);
32     foreach (string dll in dlls)
33     {
34     FileInfo fi = new FileInfo(dll);
35     logger.Debug.WriteLine(" plugin[{0}]: {1}", dlls.IndexOf(dll), fi.Name);
36     GetConfigPluginsFromDll(fi);
37     GetInputPluginsFromDll(fi);
38     GetWindowPluginsFromDll(fi);
39     }
40    
41     logger.Info.WriteLine(" Loaded {0} config plugins", LoadedConfigPlugins.Count);
42     logger.Info.WriteLine(" Loaded {0} input plugins", LoadedInputPlugins.Count);
43     logger.Info.WriteLine(" Loaded {0} window plugins", LoadedWindowPlugins.Count);
44    
45     logger.Info.WriteLine("Plugins Loaded.");
46     }
47 william 86 catch (ReflectionTypeLoadException ex)
48     {
49     StringBuilder builder = new StringBuilder();
50     if (ex.LoaderExceptions.Count() > 0)
51     {
52     foreach (Exception c in ex.LoaderExceptions)
53     {
54     builder.AppendLine(c.ToString());
55     }
56     }
57     logger.Error.WriteLine("Failed to load one or more plugins{0}Possible Reason:{0}{1}", System.Environment.NewLine, builder.ToString());
58     }
59 william 85 catch (Exception ex)
60 william 84 {
61 william 85 logger.Error.WriteLine("Failed to load one or more plugins{0}Possible Reason:{0}{1}", System.Environment.NewLine, ex.ToString());
62 william 84 }
63 william 83 }
64     public List<IConfigPlugin> LoadedConfigPlugins { get; private set; }
65     public List<IInputPlugin> LoadedInputPlugins { get; private set; }
66     public List<IWindowPlugin> LoadedWindowPlugins { get; private set; }
67     #endregion
68 william 85
69     private void GetConfigPluginsFromDll(FileInfo dll)
70     {
71     logger.Debug.WriteLine(" Getting Config plugins contained in {0}", dll.Name);
72     Assembly asm = Assembly.LoadFile(dll.FullName);
73     List<Type> types = new List<Type>(asm.GetTypes());
74     foreach (Type type in types)
75     {
76     if (type.BaseType == typeof(ConfigPlugin))
77     {
78     ConstructorInfo ci = null;
79     ci = type.GetConstructor(new Type[] {});
80     if (ci == null)
81     {
82     throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name));
83     }
84     else
85     {
86     object o = ci.Invoke(new object[] { });
87     IConfigPlugin c = (IConfigPlugin)o;
88     if (c == null)
89     {
90     throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name));
91     }
92     else
93     {
94     logger.Debug.WriteLine(" Loaded Config Plugin [name={0}] from {1}", c.Name, dll.Name);
95     LoadedConfigPlugins.Add(c);
96     }
97     }
98     }
99     }
100     }
101     private void GetInputPluginsFromDll(FileInfo dll)
102     {
103     logger.Debug.WriteLine(" Getting Input plugins contained in {0}", dll.Name);
104     Assembly asm = Assembly.LoadFile(dll.FullName);
105     List<Type> types = new List<Type>(asm.GetTypes());
106     foreach (Type type in types)
107     {
108     if (type.BaseType == typeof(InputPlugin))
109     {
110     ConstructorInfo ci = null;
111     ci = type.GetConstructor(new Type[] { });
112     if (ci == null)
113     {
114     throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name));
115     }
116     else
117     {
118     object o = ci.Invoke(new object[] { });
119     IInputPlugin c = (IInputPlugin)o;
120     if (c == null)
121     {
122     throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name));
123     }
124     else
125     {
126     logger.Debug.WriteLine(" Loaded Input Plugin [name={0}] from {1}", c.Name, dll.Name);
127     LoadedInputPlugins.Add(c);
128     }
129     }
130     }
131     }
132     }
133     private void GetWindowPluginsFromDll(FileInfo dll)
134     {
135     logger.Debug.WriteLine(" Getting Window plugins contained in {0}", dll.Name);
136     Assembly asm = Assembly.LoadFile(dll.FullName);
137     List<Type> types = new List<Type>(asm.GetTypes());
138     foreach (Type type in types)
139     {
140     if (type.BaseType == typeof(WindowPlugin))
141     {
142     ConstructorInfo ci = null;
143     ci = type.GetConstructor(new Type[] { });
144     if (ci == null)
145     {
146     throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name));
147     }
148     else
149     {
150     object o = ci.Invoke(new object[] { });
151     IWindowPlugin c = (IWindowPlugin)o;
152     if (c == null)
153     {
154     throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name));
155     }
156     else
157     {
158     logger.Debug.WriteLine(" Loaded Window Plugin [name={0}] from {1}", c.Name, dll.Name);
159     LoadedWindowPlugins.Add(c);
160     }
161     }
162     }
163     }
164     }
165 william 83 }
166     }

  ViewVC Help
Powered by ViewVC 1.1.22