prevent invalid items from crashing the game

This commit is contained in:
Jesse Plamondon-Willard 2018-12-15 13:33:22 -05:00
parent 5f620e14fa
commit 39341d772e
No known key found for this signature in database
GPG Key ID: 7D7C8097B62033CE
5 changed files with 63 additions and 5 deletions

View File

@ -1,6 +1,6 @@
**SMAPI** is an open-source modding API for [Stardew Valley](https://stardewvalley.net/) that lets
you play the game with mods. It's safely installed alongside the game's executable, and doesn't
change any of your game files. It serves eight main purposes:
**SMAPI** is an open-source modding framework and API for [Stardew Valley](https://stardewvalley.net/)
that lets you play the game with mods. It's safely installed alongside the game's executable, and
doesn't change any of your game files. It serves eight main purposes:
1. **Load mods into the game.**
_SMAPI loads mods when the game is starting up so they can interact with it. (Code mods aren't

View File

@ -1,6 +1,7 @@
# Release notes
## Upcoming release
* For players:
* SMAPI now prevents invalid items from crashing the game on hover.
* Fixed cryptic error message when the game isn't installed correctly.
* Fixed error when a mod makes invalid changes to an NPC schedule.
* Fixed invalid NPC data propagated when a mod changes NPC dispositions.

View File

@ -184,7 +184,8 @@ namespace StardewModdingAPI.Framework
// apply game patches
new GamePatcher(this.Monitor).Apply(
new DialogueErrorPatch(this.MonitorForGame, this.Reflection)
new DialogueErrorPatch(this.MonitorForGame, this.Reflection),
new ObjectErrorPatch()
);
}

View File

@ -0,0 +1,55 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Harmony;
using StardewModdingAPI.Framework.Patching;
using StardewValley;
using SObject = StardewValley.Object;
namespace StardewModdingAPI.Patches
{
/// <summary>A Harmony patch for <see cref="SObject.getDescription"/> which intercepts crashes due to the item no longer existing.</summary>
internal class ObjectErrorPatch : IHarmonyPatch
{
/*********
** Accessors
*********/
/// <summary>A unique name for this patch.</summary>
public string Name => $"{nameof(ObjectErrorPatch)}";
/*********
** Public methods
*********/
/// <summary>Apply the Harmony patch.</summary>
/// <param name="harmony">The Harmony instance.</param>
public void Apply(HarmonyInstance harmony)
{
MethodInfo method = AccessTools.Method(typeof(SObject), nameof(SObject.getDescription));
MethodInfo prefix = AccessTools.Method(this.GetType(), nameof(ObjectErrorPatch.Prefix));
harmony.Patch(method, new HarmonyMethod(prefix), null);
}
/*********
** Private methods
*********/
/// <summary>The method to call instead of <see cref="StardewValley.Object.getDescription"/>.</summary>
/// <param name="__instance">The instance being patched.</param>
/// <param name="__result">The patched method's return value.</param>
/// <returns>Returns whether to execute the original method.</returns>
/// <remarks>This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments.</remarks>
[SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony.")]
private static bool Prefix(SObject __instance, ref string __result)
{
// invalid bigcraftables crash instead of showing '???' like invalid non-bigcraftables
if (!__instance.IsRecipe && __instance.bigCraftable.Value && !Game1.bigCraftablesInformation.ContainsKey(__instance.ParentSheetIndex))
{
__result = "???";
return false;
}
return false;
}
}
}

View File

@ -326,6 +326,7 @@
<Compile Include="Framework\Monitor.cs" />
<Compile Include="Metadata\InstructionMetadata.cs" />
<Compile Include="Mod.cs" />
<Compile Include="Patches\ObjectErrorPatch.cs" />
<Compile Include="Patches\DialogueErrorPatch.cs" />
<Compile Include="PatchMode.cs" />
<Compile Include="GamePlatform.cs" />