using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; namespace CalamityMod { public static partial class CalamityUtils { // Cached for efficiency purposes. private const BindingFlags Bind_Private_Instance = BindingFlags.NonPublic | BindingFlags.Instance; private static readonly FastField Fld_BeginCalled = new("beginCalled", Bind_Private_Instance); private static readonly FastField Fld_SortMode = new("sortMode", Bind_Private_Instance); private static readonly FastField Fld_BlendState = new("blendState", Bind_Private_Instance); private static readonly FastField Fld_SamplerState = new("samplerState", Bind_Private_Instance); private static readonly FastField Fld_DepthStencilState = new("depthStencilState", Bind_Private_Instance); private static readonly FastField Fld_RasterizerState = new("rasterizerState", Bind_Private_Instance); private static readonly FastField Fld_TransformMatrix = new("transformMatrix", Bind_Private_Instance); private static readonly FastField Fld_CustomEffect = new("customEffect", Bind_Private_Instance); /// /// Sets a 's arbitrarily. /// /// The sprite batch. /// The blend state to use. public static void SetBlendState(this SpriteBatch spriteBatch, BlendState blendState) { spriteBatch.End(); spriteBatch.Begin(SpriteSortMode.Immediate, blendState, Main.DefaultSamplerState, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, Main.GameViewMatrix.TransformationMatrix); } /// /// Determines if a is in a lock due to a call. /// /// The sprite batch to check. public static bool HasBeginBeenCalled(this SpriteBatch spriteBatch) { return Fld_BeginCalled.Get(spriteBatch); } public static void SafeAction(this SpriteBatch spriteBatch, Action action) { if (spriteBatch is null) return; if (spriteBatch.HasBeginBeenCalled()) { var oldSort = Fld_SortMode.Get(spriteBatch); var oldBlend = Fld_BlendState.Get(spriteBatch); var oldSampler = Fld_SamplerState.Get(spriteBatch); var oldDepths = Fld_DepthStencilState.Get(spriteBatch); var oldRaster = Fld_RasterizerState.Get(spriteBatch); var oldEffect = Fld_CustomEffect.Get(spriteBatch); var oldMtx = Fld_TransformMatrix.Get(spriteBatch); try { action?.Invoke(); } finally { // If something has started in this block, restore the state to previous one spriteBatch.TryEnd(); spriteBatch.TryBegin(oldSort, oldBlend, oldSampler, oldDepths, oldRaster, oldEffect, oldMtx); } } else { try { action?.Invoke(); } finally { // Initial State was off, turn off the batching spriteBatch.TryEnd(); } } } /// /// Starts SpriteBatch then Re-Begin batch with old settings when it's all done /// /// /// /// /// /// /// public static void SafeBegin(this SpriteBatch spriteBatch, SpriteSortMode sortMode, BatchSetting settings, Effect effect, Matrix transformMatrix, Action batchCallback ) { if (spriteBatch is null) return; spriteBatch.SafeAction(() => { spriteBatch.TryEnd(); var rasterizer = settings.rasterizerState ?? Main.Rasterizer; spriteBatch.TryBegin(sortMode, settings.blendState, settings.samplerState, settings.depthStencilState, rasterizer, effect, transformMatrix); batchCallback?.Invoke(); }); } public static bool TryBegin(this SpriteBatch spriteBatch, SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect, Matrix transformMatrix) { if (spriteBatch.HasBeginBeenCalled()) { return false; } else { spriteBatch.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, transformMatrix); return true; } } public static bool TryEnd(this SpriteBatch spriteBatch) { if (!spriteBatch.HasBeginBeenCalled()) { return false; } else { spriteBatch.End(); return true; } } } }