Example #1
0
        public static bool PatchCharcoalKiln(ref object __instance)
        {
            try
            {
                var      recipesProp = __instance.GetType().GetField("Recipes", BindingFlags.NonPublic | BindingFlags.Instance);
                object[] recipes     = (object[])recipesProp.GetValue(__instance);

                object oldWoodRecipe = recipes[0];

                var constructor = oldWoodRecipe.GetType().GetConstructor(new Type[] { typeof(int), typeof(int), typeof(Func <Item, StardewObject>), typeof(int) });

                Func <Item, StardewObject> output = input => new StardewObject(coalID, 1);

                var woodRecipe = constructor.Invoke(new object[] { woodID, mod.config.CharcoalKilnWoodNeeded, output, mod.config.CharcoalKilnTimeNeeded });

                object driftwoodRecipe = null;
                object hardwoodRecipe  = null;

                int count = 1;

                if (mod.config.DriftwoodMultiplier > 0)
                {
                    count++;
                    driftwoodRecipe = constructor.Invoke(new object[] { driftwoodID, CountWithMultiplier(mod.config.CharcoalKilnWoodNeeded, mod.config.DriftwoodMultiplier), output, mod.config.CharcoalKilnTimeNeeded });
                }

                if (mod.config.HardwoodMultiplier > 0)
                {
                    count++;
                    hardwoodRecipe = constructor.Invoke(new object[] { hardwoodID, CountWithMultiplier(mod.config.CharcoalKilnWoodNeeded, mod.config.HardwoodMultiplier), output, mod.config.CharcoalKilnTimeNeeded });
                }

                Array resultArray = Array.CreateInstance(woodRecipe.GetType(), count);

                resultArray.SetValue(woodRecipe, 0);

                if (mod.config.DriftwoodMultiplier > 0)
                {
                    resultArray.SetValue(driftwoodRecipe, 1);
                }

                if (mod.config.HardwoodMultiplier > 0)
                {
                    resultArray.SetValue(hardwoodRecipe, count - 1);
                }

                recipesProp.SetValue(__instance, resultArray);

                return(true);
            }
            catch (Exception e)
            {
                mod.ErrorLog("There was an exception in a patch", e);
                return(true);
            }
        }
Example #2
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 #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);
                }
            }
        }