#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 { /// Harmony patch for to validate textures earlier. /// Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments. [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 { /********* ** Public methods *********/ /// #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 /// The method to call instead of . /// The texture to validate. #else /// The method to call instead of . /// The texture to validate. #endif private static void After_SpriteBatch_CheckValid(Texture2D texture) { if (texture?.IsDisposed == true) throw new ObjectDisposedException("Cannot draw this texture because it's disposed."); } } }