using System; using System.Diagnostics.CodeAnalysis; using HarmonyLib; using Microsoft.Xna.Framework.Graphics; using StardewModdingAPI.Internal.Patching; namespace StardewModdingAPI.Mods.ErrorHandler.Patches { /// Harmony patches for which 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 SpriteBatchPatcher : BasePatcher { /********* ** Public methods *********/ /// public override void Apply(Harmony harmony, IMonitor monitor) { harmony.Patch( original: Constants.GameFramework == GameFramework.Xna ? this.RequireMethod("InternalDraw") : this.RequireMethod("CheckValid", new[] { typeof(Texture2D) }), postfix: this.GetHarmonyMethod(nameof(SpriteBatchPatcher.After_CheckValid)) ); } /********* ** Private methods *********/ #if SMAPI_FOR_XNA /// The method to call after . /// The texture to validate. #else /// The method to call after . /// The texture to validate. #endif private static void After_CheckValid(Texture2D texture) { if (texture?.IsDisposed == true) throw new ObjectDisposedException("Cannot draw this texture because it's disposed."); } } }