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 |
|
|
} |
33 |
|
|
#region IPluginLoader Members |
34 |
william |
366 |
public void LoadPlugins() { LoadPlugins(false); } |
35 |
|
|
public void LoadPlugins(bool silent) |
36 |
william |
83 |
{ |
37 |
william |
85 |
try |
38 |
|
|
{ |
39 |
william |
366 |
if (!silent) |
40 |
|
|
logger.Info.WriteLine("Loading Plugins..."); |
41 |
william |
84 |
|
42 |
william |
85 |
string PluginPath = string.Format(@"{0}\Plugins", typeof(PluginLoader).Assembly.Location.Replace(@"\RomCheater.PluginFramework.dll", "")); |
43 |
william |
366 |
if (!silent) |
44 |
|
|
logger.Debug.WriteLine("Plugins Path: {0}", PluginPath); |
45 |
william |
85 |
List<string> dlls = new List<string>(Directory.GetFiles(PluginPath, "*.dll")); |
46 |
william |
366 |
if (!silent) |
47 |
|
|
logger.Debug.WriteLine(" Found: {0} plugin dlls", dlls.Count); |
48 |
william |
85 |
foreach (string dll in dlls) |
49 |
|
|
{ |
50 |
|
|
FileInfo fi = new FileInfo(dll); |
51 |
william |
366 |
if (!silent) |
52 |
|
|
logger.Debug.WriteLine(" plugin[{0}]: {1}", dlls.IndexOf(dll), fi.Name); |
53 |
william |
85 |
GetConfigPluginsFromDll(fi); |
54 |
|
|
GetInputPluginsFromDll(fi); |
55 |
|
|
GetWindowPluginsFromDll(fi); |
56 |
|
|
} |
57 |
|
|
|
58 |
william |
366 |
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 |
william |
85 |
} |
67 |
william |
86 |
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 |
william |
366 |
if (!silent) |
78 |
|
|
logger.Error.WriteLine("Failed to load one or more plugins{0}Possible Reason:{0}{1}", System.Environment.NewLine, builder.ToString()); |
79 |
william |
86 |
} |
80 |
william |
85 |
catch (Exception ex) |
81 |
william |
84 |
{ |
82 |
william |
366 |
if (!silent) |
83 |
|
|
logger.Error.WriteLine("Failed to load one or more plugins{0}Possible Reason:{0}{1}", System.Environment.NewLine, ex.ToString()); |
84 |
william |
84 |
} |
85 |
william |
83 |
} |
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 |
william |
87 |
|
90 |
|
|
public IConfigPlugin GetConfigPlugin(string t) |
91 |
|
|
{ |
92 |
|
|
foreach (IConfigPlugin c in LoadedConfigPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } } |
93 |
william |
148 |
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 |
william |
87 |
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 |
william |
148 |
return GetGenericInputPlugin(); |
104 |
|
|
} |
105 |
|
|
private IInputPlugin GetGenericInputPlugin() |
106 |
|
|
{ |
107 |
|
|
foreach (IInputPlugin c in LoadedInputPlugins) { } |
108 |
william |
87 |
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 |
william |
148 |
return GetGenericWindowPlugin(); |
114 |
|
|
} |
115 |
|
|
private IWindowPlugin GetGenericWindowPlugin() |
116 |
|
|
{ |
117 |
|
|
foreach (IWindowPlugin c in LoadedWindowPlugins) { } |
118 |
william |
87 |
return null; |
119 |
|
|
} |
120 |
william |
83 |
#endregion |
121 |
william |
85 |
|
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 |
william |
366 |
ci = type.GetConstructor(new Type[] { }); |
133 |
william |
85 |
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 |
william |
83 |
} |
219 |
|
|
} |