/[RomCheater]/trunk/RomCheater.PluginFramework/Core/PluginLoader.cs
ViewVC logotype

Diff of /trunk/RomCheater.PluginFramework/Core/PluginLoader.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/RomCheater.PluginFramework/Core/PluginLoader.cs	2012/05/09 17:55:55	84
+++ trunk/RomCheater.PluginFramework/Core/PluginLoader.cs	2012/05/09 19:41:06	86
@@ -5,6 +5,7 @@
 using RomCheater.PluginFramework.Interfaces;
 using RomCheater.Logging;
 using System.IO;
+using System.Reflection;
 
 namespace RomCheater.PluginFramework.Core
 {
@@ -20,22 +21,146 @@
 
         public void LoadPlugins()
         {
-            logger.Info.WriteLine("Loading Plugins...");
+            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);
 
-            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)
+                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)
             {
-                FileInfo fi = new FileInfo(dll);
-                logger.Debug.WriteLine("    plugin[{0}]: {1}", dlls.IndexOf(dll), fi.Name);
+                logger.Error.WriteLine("Failed to load one or more plugins{0}Possible Reason:{0}{1}", System.Environment.NewLine, ex.ToString());
             }
-            logger.Info.WriteLine("Plugins Loaded.");
         }
         public List<IConfigPlugin> LoadedConfigPlugins { get; private set; }
         public List<IInputPlugin> LoadedInputPlugins { get; private set; }
         public List<IWindowPlugin> LoadedWindowPlugins { get; private set; }
         #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);
+                        }
+                    }
+                }
+            }
+        }
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22