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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 680 - (show annotations) (download)
Mon Jun 17 08:03:34 2013 UTC (8 years, 4 months ago) by william
File size: 11748 byte(s)

1 #region Logging Defines
2 // include this any class or method that required logging, and comment-out what is not needed
3
4 #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 using System.Collections.Generic;
16 using System.Linq;
17 using System.Text;
18 using RomCheater.PluginFramework.Interfaces;
19 using RomCheater.Logging;
20 using System.IO;
21 using System.Reflection;
22
23 namespace RomCheater.PluginFramework.Core
24 {
25 public class PluginLoader : IPluginLoader
26 {
27
28 public PluginLoader()
29 {
30 LoadedConfigPlugins = new List<IConfigPlugin>();
31 LoadedInputPlugins = new List<IInputPlugin>();
32 LoadedWindowPlugins = new List<IWindowPlugin>();
33 LoadedUserControlPlugins = new List<IUserControlPlugin>();
34 }
35 #region IPluginLoader Members
36 public void LoadPlugins() { LoadPlugins(false); }
37 public void LoadPlugins(bool silent)
38 {
39 try
40 {
41 if (!silent)
42 logger.Info.WriteLine("Loading Plugins...");
43
44 string PluginPath = string.Format(@"{0}\Plugins", typeof(PluginLoader).Assembly.Location.Replace(@"\RomCheater.PluginFramework.dll", ""));
45 if (!silent)
46 logger.Debug.WriteLine("Plugins Path: {0}", PluginPath);
47 List<string> dlls = new List<string>(Directory.GetFiles(PluginPath, "*.dll"));
48 // also load any default plugins from the framework
49 dlls.Add(typeof(PluginLoader).Assembly.Location);
50
51 if (!silent)
52 logger.Debug.WriteLine(" Found: {0} plugin dlls", dlls.Count);
53 foreach (string dll in dlls)
54 {
55 FileInfo fi = new FileInfo(dll);
56 if (!silent)
57 logger.Debug.WriteLine(" plugin[{0}]: {1}", dlls.IndexOf(dll), fi.Name);
58 GetConfigPluginsFromDll(fi);
59 GetInputPluginsFromDll(fi);
60 GetWindowPluginsFromDll(fi);
61 GetUserControlPluginsFromDll(fi);
62 }
63
64
65
66 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 logger.Info.WriteLine(" Loaded {0} usercontrol plugins", LoadedUserControlPlugins.Count);
74 if (!silent)
75 logger.Info.WriteLine("Plugins Loaded.");
76 }
77 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 if (!silent)
88 logger.Error.WriteLine("Failed to load one or more plugins{0}Possible Reason:{0}{1}", System.Environment.NewLine, builder.ToString());
89 }
90 catch (Exception ex)
91 {
92 if (!silent)
93 logger.Error.WriteLine("Failed to load one or more plugins{0}Possible Reason:{0}{1}", System.Environment.NewLine, ex.ToString());
94 }
95 }
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 public List<IUserControlPlugin> LoadedUserControlPlugins { get; private set; }
100
101 public IConfigPlugin GetConfigPlugin(string t)
102 {
103 foreach (IConfigPlugin c in LoadedConfigPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } }
104 return GetGenericConfigPlugin();
105 }
106 private IConfigPlugin GetGenericConfigPlugin()
107 {
108 foreach (IConfigPlugin c in LoadedConfigPlugins) { if (c.ID.ToString().ToLower() == "478e225b-c3e8-9280-57ca-384b884fc4cc".ToLower()) { return c; } }
109 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 return GetGenericInputPlugin();
115 }
116 private IInputPlugin GetGenericInputPlugin()
117 {
118 foreach (IInputPlugin c in LoadedInputPlugins) { }
119 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 return GetGenericWindowPlugin();
125 }
126 private IWindowPlugin GetGenericWindowPlugin()
127 {
128 foreach (IWindowPlugin c in LoadedWindowPlugins) { }
129 return null;
130 }
131 private IUserControlPlugin GetUserControlPlugin()
132 {
133 foreach (IUserControlPlugin c in LoadedUserControlPlugins) { }
134 return null;
135 }
136 #endregion
137
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 ci = type.GetConstructor(new Type[] { });
149 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 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 }
266 }
267 }

  ViewVC Help
Powered by ViewVC 1.1.22