using System; using System.Diagnostics.CodeAnalysis; #if HARMONY_2 using HarmonyLib; #else using Harmony; #endif using StardewModdingAPI.Framework.Patching; using StardewValley; namespace StardewModdingAPI.Mods.ErrorHandler.Patches { /// Harmony patches for which intercept errors to log more details. /// Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments. [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] internal class EventPatches : IHarmonyPatch { /********* ** Fields *********/ /// Writes messages to the console and log file on behalf of the game. private static IMonitor MonitorForGame; /********* ** Accessors *********/ /// public string Name => nameof(EventPatches); /********* ** Public methods *********/ /// Construct an instance. /// Writes messages to the console and log file on behalf of the game. public EventPatches(IMonitor monitorForGame) { EventPatches.MonitorForGame = monitorForGame; } /// #if HARMONY_2 public void Apply(Harmony harmony) #else public void Apply(HarmonyInstance harmony) #endif { harmony.Patch( original: AccessTools.Method(typeof(Event), nameof(Event.LogErrorAndHalt)), postfix: new HarmonyMethod(this.GetType(), nameof(EventPatches.After_Event_LogErrorAndHalt)) ); } /********* ** Private methods *********/ /// The method to call after . /// The exception being logged. private static void After_Event_LogErrorAndHalt(Exception e) { EventPatches.MonitorForGame.Log(e.ToString(), LogLevel.Error); } } }