summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2021-02-06 21:12:01 -0500
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2021-02-06 21:12:01 -0500
commit67c52af72d6d0c4fddd3a07b159cb44b5c277599 (patch)
tree99d59b42c2e46dfe42f21d744386b3eb5036ca8a
parent97d3501e20c9b97dc85cfca32ccea4f55c9d61ff (diff)
downloadSMAPI-67c52af72d6d0c4fddd3a07b159cb44b5c277599.tar.gz
SMAPI-67c52af72d6d0c4fddd3a07b159cb44b5c277599.tar.bz2
SMAPI-67c52af72d6d0c4fddd3a07b159cb44b5c277599.zip
add early detection of disposed assets in error handler mod
-rw-r--r--docs/release-notes.md3
-rw-r--r--src/SMAPI.Mods.ErrorHandler/ModEntry.cs1
-rw-r--r--src/SMAPI.Mods.ErrorHandler/Patches/SpriteBatchValidationPatches.cs63
3 files changed, 67 insertions, 0 deletions
diff --git a/docs/release-notes.md b/docs/release-notes.md
index cb54ee98..9d95dacb 100644
--- a/docs/release-notes.md
+++ b/docs/release-notes.md
@@ -16,6 +16,9 @@
* Fixed SMAPI toolkit defaulting the mod type incorrectly if a mod's `manifest.json` has neither `EntryDll` nor `ContentPackFor`. This only affects external tools, since SMAPI itself validates those fields separately.
* Fixed edge case when playing in non-English where translatable assets loaded via `IAssetLoader` would no longer be applied after returning to the title screen unless manually invalidated from the cache.
+* For the ErrorHandler mod:
+ * Added early detection of disposed textures so the crash stack trace shows the actual code which used them.
+
* For the web UI:
* Updated the JSON validator/schema for Content Patcher 1.20.
diff --git a/src/SMAPI.Mods.ErrorHandler/ModEntry.cs b/src/SMAPI.Mods.ErrorHandler/ModEntry.cs
index e4bcc5bc..87f531fe 100644
--- a/src/SMAPI.Mods.ErrorHandler/ModEntry.cs
+++ b/src/SMAPI.Mods.ErrorHandler/ModEntry.cs
@@ -35,6 +35,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler
new ObjectErrorPatch(),
new LoadErrorPatch(this.Monitor, this.OnSaveContentRemoved),
new ScheduleErrorPatch(monitorForGame),
+ new SpriteBatchValidationPatches(),
new UtilityErrorPatches()
);
diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/SpriteBatchValidationPatches.cs b/src/SMAPI.Mods.ErrorHandler/Patches/SpriteBatchValidationPatches.cs
new file mode 100644
index 00000000..0211cfb1
--- /dev/null
+++ b/src/SMAPI.Mods.ErrorHandler/Patches/SpriteBatchValidationPatches.cs
@@ -0,0 +1,63 @@
+#if HARMONY_2
+using HarmonyLib;
+#else
+using Harmony;
+#endif
+using System;
+using System.Diagnostics.CodeAnalysis;
+using Microsoft.Xna.Framework.Graphics;
+using StardewModdingAPI.Framework.Patching;
+
+namespace StardewModdingAPI.Mods.ErrorHandler.Patches
+{
+ /// <summary>Harmony patch for <see cref="SpriteBatch"/> to validate textures earlier.</summary>
+ /// <remarks>Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments.</remarks>
+ [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")]
+ [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")]
+ internal class SpriteBatchValidationPatches : IHarmonyPatch
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <inheritdoc />
+ public string Name => nameof(SpriteBatchValidationPatches);
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <inheritdoc />
+#if HARMONY_2
+ public void Apply(Harmony harmony)
+#else
+ public void Apply(HarmonyInstance harmony)
+#endif
+ {
+ harmony.Patch(
+#if SMAPI_FOR_WINDOWS
+ original: AccessTools.Method(typeof(SpriteBatch), "InternalDraw"),
+#else
+ original: AccessTools.Method(typeof(SpriteBatch), "CheckValid", new[] { typeof(Texture2D) }),
+#endif
+ postfix: new HarmonyMethod(this.GetType(), nameof(SpriteBatchValidationPatches.After_SpriteBatch_CheckValid))
+ );
+ }
+
+
+ /*********
+ ** Private methods
+ *********/
+#if SMAPI_FOR_WINDOWS
+ /// <summary>The method to call instead of <see cref="SpriteBatch.InternalDraw"/>.</summary>
+ /// <param name="texture">The texture to validate.</param>
+#else
+ /// <summary>The method to call instead of <see cref="SpriteBatch.CheckValid"/>.</summary>
+ /// <param name="texture">The texture to validate.</param>
+#endif
+ private static void After_SpriteBatch_CheckValid(Texture2D texture)
+ {
+ if (texture?.IsDisposed == true)
+ throw new ObjectDisposedException("Cannot draw this texture because it's disposed.");
+ }
+ }
+}