Ejemplo n.º 1
0
        private static bool LoadSettings()
        {
            string location = QModServices.Main.GetMyMod().LoadedAssembly.Location;

            log(Logger.Level.Debug, "mod location:  {0}", location);
            DirectoryInfo dir = Directory.GetParent(location);

            log(Logger.Level.Debug, "mod directory:  {0}", dir.FullName);
            FileInfo[] settingsFiles = dir.GetFiles("Settings.xml");

            if (settingsFiles.Length < 1)
            {
                AlertUser("Manage Creature Spawns could not find \"Settings.xml\" in mod folder. Manage Creature Spawns is now disabled.");
                return(false);
            }
            else if (settingsFiles.Length > 1)
            {
                List <string> settingsFilesNames = new List <string>();
                settingsFiles.ForEach(file => { settingsFilesNames.Add(file.Name); });
                log(Logger.Level.Warn, "Multiple settings files found. Using first one available. {0}", settingsFilesNames);
            }
            string settingsFileName = settingsFiles[0].FullName;

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(SettingsManager.Settings));

                log("Filtering out comments");
                // load document
                XmlDocument doc = new XmlDocument();
                doc.Load(settingsFileName);

                // remove all comments
                XmlNodeList l = doc.SelectNodes("//comment()");
                foreach (XmlNode node in l)
                {
                    node.ParentNode.RemoveChild(node);
                }

                // store to memory stream and rewind
                MemoryStream ms = new MemoryStream();
                doc.Save(ms);
                ms.Seek(0, SeekOrigin.Begin);

                log("Reading settings.");
                settings   = (SettingsManager.Settings)serializer.Deserialize(XmlReader.Create(ms));
                serializer = null;
            }
            catch (Exception ex)
            {
                Logger.Log(Logger.Level.Error, "Exception occurred while loading settings", ex);
                AlertUser("Manage Creature Spawns could not load settings from \"Settings.xml\".  Manage Creature Spawns is now disabled.");
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public static void ApplyPatches()
        {
            log("Loading... v{0}", false, Assembly.GetExecutingAssembly().GetName().Version.ToString());

            HarmonyInstance harmony = HarmonyInstance.Create("mod.berkay2578.safeautosave");

            if (harmony != null)
            {
                log("HarmonyInstance created.");

                log("Reading settings.");
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(SettingsManager.Settings));
                    using (StreamReader reader = new StreamReader("QMods\\SafeAutosave\\Settings.xml"))
                        settings = (SettingsManager.Settings)serializer.Deserialize(reader);
                    serializer = null;

                    if (settings == null)
                    {
                        log("Could not load settings, exiting.");
                        return;
                    }
                }

                {
                    harmony.Patch(
                        typeof(SubRoot).GetMethod("OnPlayerEntered", BindingFlags.Public | BindingFlags.Instance),
                        null,
                        new HarmonyMethod(typeof(SaveManager).GetMethod("SaveOnEntry"))
                        );
                    log("Patched SubRoot.OnPlayerEntered");

                    harmony.Patch(
                        typeof(SubRoot).GetMethod("OnPlayerExited", BindingFlags.Public | BindingFlags.Instance),
                        null,
                        new HarmonyMethod(typeof(SaveManager).GetMethod("SaveOnExit"))
                        );
                    log("Patched SubRoot.OnPlayerExited");
                }

                log("Patched successfully.");
            }
            else
            {
                log("HarmonyInstance() returned null.");
            }
        }
Ejemplo n.º 3
0
        public static void ApplyPatches()
        {
            log("Loading... v{0}", Assembly.GetExecutingAssembly().GetName().Version.ToString());

            harmony = new Harmony("mod.berkay2578.managecreaturespawns");
            if (harmony != null)
            {
                log("Harmony instance created.");

                log("Reading settings.");
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(SettingsManager.Settings));
                    using (StreamReader reader = new StreamReader("QMods\\ManageCreatureSpawns\\Settings.xml"))
                        settings = (SettingsManager.Settings)serializer.Deserialize(reader);
                    serializer = null;

                    if (settings == null)
                    {
                        log("Could not load settings, exiting.");
                        return;
                    }

                    foreach (var item in settings.UnwantedCreaturesList)
                    {
                        log("Loaded creature configuration: \r\n{0}", item.ToString());
                    }
                }

                log("Patching Creature events");
                {
                    List <string> genericFunctionsToBePatched = new List <string>()
                    {
                        "InitializeAgain",
                        "InitializeOnce",
                        "OnDrop",
                        "OnTakeDamage",
                        "ProcessInfection",
                        "ScanCreatureActions",
                        "Update",
                        "UpdateBehaviour",
                        "Start"
                    };
                    List <string> creatureActionFunctionsToBePatched = new List <string>()
                    {
                        "ChooseBestAction",
                        "GetBestAction",
                        "GetLastAction"
                    };

                    foreach (string fn in genericFunctionsToBePatched)
                    {
                        harmony.Patch(
                            typeof(Creature).GetMethod(fn, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance),
                            new HarmonyMethod(typeof(Manager).GetMethod("GenericKillCreature")),
                            null
                            );
                        log("Patched Creature.{0}", fn);
                    }
                    foreach (string fn in creatureActionFunctionsToBePatched)
                    {
                        harmony.Patch(
                            typeof(Creature).GetMethod(fn, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance),
                            new HarmonyMethod(typeof(Manager).GetMethod("CreatureActionKillCreature")),
                            null
                            );
                        log("Patched Creature.{0}", fn);
                    }
                }
                log("Finished.");
            }
            else
            {
                log("Harmony() instance returned null.");
            }
        }