From 39341d772e99492f239ad8aff09cca8760ff5b83 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 15 Dec 2018 13:33:22 -0500 Subject: [PATCH] prevent invalid items from crashing the game --- docs/README.md | 6 +-- docs/release-notes.md | 3 +- src/SMAPI/Framework/SCore.cs | 3 +- src/SMAPI/Patches/ObjectErrorPatch.cs | 55 +++++++++++++++++++++++++++ src/SMAPI/StardewModdingAPI.csproj | 1 + 5 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 src/SMAPI/Patches/ObjectErrorPatch.cs diff --git a/docs/README.md b/docs/README.md index b8e3b50b..e4220de2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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 diff --git a/docs/release-notes.md b/docs/release-notes.md index fa7f4109..bcbdee22 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -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. @@ -10,7 +11,7 @@ * For modders: * Added dedicated content pack API. - * **Deprecations:** + * **Deprecations:** * The `assetData.AsDictionary().Set` methods are deprecated and will be removed in SMAPI 3.0. Mods should access the `Data` property directly instead. * FOR SMAPI developers: diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index 3bc0aca4..679838ba 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -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() ); } diff --git a/src/SMAPI/Patches/ObjectErrorPatch.cs b/src/SMAPI/Patches/ObjectErrorPatch.cs new file mode 100644 index 00000000..2cbb60c5 --- /dev/null +++ b/src/SMAPI/Patches/ObjectErrorPatch.cs @@ -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 +{ + /// A Harmony patch for which intercepts crashes due to the item no longer existing. + internal class ObjectErrorPatch : IHarmonyPatch + { + /********* + ** Accessors + *********/ + /// A unique name for this patch. + public string Name => $"{nameof(ObjectErrorPatch)}"; + + + /********* + ** Public methods + *********/ + /// Apply the Harmony patch. + /// The Harmony instance. + 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 + *********/ + /// The method to call instead of . + /// The instance being patched. + /// The patched method's return value. + /// Returns whether to execute the original method. + /// This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments. + [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; + } + } +} diff --git a/src/SMAPI/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj index 2c769f41..3f1a273d 100644 --- a/src/SMAPI/StardewModdingAPI.csproj +++ b/src/SMAPI/StardewModdingAPI.csproj @@ -326,6 +326,7 @@ +