Ejemplo n.º 1
0
 /// <summary>
 /// This function is called when the mod loads at startup
 /// </summary>
 public override void OnLoad()
 {
     GUIManager.getInstance().AddTextLine("Bobisback Combined Mods Loaded");
     //add all of our GUI's to the game
     GUIManager.getInstance().gameObject.AddComponent(typeof(GUIWindowTripleSpeed));
     GUIManager.getInstance().gameObject.AddComponent(typeof(GUIWindowIdleSettlers));
     GUIManager.getInstance().gameObject.AddComponent(typeof(GUIWindowCheatMenu));
     GUIManager.getInstance().gameObject.AddComponent(typeof(GUIWindowModOptions));
     GUIManager.getInstance().gameObject.AddComponent(typeof(GUIWindowControlGroup));
     SettingsManager.loadSettings(); //Load the settings for the game
     GUIWindowModOptions.displayMessage("Combined Mod Loaded", "Press '" + SettingsManager.hotKeys["toggleOptionsMenuHotKey"] + "' at any time to access the options menu. Thanks for checking this mod out!");
 }
        /// <summary>
        /// This function will load the plugin. It will take hte plugin info make sure the file exsits
        /// and then load the dll, find the class that needs to be loaded and then load that class.
        /// There is no checking in place to make sure the plugins do not conflict.
        /// </summary>
        /// <param name="pluginInfo">This is the plugin that will be loaded.</param>
        public static void loadPlugin(PluginInfo pluginInfo)
        {
            if (pluginInfo.isLoaded)
            {
                return;
            }

            pluginInfo.shouldLoadPlugin = true;
            FileInfo info = new FileInfo("./saves/" + pluginInfo.fileName);

            if (info != null && info.Exists == true)   //make sure the file is there
            {
                try {
                    //loading new domains do not work for some reason
                    //pluginInfo.appDomain = AppDomain.CreateDomain("Testdomain");
                    //AssemblyName assemblyName = new AssemblyName();
                    //assemblyName.CodeBase = pluginInfo.fileName;
                    //pluginInfo.assembly = pluginInfo.appDomain.Load(pluginInfo.fileName);

                    pluginInfo.assembly = Assembly.LoadFile("./saves/" + pluginInfo.fileName); //load file
                    if (pluginInfo.assembly != null)
                    {
                        Type[] types = pluginInfo.assembly.GetTypes(); //get all classes out of file
                        for (int i = 0; i < types.Length; i++)
                        {
                            Type type = types[i];
                            if (typeof(IPlugin).IsAssignableFrom(type) && type.IsClass)      //find the one that is a plugin
                            {
                                pluginInfo.plugin = (IPlugin)Activator.CreateInstance(type); //create that class
                            }
                        }
                    }
                    else
                    {
                        GUIWindowModOptions.displayErrorMessage("Assemply is null on load of '" + pluginInfo.fileName + "'");
                        AManager <GUIManager> .getInstance().AddTextLine("Assemply is null on load of '" + pluginInfo.fileName + "'");
                    }
                } catch (Exception ex) {
                    GUIWindowModOptions.displayErrorMessage("Could not load assembly: '" + pluginInfo.fileName + "'");
                    AManager <GUIManager> .getInstance().AddTextLine("Could not load assembly: '" + pluginInfo.fileName + "'");

                    AManager <GUIManager> .getInstance().AddTextLine(" " + ex.Message);

                    pluginInfo.shouldLoadPlugin = false;
                    pluginInfo.isLoaded         = false;
                }

                if (pluginInfo.plugin != null)   //make sure we created the class
                {
                    try {
                        pluginInfo.plugin.OnLoad(); //load hte plugin
                    } catch (Exception ex) {
                        GUIWindowModOptions.displayErrorMessage("Assembly " + pluginInfo.assembly.GetName().Name + " crashed in OnLoad");
                        AManager <GUIManager> .getInstance().AddTextLine("Assembly " + pluginInfo.assembly.GetName().Name + " crashed in OnLoad with exception: " + ex.Message);

                        pluginInfo.shouldLoadPlugin = false;
                        pluginInfo.isLoaded         = false;
                    }

                    try {
                        pluginInfo.plugin.OnEnable(); //enable the plugin
                    } catch (Exception ex) {
                        GUIWindowModOptions.displayErrorMessage("Assembly " + pluginInfo.assembly.GetName().Name + " crashed in OnEnable");
                        AManager <GUIManager> .getInstance().AddTextLine("Assembly " + pluginInfo.assembly.GetName().Name + " crashed in OnEnable with exception: " + ex.Message);

                        pluginInfo.shouldLoadPlugin = false;
                        pluginInfo.isLoaded         = false;
                    }
                    pluginInfo.isLoaded = true;
                }
                else
                {
                    GUIWindowModOptions.displayErrorMessage("Plugin is null");
                    AManager <GUIManager> .getInstance().AddTextLine("Plugin is null");

                    pluginInfo.shouldLoadPlugin = false;
                    pluginInfo.isLoaded         = false;
                }
            }
            else
            {
                GUIWindowModOptions.displayErrorMessage("File name does not exist: '" + pluginInfo.fileName + "'");
                AManager <GUIManager> .getInstance().AddTextLine("File name does not exist: '" + pluginInfo.fileName + "'");

                pluginInfo.shouldLoadPlugin = false;
                pluginInfo.isLoaded         = false;
            }
        }