From 1ad5913eae2f162f5da184fc55ca54b4bede8dee Mon Sep 17 00:00:00 2001 From: Stephen Thiebaud Date: Thu, 4 Dec 2025 21:20:32 -0600 Subject: [PATCH] feat: IL edit batching --- ILEditing/ILChangesLoading.cs | 15 +++-- ILEditing/ManipulatorBatch.cs | 61 +++++++++++++++++++ .../LiquidSystem/ModLavaStyleSystem_ILEdit.cs | 10 ++- 3 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 ILEditing/ManipulatorBatch.cs diff --git a/ILEditing/ILChangesLoading.cs b/ILEditing/ILChangesLoading.cs index 6213bc5ad5..a04b592ec1 100644 --- a/ILEditing/ILChangesLoading.cs +++ b/ILEditing/ILChangesLoading.cs @@ -25,6 +25,11 @@ namespace CalamityMod.ILEditing /// public override void OnModLoad() { + // Refer to the documentation in ManipulatorBatch for how this + // works. The objects are automatically disposed at the end of this + // method, applying any queued edits. + using var playerUpdate = ManipulatorBatch.From(manipulator => IL_Player.Update += manipulator); + // Wrap the vanilla town NPC spawning function in a delegate so that it can be tossed around and called at will. var updateTime = typeof(Main).GetMethod("UpdateTime_SpawnTownNPCs", BindingFlags.Static | BindingFlags.NonPublic); VanillaSpawnTownNPCs = Delegate.CreateDelegate(typeof(Action), updateTime) as Action; @@ -118,15 +123,15 @@ namespace CalamityMod.ILEditing // Movement speed balance IL_Player.UpdateJumpHeight += FixJumpHeightBoosts; - IL_Player.Update += BaseJumpSpeedAdjustment; - IL_Player.Update += RunSpeedAdjustments; - IL_Player.Update += NerfOverpoweredRunAccelerationSources; // Soaring Insignia, Magiluminescence, and Shadow Armor + playerUpdate.Add(BaseJumpSpeedAdjustment); + playerUpdate.Add(RunSpeedAdjustments); + playerUpdate.Add(NerfOverpoweredRunAccelerationSources); // Soaring Insignia, Magiluminescence, and Shadow Armor IL_Player.WingMovement += RemoveSoaringInsigniaInfiniteWingTime; // Life regen and mana regen balance IL_Player.UpdateLifeRegen += UpdateLifeRegenBalancingChanges; IL_Player.UpdateManaRegen += UpdateManaRegenBalancingChanges; - IL_Player.Update += ManaRegenDelayAdjustment; + playerUpdate.Add(ManaRegenDelayAdjustment); // Debuff balancing IL_Projectile.StatusPlayer += RemoveFrozenInflictionFromDeerclopsIceSpikes; @@ -162,7 +167,7 @@ namespace CalamityMod.ILEditing IL_Main.UpdateTime_StartNight += BloodMoonsRequire200MaxLife; IL_WorldGen.AttemptFossilShattering += PreventFossilShattering; On_Player.GetPickaxeDamage += RemoveHellforgePickaxeRequirement; - IL_Player.Update += PreventUFODismountInWater; + playerUpdate.Add(PreventUFODismountInWater); On_Player.GetAnglerReward_MainReward += AddMoreGuaranteedAnglerRewards; On_Player.GetAnglerReward_Bait += ImproveAnglerBaitReward; diff --git a/ILEditing/ManipulatorBatch.cs b/ILEditing/ManipulatorBatch.cs new file mode 100644 index 0000000000..e98d9aec28 --- /dev/null +++ b/ILEditing/ManipulatorBatch.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using MonoMod.Cil; + +namespace CalamityMod.ILEditing; + +// TODO: We can directly add the IDisposable interface if we update the C# +// version the project targets. + +/// +/// Simple helper object to capture multiple IL edits to apply in a single +/// pass to avoid additional overhead. +/// +/// +/// Since multiple edits are applied at once, a failure from one edit will +/// cause every edit to fail. Use with caution. +/// +internal readonly ref struct ManipulatorBatch() +{ + public required Action ManipulatorProvider { get; init; } + + private readonly List manipulators = []; + + /// + /// Adds a manipulator to be applied when this object is disposed. + /// + public void Add(ILContext.Manipulator manipulator) + { + manipulators.Add(manipulator); + } + + /// + /// Applies the queued manipulators. + /// + public void Dispose() + { + var theManipulators = manipulators; + + ManipulatorProvider?.Invoke( + il => + { + foreach (var manipulator in theManipulators) + { + manipulator?.Invoke(il); + } + } + ); + } + + /// + /// Initializes a batch from + /// + /// + /// + public static ManipulatorBatch From(Action manipulatorProvider) + { + ArgumentNullException.ThrowIfNull(manipulatorProvider); + + return new ManipulatorBatch { ManipulatorProvider = manipulatorProvider }; + } +} diff --git a/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_ILEdit.cs b/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_ILEdit.cs index be68bc61ae..adb5396756 100644 --- a/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_ILEdit.cs +++ b/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_ILEdit.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Reflection; +using CalamityMod.ILEditing; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Mono.Cecil; @@ -28,6 +29,11 @@ namespace CalamityMod.Systems.Graphic.LiquidSystem CalamityMod.Log.ILFailure("ModLavaStyle", "Cannot find Getter for Asset::Value"); return; } + + // Refer to the documentation in ManipulatorBatch for how this + // works. The objects are automatically disposed at the end of this + // method, applying any queued edits. + using var playerUpdate = ManipulatorBatch.From(manipulator => IL_Player.Update += manipulator); // Graphic IL_LiquidRenderer.DrawNormalLiquids += DrawNormalLiquidPatch; @@ -43,8 +49,8 @@ namespace CalamityMod.Systems.Graphic.LiquidSystem IL_NPC.Collision_WaterCollision += SplashEntityLava; IL_Projectile.Update += SplashEntityLava; IL_Item.MoveInWorld += SplashEntityLava; - IL_Player.Update += SplashEntityLava; - IL_Player.Update += PlayerDebuffEdit; + playerUpdate.Add(SplashEntityLava); + playerUpdate.Add(PlayerDebuffEdit); } #region Drawing IL Edits