1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using RomCheater.PluginFramework.Interfaces; |
6 |
using RomCheater.Logging; |
7 |
using System.IO; |
8 |
using System.Reflection; |
9 |
|
10 |
namespace RomCheater.PluginFramework.Core |
11 |
{ |
12 |
public class PluginLoader : IPluginLoader |
13 |
{ |
14 |
public PluginLoader() |
15 |
{ |
16 |
LoadedConfigPlugins = new List<IConfigPlugin>(); |
17 |
LoadedInputPlugins = new List<IInputPlugin>(); |
18 |
LoadedWindowPlugins = new List<IWindowPlugin>(); |
19 |
} |
20 |
#region IPluginLoader Members |
21 |
|
22 |
public void LoadPlugins() |
23 |
{ |
24 |
try |
25 |
{ |
26 |
logger.Info.WriteLine("Loading Plugins..."); |
27 |
|
28 |
string PluginPath = string.Format(@"{0}\Plugins", typeof(PluginLoader).Assembly.Location.Replace(@"\RomCheater.PluginFramework.dll", "")); |
29 |
logger.Debug.WriteLine("Plugins Path: {0}", PluginPath); |
30 |
List<string> dlls = new List<string>(Directory.GetFiles(PluginPath, "*.dll")); |
31 |
logger.Debug.WriteLine(" Found: {0} plugin dlls", dlls.Count); |
32 |
foreach (string dll in dlls) |
33 |
{ |
34 |
FileInfo fi = new FileInfo(dll); |
35 |
logger.Debug.WriteLine(" plugin[{0}]: {1}", dlls.IndexOf(dll), fi.Name); |
36 |
GetConfigPluginsFromDll(fi); |
37 |
GetInputPluginsFromDll(fi); |
38 |
GetWindowPluginsFromDll(fi); |
39 |
} |
40 |
|
41 |
logger.Info.WriteLine(" Loaded {0} config plugins", LoadedConfigPlugins.Count); |
42 |
logger.Info.WriteLine(" Loaded {0} input plugins", LoadedInputPlugins.Count); |
43 |
logger.Info.WriteLine(" Loaded {0} window plugins", LoadedWindowPlugins.Count); |
44 |
|
45 |
logger.Info.WriteLine("Plugins Loaded."); |
46 |
} |
47 |
catch (ReflectionTypeLoadException ex) |
48 |
{ |
49 |
StringBuilder builder = new StringBuilder(); |
50 |
if (ex.LoaderExceptions.Count() > 0) |
51 |
{ |
52 |
foreach (Exception c in ex.LoaderExceptions) |
53 |
{ |
54 |
builder.AppendLine(c.ToString()); |
55 |
} |
56 |
} |
57 |
logger.Error.WriteLine("Failed to load one or more plugins{0}Possible Reason:{0}{1}", System.Environment.NewLine, builder.ToString()); |
58 |
} |
59 |
catch (Exception ex) |
60 |
{ |
61 |
logger.Error.WriteLine("Failed to load one or more plugins{0}Possible Reason:{0}{1}", System.Environment.NewLine, ex.ToString()); |
62 |
} |
63 |
} |
64 |
public List<IConfigPlugin> LoadedConfigPlugins { get; private set; } |
65 |
public List<IInputPlugin> LoadedInputPlugins { get; private set; } |
66 |
public List<IWindowPlugin> LoadedWindowPlugins { get; private set; } |
67 |
|
68 |
public IConfigPlugin GetConfigPlugin(string t) |
69 |
{ |
70 |
foreach (IConfigPlugin c in LoadedConfigPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } } |
71 |
return GetGenericConfigPlugin(); |
72 |
} |
73 |
private IConfigPlugin GetGenericConfigPlugin() |
74 |
{ |
75 |
foreach (IConfigPlugin c in LoadedConfigPlugins) { if (c.ID.ToString().ToLower() == "478e225b-c3e8-9280-57ca-384b884fc4cc".ToLower()) { return c; } } |
76 |
return null; |
77 |
} |
78 |
public IInputPlugin GetInputPlugin(string t) |
79 |
{ |
80 |
foreach (IInputPlugin c in LoadedInputPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } } |
81 |
return GetGenericInputPlugin(); |
82 |
} |
83 |
private IInputPlugin GetGenericInputPlugin() |
84 |
{ |
85 |
foreach (IInputPlugin c in LoadedInputPlugins) { } |
86 |
return null; |
87 |
} |
88 |
public IWindowPlugin GetWindowPlugin(string t) |
89 |
{ |
90 |
foreach (IWindowPlugin c in LoadedWindowPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } } |
91 |
return GetGenericWindowPlugin(); |
92 |
} |
93 |
private IWindowPlugin GetGenericWindowPlugin() |
94 |
{ |
95 |
foreach (IWindowPlugin c in LoadedWindowPlugins) { } |
96 |
return null; |
97 |
} |
98 |
#endregion |
99 |
|
100 |
private void GetConfigPluginsFromDll(FileInfo dll) |
101 |
{ |
102 |
logger.Debug.WriteLine(" Getting Config plugins contained in {0}", dll.Name); |
103 |
Assembly asm = Assembly.LoadFile(dll.FullName); |
104 |
List<Type> types = new List<Type>(asm.GetTypes()); |
105 |
foreach (Type type in types) |
106 |
{ |
107 |
if (type.BaseType == typeof(ConfigPlugin)) |
108 |
{ |
109 |
ConstructorInfo ci = null; |
110 |
ci = type.GetConstructor(new Type[] {}); |
111 |
if (ci == null) |
112 |
{ |
113 |
throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name)); |
114 |
} |
115 |
else |
116 |
{ |
117 |
object o = ci.Invoke(new object[] { }); |
118 |
IConfigPlugin c = (IConfigPlugin)o; |
119 |
if (c == null) |
120 |
{ |
121 |
throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name)); |
122 |
} |
123 |
else |
124 |
{ |
125 |
logger.Debug.WriteLine(" Loaded Config Plugin [name={0}] from {1}", c.Name, dll.Name); |
126 |
LoadedConfigPlugins.Add(c); |
127 |
} |
128 |
} |
129 |
} |
130 |
} |
131 |
} |
132 |
private void GetInputPluginsFromDll(FileInfo dll) |
133 |
{ |
134 |
logger.Debug.WriteLine(" Getting Input 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(InputPlugin)) |
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 |
IInputPlugin c = (IInputPlugin)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 Input Plugin [name={0}] from {1}", c.Name, dll.Name); |
158 |
LoadedInputPlugins.Add(c); |
159 |
} |
160 |
} |
161 |
} |
162 |
} |
163 |
} |
164 |
private void GetWindowPluginsFromDll(FileInfo dll) |
165 |
{ |
166 |
logger.Debug.WriteLine(" Getting Window 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(WindowPlugin)) |
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 |
IWindowPlugin c = (IWindowPlugin)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 Window Plugin [name={0}] from {1}", c.Name, dll.Name); |
190 |
LoadedWindowPlugins.Add(c); |
191 |
} |
192 |
} |
193 |
} |
194 |
} |
195 |
} |
196 |
} |
197 |
} |