From 91c74e34021d7ffbf3c94c3ca5c807dec518613f Mon Sep 17 00:00:00 2001 From: Dominic Date: Mon, 3 Jan 2022 00:45:33 -0600 Subject: [PATCH] Made the update priority edit more specific --- ILEditing/VanillaBugfixingILChanges.cs | 63 ++++++++++---------------- 1 file changed, 24 insertions(+), 39 deletions(-) diff --git a/ILEditing/VanillaBugfixingILChanges.cs b/ILEditing/VanillaBugfixingILChanges.cs index 3f451c662f..832b5db2dc 100644 --- a/ILEditing/VanillaBugfixingILChanges.cs +++ b/ILEditing/VanillaBugfixingILChanges.cs @@ -5,6 +5,7 @@ using MonoMod.Cil; using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using Terraria; using Terraria.GameInput; using Terraria.ID; @@ -72,6 +73,8 @@ namespace CalamityMod.ILEditing NPCID.AncientCultistSquidhead, }; + public static readonly List OrderedProjectiles = new List(); + #region Fixing NPC HP Sync Byte Counts in Boss Rush // CONTEXT FOR FIX: When NPCs sync they have a pre-determined amount of bytes that are used to store HP/Max NPC information in packets for efficiency. // However, this may not always coincide with the true HP of the NPC when it's created if it has more HP than the allocated bytes can sufficiently store, which can @@ -140,56 +143,38 @@ namespace CalamityMod.ILEditing var cursor = new ILCursor(il); // Locate the location where the projectile loop starts. - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchCallOrCallvirt("SetUP"))) + if (!cursor.TryGotoNext(MoveType.Before, i => i.MatchCallOrCallvirt("SetUP"))) { LogFailure("Projectile Update Priority fix", "Could not locate the LockOnHelper.SetUP method."); return; } - int startIndex = cursor.Index; - - // Then go to where it ends. - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchCallOrCallvirt("SetDOWN"))) + // Before doing anything else, prepare the list of ordered projectiles. + cursor.EmitDelegate>>(() => { - LogFailure("Projectile Update Priority fix", "Could not locate the LockOnHelper.SetDOWN method."); + return Main.projectile.Take(Main.maxProjectiles).OrderByDescending(p => p.active ? p.Calamity().UpdatePriority : 0f).ToList(); + }); + cursor.Emit(OpCodes.Stsfld, typeof(ILChanges).GetField("OrderedProjectiles", BindingFlags.Static | BindingFlags.Public)); + + // Go before the declaration of the projectile loop index and declare the ordered projectile. + if (!cursor.TryGotoNext(MoveType.Before, i => i.MatchStsfld
("ProjectileUpdateLoopIndex"))) + { + LogFailure("Projectile Update Priority fix", "Could not locate the ProjectileUpdateLoopIndex field."); return; } - int endIndex = cursor.Index; - - // Go back to the start. - cursor.Goto(startIndex); - - // And delete the range that handles update logic intent to replace it. - cursor.RemoveRange(Math.Abs(endIndex - startIndex)); - - // And do projectile update logic manually. - cursor.EmitDelegate(() => + // Replace the projectile reference on the Projectile.Update call. + if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdelemRef())) { - List orderedProjectiles = Main.projectile.Take(Main.maxProjectiles).OrderByDescending(p => p.active ? p.Calamity().UpdatePriority : 0f).ToList(); - for (int i = 0; i < Main.maxProjectiles; i++) - { - Projectile projectile = orderedProjectiles[i]; - Main.ProjectileUpdateLoopIndex = Array.IndexOf(Main.projectile, projectile); + LogFailure("Projectile Update Priority fix", $"Could not locate the Main.projectile index load reference."); + return; + } - // Yes, this is completely ridiculous, but it's probably best to leave it this way to be as in-accordance with - // vanilla's normal update logic as possible. - if (Main.ignoreErrors) - { - try - { - projectile.Update(Main.ProjectileUpdateLoopIndex); - } - catch - { - Main.projectile[Main.ProjectileUpdateLoopIndex] = new Projectile(); - } - } - else - projectile.Update(i); - } - Main.ProjectileUpdateLoopIndex = -1; - }); + // Pop the Main.projectile[i] reference and replace it with OrderedProjectiles[i]. + cursor.Emit(OpCodes.Pop); + cursor.Emit(OpCodes.Ldsfld, typeof(ILChanges).GetField("OrderedProjectiles", BindingFlags.Static | BindingFlags.Public)); + cursor.Emit(OpCodes.Ldloc, 29); + cursor.Emit(OpCodes.Callvirt, typeof(List).GetMethod("get_Item")); } #endregion Fix Projectile Update Priority Problems