From 424cb4f7ee809c6f0b8a1219ecd88794be94079f Mon Sep 17 00:00:00 2001
From: ben-tk <45545085+ben-tk@users.noreply.github.com>
Date: Fri, 31 Dec 2021 12:09:34 -0500
Subject: [PATCH] moved firefly statue IL edits to the IL Changes files
---
ILEditing/ILChangesLoading.cs | 1 +
ILEditing/MechanicILChanges.cs | 87 ++++++++++++++++++++++++++++++++
NPCs/Astral/Twinkler.cs | 90 ----------------------------------
3 files changed, 88 insertions(+), 90 deletions(-)
diff --git a/ILEditing/ILChangesLoading.cs b/ILEditing/ILChangesLoading.cs
index 222f8d54cb..ce55c7ec3a 100644
--- a/ILEditing/ILChangesLoading.cs
+++ b/ILEditing/ILChangesLoading.cs
@@ -51,6 +51,7 @@ namespace CalamityMod.ILEditing
IL.Terraria.Main.oldDrawWater += DrawCustomLava3;
IL.Terraria.WaterfallManager.DrawWaterfall += DrawCustomLavafalls;
On.Terraria.NPC.Collision_DecideFallThroughPlatforms += EnableCalamityBossPlatformCollision;
+ IL.Terraria.Wiring.HitWireSingle += AddTwinklersToStatue;
// Damage and health balance
IL.Terraria.Main.DamageVar += AdjustDamageVariance;
diff --git a/ILEditing/MechanicILChanges.cs b/ILEditing/MechanicILChanges.cs
index 41fc99867d..9c89c713e7 100644
--- a/ILEditing/MechanicILChanges.cs
+++ b/ILEditing/MechanicILChanges.cs
@@ -1,5 +1,6 @@
using CalamityMod.Buffs.DamageOverTime;
using CalamityMod.CalPlayer;
+using CalamityMod.NPCs.Astral;
using CalamityMod.NPCs.AstrumAureus;
using CalamityMod.NPCs.Crabulon;
using CalamityMod.NPCs.Ravager;
@@ -647,5 +648,91 @@ namespace CalamityMod.ILEditing
cursor.Emit(OpCodes.Stloc, 51);
}
#endregion Custom Lava Visuals
+
+ #region Statue Additions
+ ///
+ /// Change the following code sequence in Wiring.HitWireSingle
+ /// num8 = (int) Utils.SelectRandom(Main.rand, new short[2]
+ /// {
+ /// 355,
+ /// 358
+ /// });
+ ///
+ /// to
+ ///
+ /// var arr = new short[2]
+ /// {
+ /// 355,
+ /// 358
+ /// });
+ /// arr = arr.ToList().Add(id).ToArray();
+ /// num8 = Utils.SelectRandom(Main.rand, arr);
+ ///
+ ///
+ ///
+ private static void AddTwinklersToStatue(ILContext il)
+ {
+ // obtain a cursor positioned before the first instruction of the method
+ // the cursor is used for navigating and modifying the il
+ var c = new ILCursor(il);
+
+ // the exact location for this hook is very complex to search for due to the hook instructions not being unique, and buried deep in control flow
+ // switch statements are sometimes compiled to if-else chains, and debug builds litter the code with no-ops and redundant locals
+
+ // in general you want to search using structure and function rather than numerical constants which may change across different versions or compile settings
+ // using local variable indices is almost always a bad idea
+
+ // we can search for
+ // switch (*)
+ // case 54:
+ // Utils.SelectRandom *
+
+ // in general you'd want to look for a specific switch variable, or perhaps the containing switch (type) { case 105:
+ // but the generated IL is really variable and hard to match in this case
+
+ // we'll just use the fact that there are no other switch statements with case 54, followed by a SelectRandom
+
+ ILLabel[] targets = null;
+ while (c.TryGotoNext(i => i.MatchSwitch(out targets)))
+ {
+ // some optimising compilers generate a sub so that all the switch cases start at 0
+ // ldc.i4.s 51
+ // sub
+ // switch
+ int offset = 0;
+ if (c.Prev.MatchSub() && c.Prev.Previous.MatchLdcI4(out offset))
+ {
+ ;
+ }
+
+ // get the label for case 54: if it exists
+ int case54Index = 54 - offset;
+ if (case54Index < 0 || case54Index >= targets.Length || !(targets[case54Index] is ILLabel target))
+ {
+ continue;
+ }
+
+ // move the cursor to case 54:
+ c.GotoLabel(target);
+ // there's lots of extra checks we could add here to make sure we're at the right spot, such as not encountering any branching instructions
+ c.GotoNext(i => i.MatchCall(typeof(Utils), nameof(Utils.SelectRandom)));
+
+ // goto next positions us before the instruction we searched for, so we can insert our array modifying code right here
+ c.EmitDelegate>(arr =>
+ {
+ // resize the array and add our custom firefly
+ Array.Resize(ref arr, arr.Length+1);
+ arr[arr.Length-1] = (short)ModContent.NPCType();
+ return arr;
+ });
+
+ // hook applied successfully
+ return;
+ }
+
+ // couldn't find the right place to insert
+ throw new Exception("Hook location not found, switch(*) { case 54: ...");
+ }
+ #endregion Statue Additions
}
}
diff --git a/NPCs/Astral/Twinkler.cs b/NPCs/Astral/Twinkler.cs
index 1cc77dad77..44b5f588bf 100644
--- a/NPCs/Astral/Twinkler.cs
+++ b/NPCs/Astral/Twinkler.cs
@@ -76,95 +76,5 @@ namespace CalamityMod.NPCs.Astral
return;
}
}
-
- public override bool Autoload(ref string name)
- {
- IL.Terraria.Wiring.HitWireSingle += HookStatue;
- return base.Autoload(ref name);
- }
-
- ///
- /// Change the following code sequence in Wiring.HitWireSingle
- /// num8 = (int) Utils.SelectRandom(Main.rand, new short[2]
- /// {
- /// 355,
- /// 358
- /// });
- ///
- /// to
- ///
- /// var arr = new short[2]
- /// {
- /// 355,
- /// 358
- /// });
- /// arr = arr.ToList().Add(id).ToArray();
- /// num8 = Utils.SelectRandom(Main.rand, arr);
- ///
- ///
- ///
- private void HookStatue(ILContext il)
- {
- // obtain a cursor positioned before the first instruction of the method
- // the cursor is used for navigating and modifying the il
- var c = new ILCursor(il);
-
- // the exact location for this hook is very complex to search for due to the hook instructions not being unique, and buried deep in control flow
- // switch statements are sometimes compiled to if-else chains, and debug builds litter the code with no-ops and redundant locals
-
- // in general you want to search using structure and function rather than numerical constants which may change across different versions or compile settings
- // using local variable indices is almost always a bad idea
-
- // we can search for
- // switch (*)
- // case 54:
- // Utils.SelectRandom *
-
- // in general you'd want to look for a specific switch variable, or perhaps the containing switch (type) { case 105:
- // but the generated IL is really variable and hard to match in this case
-
- // we'll just use the fact that there are no other switch statements with case 54, followed by a SelectRandom
-
- ILLabel[] targets = null;
- while (c.TryGotoNext(i => i.MatchSwitch(out targets)))
- {
- // some optimising compilers generate a sub so that all the switch cases start at 0
- // ldc.i4.s 51
- // sub
- // switch
- int offset = 0;
- if (c.Prev.MatchSub() && c.Prev.Previous.MatchLdcI4(out offset))
- {
- ;
- }
-
- // get the label for case 54: if it exists
- int case54Index = 54 - offset;
- if (case54Index < 0 || case54Index >= targets.Length || !(targets[case54Index] is ILLabel target))
- {
- continue;
- }
-
- // move the cursor to case 54:
- c.GotoLabel(target);
- // there's lots of extra checks we could add here to make sure we're at the right spot, such as not encountering any branching instructions
- c.GotoNext(i => i.MatchCall(typeof(Utils), nameof(Utils.SelectRandom)));
-
- // goto next positions us before the instruction we searched for, so we can insert our array modifying code right here
- c.EmitDelegate>(arr =>
- {
- // resize the array and add our custom firefly
- Array.Resize(ref arr, arr.Length+1);
- arr[arr.Length-1] = (short)ModContent.NPCType();
- return arr;
- });
-
- // hook applied successfully
- return;
- }
-
- // couldn't find the right place to insert
- throw new Exception("Hook location not found, switch(*) { case 54: ...");
- }
}
}