/[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 85 - (show annotations) (download)
Wed May 9 18:13:51 2012 UTC (8 years, 8 months ago) by william
File size: 6811 byte(s)
+ implement plugin loading support

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 (Exception ex)
48 {
49 logger.Error.WriteLine("Failed to load one or more plugins{0}Possible Reason:{0}{1}", System.Environment.NewLine, ex.ToString());
50 }
51 }
52 public List<IConfigPlugin> LoadedConfigPlugins { get; private set; }
53 public List<IInputPlugin> LoadedInputPlugins { get; private set; }
54 public List<IWindowPlugin> LoadedWindowPlugins { get; private set; }
55 #endregion
56
57 private void GetConfigPluginsFromDll(FileInfo dll)
58 {
59 logger.Debug.WriteLine(" Getting Config plugins contained in {0}", dll.Name);
60 Assembly asm = Assembly.LoadFile(dll.FullName);
61 List<Type> types = new List<Type>(asm.GetTypes());
62 foreach (Type type in types)
63 {
64 if (type.BaseType == typeof(ConfigPlugin))
65 {
66 ConstructorInfo ci = null;
67 ci = type.GetConstructor(new Type[] {});
68 if (ci == null)
69 {
70 throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name));
71 }
72 else
73 {
74 object o = ci.Invoke(new object[] { });
75 IConfigPlugin c = (IConfigPlugin)o;
76 if (c == null)
77 {
78 throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name));
79 }
80 else
81 {
82 logger.Debug.WriteLine(" Loaded Config Plugin [name={0}] from {1}", c.Name, dll.Name);
83 LoadedConfigPlugins.Add(c);
84 }
85 }
86 }
87 }
88 }
89 private void GetInputPluginsFromDll(FileInfo dll)
90 {
91 logger.Debug.WriteLine(" Getting Input plugins contained in {0}", dll.Name);
92 Assembly asm = Assembly.LoadFile(dll.FullName);
93 List<Type> types = new List<Type>(asm.GetTypes());
94 foreach (Type type in types)
95 {
96 if (type.BaseType == typeof(InputPlugin))
97 {
98 ConstructorInfo ci = null;
99 ci = type.GetConstructor(new Type[] { });
100 if (ci == null)
101 {
102 throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name));
103 }
104 else
105 {
106 object o = ci.Invoke(new object[] { });
107 IInputPlugin c = (IInputPlugin)o;
108 if (c == null)
109 {
110 throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name));
111 }
112 else
113 {
114 logger.Debug.WriteLine(" Loaded Input Plugin [name={0}] from {1}", c.Name, dll.Name);
115 LoadedInputPlugins.Add(c);
116 }
117 }
118 }
119 }
120 }
121 private void GetWindowPluginsFromDll(FileInfo dll)
122 {
123 logger.Debug.WriteLine(" Getting Window plugins contained in {0}", dll.Name);
124 Assembly asm = Assembly.LoadFile(dll.FullName);
125 List<Type> types = new List<Type>(asm.GetTypes());
126 foreach (Type type in types)
127 {
128 if (type.BaseType == typeof(WindowPlugin))
129 {
130 ConstructorInfo ci = null;
131 ci = type.GetConstructor(new Type[] { });
132 if (ci == null)
133 {
134 throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name));
135 }
136 else
137 {
138 object o = ci.Invoke(new object[] { });
139 IWindowPlugin c = (IWindowPlugin)o;
140 if (c == null)
141 {
142 throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name));
143 }
144 else
145 {
146 logger.Debug.WriteLine(" Loaded Window Plugin [name={0}] from {1}", c.Name, dll.Name);
147 LoadedWindowPlugins.Add(c);
148 }
149 }
150 }
151 }
152 }
153 }
154 }

  ViewVC Help
Powered by ViewVC 1.1.22