ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.PluginFramework/Core/PluginLoader.cs
Revision: 87
Committed: Wed May 9 19:51:42 2012 UTC (11 years, 1 month ago) by william
File size: 8051 byte(s)
Log Message:
+ add support for loading and saving config plugins

File Contents

# Content
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 null;
72 }
73 public IInputPlugin GetInputPlugin(string t)
74 {
75 foreach (IInputPlugin c in LoadedInputPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } }
76 return null;
77 }
78 public IWindowPlugin GetWindowPlugin(string t)
79 {
80 foreach (IWindowPlugin c in LoadedWindowPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } }
81 return null;
82 }
83
84 #endregion
85
86 private void GetConfigPluginsFromDll(FileInfo dll)
87 {
88 logger.Debug.WriteLine(" Getting Config plugins contained in {0}", dll.Name);
89 Assembly asm = Assembly.LoadFile(dll.FullName);
90 List<Type> types = new List<Type>(asm.GetTypes());
91 foreach (Type type in types)
92 {
93 if (type.BaseType == typeof(ConfigPlugin))
94 {
95 ConstructorInfo ci = null;
96 ci = type.GetConstructor(new Type[] {});
97 if (ci == null)
98 {
99 throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name));
100 }
101 else
102 {
103 object o = ci.Invoke(new object[] { });
104 IConfigPlugin c = (IConfigPlugin)o;
105 if (c == null)
106 {
107 throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name));
108 }
109 else
110 {
111 logger.Debug.WriteLine(" Loaded Config Plugin [name={0}] from {1}", c.Name, dll.Name);
112 LoadedConfigPlugins.Add(c);
113 }
114 }
115 }
116 }
117 }
118 private void GetInputPluginsFromDll(FileInfo dll)
119 {
120 logger.Debug.WriteLine(" Getting Input plugins contained in {0}", dll.Name);
121 Assembly asm = Assembly.LoadFile(dll.FullName);
122 List<Type> types = new List<Type>(asm.GetTypes());
123 foreach (Type type in types)
124 {
125 if (type.BaseType == typeof(InputPlugin))
126 {
127 ConstructorInfo ci = null;
128 ci = type.GetConstructor(new Type[] { });
129 if (ci == null)
130 {
131 throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name));
132 }
133 else
134 {
135 object o = ci.Invoke(new object[] { });
136 IInputPlugin c = (IInputPlugin)o;
137 if (c == null)
138 {
139 throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name));
140 }
141 else
142 {
143 logger.Debug.WriteLine(" Loaded Input Plugin [name={0}] from {1}", c.Name, dll.Name);
144 LoadedInputPlugins.Add(c);
145 }
146 }
147 }
148 }
149 }
150 private void GetWindowPluginsFromDll(FileInfo dll)
151 {
152 logger.Debug.WriteLine(" Getting Window plugins contained in {0}", dll.Name);
153 Assembly asm = Assembly.LoadFile(dll.FullName);
154 List<Type> types = new List<Type>(asm.GetTypes());
155 foreach (Type type in types)
156 {
157 if (type.BaseType == typeof(WindowPlugin))
158 {
159 ConstructorInfo ci = null;
160 ci = type.GetConstructor(new Type[] { });
161 if (ci == null)
162 {
163 throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name));
164 }
165 else
166 {
167 object o = ci.Invoke(new object[] { });
168 IWindowPlugin c = (IWindowPlugin)o;
169 if (c == null)
170 {
171 throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name));
172 }
173 else
174 {
175 logger.Debug.WriteLine(" Loaded Window Plugin [name={0}] from {1}", c.Name, dll.Name);
176 LoadedWindowPlugins.Add(c);
177 }
178 }
179 }
180 }
181 }
182 }
183 }