From 079c829e4f7fdb6e84d88276e7ebade6535e23cd Mon Sep 17 00:00:00 2001 From: DominicKarma <51094361+DominicKarma@users.noreply.github.com> Date: Wed, 27 Apr 2022 16:26:55 -0500 Subject: [PATCH] Brimstone works again (kinda) --- ILEditing/ILChangesLoading.cs | 4 +- ILEditing/ILHelperMethods.cs | 22 +++++++++-- ILEditing/MechanicILChanges.cs | 66 ++++++++++++++++++++++++--------- Waters/CragsLavaStyle.cs | 2 + Waters/CragsLava_Slope.png | Bin 0 -> 252 bytes Waters/CustomLavaManagement.cs | 7 +++- Waters/CustomLavaStyle.cs | 4 ++ Waters/LiquidTileType.cs | 13 +++++++ 8 files changed, 93 insertions(+), 25 deletions(-) create mode 100644 Waters/CragsLava_Slope.png create mode 100644 Waters/LiquidTileType.cs diff --git a/ILEditing/ILChangesLoading.cs b/ILEditing/ILChangesLoading.cs index 2d48e44d7f..de2876c3ed 100644 --- a/ILEditing/ILChangesLoading.cs +++ b/ILEditing/ILChangesLoading.cs @@ -51,7 +51,7 @@ namespace CalamityMod.ILEditing On.Terraria.Main.DrawInterface += DrawGeneralParticles; On.Terraria.Main.SortDrawCacheWorms += DrawFusableParticles; On.Terraria.Main.SetDisplayMode += ResetRenderTargetSizes; - IL.Terraria.Main.DrawTiles += DrawCustomLava; + IL.Terraria.GameContent.Drawing.TileDrawing.DrawPartialLiquid += DrawCustomLava; IL.Terraria.GameContent.Liquid.LiquidRenderer.InternalDraw += DrawCustomLava2; IL.Terraria.Main.oldDrawWater += DrawCustomLava3; @@ -136,7 +136,7 @@ namespace CalamityMod.ILEditing On.Terraria.Main.DrawInterface -= DrawGeneralParticles; On.Terraria.Main.SortDrawCacheWorms -= DrawFusableParticles; On.Terraria.Main.SetDisplayMode -= ResetRenderTargetSizes; - IL.Terraria.Main.DrawTiles -= DrawCustomLava; + IL.Terraria.GameContent.Drawing.TileDrawing.DrawPartialLiquid -= DrawCustomLava; IL.Terraria.GameContent.Liquid.LiquidRenderer.InternalDraw -= DrawCustomLava2; IL.Terraria.Main.oldDrawWater -= DrawCustomLava3; IL.Terraria.WaterfallManager.DrawWaterfall -= DrawCustomLavafalls; diff --git a/ILEditing/ILHelperMethods.cs b/ILEditing/ILHelperMethods.cs index 150451662d..4f903766db 100644 --- a/ILEditing/ILHelperMethods.cs +++ b/ILEditing/ILHelperMethods.cs @@ -60,16 +60,28 @@ namespace CalamityMod.ILEditing return true; } - private static Texture2D SelectLavaTexture(Texture2D initialTexture, bool blockTexture) + private static Texture2D SelectLavaTexture(Texture2D initialTexture, LiquidTileType type) { // Use the initial texture if it isn't lava. - if (initialTexture != CustomLavaManagement.LavaTexture && initialTexture != CustomLavaManagement.LavaBlockTexture) + if (initialTexture != CustomLavaManagement.LavaTexture && + initialTexture != CustomLavaManagement.LavaBlockTexture && + initialTexture != CustomLavaManagement.LavaSlopeTexture) return initialTexture; foreach (CustomLavaStyle lavaStyle in CustomLavaManagement.CustomLavaStyles) { if (lavaStyle.ChooseLavaStyle()) - return blockTexture ? lavaStyle.BlockTexture : lavaStyle.LavaTexture; + { + switch (type) + { + case LiquidTileType.Block: + return lavaStyle.BlockTexture; + case LiquidTileType.Waterflow: + return lavaStyle.LavaTexture; + case LiquidTileType.Slope: + return lavaStyle.SlopeTexture; + } + } } return initialTexture; @@ -78,7 +90,9 @@ namespace CalamityMod.ILEditing private static Color SelectLavaColor(Texture2D initialTexture, Color initialLightColor) { // Use the initial color if it isn't lava. - if (initialTexture != CustomLavaManagement.LavaTexture && initialTexture != CustomLavaManagement.LavaBlockTexture) + if (initialTexture != CustomLavaManagement.LavaTexture && + initialTexture != CustomLavaManagement.LavaBlockTexture && + initialTexture != CustomLavaManagement.LavaSlopeTexture) return initialLightColor; foreach (CustomLavaStyle lavaStyle in CustomLavaManagement.CustomLavaStyles) diff --git a/ILEditing/MechanicILChanges.cs b/ILEditing/MechanicILChanges.cs index 7a8e637646..2304b006c2 100644 --- a/ILEditing/MechanicILChanges.cs +++ b/ILEditing/MechanicILChanges.cs @@ -514,28 +514,60 @@ namespace CalamityMod.ILEditing { ILCursor cursor = new ILCursor(il); - if (!cursor.TryGotoNext(c => c.MatchLdsfld
("liquidTexture"))) + FieldInfo liquidTexturesField = typeof(TextureAssets).GetField("Liquid"); + FieldInfo liquidSlopeTexturesField = typeof(TextureAssets).GetField("LiquidSlope"); + MethodInfo textureGetValueMethod = typeof(Asset).GetMethod("get_Value"); + + void replaceLiquidTexture(LiquidTileType type) + { + // While this may seem crazy, under no circumstances should there not be a load after exactly 3 instructions. + // The order is load is texture array field -> load index -> load the reference to the texture at that index -> call get_Value(). + cursor.Index += 4; + cursor.EmitDelegate>(initialTexture => SelectLavaTexture(initialTexture, type)); + } + + void replaceLiquidColor(bool sloped) + { + // Pass the texture in so that the method can ensure it is not messing around with non-lava textures. + cursor.Emit(OpCodes.Ldsfld, sloped ? liquidSlopeTexturesField : liquidTexturesField); + cursor.Emit(OpCodes.Ldarg, 4); + cursor.Emit(OpCodes.Ldelem_Ref); + cursor.Emit(OpCodes.Callvirt, textureGetValueMethod); + cursor.EmitDelegate>((initialColor, initialTexture) => SelectLavaColor(initialTexture, initialColor)); + } + + // Replace initial textures and colors. + if (!cursor.TryGotoNext(c => c.MatchLdsfld(liquidTexturesField))) { LogFailure("Custom Lava Drawing", "Could not locate the liquid texture array load."); return; } + replaceLiquidTexture(LiquidTileType.Waterflow); - // While this may seem crazy, under no circumstances should there not be a load after exactly 3 instructions. - // The order is load is texture array field -> load index -> load the reference to the texture at that index. - cursor.Index += 3; - cursor.EmitDelegate>(initialTexture => SelectLavaTexture(initialTexture, true)); - - if (!cursor.TryGotoNext(MoveType.After, c => c.MatchLdloc(155))) + if (!cursor.TryGotoNext(MoveType.After, c => c.MatchLdarg(5))) { LogFailure("Custom Lava Drawing", "Could not locate the liquid light color."); return; } + replaceLiquidColor(false); - // Pass the texture in so that the method can ensure it is not messing around with non-lava textures. - cursor.Emit(OpCodes.Ldsfld, typeof(Main).GetField("liquidTexture")); - cursor.Emit(OpCodes.Ldloc, 151); - cursor.Emit(OpCodes.Ldelem_Ref); - cursor.EmitDelegate>((initialColor, initialTexture) => SelectLavaColor(initialTexture, initialColor)); + // Replace sloped textures and colors. + for (int i = 0; i < 4; i++) + { + if (!cursor.TryGotoNext(c => c.MatchLdsfld(liquidSlopeTexturesField))) + { + LogFailure("Custom Lava Drawing", "Could not locate the sloped liquid texture array load."); + return; + } + replaceLiquidTexture(LiquidTileType.Slope); + + if (!cursor.TryGotoNext(MoveType.After, c => c.MatchLdarg(5))) + { + LogFailure("Custom Lava Drawing", "Could not locate the liquid light color."); + return; + } + replaceLiquidColor(true); + } } private static void DrawCustomLava2(ILContext il) @@ -549,9 +581,9 @@ namespace CalamityMod.ILEditing } // While this may seem crazy, under no circumstances should there not be a load after exactly 3 instructions. - // The order is load is texture array field -> load index -> load the reference to the texture at that index. - cursor.Index += 3; - cursor.EmitDelegate>(initialTexture => SelectLavaTexture(initialTexture, false)); + // The order is load is texture array field -> load index -> load the reference to the texture at that index -> call get_Value(). + cursor.Index += 4; + cursor.EmitDelegate>(initialTexture => SelectLavaTexture(initialTexture, LiquidTileType.Waterflow)); if (!cursor.TryGotoNext(MoveType.After, c => c.MatchLdloc(9))) { @@ -600,9 +632,9 @@ namespace CalamityMod.ILEditing while (cursor.TryGotoNext(c => c.MatchLdsfld(typeof(TextureAssets).GetField("Liquid")))) { // While this may seem crazy, under no circumstances should there not be a load after exactly 3 instructions. - // The order is load is texture array field -> load index -> load the reference to the texture at that index -> call Value. + // The order is load is texture array field -> load index -> load the reference to the texture at that index -> call get_Value(). cursor.Index += 4; - cursor.EmitDelegate>(initialTexture => SelectLavaTexture(initialTexture, true)); + cursor.EmitDelegate>(initialTexture => SelectLavaTexture(initialTexture, LiquidTileType.Block)); } } diff --git a/Waters/CragsLavaStyle.cs b/Waters/CragsLavaStyle.cs index 8208101505..999ffd7d12 100644 --- a/Waters/CragsLavaStyle.cs +++ b/Waters/CragsLavaStyle.cs @@ -12,6 +12,8 @@ namespace CalamityMod.Waters public override string BlockTexturePath => LavaTexturePath + "_Block"; + public override string SlopeTexturePath => LavaTexturePath + "_Slope"; + public override bool ChooseLavaStyle() => Main.LocalPlayer.Calamity().ZoneCalamity; public override int ChooseWaterfallStyle() => ModContent.Find("CalamityMod/CragsLavaflow").Slot; diff --git a/Waters/CragsLava_Slope.png b/Waters/CragsLava_Slope.png new file mode 100644 index 0000000000000000000000000000000000000000..18e07be5d7617292181b8ed4f96f63670bd92aad GIT binary patch literal 252 zcmeAS@N?(olHy`uVBq!ia0vp^9zZO>!3-pu-kp38q!^2X+?^QKos)S9vjzBsxSml905aVUZejycOeH~n!T%YaZa44(@;D1TB8wRq_zrI}c*~N5XNSKVi?2IV)-$^g!4o7`yYwbo zYt+l6Gd<1QpfQK1EB>>~0 CustomLavaStyles; internal static Texture2D LavaBlockTexture; internal static Texture2D LavaTexture; + internal static Texture2D LavaSlopeTexture; // The IL Edits are loaded separately. internal static void Load() @@ -33,8 +35,9 @@ namespace CalamityMod.Waters if (Main.netMode != NetmodeID.Server) { - LavaBlockTexture = ModContent.Request("Terraria/Images/Liquid_1").Value; - LavaTexture = ModContent.Request("Terraria/Images/Misc/water_1").Value; + LavaBlockTexture = ModContent.Request("Terraria/Images/Liquid_1", AssetRequestMode.ImmediateLoad).Value; + LavaTexture = ModContent.Request("Terraria/Images/Misc/water_1", AssetRequestMode.ImmediateLoad).Value; + LavaSlopeTexture = ModContent.Request("Terraria/Images/LiquidSlope_1", AssetRequestMode.ImmediateLoad).Value; } } diff --git a/Waters/CustomLavaStyle.cs b/Waters/CustomLavaStyle.cs index 0f9f1ad7f5..5059a1134d 100644 --- a/Waters/CustomLavaStyle.cs +++ b/Waters/CustomLavaStyle.cs @@ -11,6 +11,7 @@ namespace CalamityMod.Waters { internal Texture2D LavaTexture; internal Texture2D BlockTexture; + internal Texture2D SlopeTexture; internal void Load() { @@ -20,6 +21,7 @@ namespace CalamityMod.Waters LavaTexture = ModContent.Request(LavaTexturePath, AssetRequestMode.ImmediateLoad).Value; BlockTexture = ModContent.Request(BlockTexturePath, AssetRequestMode.ImmediateLoad).Value; + SlopeTexture = ModContent.Request(SlopeTexturePath, AssetRequestMode.ImmediateLoad).Value; } internal void Unload() @@ -32,6 +34,8 @@ namespace CalamityMod.Waters public abstract string BlockTexturePath { get; } + public abstract string SlopeTexturePath { get; } + public virtual bool ChooseLavaStyle() => false; public abstract int ChooseWaterfallStyle(); diff --git a/Waters/LiquidTileType.cs b/Waters/LiquidTileType.cs new file mode 100644 index 0000000000..18198f4ba2 --- /dev/null +++ b/Waters/LiquidTileType.cs @@ -0,0 +1,13 @@ +using Microsoft.Xna.Framework; +using Terraria; +using Terraria.ModLoader; + +namespace CalamityMod.Waters +{ + public enum LiquidTileType + { + Block, + Waterflow, + Slope + } +}