/[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 675 - (hide annotations) (download)
Mon Jun 17 05:42:02 2013 UTC (8 years, 1 month ago) by william
File size: 11738 byte(s)
PluginLoader: include any plugins also contained in the PluginFramework (ie default core plugins)

1 william 414 #region Logging Defines
2     // include this any class or method that required logging, and comment-out what is not needed
3 william 415
4 william 414 #region Enabled logging levels
5     #define LOGGING_ENABLE_INFO
6     #define LOGGING_ENABLE_WARN
7     #define LOGGING_ENABLE_DEBUG
8     #define LOGGING_ENABLE_VERBOSEDEBUG
9     #define LOGGING_ENABLE_ERROR
10     #define LOGGING_ENABLE_VERBOSEERROR
11     #define LOGGING_ENABLE_PROFILER
12     #endregion
13     #endregion
14     using System;
15 william 83 using System.Collections.Generic;
16     using System.Linq;
17     using System.Text;
18     using RomCheater.PluginFramework.Interfaces;
19     using RomCheater.Logging;
20 william 84 using System.IO;
21 william 85 using System.Reflection;
22 william 83
23     namespace RomCheater.PluginFramework.Core
24     {
25     public class PluginLoader : IPluginLoader
26     {
27     public PluginLoader()
28     {
29     LoadedConfigPlugins = new List<IConfigPlugin>();
30     LoadedInputPlugins = new List<IInputPlugin>();
31     LoadedWindowPlugins = new List<IWindowPlugin>();
32 william 437 LoadedUserControlPlugins = new List<IUserControlPlugin>();
33 william 83 }
34     #region IPluginLoader Members
35 william 366 public void LoadPlugins() { LoadPlugins(false); }
36     public void LoadPlugins(bool silent)
37 william 83 {
38 william 85 try
39     {
40 william 366 if (!silent)
41     logger.Info.WriteLine("Loading Plugins...");
42 william 84
43 william 85 string PluginPath = string.Format(@"{0}\Plugins", typeof(PluginLoader).Assembly.Location.Replace(@"\RomCheater.PluginFramework.dll", ""));
44 william 366 if (!silent)
45     logger.Debug.WriteLine("Plugins Path: {0}", PluginPath);
46 william 85 List<string> dlls = new List<string>(Directory.GetFiles(PluginPath, "*.dll"));
47 william 675 // also load any default plugins from the framework
48     dlls.Add(typeof(PluginLoader).Assembly.Location);
49    
50 william 366 if (!silent)
51     logger.Debug.WriteLine(" Found: {0} plugin dlls", dlls.Count);
52 william 85 foreach (string dll in dlls)
53     {
54     FileInfo fi = new FileInfo(dll);
55 william 366 if (!silent)
56     logger.Debug.WriteLine(" plugin[{0}]: {1}", dlls.IndexOf(dll), fi.Name);
57 william 85 GetConfigPluginsFromDll(fi);
58     GetInputPluginsFromDll(fi);
59     GetWindowPluginsFromDll(fi);
60 william 437 GetUserControlPluginsFromDll(fi);
61 william 85 }
62    
63 william 675
64    
65 william 366 if (!silent)
66     logger.Info.WriteLine(" Loaded {0} config plugins", LoadedConfigPlugins.Count);
67     if (!silent)
68     logger.Info.WriteLine(" Loaded {0} input plugins", LoadedInputPlugins.Count);
69     if (!silent)
70     logger.Info.WriteLine(" Loaded {0} window plugins", LoadedWindowPlugins.Count);
71     if (!silent)
72 william 437 logger.Info.WriteLine(" Loaded {0} usercontrol plugins", LoadedUserControlPlugins.Count);
73     if (!silent)
74 william 366 logger.Info.WriteLine("Plugins Loaded.");
75 william 85 }
76 william 86 catch (ReflectionTypeLoadException ex)
77     {
78     StringBuilder builder = new StringBuilder();
79     if (ex.LoaderExceptions.Count() > 0)
80     {
81     foreach (Exception c in ex.LoaderExceptions)
82     {
83     builder.AppendLine(c.ToString());
84     }
85     }
86 william 366 if (!silent)
87     logger.Error.WriteLine("Failed to load one or more plugins{0}Possible Reason:{0}{1}", System.Environment.NewLine, builder.ToString());
88 william 86 }
89 william 85 catch (Exception ex)
90 william 84 {
91 william 366 if (!silent)
92     logger.Error.WriteLine("Failed to load one or more plugins{0}Possible Reason:{0}{1}", System.Environment.NewLine, ex.ToString());
93 william 84 }
94 william 83 }
95     public List<IConfigPlugin> LoadedConfigPlugins { get; private set; }
96     public List<IInputPlugin> LoadedInputPlugins { get; private set; }
97     public List<IWindowPlugin> LoadedWindowPlugins { get; private set; }
98 william 437 public List<IUserControlPlugin> LoadedUserControlPlugins { get; private set; }
99 william 87
100     public IConfigPlugin GetConfigPlugin(string t)
101     {
102     foreach (IConfigPlugin c in LoadedConfigPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } }
103 william 148 return GetGenericConfigPlugin();
104     }
105     private IConfigPlugin GetGenericConfigPlugin()
106     {
107     foreach (IConfigPlugin c in LoadedConfigPlugins) { if (c.ID.ToString().ToLower() == "478e225b-c3e8-9280-57ca-384b884fc4cc".ToLower()) { return c; } }
108 william 87 return null;
109     }
110     public IInputPlugin GetInputPlugin(string t)
111     {
112     foreach (IInputPlugin c in LoadedInputPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } }
113 william 148 return GetGenericInputPlugin();
114     }
115     private IInputPlugin GetGenericInputPlugin()
116     {
117     foreach (IInputPlugin c in LoadedInputPlugins) { }
118 william 87 return null;
119     }
120     public IWindowPlugin GetWindowPlugin(string t)
121     {
122     foreach (IWindowPlugin c in LoadedWindowPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } }
123 william 148 return GetGenericWindowPlugin();
124     }
125     private IWindowPlugin GetGenericWindowPlugin()
126     {
127     foreach (IWindowPlugin c in LoadedWindowPlugins) { }
128 william 87 return null;
129     }
130 william 437 private IUserControlPlugin GetUserControlPlugin()
131     {
132     foreach (IUserControlPlugin c in LoadedUserControlPlugins) { }
133     return null;
134     }
135 william 83 #endregion
136 william 85
137     private void GetConfigPluginsFromDll(FileInfo dll)
138     {
139     logger.Debug.WriteLine(" Getting Config plugins contained in {0}", dll.Name);
140     Assembly asm = Assembly.LoadFile(dll.FullName);
141     List<Type> types = new List<Type>(asm.GetTypes());
142     foreach (Type type in types)
143     {
144     if (type.BaseType == typeof(ConfigPlugin))
145     {
146     ConstructorInfo ci = null;
147 william 366 ci = type.GetConstructor(new Type[] { });
148 william 85 if (ci == null)
149     {
150     throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name));
151     }
152     else
153     {
154     object o = ci.Invoke(new object[] { });
155     IConfigPlugin c = (IConfigPlugin)o;
156     if (c == null)
157     {
158     throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name));
159     }
160     else
161     {
162     logger.Debug.WriteLine(" Loaded Config Plugin [name={0}] from {1}", c.Name, dll.Name);
163     LoadedConfigPlugins.Add(c);
164     }
165     }
166     }
167     }
168     }
169     private void GetInputPluginsFromDll(FileInfo dll)
170     {
171     logger.Debug.WriteLine(" Getting Input plugins contained in {0}", dll.Name);
172     Assembly asm = Assembly.LoadFile(dll.FullName);
173     List<Type> types = new List<Type>(asm.GetTypes());
174     foreach (Type type in types)
175     {
176     if (type.BaseType == typeof(InputPlugin))
177     {
178     ConstructorInfo ci = null;
179     ci = type.GetConstructor(new Type[] { });
180     if (ci == null)
181     {
182     throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name));
183     }
184     else
185     {
186     object o = ci.Invoke(new object[] { });
187     IInputPlugin c = (IInputPlugin)o;
188     if (c == null)
189     {
190     throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name));
191     }
192     else
193     {
194     logger.Debug.WriteLine(" Loaded Input Plugin [name={0}] from {1}", c.Name, dll.Name);
195     LoadedInputPlugins.Add(c);
196     }
197     }
198     }
199     }
200     }
201     private void GetWindowPluginsFromDll(FileInfo dll)
202     {
203     logger.Debug.WriteLine(" Getting Window plugins contained in {0}", dll.Name);
204     Assembly asm = Assembly.LoadFile(dll.FullName);
205     List<Type> types = new List<Type>(asm.GetTypes());
206     foreach (Type type in types)
207     {
208     if (type.BaseType == typeof(WindowPlugin))
209     {
210     ConstructorInfo ci = null;
211     ci = type.GetConstructor(new Type[] { });
212     if (ci == null)
213     {
214     throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name));
215     }
216     else
217     {
218     object o = ci.Invoke(new object[] { });
219     IWindowPlugin c = (IWindowPlugin)o;
220     if (c == null)
221     {
222     throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name));
223     }
224     else
225     {
226     logger.Debug.WriteLine(" Loaded Window Plugin [name={0}] from {1}", c.Name, dll.Name);
227     LoadedWindowPlugins.Add(c);
228     }
229     }
230     }
231     }
232     }
233 william 437 private void GetUserControlPluginsFromDll(FileInfo dll)
234     {
235     logger.Debug.WriteLine(" Getting UserControl plugins contained in {0}", dll.Name);
236     Assembly asm = Assembly.LoadFile(dll.FullName);
237     List<Type> types = new List<Type>(asm.GetTypes());
238     foreach (Type type in types)
239     {
240     if (type.BaseType == typeof(UserControlPlugin))
241     {
242     ConstructorInfo ci = null;
243     ci = type.GetConstructor(new Type[] { });
244     if (ci == null)
245     {
246     throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name));
247     }
248     else
249     {
250     object o = ci.Invoke(new object[] { });
251     IUserControlPlugin c = (IUserControlPlugin)o;
252     if (c == null)
253     {
254     throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name));
255     }
256     else
257     {
258     logger.Debug.WriteLine(" Loaded UserControl Plugin [name={0}] from {1}", c.Name, dll.Name);
259     LoadedUserControlPlugins.Add(c);
260     }
261     }
262     }
263     }
264     }
265 william 83 }
266     }

  ViewVC Help
Powered by ViewVC 1.1.22