[Harmony] trying to postfix

Started by Red192, October 04, 2019, 10:41:48 AM

Previous topic - Next topic

Red192

So, i'm trying to use Harmony for make the mining ticks been affected by a certain tecnology, but it does not work, i tried a bit but i guess is better to try to ask here:

using RimWorld;
using System.Reflection;
using System;
using Verse;
using Harmony;

namespace NoName
{
    [HarmonyPatch(typeof(JobDriver_Mine))]
    [HarmonyPatch("ResetTicksToPickHit")]
    static class JobDriver_Mine_ResearchPatch
    {
        static void Postfix(JobDriver_Mine __istance, ref int ___ticksToPickHit)
        {
            float num = 1;
            if (ResearchProjectDef.Named("IronWorking").IsFinished)
            {
                num = 0.5f;
            }
           ___ticksToPickHit = (int)(___ticksToPickHit*num);
        }
    }
}


Is something wrong?

EDIT: A reference cannot be modified... of course

K

I'm not sure what you mean by your edit. References certainly can be modified, and references to private fields are the only way that Harmony makes such fields available for modification. Harmony might be having trouble because there's a typo, with __instance being misspelled __istance. Harmony needs exact variable names to function properly.

However, patching the JobDriver probably isn't advisable anyway since it's not unlikely that there would be conflicts with other mods. Instead, you might try to apply the research upgrade to the MiningSpeed stat, either through a Harmony patch or some other stat application method. Using stats generally ensures fairly good compatibility with other mods.

Red192

#2
Thanks K

Edit: I just tried without the typo and still does not work, but since you even advise against patching the jobDriver i might try to modify the stats, even if i don't know exactly how right now.
I might have confuded that a ref variable is a copy of the original field and for that reason the original is not edited

K

Not sure why it doesn't work then, seems like it should, though it is a little invasive.

Generally you should try to avoid Harmony as much as possible. It's great and there are many problems that can only be solved with it, but solving every problem with Harmony can lead to conflicts in the behavior of mods that modify the same methods, even though Harmony is nondestructive. There are many other options available in the game to get the behaviors that you want your mod to have, and often times they don't require modifications to the game's own methods. In this case, you might end up needing Harmony if only because the base game doesn't implement global stats on research project anymore.

Since there's already a MiningSpeed stat in the game look at cutting into the StatWorker for that stat if you end up going the Harmony route. StatWorkers have two methods, GetValueUnfinalized and GetExplanationUnfinalized, which you can postfix with relative safety to add new stat sources to the game. It can be a little bit difficult to get the right results, though.

Red192

#4
I did it! i was quite surprised that it is worked, but, ok :)

at the end i have made a new class derived from statPart. Then i made a new StatDef in which put the new part, and finaly I've made a simple xml patch for add the new StatDef into the statFactors node of miningSpeed. No harmony required :)

Maybe I rewrite it for make it a bit more universal, so it can be reused in other problems