From b3a3b5da7b122735be683faed195cc7652e156ff Mon Sep 17 00:00:00 2001 From: Dreitone Date: Mon, 8 Dec 2025 04:52:07 -0600 Subject: [PATCH] shitty debugging interface --- Debugging/SpritebatchDebug.cs | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Debugging/SpritebatchDebug.cs diff --git a/Debugging/SpritebatchDebug.cs b/Debugging/SpritebatchDebug.cs new file mode 100644 index 0000000000..79f03c7474 --- /dev/null +++ b/Debugging/SpritebatchDebug.cs @@ -0,0 +1,56 @@ +using System; +using System.Diagnostics; +using System.Reflection; +using System.Reflection.Emit; +using Microsoft.Xna.Framework.Graphics; +using MonoMod.RuntimeDetour; +using MonoMod.RuntimeDetour.HookGen; +using Terraria.ModLoader; + +namespace CalamityMod.Debugging +{ + public static class SpritebatchDebug + { + public static string Trace { get; set; } + } + + public class SpritebatchDebugInitializer : ILoadable + { + private static Hook _beginHook; + private static Hook _endHook; + + public void Load(Mod mod) + { + MethodInfo begin = typeof(SpriteBatch).GetMethod(nameof(SpriteBatch.CheckBegin), BindingFlags.NonPublic | BindingFlags.Instance); + MethodInfo end = typeof(SpriteBatch).GetMethod(nameof(SpriteBatch.End), BindingFlags.Public | BindingFlags.Instance); + + Debug.Assert(begin != null); + Debug.Assert(end != null); + + _beginHook = new Hook(begin, CheckBegin_Impl); + _endHook = new Hook(end, End_Impl); + } + + private static void CheckBegin_Impl(Action orig, SpriteBatch self, string method) + { + if (self.beginCalled) ModLoader.GetMod(nameof(CalamityMod)).Logger.Debug(SpritebatchDebug.Trace); + SpritebatchDebug.Trace = Environment.StackTrace; + + orig(self, method); + } + + private static void End_Impl(Action orig, SpriteBatch self) + { + if (!self.beginCalled) ModLoader.GetMod(nameof(CalamityMod)).Logger.Debug(SpritebatchDebug.Trace); + SpritebatchDebug.Trace = Environment.StackTrace; + + orig(self); + } + + public void Unload() + { + _beginHook.Dispose(); + _endHook.Dispose(); + } + } +}