--- trunk/RomCheater.PluginFramework/Core/PluginLoader.cs 2012/05/09 17:48:15 83 +++ trunk/RomCheater.PluginFramework/Core/PluginLoader.cs 2012/05/09 19:51:42 87 @@ -4,6 +4,8 @@ using System.Linq; using System.Text; using RomCheater.PluginFramework.Interfaces; using RomCheater.Logging; +using System.IO; +using System.Reflection; namespace RomCheater.PluginFramework.Core { @@ -19,12 +21,163 @@ namespace RomCheater.PluginFramework.Cor public void LoadPlugins() { - logger.Info.WriteLine("Loading Plugins..."); - logger.Info.WriteLine("Plguins Loaded."); + try + { + logger.Info.WriteLine("Loading Plugins..."); + + string PluginPath = string.Format(@"{0}\Plugins", typeof(PluginLoader).Assembly.Location.Replace(@"\RomCheater.PluginFramework.dll", "")); + logger.Debug.WriteLine("Plugins Path: {0}", PluginPath); + List<string> dlls = new List<string>(Directory.GetFiles(PluginPath, "*.dll")); + logger.Debug.WriteLine(" Found: {0} plugin dlls", dlls.Count); + foreach (string dll in dlls) + { + FileInfo fi = new FileInfo(dll); + logger.Debug.WriteLine(" plugin[{0}]: {1}", dlls.IndexOf(dll), fi.Name); + GetConfigPluginsFromDll(fi); + GetInputPluginsFromDll(fi); + GetWindowPluginsFromDll(fi); + } + + logger.Info.WriteLine(" Loaded {0} config plugins", LoadedConfigPlugins.Count); + logger.Info.WriteLine(" Loaded {0} input plugins", LoadedInputPlugins.Count); + logger.Info.WriteLine(" Loaded {0} window plugins", LoadedWindowPlugins.Count); + + logger.Info.WriteLine("Plugins Loaded."); + } + catch (ReflectionTypeLoadException ex) + { + StringBuilder builder = new StringBuilder(); + if (ex.LoaderExceptions.Count() > 0) + { + foreach (Exception c in ex.LoaderExceptions) + { + builder.AppendLine(c.ToString()); + } + } + logger.Error.WriteLine("Failed to load one or more plugins{0}Possible Reason:{0}{1}", System.Environment.NewLine, builder.ToString()); + } + catch (Exception ex) + { + logger.Error.WriteLine("Failed to load one or more plugins{0}Possible Reason:{0}{1}", System.Environment.NewLine, ex.ToString()); + } } public List<IConfigPlugin> LoadedConfigPlugins { get; private set; } public List<IInputPlugin> LoadedInputPlugins { get; private set; } public List<IWindowPlugin> LoadedWindowPlugins { get; private set; } + + public IConfigPlugin GetConfigPlugin(string t) + { + foreach (IConfigPlugin c in LoadedConfigPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } } + return null; + } + public IInputPlugin GetInputPlugin(string t) + { + foreach (IInputPlugin c in LoadedInputPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } } + return null; + } + public IWindowPlugin GetWindowPlugin(string t) + { + foreach (IWindowPlugin c in LoadedWindowPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } } + return null; + } + #endregion + + private void GetConfigPluginsFromDll(FileInfo dll) + { + logger.Debug.WriteLine(" Getting Config plugins contained in {0}", dll.Name); + Assembly asm = Assembly.LoadFile(dll.FullName); + List<Type> types = new List<Type>(asm.GetTypes()); + foreach (Type type in types) + { + if (type.BaseType == typeof(ConfigPlugin)) + { + ConstructorInfo ci = null; + ci = type.GetConstructor(new Type[] {}); + if (ci == null) + { + throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name)); + } + else + { + object o = ci.Invoke(new object[] { }); + IConfigPlugin c = (IConfigPlugin)o; + if (c == null) + { + throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name)); + } + else + { + logger.Debug.WriteLine(" Loaded Config Plugin [name={0}] from {1}", c.Name, dll.Name); + LoadedConfigPlugins.Add(c); + } + } + } + } + } + private void GetInputPluginsFromDll(FileInfo dll) + { + logger.Debug.WriteLine(" Getting Input plugins contained in {0}", dll.Name); + Assembly asm = Assembly.LoadFile(dll.FullName); + List<Type> types = new List<Type>(asm.GetTypes()); + foreach (Type type in types) + { + if (type.BaseType == typeof(InputPlugin)) + { + ConstructorInfo ci = null; + ci = type.GetConstructor(new Type[] { }); + if (ci == null) + { + throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name)); + } + else + { + object o = ci.Invoke(new object[] { }); + IInputPlugin c = (IInputPlugin)o; + if (c == null) + { + throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name)); + } + else + { + logger.Debug.WriteLine(" Loaded Input Plugin [name={0}] from {1}", c.Name, dll.Name); + LoadedInputPlugins.Add(c); + } + } + } + } + } + private void GetWindowPluginsFromDll(FileInfo dll) + { + logger.Debug.WriteLine(" Getting Window plugins contained in {0}", dll.Name); + Assembly asm = Assembly.LoadFile(dll.FullName); + List<Type> types = new List<Type>(asm.GetTypes()); + foreach (Type type in types) + { + if (type.BaseType == typeof(WindowPlugin)) + { + ConstructorInfo ci = null; + ci = type.GetConstructor(new Type[] { }); + if (ci == null) + { + throw new NullReferenceException(string.Format("Unable to bind to constructor for type: {0}", type.Name)); + } + else + { + object o = ci.Invoke(new object[] { }); + IWindowPlugin c = (IWindowPlugin)o; + if (c == null) + { + throw new NullReferenceException(string.Format("Failed to cast type {0} to IConfigPlugin", type.Name)); + } + else + { + logger.Debug.WriteLine(" Loaded Window Plugin [name={0}] from {1}", c.Name, dll.Name); + LoadedWindowPlugins.Add(c); + } + } + } + } + } } } |