diff options
Diffstat (limited to 'src/SMAPI.Mods.ErrorHandler')
-rw-r--r-- | src/SMAPI.Mods.ErrorHandler/ModEntry.cs | 16 | ||||
-rw-r--r-- | src/SMAPI.Mods.ErrorHandler/Patches/LoadErrorPatch.cs | 3 |
2 files changed, 13 insertions, 6 deletions
diff --git a/src/SMAPI.Mods.ErrorHandler/ModEntry.cs b/src/SMAPI.Mods.ErrorHandler/ModEntry.cs index 719eddab..b1081218 100644 --- a/src/SMAPI.Mods.ErrorHandler/ModEntry.cs +++ b/src/SMAPI.Mods.ErrorHandler/ModEntry.cs @@ -1,3 +1,4 @@ +using System; using System.Reflection; using StardewModdingAPI.Events; using StardewModdingAPI.Framework; @@ -71,12 +72,17 @@ namespace StardewModdingAPI.Mods.ErrorHandler /// <summary>Get the monitor with which to log game errors.</summary> private IMonitor GetMonitorForGame() { - SCore core = SCore.Instance; - LogManager logManager = core.GetType().GetField("LogManager", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(core) as LogManager; - if (logManager == null) - this.Monitor.Log("Can't access SMAPI's internal log manager. Some game errors may be reported as being from Error Handler.", LogLevel.Error); + // get SMAPI core + Type coreType = Type.GetType("StardewModdingAPI.Framework.SCore, StardewModdingAPI", throwOnError: false) + ?? throw new InvalidOperationException("Can't access SMAPI's core type. This mod may not work correctly."); + object core = coreType.GetProperty("Instance", BindingFlags.Static | BindingFlags.NonPublic)?.GetValue(null) + ?? throw new InvalidOperationException("Can't access SMAPI's core instance. This mod may not work correctly."); - return logManager?.MonitorForGame ?? this.Monitor; + // get monitor + MethodInfo getMonitorForGame = coreType.GetMethod("GetMonitorForGame") + ?? throw new InvalidOperationException("Can't access the SMAPI's 'GetMonitorForGame' method. This mod may not work correctly."); + + return (IMonitor)getMonitorForGame.Invoke(core, new object[0]) ?? this.Monitor; } } } diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/LoadErrorPatch.cs b/src/SMAPI.Mods.ErrorHandler/Patches/LoadErrorPatch.cs index e14dc662..cc0e5e52 100644 --- a/src/SMAPI.Mods.ErrorHandler/Patches/LoadErrorPatch.cs +++ b/src/SMAPI.Mods.ErrorHandler/Patches/LoadErrorPatch.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using HarmonyLib; +using Microsoft.Xna.Framework.Content; using StardewModdingAPI.Framework.Exceptions; using StardewModdingAPI.Framework.Patching; using StardewValley; @@ -82,7 +83,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches { BluePrint _ = new BluePrint(building.buildingType.Value); } - catch (SContentLoadException) + catch (ContentLoadException) { LoadErrorPatch.Monitor.Log($"Removed invalid building type '{building.buildingType.Value}' in {location.Name} ({building.tileX}, {building.tileY}) to avoid a crash when loading save '{Constants.SaveFolderName}'. (Did you remove a custom building mod?)", LogLevel.Warn); location.buildings.Remove(building); |