summaryrefslogtreecommitdiff
path: root/src/SMAPI.Mods.ErrorHandler/Patches/SaveGamePatcher.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-01-16 18:01:19 -0500
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-01-16 18:01:19 -0500
commit1b9ff9cd101621cacc246fc2b7d936f964ae4b3f (patch)
treeff742b2a12270b57c28e8ddecdc5322c64f6c2ce /src/SMAPI.Mods.ErrorHandler/Patches/SaveGamePatcher.cs
parent8a2618d812e4e2f36161907af5af5c484eb37abf (diff)
parentad0e6b315dc7b4e616dcdf6c090cd54a2f3b7499 (diff)
downloadSMAPI-1b9ff9cd101621cacc246fc2b7d936f964ae4b3f.tar.gz
SMAPI-1b9ff9cd101621cacc246fc2b7d936f964ae4b3f.tar.bz2
SMAPI-1b9ff9cd101621cacc246fc2b7d936f964ae4b3f.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI.Mods.ErrorHandler/Patches/SaveGamePatcher.cs')
-rw-r--r--src/SMAPI.Mods.ErrorHandler/Patches/SaveGamePatcher.cs30
1 files changed, 28 insertions, 2 deletions
diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/SaveGamePatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/SaveGamePatcher.cs
index 2a43cb10..0a7ed212 100644
--- a/src/SMAPI.Mods.ErrorHandler/Patches/SaveGamePatcher.cs
+++ b/src/SMAPI.Mods.ErrorHandler/Patches/SaveGamePatcher.cs
@@ -4,11 +4,11 @@ using System.Diagnostics.CodeAnalysis;
using System.Linq;
using HarmonyLib;
using Microsoft.Xna.Framework.Content;
+using StardewModdingAPI.Internal;
using StardewModdingAPI.Internal.Patching;
using StardewValley;
using StardewValley.Buildings;
using StardewValley.Locations;
-using SObject = StardewValley.Object;
namespace StardewModdingAPI.Mods.ErrorHandler.Patches
{
@@ -47,6 +47,11 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches
original: this.RequireMethod<SaveGame>(nameof(SaveGame.loadDataToLocations)),
prefix: this.GetHarmonyMethod(nameof(SaveGamePatcher.Before_LoadDataToLocations))
);
+
+ harmony.Patch(
+ original: this.RequireMethod<SaveGame>(nameof(SaveGame.LoadFarmType)),
+ finalizer: this.GetHarmonyMethod(nameof(SaveGamePatcher.Finalize_LoadFarmType))
+ );
}
@@ -58,14 +63,35 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches
/// <returns>Returns whether to execute the original method.</returns>
private static bool Before_LoadDataToLocations(List<GameLocation> gamelocations)
{
+ // missing locations/NPCs
IDictionary<string, string> npcs = Game1.content.Load<Dictionary<string, string>>("Data\\NPCDispositions");
-
if (SaveGamePatcher.RemoveBrokenContent(gamelocations, npcs))
SaveGamePatcher.OnContentRemoved();
return true;
}
+ /// <summary>The method to call after <see cref="SaveGame.LoadFarmType"/> throws an exception.</summary>
+ /// <param name="__exception">The exception thrown by the wrapped method, if any.</param>
+ /// <returns>Returns the exception to throw, if any.</returns>
+ private static Exception Finalize_LoadFarmType(Exception __exception)
+ {
+ // missing custom farm type
+ if (__exception?.Message?.Contains("not a valid farm type") == true && !int.TryParse(SaveGame.loaded.whichFarm, out _))
+ {
+ SaveGamePatcher.Monitor.Log(__exception.GetLogSummary(), LogLevel.Error);
+ SaveGamePatcher.Monitor.Log($"Removed invalid custom farm type '{SaveGame.loaded.whichFarm}' to avoid a crash when loading save '{Constants.SaveFolderName}'. (Did you remove a custom farm type mod?)", LogLevel.Warn);
+
+ SaveGame.loaded.whichFarm = Farm.default_layout.ToString();
+ SaveGame.LoadFarmType();
+ SaveGamePatcher.OnContentRemoved();
+
+ __exception = null;
+ }
+
+ return __exception;
+ }
+
/// <summary>Remove content which no longer exists in the game data.</summary>
/// <param name="locations">The current game locations.</param>
/// <param name="npcs">The NPC data.</param>