summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2018-12-15 13:33:22 -0500
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2018-12-15 13:33:22 -0500
commit39341d772e99492f239ad8aff09cca8760ff5b83 (patch)
tree411edbf7104abf0c6386ae412fee935c12d952b1
parent5f620e14fa331b0721bd4044011363477dc79ef5 (diff)
downloadSMAPI-39341d772e99492f239ad8aff09cca8760ff5b83.tar.gz
SMAPI-39341d772e99492f239ad8aff09cca8760ff5b83.tar.bz2
SMAPI-39341d772e99492f239ad8aff09cca8760ff5b83.zip
prevent invalid items from crashing the game
-rw-r--r--docs/README.md6
-rw-r--r--docs/release-notes.md3
-rw-r--r--src/SMAPI/Framework/SCore.cs3
-rw-r--r--src/SMAPI/Patches/ObjectErrorPatch.cs55
-rw-r--r--src/SMAPI/StardewModdingAPI.csproj1
5 files changed, 63 insertions, 5 deletions
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<TKey, TValue>().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
+{
+ /// <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;
+ }
+ }
+}
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 @@
<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" />