DefendBase Harmony Patch Help Please

Started by AlexandrosPrice, February 02, 2020, 07:29:08 AM

Previous topic - Next topic

AlexandrosPrice

I posted this to the Harmony Tools forum here:
Quote from: AlexandrosPrice on February 06, 2020, 02:24:56 PM
Alright, so I'm having some trouble with a Harmony mod.

using RimWorld;
using Harmony;
using Verse.AI.Group;
using System.Reflection;
using System.Linq;

namespace RimGetReady
{
    public class RimGetReadyMain
    {
        public static void DoRimGetReadyMain()
        {

            HarmonyInstance.Create("com.Steam.RimWorld.Mod.RimGetReady").PatchAll(Assembly.GetExecutingAssembly());

        }
   
    }
    [HarmonyPatch(typeof(LordJob_DefendBase))]
    [HarmonyPatch("CreateGraph")]
    public class LordJob_DefendBase_Patch
    {
        public static void Postfix(ref StateGraph __result)
        {
            Transition trans = __result.transitions.Last();
            trans.triggers.Clear();
            trans.AddTrigger(new Trigger_TicksPassed(GenDate.TicksPerDay * 5));
        }
    }
}

I'm very new to C# and modding. I've been getting help from the forum and the Rimworld Discord. But I've run into a problem. When I test this in game it doesn't allow me to select world tiles unless they are occupied and zoomed out, it creates duplicate side-tabs(ex: Low Food), and it doesn't do what the code says to do. Anyone know what I'm doing wrong? I was testing in a clean environment, so there shouldn't be any conflicts.
So yeah, even when the code is stripped to its bare bones, it still doesn't work. I even realized that the [StaticConstructorOnStartup] was missing, and put it in the code. But no dice. LWM, do you know what might be happening?

K

Are you sure your patch is even being called? You can throw a Log.Message into the patch so that it puts something in the debug log when called. Ensure that your patch is being called first, because it's starting to sound like it isn't.

LWM

This is the sort of thing I do for debugging Harmony that doesn't work:
Also, you have to have an actual C# static constructor?  Maybe that's it.


using RimWorld;
using Harmony;
using Verse.AI.Group;
using System.Reflection;
using System.Linq;

namespace RimGetReady
{
    [StaticConstructorOnStartup]
    public class RimGetReadyMain
    {
        static RimGetReadyMain()
        {
            HarmonyInstance.Create("com.Steam.RimWorld.Mod.RimGetReady").PatchAll(Assembly.GetExecutingAssembly());
Log.Message("RimGetReady: patching"); // highly recommend setting up a Debug.Message setup
// See https://github.com/n-fisher/cookiecutter-rimworld-mod-development/blob/master/%7B%7Bcookiecutter.mod_name%7D%7D/Source/%7B%7Bcookiecutter.mod_name.replace('%20'%2C%20'_')%7D%7D.cs
        }
   
    }
    [HarmonyPatch(typeof(LordJob_DefendBase), "CreateGraph")]
    public class LordJob_DefendBase_Patch
    {
        public static void Postfix(ref StateGraph __result)
        {
            Log.Message("Starting Postfix for GreateGraph: removing "+__result.transitions.Last().triggers.Count+" triggers");
            Transition trans = __result.transitions.Last();
            trans.triggers.Clear();
            trans.AddTrigger(new Trigger_TicksPassed(GenDate.TicksPerDay * 5));
        }
    }
}

AlexandrosPrice

#18
Awesome! That worked! It's making them wait five days before counter-attacking! Thank you LWM and K! The only problem now is the fact that there was one single enemy pawn that aggro'd before all the others (you sure this shouldn't be a prefix?), the map tile un-select-ability (someone on discord said that the mod could be 'suppressing inputs'?), and the duplicate 'tabs'/alerts (how is this messing with the alerts?). p.s.-I'll work on the debug when I get back home.

Can I use this?:
https://ludeon.com/forums/index.php?topic=4664.0