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

  ViewVC Help
Powered by ViewVC 1.1.22