fix error when a mod makes invalid changes to an NPC schedule

This commit is contained in:
Jesse Plamondon-Willard 2018-12-09 18:52:21 -05:00
parent 13ed6decf5
commit 441ded8c9a
No known key found for this signature in database
GPG Key ID: 7D7C8097B62033CE
3 changed files with 16 additions and 2 deletions

View File

@ -1,4 +1,8 @@
# Release notes # Release notes
## Upcoming release
* For players:
* Fixed error when a mod makes invalid changes to an NPC schedule.
## 2.9.1 ## 2.9.1
* For players: * For players:
* Fixed crash in SMAPI 2.9 when constructing certain buildings. * Fixed crash in SMAPI 2.9 when constructing certain buildings.

View File

@ -81,7 +81,7 @@ namespace StardewModdingAPI.Framework
this.ContentManagers.Add( this.ContentManagers.Add(
this.MainContentManager = new GameContentManager("Game1.content", serviceProvider, rootDirectory, currentCulture, this, monitor, reflection, this.OnDisposing) this.MainContentManager = new GameContentManager("Game1.content", serviceProvider, rootDirectory, currentCulture, this, monitor, reflection, this.OnDisposing)
); );
this.CoreAssets = new CoreAssetPropagator(this.MainContentManager.AssertAndNormaliseAssetName, reflection); this.CoreAssets = new CoreAssetPropagator(this.MainContentManager.AssertAndNormaliseAssetName, reflection, monitor);
} }
/// <summary>Get a new content manager which handles reading files from the game content folder with support for interception.</summary> /// <summary>Get a new content manager which handles reading files from the game content folder with support for interception.</summary>

View File

@ -31,6 +31,9 @@ namespace StardewModdingAPI.Metadata
/// <summary>Simplifies access to private game code.</summary> /// <summary>Simplifies access to private game code.</summary>
private readonly Reflector Reflection; private readonly Reflector Reflection;
/// <summary>Encapsulates monitoring and logging.</summary>
private readonly IMonitor Monitor;
/********* /*********
** Public methods ** Public methods
@ -38,10 +41,12 @@ namespace StardewModdingAPI.Metadata
/// <summary>Initialise the core asset data.</summary> /// <summary>Initialise the core asset data.</summary>
/// <param name="getNormalisedPath">Normalises an asset key to match the cache key.</param> /// <param name="getNormalisedPath">Normalises an asset key to match the cache key.</param>
/// <param name="reflection">Simplifies access to private code.</param> /// <param name="reflection">Simplifies access to private code.</param>
public CoreAssetPropagator(Func<string, string> getNormalisedPath, Reflector reflection) /// <param name="monitor">Encapsulates monitoring and logging.</param>
public CoreAssetPropagator(Func<string, string> getNormalisedPath, Reflector reflection, IMonitor monitor)
{ {
this.GetNormalisedPath = getNormalisedPath; this.GetNormalisedPath = getNormalisedPath;
this.Reflection = reflection; this.Reflection = reflection;
this.Monitor = monitor;
} }
/// <summary>Reload one of the game's core assets (if applicable).</summary> /// <summary>Reload one of the game's core assets (if applicable).</summary>
@ -619,6 +624,11 @@ namespace StardewModdingAPI.Metadata
{ {
// reload schedule // reload schedule
villager.Schedule = villager.getSchedule(Game1.dayOfMonth); villager.Schedule = villager.getSchedule(Game1.dayOfMonth);
if (villager.Schedule == null)
{
this.Monitor.Log($"A mod set an invalid schedule for {villager.Name ?? key}, so the NPC may not behave correctly.", LogLevel.Warn);
return true;
}
// switch to new schedule if needed // switch to new schedule if needed
int lastScheduleTime = villager.Schedule.Keys.Where(p => p <= Game1.timeOfDay).OrderByDescending(p => p).FirstOrDefault(); int lastScheduleTime = villager.Schedule.Keys.Where(p => p <= Game1.timeOfDay).OrderByDescending(p => p).FirstOrDefault();