summaryrefslogtreecommitdiff
path: root/src/SMAPI/Patches/LoadErrorPatch.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-10-01 21:41:15 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-10-01 21:41:15 -0400
commit65997c1243a60ae15cc0b832ebcd41d96c3ea06a (patch)
tree4b55e07011ed3144d6a996821ddc1886f35de83f /src/SMAPI/Patches/LoadErrorPatch.cs
parent845deb43d60147603ec31fe4ae5fd7d747556d8c (diff)
downloadSMAPI-65997c1243a60ae15cc0b832ebcd41d96c3ea06a.tar.gz
SMAPI-65997c1243a60ae15cc0b832ebcd41d96c3ea06a.tar.bz2
SMAPI-65997c1243a60ae15cc0b832ebcd41d96c3ea06a.zip
auto-fix save data when a custom location mod is removed
Diffstat (limited to 'src/SMAPI/Patches/LoadErrorPatch.cs')
-rw-r--r--src/SMAPI/Patches/LoadErrorPatch.cs28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/SMAPI/Patches/LoadErrorPatch.cs b/src/SMAPI/Patches/LoadErrorPatch.cs
index 87e8ee14..eedb4164 100644
--- a/src/SMAPI/Patches/LoadErrorPatch.cs
+++ b/src/SMAPI/Patches/LoadErrorPatch.cs
@@ -1,3 +1,4 @@
+using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
@@ -20,6 +21,9 @@ namespace StardewModdingAPI.Patches
/// <summary>Writes messages to the console and log file.</summary>
private static IMonitor Monitor;
+ /// <summary>A callback invoked when custom content is removed from the save data to avoid a crash.</summary>
+ private static Action OnContentRemoved;
+
/*********
** Accessors
@@ -33,9 +37,11 @@ namespace StardewModdingAPI.Patches
*********/
/// <summary>Construct an instance.</summary>
/// <param name="monitor">Writes messages to the console and log file.</param>
- public LoadErrorPatch(IMonitor monitor)
+ /// <param name="onContentRemoved">A callback invoked when custom content is removed from the save data to avoid a crash.</param>
+ public LoadErrorPatch(IMonitor monitor, Action onContentRemoved)
{
LoadErrorPatch.Monitor = monitor;
+ LoadErrorPatch.OnContentRemoved = onContentRemoved;
}
@@ -58,6 +64,22 @@ namespace StardewModdingAPI.Patches
/// <returns>Returns whether to execute the original method.</returns>
private static bool Before_SaveGame_LoadDataToLocations(List<GameLocation> gamelocations)
{
+ bool removedAny = false;
+
+ // remove invalid locations
+ foreach (GameLocation location in gamelocations.ToArray())
+ {
+ if (location is Cellar)
+ continue; // missing cellars will be added by the game code
+
+ if (Game1.getLocationFromName(location.name) == null)
+ {
+ LoadErrorPatch.Monitor.Log($"Removed invalid location '{location.Name}' to avoid a crash when loading save '{Constants.SaveFolderName}'. (Did you remove a custom location mod?)", LogLevel.Warn);
+ gamelocations.Remove(location);
+ removedAny = true;
+ }
+ }
+
// get building interiors
var interiors =
(
@@ -83,11 +105,15 @@ namespace StardewModdingAPI.Patches
{
LoadErrorPatch.Monitor.Log($"Removed invalid villager '{npc.Name}' to avoid a crash when loading save '{Constants.SaveFolderName}'. (Did you remove a custom NPC mod?)", LogLevel.Warn);
location.characters.Remove(npc);
+ removedAny = true;
}
}
}
}
+ if (removedAny)
+ LoadErrorPatch.OnContentRemoved();
+
return true;
}
}