From 09583cd69647f087b49a24ae55a820b8154aa6b0 Mon Sep 17 00:00:00 2001 From: Kolton Noreen <21249976+noreenko@users.noreply.github.com> Date: Sun, 23 Jun 2024 20:17:25 -0500 Subject: [PATCH 1/2] C# changes for IsUndead --- .../RimWorldOfMagic/HarmonyPatches.cs | 43 +++++++++++++++++-- .../RimWorldOfMagic/RimWorldOfMagic.csproj | 3 +- RimWorldOfMagic/RimWorldOfMagic/TM_Calc.cs | 39 +++++++---------- 3 files changed, 56 insertions(+), 29 deletions(-) diff --git a/RimWorldOfMagic/RimWorldOfMagic/HarmonyPatches.cs b/RimWorldOfMagic/RimWorldOfMagic/HarmonyPatches.cs index d238e8b5..7eb79945 100644 --- a/RimWorldOfMagic/RimWorldOfMagic/HarmonyPatches.cs +++ b/RimWorldOfMagic/RimWorldOfMagic/HarmonyPatches.cs @@ -80,7 +80,6 @@ public TorannMagicMod(ModContentPack content) : base(content) harmonyInstance.Patch(AccessTools.Method(typeof(RecipeDef), "get_AvailableNow", null, null), null, new HarmonyMethod(typeof(TorannMagicMod), "Get_GolemsRecipeAvailable", null), null); harmonyInstance.Patch(AccessTools.Method(typeof(Pawn), "get_ShouldAvoidFences", null, null), new HarmonyMethod(typeof(TorannMagicMod), "Get_GolemShouldAvoidFences"), null, null); harmonyInstance.Patch(AccessTools.Method(typeof(PawnRenderer), "get_CurRotDrawMode", null, null), null, new HarmonyMethod(typeof(TorannMagicMod), "Get_RotBodyForUndead"), null, null); - harmonyInstance.Patch(AccessTools.Method(typeof(Pawn_HealthTracker), "get_CanBleed", null, null), null, new HarmonyMethod(typeof(TorannMagicMod), "Get_Undead_CanBleed"), null, null); //harmonyInstance.Patch(AccessTools.Method(typeof(Precept_Relic), "get_RelicInPlayerPossession", null, null),null, new HarmonyMethod(typeof(TorannMagicMod), "Get_DelayRelicLost"), null); //harmonyInstance.Patch(AccessTools.Method(typeof(Pawn), "get_InAggroMentalState", null, null), new HarmonyMethod(typeof(TorannMagicMod), "Get_UndeadAggroMentalState"), null, null); //harmonyInstance.Patch(AccessTools.Method(typeof(Pawn), "get_InMentalState", null, null), new HarmonyMethod(typeof(TorannMagicMod), "Get_UndeadMentalState"), null, null); @@ -410,11 +409,47 @@ private static void Postfix(Pawn bloodfeeder, Pawn prisoner, ref AcceptanceRepor } } - public static void Get_Undead_CanBleed(Pawn_HealthTracker __instance, Pawn ___pawn, ref bool __result) + [HarmonyPatch(typeof(Pawn_HealthTracker), "get_CanBleed", null)] + public static class Get_Undead_CanBleed { - if (TM_Calc.IsUndead(___pawn)) + // Transpiler to avoid harmony overhead since Anomaly update added lots of calls to CanBleed + static IEnumerable Transpiler( + IEnumerable instructions, + ILGenerator generator) { - __result = false; + CodeInstruction[] codes = instructions.ToArray(); + Label startOfOurCheck = generator.DefineLabel(); // Label for the if statement we are adding + Label returnFalse = generator.DefineLabel(); + + // We are keeping the method the same, but adding a final if condition + for (int i = 0; i < codes.Length - 1; i++) + { + yield return codes[i]; + } + + // Add a jump to our code + // If IsFlesh, continue to our check. Else, return false + yield return new CodeInstruction(OpCodes.Brtrue_S, startOfOurCheck); // If true, jump + yield return new CodeInstruction(OpCodes.Ret); // return loaded value (0) aka, false + + // Here is our if condition of TM_Calc.IsUndead + var startOfIfStatement = new CodeInstruction(OpCodes.Ldarg_0) + { + labels = new List