ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.PluginFramework/Core/PluginLoader.cs
(Generate patch)

Comparing trunk/RomCheater.PluginFramework/Core/PluginLoader.cs (file contents):
Revision 710 by william, Mon Jun 17 09:31:52 2013 UTC vs.
Revision 711 by william, Tue Jun 18 08:36:08 2013 UTC

--- trunk/RomCheater.PluginFramework/Core/PluginLoader.cs	2013/06/18 07:28:11	710
+++ trunk/RomCheater.PluginFramework/Core/PluginLoader.cs	2013/06/18 08:36:08	711
@@ -19,9 +19,38 @@ using RomCheater.Logging;
 using System.IO;
 using System.Reflection;
 using RomCheater.Core;
+using System.Runtime.Serialization;
+using System.Security;
 
 namespace RomCheater.PluginFramework.Core
 {
+    #region plugin loader execeptions
+    public class PluginNotFoundException : System.Exception
+    {
+        public PluginNotFoundException() : base() { }
+        public PluginNotFoundException(string message) : base(message) { }
+        public PluginNotFoundException(string message, Exception innerException) : base(message, innerException) { }
+        [SecuritySafeCritical]
+        protected PluginNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { }
+    }
+    public class InvalidPluginException : System.Exception
+    {
+        public InvalidPluginException() : base() { }
+        public InvalidPluginException(string message) : base(message) { }
+        public InvalidPluginException(string message, Exception innerException) : base(message, innerException) { }
+        [SecuritySafeCritical]
+        protected InvalidPluginException(SerializationInfo info, StreamingContext context) : base(info, context) { }
+    }
+    public class InvalidPluginTypeException : System.Exception
+    {
+        public InvalidPluginTypeException() : base() { }
+        public InvalidPluginTypeException(string message) : base(message) { }
+        public InvalidPluginTypeException(string message, Exception innerException) : base(message, innerException) { }
+        [SecuritySafeCritical]
+        protected InvalidPluginTypeException(SerializationInfo info, StreamingContext context) : base(info, context) { }
+    }
+    #endregion
+
     public class PluginLoader : IPluginLoader
     {
 
@@ -33,6 +62,346 @@ namespace RomCheater.PluginFramework.Cor
             LoadedUserControlPlugins = new List<IUserControlPlugin>();
         }
         #region IPluginLoader Members
+
+        public T GetPluginByName<T>(string name) where T : IPluginBase
+        {
+            T plugin = default(T);
+            if (string.IsNullOrEmpty(name))
+            {
+                throw new ArgumentException("name cannot be null or an empty string", "name");
+            }
+
+            bool known = IsKnownPluginName<T>(name, out plugin);
+            if (known)
+            {
+                if (plugin != null)
+                {
+                    return plugin;
+                }
+                else
+                {
+                    throw new InvalidPluginException(string.Format("Plugin name: '{0}' is known, but the returned plugin was null", name));
+                }
+            }
+            else
+            {
+                if (LoadedConfigPlugins.Count > 0)
+                {
+                    foreach (var p in LoadedConfigPlugins)
+                    {
+                        if (p.Name.ToLower() == name.ToLower())
+                        {
+                            plugin = (T)p;
+                            known = true;
+                            break;
+                        }
+                    }
+                }
+                if (LoadedInputPlugins.Count > 0)
+                {
+                    foreach (var p in LoadedInputPlugins)
+                    {
+                        if (p.Name.ToLower() == name.ToLower())
+                        {
+                            plugin = (T)p;
+                            known = true;
+                            break;
+                        }
+                    }
+                }
+                if (LoadedWindowPlugins.Count > 0)
+                {
+                    foreach (var p in LoadedWindowPlugins)
+                    {
+                        if (p.Name.ToLower() == name.ToLower())
+                        {
+                            plugin = (T)p;
+                            known = true;
+                            break;
+                        }
+                    }
+                }
+                if (LoadedUserControlPlugins.Count > 0)
+                {
+                    foreach (var p in LoadedUserControlPlugins)
+                    {
+                        if (p.Name.ToLower() == name.ToLower())
+                        {
+                            plugin = (T)p;
+                            known = true;
+                            break;
+                        }
+                    }
+                }
+                if (known)
+                {
+                    return plugin;
+                }
+                else
+                {
+                    throw new PluginNotFoundException(string.Format("Could not find a plugin with name: '{0}'", name));
+                }
+            }
+        }
+        public T GetPluginByGuid<T>(string plugin_guid) where T : IPluginBase
+        {
+            T plugin = default(T);
+
+            if (string.IsNullOrEmpty(plugin_guid))
+            {
+                throw new ArgumentException("plugin_plugin cannot be null or an empty string", "plugin_guid");
+            }
+
+            bool known = IsKnownPluginGuid<T>(plugin_guid, out plugin);
+            if (known)
+            {
+                if (plugin != null)
+                {
+                    return plugin;
+                }
+                else
+                {
+                    throw new InvalidPluginException(string.Format("Plugin guid: '{0}' is known, but the returned plugin was null", plugin_guid));
+                }
+            }
+            else
+            {                
+                if (LoadedConfigPlugins.Count > 0)
+                {
+                    foreach (var p in LoadedConfigPlugins)
+                    {
+                        if (p.ID == new Guid(plugin_guid))
+                        {
+                            plugin = (T)p;
+                            known = true;
+                            break;
+                        }
+                    }
+                }
+                if (LoadedInputPlugins.Count > 0)
+                {
+                    foreach (var p in LoadedInputPlugins)
+                    {
+                        if (p.ID == new Guid(plugin_guid))
+                        {
+                            plugin = (T)p;
+                            known = true;
+                            break;
+                        }
+                    }
+                }
+                if (LoadedWindowPlugins.Count > 0)
+                {
+                    foreach (var p in LoadedWindowPlugins)
+                    {
+                        if (p.ID == new Guid(plugin_guid))
+                        {
+                            plugin = (T)p;
+                            known = true;
+                            break;
+                        }
+                    }
+                }
+                if (LoadedUserControlPlugins.Count > 0)
+                {
+                    foreach (var p in LoadedUserControlPlugins)
+                    {
+                        if (p.ID == new Guid(plugin_guid))
+                        {
+                            plugin = (T)p;
+                            known = true;
+                            break;
+                        }
+                    }
+                }
+            }
+            if (known)
+            {
+                return plugin;
+            }
+            else
+            {
+                throw new PluginNotFoundException(string.Format("Could not find a plugin with guid: '{0}'", plugin_guid));
+            }
+        }
+        public string[] GetKnownPluginGuids<T>() where T : IPluginBase
+        {
+            Type t = typeof(T);
+            string name = t.Name.ToLower();
+
+            List<string> guids = new List<string>();
+            guids.Add(new Guid().ToString().ToLower()); // add null guids
+            switch (name)
+            {
+                case "iconfigplugin":
+                    guids.Add(new Guid("478e225b-c3e8-9280-57ca-384b884fc4cc").ToString().ToLower()); // generic config
+                    break;
+                case "iinputplugin": 
+                    // no known plugins (other than null at this time)
+                    break;
+                case "iwindowplugin":
+                    // no known plugins (other than null at this time)
+                    break;
+                case "iusercontrolplugin":
+                    guids.RemoveAt(0); // userplugins should not have a null plugin guid
+                    guids.Add(new Guid("09d0c77b-e0d1-47ca-907e-997d19f2c8c2").ToString().ToLower()); // cheat code converter plugin
+                    guids.Add(new Guid("7b0b9c38-8774-491a-8d9b-3255eddf1407").ToString().ToLower()); // emulator memory map plugin
+                    guids.Add(new Guid("bbd24cd2-5ab0-470c-9e32-6a9458025855").ToString().ToLower()); // rva calculator plugin
+                    guids.Add(new Guid("de686ea8-3f5f-479b-b742-5b13d93cb579").ToString().ToLower()); // scrathpad plugin
+                    break;
+                default:
+                    throw new InvalidPluginTypeException(string.Format("'{0}' is not a valid plugin type", t.Name));
+            }
+
+
+            return guids.ToArray();
+        }
+
+        public bool IsKnownPluginName<T>(string plugin_name, out T plugin) where T : IPluginBase
+        {
+            plugin = default(T);
+            bool known = false;
+
+            Type t = typeof(T);
+            string name = t.Name.ToLower();
+            switch (name)
+            {
+                case "iconfigplugin":
+                    if (LoadedConfigPlugins.Count > 0)
+                    {
+                        foreach (var p in LoadedConfigPlugins)
+                        {
+                            if (p.Name.ToLower() == plugin_name.ToLower())
+                            {
+                                plugin = (T)p;
+                                known = true;
+                                break;
+                            }
+                        }
+                    }
+                    break;
+                case "iinputplugin":
+                    if (LoadedInputPlugins.Count > 0)
+                    {
+                        foreach (var p in LoadedInputPlugins)
+                        {
+                            if (p.Name.ToLower() == plugin_name.ToLower())
+                            {
+                                plugin = (T)p;
+                                known = true;
+                                break;
+                            }
+                        }
+                    }
+                    break;
+                case "iwindowplugin":
+                    if (LoadedWindowPlugins.Count > 0)
+                    {
+                        foreach (var p in LoadedWindowPlugins)
+                        {
+                            if (p.Name.ToLower() == plugin_name.ToLower())
+                            {
+                                plugin = (T)p;
+                                known = true;
+                                break;
+                            }
+                        }
+                    }
+                    break;
+                case "iusercontrolplugin":
+                    if (LoadedUserControlPlugins.Count > 0)
+                    {
+                        foreach (var p in LoadedUserControlPlugins)
+                        {
+                            if (p.Name.ToLower() == plugin_name.ToLower())
+                            {
+                                plugin = (T)p;
+                                known = true;
+                                break;
+                            }
+                        }
+                    }
+                    break;
+                default:
+                    throw new InvalidPluginTypeException(string.Format("'{0}' is not a valid plugin type", t.Name));
+            }
+
+
+            return known;
+        }
+        public bool IsKnownPluginGuid<T>(string plugin_guid, out T plugin) where T : IPluginBase
+        {
+            plugin = default(T);
+            bool known = false;
+            var guids = GetKnownPluginGuids<T>();
+            known = guids.ToList().Contains(plugin_guid.ToLower());
+
+            if (known)
+            {
+                Type t = typeof(T);
+                string name = t.Name.ToLower();
+                switch (name)
+                {
+                    case "iconfigplugin":
+                        if (LoadedConfigPlugins.Count > 0)
+                        {
+                            foreach (var p in LoadedConfigPlugins)
+                            {
+                                if (p.ID == new Guid(plugin_guid))
+                                {
+                                    plugin = (T)p;
+                                    break;
+                                }
+                            }
+                        }
+                        break;
+                    case "iinputplugin":
+                        if (LoadedInputPlugins.Count > 0)
+                        {
+                            foreach (var p in LoadedInputPlugins)
+                            {
+                                if (p.ID == new Guid(plugin_guid))
+                                {
+                                    plugin = (T)p;
+                                    break;
+                                }
+                            }
+                        }
+                        break;
+                    case "iwindowplugin":
+                        if (LoadedWindowPlugins.Count > 0)
+                        {
+                            foreach (var p in LoadedWindowPlugins)
+                            {
+                                if (p.ID == new Guid(plugin_guid))
+                                {
+                                    plugin = (T)p;
+                                    break;
+                                }
+                            }
+                        }
+                        break;
+                    case "iusercontrolplugin":
+                        if (LoadedUserControlPlugins.Count > 0)
+                        {
+                            foreach (var p in LoadedUserControlPlugins)
+                            {
+                                if (p.ID == new Guid(plugin_guid))
+                                {
+                                    plugin = (T)p;
+                                    break;
+                                }
+                            }
+                        }
+                        break;
+                    default:
+                        throw new InvalidPluginTypeException(string.Format("'{0}' is not a valid plugin type", t.Name));
+                }
+            }
+
+            return known;
+        }
+
         public void LoadPlugins() { LoadPlugins(false); }
         public void LoadPlugins(bool silent)
         {
@@ -103,7 +472,7 @@ namespace RomCheater.PluginFramework.Cor
             foreach (IConfigPlugin c in LoadedConfigPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } }
             return GetGenericConfigPlugin();
         }
