From 10f91e63398fbc8a8d53dec71171eb139c1f3bfb Mon Sep 17 00:00:00 2001 From: Flowaria Date: Thu, 2 Jan 2025 05:28:10 +0900 Subject: [PATCH] Added MultiVariantModWall --- .../Graphic/MultiVariantWallDrawingSystem.cs | 72 +++++++++++++++++++ Walls/MultiVariantModWall.cs | 9 +++ 2 files changed, 81 insertions(+) create mode 100644 Systems/Graphic/MultiVariantWallDrawingSystem.cs create mode 100644 Walls/MultiVariantModWall.cs diff --git a/Systems/Graphic/MultiVariantWallDrawingSystem.cs b/Systems/Graphic/MultiVariantWallDrawingSystem.cs new file mode 100644 index 0000000000..15300be89e --- /dev/null +++ b/Systems/Graphic/MultiVariantWallDrawingSystem.cs @@ -0,0 +1,72 @@ +using CalamityMod.ILEditing; +using CalamityMod.Walls; +using Microsoft.Xna.Framework; +using MonoMod.Cil; +using Terraria; +using Terraria.GameContent.Drawing; +using Terraria.ModLoader; + +namespace CalamityMod.Systems +{ + internal sealed class MultiVariantWallDrawingSystem : ModSystem + { + public sealed override void Load() + { + IL_WallDrawing.DrawWalls += DrawingFrameOffsetForWallSupport; + } + + private void DrawingFrameOffsetForWallSupport(ILContext il) + { + var cursor = new ILCursor(il); + + if (!cursor.TryGotoNext(x => x.MatchCall("wallFrameY"))) + { + LogILFailure("call::Tile.wallFrameY was not found!"); + return; + } + + int tileLocalIdx = -1; + int rectLocalIdx = -1; + if (!cursor.TryGotoPrev(x => x.MatchLdloca(out tileLocalIdx))) // First ldloca should be "tile" + { + LogILFailure("Ldloca::tile was not found!"); + return; + } + + if (!cursor.TryGotoPrev(x => x.MatchLdloca(out rectLocalIdx))) // Second ldloca should be "value" which is Rectangle for drawing + { + LogILFailure("Ldloca::rect was not found!"); + return; + } + + if (!cursor.TryGotoNext(x => x.MatchStfld(nameof(Rectangle.Y)))) + { + LogILFailure("Stfld::Rectangle.Y was not found!"); + return; + } + + cursor.EmitLdloc(tileLocalIdx); + cursor.EmitLdloca(rectLocalIdx); + cursor.EmitDelegate((Tile tile, ref Rectangle drawRect) => + { + if (ModContent.GetModWall(tile.WallType) is MultiVariantModWall mvWall) + { + tile.TilePos(out var i, out var j); + + int offsetX = 0; + int offsetY = 0; + + mvWall.PopulateWallVariant(i, j, ref offsetX, ref offsetY); + + drawRect.X += offsetX; + drawRect.Y += offsetY; + } + }); + } + + private static void LogILFailure(string reason) + { + ILChanges.LogFailure("Support for Wall FrameOffset", reason); + } + } +} diff --git a/Walls/MultiVariantModWall.cs b/Walls/MultiVariantModWall.cs new file mode 100644 index 0000000000..bfc39416ba --- /dev/null +++ b/Walls/MultiVariantModWall.cs @@ -0,0 +1,9 @@ +using Terraria.ModLoader; + +namespace CalamityMod.Walls +{ + public abstract class MultiVariantModWall : ModWall + { + public virtual void PopulateWallVariant(int i, int j, ref int frameXOffset, ref int frameYOffset) { } + } +}