using System; using System.Collections.Generic; using System.Linq; using System.Text; using RomCheater.PluginFramework.Interfaces; using RomCheater.Logging; using System.IO; using System.Reflection; namespace RomCheater.PluginFramework.Core { public class PluginLoader : IPluginLoader { public PluginLoader() { LoadedConfigPlugins = new List(); LoadedInputPlugins = new List(); LoadedWindowPlugins = new List(); } #region IPluginLoader Members public void LoadPlugins() { 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 dlls = new List(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 LoadedConfigPlugins { get; private set; } public List LoadedInputPlugins { get; private set; } public List 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 types = new List(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 types = new List(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 types = new List(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); } } } } } } }