From b582f701dce9bb0bf9b1638b8edd1ff0e7ddee18 Mon Sep 17 00:00:00 2001 From: Skipcast Date: Sun, 20 Mar 2022 20:34:40 +0100 Subject: [PATCH] Fixed game crashing when running vanilla game (without jk+) due to .net runtime optimisation causing harmony patch to sometimes not work properly on methods returning structs --- JKMP.Core/Patches/InputPatches.cs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/JKMP.Core/Patches/InputPatches.cs b/JKMP.Core/Patches/InputPatches.cs index 8daf6c6..289dc22 100644 --- a/JKMP.Core/Patches/InputPatches.cs +++ b/JKMP.Core/Patches/InputPatches.cs @@ -1,3 +1,4 @@ +using System.Reflection; using HarmonyLib; using JKMP.Core.Input; using JumpKing.Controller; @@ -7,22 +8,17 @@ namespace JKMP.Core.Patches { - [HarmonyPatch(typeof(PadInstance), nameof(PadInstance.GetState))] - internal static class PadInstanceGetStatePatch + [HarmonyPatch(typeof(PadInstance), nameof(PadInstance.Update))] + internal static class PadInstanceUpdatePatch { - private static bool Prefix(ref PadState __result) - { - __result = VanillaKeyBindRouter.GetState(); - return false; - } - } + private static readonly FieldInfo LastStateField = AccessTools.Field(typeof(PadInstance), "last_state"); + private static readonly FieldInfo CurrentStateField = AccessTools.Field(typeof(PadInstance), "current_state"); - [HarmonyPatch(typeof(PadInstance), nameof(PadInstance.GetPressed))] - internal static class PadInstanceGetPressedPatch - { - private static bool Prefix(ref PadState __result) + private static bool Prefix(PadInstance __instance) { - __result = VanillaKeyBindRouter.GetPressedState(); + LastStateField.SetValue(__instance, CurrentStateField.GetValue(__instance)); + CurrentStateField.SetValue(__instance, VanillaKeyBindRouter.GetState()); + return false; } }