/[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 437 - (show annotations) (download)
Tue May 28 18:08:23 2013 UTC (7 years, 11 months ago) by william
File size: 11580 byte(s)
+ current wip for custom usercontrol plugins

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

  ViewVC Help
Powered by ViewVC 1.1.22