From a13af946e21b12953454540ef4e3ef54dbe50f2d Mon Sep 17 00:00:00 2001 From: berkay2578 Date: Sun, 14 Apr 2019 19:29:47 +0300 Subject: Implement the return value of the original method --- src/SMAPI/Patches/CheckEventPreconditionErrorPatch.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/SMAPI') diff --git a/src/SMAPI/Patches/CheckEventPreconditionErrorPatch.cs b/src/SMAPI/Patches/CheckEventPreconditionErrorPatch.cs index 63eca5d7..54168969 100644 --- a/src/SMAPI/Patches/CheckEventPreconditionErrorPatch.cs +++ b/src/SMAPI/Patches/CheckEventPreconditionErrorPatch.cs @@ -17,8 +17,8 @@ namespace StardewModdingAPI.Patches { /// Writes messages to the console and log file on behalf of the game. private static IMonitor MonitorForGame; - /// Local variable to store the patched method. - private static MethodInfo method; + /// Local variable to store the original method. + private static MethodInfo originalMethod; /// Local variable to check if the method was already arbitrated. private static bool isArbitrated; @@ -44,9 +44,8 @@ namespace StardewModdingAPI.Patches { /// Apply the Harmony patch. /// The Harmony instance. public void Apply(HarmonyInstance harmony) { - method = AccessTools.Method(typeof(GameLocation), "checkEventPrecondition"); - MethodInfo transpiler = AccessTools.Method(this.GetType(), nameof(CheckEventPreconditionErrorPatch.Prefix)); - harmony.Patch(method, new HarmonyMethod(transpiler)); + originalMethod = AccessTools.Method(typeof(GameLocation), "checkEventPrecondition"); + harmony.Patch(originalMethod, new HarmonyMethod(AccessTools.Method(this.GetType(), "Prefix"))); } /********* @@ -54,19 +53,22 @@ namespace StardewModdingAPI.Patches { *********/ /// The method to call instead of the GameLocation.CheckEventPrecondition. /// The instance being patched. + /// The return value of the original method. /// The precondition to be parsed. /// Returns whether to execute the original method. /// This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments. [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony.")] - private static bool Prefix(GameLocation __instance, string precondition) { + private static bool Prefix(GameLocation __instance, ref int __result, string precondition) { if (isArbitrated) { isArbitrated = false; return true; } else { isArbitrated = true; try { - method.Invoke(__instance, new object[] { precondition }); + object _ = originalMethod.Invoke(__instance, new object[] { precondition }); + __result = _ is null ? -1 : (int)_; } catch (System.Exception ex) { + __result = -1; CheckEventPreconditionErrorPatch.MonitorForGame.Log($"Failed parsing event info. Event precondition: {precondition}\n{ex}", LogLevel.Error); } -- cgit