Implement the return value of the original method

This commit is contained in:
berkay2578 2019-04-14 19:29:47 +03:00 committed by Jesse Plamondon-Willard
parent accaa6c0e0
commit a13af946e2
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
1 changed files with 9 additions and 7 deletions

View File

@ -17,8 +17,8 @@ namespace StardewModdingAPI.Patches {
/// <summary>Writes messages to the console and log file on behalf of the game.</summary> /// <summary>Writes messages to the console and log file on behalf of the game.</summary>
private static IMonitor MonitorForGame; private static IMonitor MonitorForGame;
/// <summary>Local variable to store the patched method.</summary> /// <summary>Local variable to store the original method.</summary>
private static MethodInfo method; private static MethodInfo originalMethod;
/// <summary>Local variable to check if the method was already arbitrated.</summary> /// <summary>Local variable to check if the method was already arbitrated.</summary>
private static bool isArbitrated; private static bool isArbitrated;
@ -44,9 +44,8 @@ namespace StardewModdingAPI.Patches {
/// <summary>Apply the Harmony patch.</summary> /// <summary>Apply the Harmony patch.</summary>
/// <param name="harmony">The Harmony instance.</param> /// <param name="harmony">The Harmony instance.</param>
public void Apply(HarmonyInstance harmony) { public void Apply(HarmonyInstance harmony) {
method = AccessTools.Method(typeof(GameLocation), "checkEventPrecondition"); originalMethod = AccessTools.Method(typeof(GameLocation), "checkEventPrecondition");
MethodInfo transpiler = AccessTools.Method(this.GetType(), nameof(CheckEventPreconditionErrorPatch.Prefix)); harmony.Patch(originalMethod, new HarmonyMethod(AccessTools.Method(this.GetType(), "Prefix")));
harmony.Patch(method, new HarmonyMethod(transpiler));
} }
/********* /*********
@ -54,19 +53,22 @@ namespace StardewModdingAPI.Patches {
*********/ *********/
/// <summary>The method to call instead of the GameLocation.CheckEventPrecondition.</summary> /// <summary>The method to call instead of the GameLocation.CheckEventPrecondition.</summary>
/// <param name="__instance">The instance being patched.</param> /// <param name="__instance">The instance being patched.</param>
/// <param name="__result">The return value of the original method.</param>
/// <param name="precondition">The precondition to be parsed.</param> /// <param name="precondition">The precondition to be parsed.</param>
/// <returns>Returns whether to execute the original method.</returns> /// <returns>Returns whether to execute the original method.</returns>
/// <remarks>This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments.</remarks> /// <remarks>This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments.</remarks>
[SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony.")] [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) { if (isArbitrated) {
isArbitrated = false; isArbitrated = false;
return true; return true;
} else { } else {
isArbitrated = true; isArbitrated = true;
try { try {
method.Invoke(__instance, new object[] { precondition }); object _ = originalMethod.Invoke(__instance, new object[] { precondition });
__result = _ is null ? -1 : (int)_;
} catch (System.Exception ex) { } catch (System.Exception ex) {
__result = -1;
CheckEventPreconditionErrorPatch.MonitorForGame.Log($"Failed parsing event info. Event precondition: {precondition}\n{ex}", LogLevel.Error); CheckEventPreconditionErrorPatch.MonitorForGame.Log($"Failed parsing event info. Event precondition: {precondition}\n{ex}", LogLevel.Error);
} }