From 548d42e82b319b45c3ed1b67b44fcde4eaf4f712 Mon Sep 17 00:00:00 2001 From: Dreitone Date: Fri, 5 Dec 2025 23:50:34 -0600 Subject: [PATCH] deprecate tryend and trybegin, add comments identifying potentially notable safeaction spots --- ILEditing/MechanicILChanges.cs | 4 ++-- Systems/TileMerging/TileBlendTexture_Baking.cs | 4 ++-- Utilities/DrawingUtils.SpriteBatch.cs | 17 +++++++++-------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/ILEditing/MechanicILChanges.cs b/ILEditing/MechanicILChanges.cs index f2cf0e0530..aa2b8ef64d 100644 --- a/ILEditing/MechanicILChanges.cs +++ b/ILEditing/MechanicILChanges.cs @@ -890,7 +890,7 @@ namespace CalamityMod.ILEditing private static void GeneralDrawLayer_DrawToLayer_BeforeAllTiles(On_Main.orig_DrawBackgroundBlackFill orig, Main self) { GeneralDrawLayer_DrawForSupportedSystems(GeneralDrawLayer.BeforeAllTiles); - // TODO: this is a bandaid solution, but almost assuredly the method call here does not always correct state + // TODO: [SAFEACTION] this is a bandaid solution, but almost assuredly the method call here does not always correct state Main.spriteBatch.TryBegin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix); orig(self); } @@ -931,7 +931,7 @@ namespace CalamityMod.ILEditing { orig(self); GeneralDrawLayer_DrawForSupportedSystems(GeneralDrawLayer.AfterEverything); - // TODO: this is a bandaid solution, but almost assuredly the method call here does not always correct state + // TODO: [SAFEACTION] this is a bandaid solution, but almost assuredly the method call here does not always correct state Main.spriteBatch.TryBegin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix); } #endregion diff --git a/Systems/TileMerging/TileBlendTexture_Baking.cs b/Systems/TileMerging/TileBlendTexture_Baking.cs index 241887b900..9bb30a1293 100644 --- a/Systems/TileMerging/TileBlendTexture_Baking.cs +++ b/Systems/TileMerging/TileBlendTexture_Baking.cs @@ -16,7 +16,7 @@ namespace CalamityMod.Systems Main.QueueMainThreadAction(() => { BakeBlendTexture_Inner(texture); - // TODO: Bandaid fix + // TODO: [SAFEACTION] Bandaid fix Main.spriteBatch.TryEnd(); }); } @@ -60,7 +60,7 @@ namespace CalamityMod.Systems } } }); - // TODO: bandaid fix + // TODO: [SAFEACTION] bandaid fix Main.spriteBatch.TryEnd(); graphicsDevice.SetRenderTarget(null); } diff --git a/Utilities/DrawingUtils.SpriteBatch.cs b/Utilities/DrawingUtils.SpriteBatch.cs index a47ef33ffd..e990197f9f 100644 --- a/Utilities/DrawingUtils.SpriteBatch.cs +++ b/Utilities/DrawingUtils.SpriteBatch.cs @@ -49,14 +49,14 @@ namespace CalamityMod { if (spriteBatch is null) return; - - - spriteBatch.TryEnd(); - var rasterizer = settings.rasterizerState ?? Main.Rasterizer; - spriteBatch.TryBegin(sortMode, settings.blendState, settings.samplerState, settings.depthStencilState, rasterizer, effect, transformMatrix); - batchCallback?.Invoke(); + + spriteBatch.TryEnd(); + var rasterizer = settings.rasterizerState ?? Main.Rasterizer; + spriteBatch.TryBegin(sortMode, settings.blendState, settings.samplerState, settings.depthStencilState, rasterizer, effect, transformMatrix); + batchCallback?.Invoke(); } - + + [Obsolete("This is violative of spritebatch's control flow and will eventually be removed")] public static bool TryBegin(this SpriteBatch spriteBatch, SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, @@ -75,7 +75,8 @@ namespace CalamityMod return true; } } - + + [Obsolete("This is violative of spritebatch's control flow and will eventually be removed")] public static bool TryEnd(this SpriteBatch spriteBatch) { if (!spriteBatch.HasBeginBeenCalled())