diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-04-14 21:55:11 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-09-13 15:17:43 -0400 |
commit | 9732ddb2759774e6b22c9414e3f5341865257270 (patch) | |
tree | 927ea3406cb7d525e20606eabb89e7b45b4ebde2 /src/SMAPI/Patches | |
parent | bac92d6f26af84f3492db1fc8d1724155d6245ee (diff) | |
download | SMAPI-9732ddb2759774e6b22c9414e3f5341865257270.tar.gz SMAPI-9732ddb2759774e6b22c9414e3f5341865257270.tar.bz2 SMAPI-9732ddb2759774e6b22c9414e3f5341865257270.zip |
avoid possible invalid state if checkEventPrecondition is called asynchronously (#636)
Diffstat (limited to 'src/SMAPI/Patches')
-rw-r--r-- | src/SMAPI/Patches/CheckEventPreconditionErrorPatch.cs | 45 |
1 files changed, 22 insertions, 23 deletions
diff --git a/src/SMAPI/Patches/CheckEventPreconditionErrorPatch.cs b/src/SMAPI/Patches/CheckEventPreconditionErrorPatch.cs index 673fae1c..74cfbfb0 100644 --- a/src/SMAPI/Patches/CheckEventPreconditionErrorPatch.cs +++ b/src/SMAPI/Patches/CheckEventPreconditionErrorPatch.cs @@ -15,11 +15,8 @@ namespace StardewModdingAPI.Patches /// <summary>Writes messages to the console and log file on behalf of the game.</summary> private static IMonitor MonitorForGame; - /// <summary>The method being wrapped.</summary> - private static MethodInfo OriginalMethod; - /// <summary>Whether the method is currently being intercepted.</summary> - private static bool IsArbitrated; + private static bool IsIntercepted; /********* @@ -43,8 +40,10 @@ namespace StardewModdingAPI.Patches /// <param name="harmony">The Harmony instance.</param> public void Apply(HarmonyInstance harmony) { - CheckEventPreconditionErrorPatch.OriginalMethod = AccessTools.Method(typeof(GameLocation), "checkEventPrecondition"); - harmony.Patch(CheckEventPreconditionErrorPatch.OriginalMethod, new HarmonyMethod(AccessTools.Method(this.GetType(), nameof(CheckEventPreconditionErrorPatch.Prefix)))); + harmony.Patch( + original: AccessTools.Method(typeof(GameLocation), "checkEventPrecondition"), + prefix: new HarmonyMethod(AccessTools.Method(this.GetType(), nameof(CheckEventPreconditionErrorPatch.Prefix))) + ); } @@ -55,30 +54,30 @@ namespace StardewModdingAPI.Patches /// <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="__originalMethod">The method being wrapped.</param> /// <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> [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony.")] - private static bool Prefix(GameLocation __instance, ref int __result, string precondition) + private static bool Prefix(GameLocation __instance, ref int __result, string precondition, MethodInfo __originalMethod) { - if (CheckEventPreconditionErrorPatch.IsArbitrated) - { - CheckEventPreconditionErrorPatch.IsArbitrated = false; + if (CheckEventPreconditionErrorPatch.IsIntercepted) return true; + + try + { + CheckEventPreconditionErrorPatch.IsIntercepted = true; + __result = (int)__originalMethod.Invoke(__instance, new object[] { precondition }); + return false; + } + catch (TargetInvocationException ex) + { + __result = -1; + CheckEventPreconditionErrorPatch.MonitorForGame.Log($"Failed parsing event precondition ({precondition}):\n{ex.InnerException}", LogLevel.Error); + return false; } - else + finally { - CheckEventPreconditionErrorPatch.IsArbitrated = true; - try - { - __result = (int)CheckEventPreconditionErrorPatch.OriginalMethod.Invoke(__instance, new object[] { precondition }); - return false; - } - catch (TargetInvocationException ex) - { - __result = -1; - CheckEventPreconditionErrorPatch.MonitorForGame.Log($"Failed parsing event precondition ({precondition}):\n{ex.InnerException}", LogLevel.Error); - return false; - } + CheckEventPreconditionErrorPatch.IsIntercepted = false; } } } |