Rewrote Twinkler from statues IL edit in modern style

This commit is contained in:
CongratsIsTrash authored and Cameron Ewell committed 2025-06-17 11:38:29 -04:00
1 parent 95ba9e6c70
commit e522adcbab
1 file changed
+14 -74
+14 -74
View File
@@ -1489,88 +1489,28 @@ namespace CalamityMod.ILEditing
#endregion
#region Statue Additions
/// <summary>
/// Change the following code sequence in Wiring.HitWireSingle
/// num8 = (int) Utils.SelectRandom<short>(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);
///
/// </summary>
/// <param name="il"></param>
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);
// Allow Twinklers to be spawned by the Firefly Statue.
var cursor = 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)))
// Move to the method which randomly selects an NPC for the Statue to spawn. This is the second call of the method in this function.
for (int i = 0; i < 2; i++)
{
// 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))
if (!cursor.TryGotoNext(MoveType.Before, i => i.MatchCall(typeof(Utils), nameof(Utils.SelectRandom))))
{
;
LogFailure("Add Twinkler to Firefly Statue", "Could not move to the SelectRandom method.");
return;
}
// 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<Func<short[], short[]>>(arr =>
{
// resize the array and add our custom firefly
Array.Resize(ref arr, arr.Length + 1);
arr[arr.Length - 1] = (short)ModContent.NPCType<Twinkler>();
return arr;
});
// hook applied successfully
return;
}
// couldn't find the right place to insert
throw new Exception("Hook location not found, switch(*) { case 54: ...");
// Emit a delegate which resizes the array and adds Twinkler to it.
cursor.EmitDelegate<Func<short[], short[]>>(arr =>
{
Array.Resize(ref arr, arr.Length + 1);
arr[arr.Length - 1] = (short)ModContent.NPCType<Twinkler>();
return arr;
});
}
#endregion