-        private IConfigPlugin GetGenericConfigPlugin()
+        public IConfigPlugin GetGenericConfigPlugin()
         {
             foreach (IConfigPlugin c in LoadedConfigPlugins) { if (c.IsGenericPlugin || c.IsNullPlugin) { return c; } }
             return null;
@@ -113,7 +482,7 @@ namespace RomCheater.PluginFramework.Cor
             foreach (IInputPlugin c in LoadedInputPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } }
             return GetGenericInputPlugin();
         }
-        private IInputPlugin GetGenericInputPlugin()
+        public IInputPlugin GetGenericInputPlugin()
         {
             foreach (IInputPlugin c in LoadedInputPlugins) { if (c.IsGenericPlugin || c.IsNullPlugin) { return c; } }
             return null;
@@ -123,12 +492,12 @@ namespace RomCheater.PluginFramework.Cor
             foreach (IWindowPlugin c in LoadedWindowPlugins) { if (c.ToString().ToLower() == t.ToLower()) { return c; } }
             return GetGenericWindowPlugin();
         }
-        private IWindowPlugin GetGenericWindowPlugin()
+        public IWindowPlugin GetGenericWindowPlugin()
         {
             foreach (IWindowPlugin c in LoadedWindowPlugins) { if (c.IsGenericPlugin || c.IsNullPlugin) { return c; } }
             return null;
         }
-        private IUserControlPlugin GetUserControlPlugin()
+        public IUserControlPlugin GetUserControlPlugin()
         {
             foreach (IUserControlPlugin c in LoadedUserControlPlugins) { if (c.IsGenericPlugin || c.IsNullPlugin) { return c; } }
             return null;