Harmony Help

Started by Jumper, September 27, 2018, 05:25:00 PM

Previous topic - Next topic

Jumper

Hi All,

after a rubbish post on here a few days ago. I have moved on with my code but get an error from rimworld.
Any help appreciated
The code ....

namespace rimtest
{
    [StaticConstructorOnStartup]
    static class HarmonyPatches
    {

        static HarmonyPatches()
        {
            HarmonyInstance harmony = HarmonyInstance.Create("rimworld.Jumper.rimtest");
             harmony.Patch(AccessTools.Method(typeof(ITab_Pawn_Character), "IsVisible"),
                new HarmonyMethod(typeof(HarmonyPatches), nameof(IsVisible_Prefix)), null);
        }
        public static bool IsVisible_Prefix(ITab_Pawn_Character __instance)
        {
            Pawn p = (Pawn)AccessTools.Method(typeof(ITab_Pawn_Character), "get_PawnToShowInfoAbout")
                .Invoke(__instance, null);
            if (p.Faction == Faction.OfPlayer)
            {
                return true;
            }

            return false;
        }

    }
}


The error ....
QuoteCould not execute post-long-event action. Exception: System.TypeInitializationException: An exception was thrown by the type initializer for rimtest.HarmonyPatches ---> System.NullReferenceException: original
  at Harmony.PatchProcessor.Patch () [0x00000] in <filename unknown>:0
  at Harmony.HarmonyInstance.Patch (System.Reflection.MethodBase original, Harmony.HarmonyMethod prefix, Harmony.HarmonyMethod postfix, Harmony.HarmonyMethod transpiler) [0x00000] in <filename unknown>:0
  at rimtest.HarmonyPatches..cctor () [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at (wrapper managed-to-native) System.Runtime.CompilerServices.RuntimeHelpers:RunClassConstructor (intptr)
  at System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (RuntimeTypeHandle type) [0x00000] in <filename unknown>:0
  at Verse.StaticConstructorOnStartupUtility.CallAll () [0x00000] in <filename unknown>:0
  at Verse.PlayDataLoader.<DoPlayLoad>m__2 () [0x00000] in <filename unknown>:0
  at Verse.LongEventHandler.ExecuteToExecuteWhenFinished () [0x00000] in <filename unknown>:0
Verse.Log:Error(String, Boolean)
Verse.LongEventHandler:ExecuteToExecuteWhenFinished()
Verse.LongEventHandler:UpdateCurrentAsynchronousEvent()
Verse.LongEventHandler:LongEventsUpdate(Boolean&)
Verse.Root:Update_Patch1(Object)
Verse.Root_Entry:Update()

Mehni

isVisible is a getter, not a method. AccessTools.Property and .GetGetMethod will help.

As an aside, Harmony v1.2 can get you private fields with three underscores. Cached too, so quicker than that mess of a reflection.

Harmony encyclopedia: https://github.com/RimWorld-CCL-Reborn/AlienRaces/blob/master/Source/AlienRace/AlienRace/HarmonyPatches.cs