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

  ViewVC Help
Powered by ViewVC 1.1.22