diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2018-12-15 13:33:22 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2018-12-15 13:33:22 -0500 |
commit | 39341d772e99492f239ad8aff09cca8760ff5b83 (patch) | |
tree | 411edbf7104abf0c6386ae412fee935c12d952b1 /src/SMAPI/Patches | |
parent | 5f620e14fa331b0721bd4044011363477dc79ef5 (diff) | |
download | SMAPI-39341d772e99492f239ad8aff09cca8760ff5b83.tar.gz SMAPI-39341d772e99492f239ad8aff09cca8760ff5b83.tar.bz2 SMAPI-39341d772e99492f239ad8aff09cca8760ff5b83.zip |
prevent invalid items from crashing the game
Diffstat (limited to 'src/SMAPI/Patches')
-rw-r--r-- | src/SMAPI/Patches/ObjectErrorPatch.cs | 55 |
1 files changed, 55 insertions, 0 deletions
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; + } + } +} |