diff options
author | berkay2578 <berkaytgy@gmail.com> | 2019-04-14 12:29:23 +0300 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-09-13 15:12:45 -0400 |
commit | accaa6c0e03e99411d7b10f938dd17b142b8eedc (patch) | |
tree | 5637f936a7bbf5dd79f7d9cf15b4343573f88347 /src/SMAPI/Patches | |
parent | e22a54212182d0adc443ac95bc791e83c90f7e10 (diff) | |
download | SMAPI-accaa6c0e03e99411d7b10f938dd17b142b8eedc.tar.gz SMAPI-accaa6c0e03e99411d7b10f938dd17b142b8eedc.tar.bz2 SMAPI-accaa6c0e03e99411d7b10f938dd17b142b8eedc.zip |
checkEventPrecondition crash fix
Diffstat (limited to 'src/SMAPI/Patches')
-rw-r--r-- | src/SMAPI/Patches/CheckEventPreconditionErrorPatch.cs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/SMAPI/Patches/CheckEventPreconditionErrorPatch.cs b/src/SMAPI/Patches/CheckEventPreconditionErrorPatch.cs new file mode 100644 index 00000000..63eca5d7 --- /dev/null +++ b/src/SMAPI/Patches/CheckEventPreconditionErrorPatch.cs @@ -0,0 +1,77 @@ +using System.Diagnostics.CodeAnalysis; +using System.Reflection; + +using Harmony; + +using StardewModdingAPI.Framework.Patching; +using StardewModdingAPI.Framework.Reflection; + +using StardewValley; + +namespace StardewModdingAPI.Patches { + /// <summary>A Harmony patch for the <see cref="Dialogue"/> constructor which intercepts invalid dialogue lines and logs an error instead of crashing.</summary> + internal class CheckEventPreconditionErrorPatch : IHarmonyPatch { + /********* + ** Private methods + *********/ + /// <summary>Writes messages to the console and log file on behalf of the game.</summary> + private static IMonitor MonitorForGame; + + /// <summary>Local variable to store the patched method.</summary> + private static MethodInfo method; + /// <summary>Local variable to check if the method was already arbitrated.</summary> + private static bool isArbitrated; + + + /********* + ** Accessors + *********/ + /// <summary>A unique name for this patch.</summary> + public string Name => $"{nameof(CheckEventPreconditionErrorPatch)}"; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="monitorForGame">Writes messages to the console and log file on behalf of the game.</param> + /// <param name="reflector">Simplifies access to private code.</param> + public CheckEventPreconditionErrorPatch(IMonitor monitorForGame, Reflector reflector) { + CheckEventPreconditionErrorPatch.MonitorForGame = monitorForGame; + } + + + /// <summary>Apply the Harmony patch.</summary> + /// <param name="harmony">The Harmony instance.</param> + 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)); + } + + /********* + ** Private methods + *********/ + /// <summary>The method to call instead of the GameLocation.CheckEventPrecondition.</summary> + /// <param name="__instance">The instance being patched.</param> + /// <param name="precondition">The precondition to be parsed.</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, string precondition) { + if (isArbitrated) { + isArbitrated = false; + return true; + } else { + isArbitrated = true; + try { + method.Invoke(__instance, new object[] { precondition }); + } catch (System.Exception ex) { + CheckEventPreconditionErrorPatch.MonitorForGame.Log($"Failed parsing event info. Event precondition: {precondition}\n{ex}", LogLevel.Error); + } + + return false; + } + } + } +} |