From cf3930b0c89833ca4e84a88ac14542121d6ed4a4 Mon Sep 17 00:00:00 2001 From: Lion8cake Date: Thu, 27 Nov 2025 21:55:49 +1100 Subject: [PATCH] Capture and normal rendering fixes CONTAINS UNSAFE/POINTERS, IF CALAMITY RULES THIS AS A NO-GO, THIS IS WHERE IT WAS INTRODUCED --- CalamityMod.csproj | 1 + ILEditing/MechanicILChanges.cs | 113 +++++++++++++------------------ Systems/CalamityModWaterStyle.cs | 6 +- Systems/LavaRendering.cs | 64 ++++++++++++++--- 4 files changed, 102 insertions(+), 82 deletions(-) diff --git a/CalamityMod.csproj b/CalamityMod.csproj index 06a7b9a67d..644d993a43 100644 --- a/CalamityMod.csproj +++ b/CalamityMod.csproj @@ -6,6 +6,7 @@ net8.0 AnyCPU latest + true diff --git a/ILEditing/MechanicILChanges.cs b/ILEditing/MechanicILChanges.cs index 54b70ebe4b..0caea8e342 100644 --- a/ILEditing/MechanicILChanges.cs +++ b/ILEditing/MechanicILChanges.cs @@ -40,6 +40,7 @@ using Terraria.ID; using Terraria.Localization; using Terraria.ModLoader; using Terraria.UI.Gamepad; +using static Terraria.GameContent.Liquid.LiquidRenderer; namespace CalamityMod.ILEditing { @@ -823,79 +824,87 @@ namespace CalamityMod.ILEditing }); } - //Rewrite, reuse biome lava's newer capture code private void DrawLavatoCapture(ILContext il) { ILCursor cursor = new ILCursor(il); + //Added a new local variable to keep track of the lava alpha so we don't lose which liquid is visually active + VariableDefinition alphaSave_varDef = new(il.Import(typeof(float[]))); + il.Body.Variables.Add(alphaSave_varDef); + + //First the capture saves the initial alpha state of the lavas if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdsfld
("liquidAlpha"), i => i.MatchCall(out _), i => i.MatchStloc(out _))) { LogFailure("Draw lavas to captures", "Could not locate the saving of water alphas"); return; } - cursor.EmitDelegate(() => - { - LavaRendering.alphaSave = CalamityMod.lavaAlpha.ToArray(); + cursor.Emit(OpCodes.Ldloca, alphaSave_varDef); + cursor.EmitDelegate((ref float[] alphaSave) => { + alphaSave = CalamityMod.lavaAlpha.ToArray(); }); - if (!cursor.TryGotoNext(MoveType.Before, i => i.MatchLdcI4(0), i => i.MatchStloc(34), i => i.MatchBr(out _), i => i.MatchLdloc(34), i => i.MatchLdcI4(1), i => i.MatchBeq(out _))) + //Then it resets the alpha to be fully visible + if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdcR4(0.0f), i => i.MatchStsfld
(nameof(Main.cloudAlpha)))) { - LogFailure("Draw lavas to captures", "Could not locate the resetting of water alphas"); + LogFailure("Draw lavas to captures", "Could not locate the setting lava alphas"); return; } - cursor.EmitLdloc(8); - cursor.EmitDelegate((CaptureBiome biome) => - { - for (int i = 0; i < 1; i++) + cursor.EmitLdarg(2); + cursor.EmitDelegate((CaptureSettings settings) => { + + for (int i = 0; i < 1; i++) //1 is the amount of vanilla lava styles, can be increased to include additional lava styles { CalamityMod.lavaAlpha[i] = ((i == CalamityMod.LavaStyle) ? 1f : 0f); } }); - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdarg0(), i => i.MatchLdcI4(1), i => i.MatchLdsfld
("waterStyle"), i => i.MatchLdcR4(1), i => i.MatchLdcI4(1), i => i.MatchCall
("DrawLiquid"))) + + //Then lavas are draw twice for backgrounds, and twice again for both the capture biome and the current lava style + if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdsfld
("waterStyle"), i => i.MatchLdcR4(1), i => i.MatchLdcI4(1), i => i.MatchCall
("DrawLiquid"))) { LogFailure("Draw lavas to captures", "Could not locate the background of liquid capture drawing"); return; } - cursor.EmitDelegate(() => - { + cursor.EmitDelegate(() => { LavaRendering.instance.DrawLiquid(bg: true, CalamityMod.LavaStyle); }); - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdarg0(), i => i.MatchLdcI4(1), i => i.MatchLdsfld
("bloodMoon"), i => i.MatchBrtrue(out _), i => i.MatchLdloc(8), i => i.MatchLdfld("WaterStyle"), i => i.MatchBr(out _), i => i.MatchLdcI4(9), i => i.MatchLdcR4(1), i => i.MatchLdcI4(1), i => i.MatchCall
("DrawLiquid"))) + if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdcI4(9), i => i.MatchLdcR4(1), i => i.MatchLdcI4(1), i => i.MatchCall
("DrawLiquid"))) { - LogFailure("Draw lavas to captures", "Could not locate the second background of liquid capture drawing"); + LogFailure("Draw lavas to captures", "Could not locate the capture biome background of liquid capture drawing"); return; } - cursor.EmitDelegate(() => - { + cursor.EmitLdarg(2); + cursor.EmitDelegate((CaptureSettings settings) => { LavaRendering.instance.DrawLiquid(bg: true, CalamityMod.LavaStyle); }); - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdarg0(), i => i.MatchLdcI4(0), i => i.MatchLdsfld
("waterStyle"), i => i.MatchLdcR4(1), i => i.MatchLdcI4(1), i => i.MatchCall
("DrawLiquid"))) + if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdcI4(0), i => i.MatchLdsfld
("waterStyle"), i => i.MatchLdcR4(1), i => i.MatchLdcI4(1), i => i.MatchCall
("DrawLiquid"))) { LogFailure("Draw lavas to captures", "Could not locate the liquid capture drawing"); return; } - cursor.EmitDelegate(() => - { + cursor.EmitDelegate(() => { LavaRendering.instance.DrawLiquid(bg: false, CalamityMod.LavaStyle); }); - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdarg0(), i => i.MatchLdcI4(0), i => i.MatchLdloc(8), i => i.MatchLdfld("WaterStyle"), i => i.MatchLdcR4(1), i => i.MatchLdcI4(1), i => i.MatchCall
("DrawLiquid"))) + if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdcI4(0), i => i.MatchLdloc(out _), i => i.MatchLdfld("WaterStyle"), i => i.MatchLdcR4(1), i => i.MatchLdcI4(1), i => i.MatchCall
("DrawLiquid"))) { - LogFailure("Draw lavas to captures", "Could not locate the second liquid capture drawing"); + LogFailure("Draw lavas to captures", "Could not locate the capture biome liquid capture drawing"); return; } - cursor.EmitDelegate(() => - { + cursor.EmitLdarg(2); + cursor.EmitDelegate((CaptureSettings settings) => { LavaRendering.instance.DrawLiquid(bg: false, CalamityMod.LavaStyle); }); - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdloc2(), i => i.MatchStsfld
("liquidAlpha"))) + //Finally, the original alpha is returned to the lava alpha + if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdloc(out _), i => i.MatchStsfld
("liquidAlpha"))) { - LogFailure("Draw lavas to captures", "Could not locate water style value returner"); + LogFailure("Draw lavas to captures", "Could not locate water style resetter"); return; } - cursor.EmitDelegate(() => - { - CalamityMod.lavaAlpha = LavaRendering.alphaSave; + cursor.Emit(OpCodes.Ldloc, alphaSave_varDef); + cursor.EmitDelegate((float[] alphaSave) => { + CalamityMod.lavaAlpha = alphaSave; }); } + + //Rewrite to prevent ldloc hardcoding private void AddTileLiquidDrawing(ILContext il) { @@ -920,50 +929,20 @@ namespace CalamityMod.ILEditing #region Lava Blocking private void BlockLavaDrawing(ILContext il) { - //This edit to DrawNormalLiquids makes lavas in normal and white lighting draw with an alpha and with new textures - //If the parameter for the waterstyle is more than the max waterstyles then its subtracted by the max water style count and thats the lava style ID + //Currently just prevents lava rendering, to see the new lava renderer, please see LavaRendering.DrawLava ILCursor cursor = new ILCursor(il); - - //Continue if statement, basically - //if the liquid being drawn is lava and the water style is greater than the max water styles or if the liquid is water and less than the max water styles then the draw code is ran - //otherwise the loop/s are continued for the next liquid - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchStloc(4), i => i.MatchBr(out _), i => i.MatchLdloc(2), i => i.MatchLdfld(typeof(LiquidRenderer).GetNestedType("LiquidDrawCache", BindingFlags.Public), "IsVisible"))) + int pointer2_varNum = -1; + if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdloc(out pointer2_varNum), i => i.MatchLdfld("IsVisible"))) { - LogFailure("Liquid Renderer Drawing", "Could not locate the IsVisible boolean check"); + LogFailure("Block Lava Drawing", "Could not locate the visibility check for liquid drawing"); return; } - cursor.EmitLdarg3(); - cursor.EmitLdloc2(); //Initiated Liquid Draw Cache (needed for the Type parameter) - cursor.EmitLdfld(typeof(LiquidRenderer).GetNestedType("LiquidDrawCache", BindingFlags.Public).GetRuntimeField("Type")); - cursor.EmitDelegate>((IsVisible, style, type) => IsVisible && ((type == 1 && style >= LavaRendering.instance.WaterStyleMaxCount + 1) || (type != 1 && style <= LavaRendering.instance.WaterStyleMaxCount))); - - //Lava alpha color, if the liquid drawn is lava, multiply num by the water alpha - if (!cursor.TryGotoNext(MoveType.Before, i => i.MatchLdloc(2), i => i.MatchLdfld(typeof(LiquidRenderer).GetNestedType("LiquidDrawCache", BindingFlags.Public).GetRuntimeField("Type")), i => i.MatchStloc(8))) + cursor.EmitLdloc(pointer2_varNum); + cursor.EmitLdfld(typeof(LiquidDrawCache).GetField("Type")); + cursor.EmitDelegate((bool isVisibleCondition, byte type) => { - LogFailure("Liquid Renderer Drawing", "Could not locate creation of the local variable num2 (the liquid type holder variable)"); - return; - } - cursor.EmitLdloc(8); - cursor.EmitLdloca(7); - cursor.EmitLdarg(4); - cursor.EmitDelegate((int num2, ref float num, float globalAlpha) => - { - if (num2 == LiquidID.Lava) - { - num *= globalAlpha; - } + return isVisibleCondition && type != LiquidID.Lava; }); - - //Conditionally replace the liquid texture whether the liquid is lava or water - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchCallvirt(typeof(Asset).GetMethod("get_Value", BindingFlags.Public | BindingFlags.Instance)))) - { - LogFailure("Liquid Renderer Drawing", "Could not locate the Texture2D array of liquids"); - return; - } - cursor.EmitLdarg3(); - cursor.EmitLdloc2(); //Initiated Liquid Draw Cache (needed for the Type parameter) - cursor.EmitLdfld(typeof(LiquidRenderer).GetNestedType("LiquidDrawCache", BindingFlags.Public).GetRuntimeField("Type")); - cursor.EmitDelegate>((initialTexture, style, type) => (style >= LavaRendering.instance.WaterStyleMaxCount + 1 && type == LiquidID.Lava) ? CalamityMod.LavaTextures.liquid[style - LavaRendering.instance.WaterStyleMaxCount - 1].Value : initialTexture); } private void BlockLavaDrawingForSlopes(On_TileDrawing.orig_DrawTile_LiquidBehindTile orig, TileDrawing self, bool solidLayer, bool inFrontOfPlayers, int waterStyleOverride, Vector2 screenPosition, Vector2 screenOffset, int tileX, int tileY, Tile tileCache) diff --git a/Systems/CalamityModWaterStyle.cs b/Systems/CalamityModWaterStyle.cs index 8c523342fe..bd471ddbd1 100644 --- a/Systems/CalamityModWaterStyle.cs +++ b/Systems/CalamityModWaterStyle.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using Terraria; -using Terraria.Graphics; +using Terraria.Graphics; using Terraria.ModLoader; namespace CalamityMod.Systems diff --git a/Systems/LavaRendering.cs b/Systems/LavaRendering.cs index 8dc526e169..6298151daf 100644 --- a/Systems/LavaRendering.cs +++ b/Systems/LavaRendering.cs @@ -12,6 +12,7 @@ using Terraria.GameContent.Liquid; using Terraria.Graphics; using Terraria.ID; using Terraria.ModLoader; +using static Terraria.GameContent.Liquid.LiquidRenderer; using static Terraria.WaterfallManager; namespace CalamityMod.Systems @@ -21,14 +22,9 @@ namespace CalamityMod.Systems //Welcome to Calamity's lava rendering. Prepare your eyes public static LavaRendering instance; - public int WaterStyleMaxCount = ModContent.GetContent().Count() + LoaderManager.Get().VanillaCount; - - internal static float[] alphaSave; - - public override void Load() - { - LavaRendering.alphaSave = new float[CalamityMod.lavaAlpha.Length]; - } + internal static readonly FieldInfo _drawArea = typeof(LiquidRenderer).GetField("_drawArea", BindingFlags.NonPublic | BindingFlags.Instance); + internal static readonly FieldInfo _drawCache = typeof(LiquidRenderer).GetField("_drawCache", BindingFlags.NonPublic | BindingFlags.Instance); + internal static readonly FieldInfo _animationFrame = typeof(LiquidRenderer).GetField("_animationFrame", BindingFlags.NonPublic | BindingFlags.Instance); public void DrawLavas(bool isBackground = false) { @@ -62,6 +58,7 @@ namespace CalamityMod.Systems DrawLiquid(isBackground, CalamityMod.LavaStyle, flag ? CalamityMod.lavaAlpha[CalamityMod.LavaStyle] : 1f); } + protected internal void DrawLiquid(bool bg = false, int lavaStyle = 0, float Alpha = 1f, bool drawSinglePassLiquids = true) { if (!Lighting.NotRetro) @@ -72,17 +69,64 @@ namespace CalamityMod.Systems Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Vector2 drawOffset = (Vector2)(Main.drawToScreen ? Vector2.Zero : new Vector2((float)Main.offScreenRange, (float)Main.offScreenRange)) - Main.screenPosition; - if (bg) + if (bg && !LiquidEdgeRenderer.Active) { DrawLiquidBehindTiles(lavaStyle); } - LiquidRenderer.Instance.DrawNormalLiquids(Main.spriteBatch, drawOffset, lavaStyle + ModContent.GetContent().Count() + LoaderManager.Get().VanillaCount + 1, Alpha, bg); + DrawLava(Main.spriteBatch, drawOffset, lavaStyle, Alpha, bg); if (!bg) { TimeLogger.DrawTime(4, stopwatch.Elapsed.TotalMilliseconds); } } + public unsafe void DrawLava(SpriteBatch spriteBatch, Vector2 drawOffset, int LavaStyle, float globalAlpha, bool isBackgroundDraw) + { + Rectangle drawArea = (Rectangle)_drawArea.GetValue(Instance); + Main.tileBatch.Begin(); + fixed (LiquidDrawCache* ptr3 = &((LiquidDrawCache[])_drawCache.GetValue(Instance))[0]) + { + LiquidDrawCache* ptr2 = ptr3; + int cacheLength = ((LiquidDrawCache[])_drawCache.GetValue(Instance)).Length; + for (int i = drawArea.X; i < drawArea.X + drawArea.Width; i++) + { + for (int j = drawArea.Y; j < drawArea.Y + drawArea.Height; j++) + { + if (ptr2->IsVisible && ptr2->Type == LiquidID.Lava) + { + Rectangle sourceRectangle = ptr2->SourceRectangle; + if (ptr2->IsSurfaceLiquid) + { + sourceRectangle.Y = 1280; + } + else + { + sourceRectangle.Y += ((int)_animationFrame.GetValue(Instance)) * 80; + } + Vector2 liquidOffset = ptr2->LiquidOffset; + float num = ptr2->Opacity * (isBackgroundDraw ? 1f : DEFAULT_OPACITY[ptr2->Type]); + int num2 = LavaStyle; + num *= globalAlpha; + num = Math.Min(1f, num); + Lighting.GetCornerColors(i, j, out var vertices); + ref Color bottomLeftColor = ref vertices.BottomLeftColor; + bottomLeftColor *= num; + ref Color bottomRightColor = ref vertices.BottomRightColor; + bottomRightColor *= num; + ref Color topLeftColor = ref vertices.TopLeftColor; + topLeftColor *= num; + ref Color topRightColor = ref vertices.TopRightColor; + topRightColor *= num; + Main.DrawTileInWater(drawOffset, i, j); + Main.tileBatch.Draw(CalamityMod.LavaTextures.liquid[num2].Value, new Vector2((float)(i << 4), (float)(j << 4)) + drawOffset + liquidOffset, sourceRectangle, vertices, Vector2.Zero, 1f, (SpriteEffects)0); + } + ptr2++; + } + } + } + Main.tileBatch.End(); + } + public void oldDrawLava(bool bg = false, int Style = 0, float Alpha = 1f) { float num = 0f;