Example #1
0
        public static void PatchAll(PermanentCookoutKit permanentCookout)
        {
            mod = permanentCookout;

            var harmony = new Harmony(mod.ModManifest.UniqueID);

            try
            {
                harmony.Patch(
                    original: AccessTools.Method(typeof(Torch), nameof(Torch.draw), new[] { typeof(SpriteBatch), typeof(int), typeof(int), typeof(float) }),
                    postfix: new HarmonyMethod(typeof(Patcher), nameof(Draw_Post)));

                harmony.Patch(
                    original: AccessTools.Method(typeof(StardewObject), nameof(StardewObject.performToolAction)),
                    postfix: new HarmonyMethod(typeof(Patcher), nameof(PerformToolAction_Post)));

                harmony.Patch(
                    original: AccessTools.Method(typeof(Torch), nameof(Torch.updateWhenCurrentLocation)),
                    postfix: new HarmonyMethod(typeof(Patcher), nameof(UpdateWhenCurrentLocation_Post)));

                harmony.Patch(
                    original: AccessTools.Method(typeof(Torch), nameof(Torch.checkForAction)),
                    prefix: new HarmonyMethod(typeof(Patcher), nameof(CheckForAction_Pre)));

                harmony.Patch(
                    original: AccessTools.Method(typeof(StardewObject), nameof(StardewObject.performObjectDropInAction), new[] { typeof(Item), typeof(bool), typeof(Farmer) }),
                    prefix: new HarmonyMethod(typeof(Patcher), nameof(UpdateCharcoalKilnInput)));
            }
            catch (Exception e)
            {
                mod.ErrorLog("Error while trying to setup required patches:", e);
            }

            if (mod.Helper.ModRegistry.IsLoaded("Pathoschild.Automate"))
            {
                try
                {
                    mod.DebugLog("This mod patches Automate. If you notice issues with Automate, make sure it happens without this mod before reporting it to the Automate page.");

                    // I don't see a use in using MachineWrapper because it's also internal I need to check for the type of the machine anyway which would be way too much reflection at runtime
                    var charcoalKiln = AccessTools.TypeByName("Pathoschild.Stardew.Automate.Framework.Machines.Objects.CharcoalKilnMachine");

                    harmony.Patch(
                        original: AccessTools.Method(charcoalKiln, "SetInput"),
                        prefix: new HarmonyMethod(typeof(Patcher), nameof(PatchCharcoalKiln)));
                }
                catch (Exception e)
                {
                    mod.ErrorLog($"Error while trying to patch Automate. Please report this to the mod page of {mod.ModManifest.Name}, not Automate:", e);
                }
            }
        }
Example #2
0
        public static void VerifyConfigValues(CookoutKitConfig config, PermanentCookoutKit mod)
        {
            bool invalidConfig = false;

            if (config.WoodNeeded < 0)
            {
                invalidConfig     = true;
                config.WoodNeeded = 0;
            }

            if (config.FiberNeeded < 0)
            {
                invalidConfig      = true;
                config.FiberNeeded = 0;
            }

            if (config.CoalNeeded < 0)
            {
                invalidConfig     = true;
                config.CoalNeeded = 0;
            }

            if (config.DriftwoodMultiplier < 0)
            {
                invalidConfig = true;
                config.DriftwoodMultiplier = 0;
            }

            if (config.HardwoodMultiplier < 0)
            {
                invalidConfig             = true;
                config.HardwoodMultiplier = 0;
            }

            if (config.NewspaperMultiplier < 0)
            {
                invalidConfig = true;
                config.NewspaperMultiplier = 0;
            }

            if (config.WoolMultiplier < 0)
            {
                invalidConfig         = true;
                config.WoolMultiplier = 0;
            }

            if (config.ClothMultiplier < 0)
            {
                invalidConfig          = true;
                config.ClothMultiplier = 0;
            }

            if (config.CharcoalKilnWoodNeeded < 1)
            {
                invalidConfig = true;
                config.CharcoalKilnWoodNeeded = 1;
            }

            if (config.CharcoalKilnTimeNeeded < 10)
            {
                invalidConfig = true;
                config.CharcoalKilnTimeNeeded = 10;
            }

            if (invalidConfig)
            {
                mod.DebugLog("At least one config value was out of range and was reset.");
                mod.Helper.WriteConfig(config);
            }
        }
Example #3
0
        public override void Entry(IModHelper helper)
        {
            mod = this;

            config = Helper.ReadConfig <CookoutKitConfig>();

            CookoutKitConfig.VerifyConfigValues(config, this);

            Helper.Events.GameLoop.GameLaunched += delegate { CookoutKitConfig.SetUpModConfigMenu(config, this); };

            Helper.Events.GameLoop.DayEnding += delegate { SaveCookingKits(); };

            var harmony = HarmonyInstance.Create(ModManifest.UniqueID);

            try
            {
                harmony.Patch(
                    original: AccessTools.Method(typeof(Torch), "draw", new[] { typeof(SpriteBatch), typeof(int), typeof(int), typeof(float) }),
                    postfix: new HarmonyMethod(typeof(PermanentCookoutKit), nameof(Draw_Post))
                    );

                harmony.Patch(
                    original: AccessTools.Method(typeof(Torch), "updateWhenCurrentLocation"),
                    postfix: new HarmonyMethod(typeof(PermanentCookoutKit), nameof(UpdateWhenCurrentLocation_Post))
                    );

                harmony.Patch(
                    original: AccessTools.Method(typeof(Torch), "checkForAction"),
                    prefix: new HarmonyMethod(typeof(PermanentCookoutKit), nameof(CheckForAction_Pre))
                    );

                harmony.Patch(
                    original: AccessTools.Method(typeof(StardewObject), "performObjectDropInAction", new[] { typeof(Item), typeof(bool), typeof(Farmer) }),
                    prefix: new HarmonyMethod(typeof(PermanentCookoutKit), nameof(UpdateCharcoalKilnInput))
                    );
            }
            catch (Exception e)
            {
                ErrorLog("Error while trying to setup required patches:", e);
            }

            if (mod.Helper.ModRegistry.IsLoaded("Pathoschild.Automate"))
            {
                try
                {
                    mod.DebugLog("This mod patches Automate. If you notice issues with Automate, make sure it happens without this mod before reporting it to the Automate page.");

                    // this is so ugly but I can't include a reference
                    Assembly assembly = null;

                    foreach (var item in AppDomain.CurrentDomain.GetAssemblies())
                    {
                        if (item.GetName().Name.Trim() == "Automate")
                        {
                            assembly = item;
                            break;
                        }
                    }

                    if (assembly == null)
                    {
                        mod.ErrorLog($"Error while trying to patch Automate. Please report this to the mod page of {mod.ModManifest.Name}, not Automate.");
                        return;
                    }

                    // I don't see a use in using MachineWrapper because it's also internal I need to check for the type of the machine anyway which would be way too much reflection at runtime
                    var charcoalKiln = assembly.GetType("Pathoschild.Stardew.Automate.Framework.Machines.Objects.CharcoalKilnMachine");

                    harmony.Patch(
                        original: AccessTools.Method(charcoalKiln, "SetInput"),
                        prefix: new HarmonyMethod(typeof(PermanentCookoutKit), nameof(PatchCharcoalKiln))
                        );
                }
                catch (Exception e)
                {
                    mod.ErrorLog($"Error while trying to patch Automate. Please report this to the mod page of {mod.ModManifest.Name}, not Automate:", e);
                }
            }
        }
        public static bool CheckForAction_Pre(Torch __instance, Farmer who, bool justCheckingForActivity)
        {
            try
            {
                if (justCheckingForActivity)
                {
                    return(true);
                }

                // check for ignition
                if (__instance.parentSheetIndex == 278 && !__instance.IsOn && who != null)
                {
                    int coalCount         = mod.config.CoalNeeded;
                    int baseKindlingCount = mod.config.FiberNeeded;
                    int baseWoodCount     = mod.config.WoodNeeded;

                    float driftwoodMult = mod.config.DriftwoodMultiplier;
                    float hardwoodMult  = mod.config.HardwoodMultiplier;

                    bool hasCoal = who.hasItemInInventory(coalID, coalCount);

                    bool hasKindling         = false;
                    int  kindlingID          = -1;
                    int  actualKindlingCount = -1;

                    if (hasCoal)
                    {
                        if (!hasKindling)
                        {
                            // soggy newspaper
                            hasKindling = CheckForResource(who, 172, baseKindlingCount, mod.config.NewspaperMultiplier, ref kindlingID, ref actualKindlingCount);
                        }

                        if (!hasKindling)
                        {
                            // fiber
                            hasKindling = CheckForResource(who, fiberID, baseKindlingCount, 1, ref kindlingID, ref actualKindlingCount);
                        }

                        if (!hasKindling)
                        {
                            // wool
                            hasKindling = CheckForResource(who, 440, baseKindlingCount, mod.config.WoolMultiplier, ref kindlingID, ref actualKindlingCount);
                        }

                        if (!hasKindling)
                        {
                            // cloth
                            hasKindling = CheckForResource(who, 428, baseKindlingCount, mod.config.ClothMultiplier, ref kindlingID, ref actualKindlingCount);
                        }
                    }

                    bool hasWood         = false;
                    int  chosenWoodID    = -1;
                    int  actualWoodCount = -1;

                    if (hasCoal && hasKindling)
                    {
                        if (!hasWood)
                        {
                            // driftwood
                            hasWood = CheckForResource(who, driftwoodID, baseKindlingCount, mod.config.DriftwoodMultiplier, ref chosenWoodID, ref actualWoodCount);
                        }

                        if (!hasWood)
                        {
                            // wood
                            hasWood = CheckForResource(who, woodID, baseKindlingCount, 1, ref chosenWoodID, ref actualWoodCount);
                        }

                        if (!hasWood)
                        {
                            // hardwood
                            hasWood = CheckForResource(who, hardwoodID, baseKindlingCount, mod.config.HardwoodMultiplier, ref chosenWoodID, ref actualWoodCount);
                        }
                    }

                    if (hasCoal && hasKindling && hasWood)
                    {
                        mod.DebugLog(coalID + " " + coalCount + " " + kindlingID + " " + actualKindlingCount + " " + chosenWoodID + " " + actualWoodCount);
                        who.removeItemsFromInventory(coalID, coalCount);
                        who.removeItemsFromInventory(kindlingID, actualKindlingCount);
                        who.removeItemsFromInventory(chosenWoodID, actualWoodCount);

                        __instance.isOn.Value = true;

                        if (__instance.bigCraftable)
                        {
                            Game1.playSound("fireball");

                            __instance.initializeLightSource(__instance.tileLocation, false);
                            AmbientLocationSounds.addSound(__instance.tileLocation, 1);
                        }
                    }
                    else
                    {
                        var coal  = new StardewObject(coalID, 0);
                        var fiber = new StardewObject(fiberID, 0);
                        var wood  = new StardewObject(woodID, 0);

                        Game1.showRedMessage($"{coalCount} {coal.DisplayName}, {baseKindlingCount} {fiber.DisplayName}, {baseWoodCount} {wood.DisplayName}");
                    }

                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            catch (Exception e)
            {
                mod.ErrorLog("There was an exception in a patch", e);
                return(true);
            }
        }