feat: IL edit batching

This commit is contained in:
Stephen Thiebaud committed 2025-12-04 21:20:32 -06:00
1 parent c48a64ad6d
commit 1ad5913eae
3 files changed
+79 -7

No files matched your search

+10 -5
View File
@@ -25,6 +25,11 @@ namespace CalamityMod.ILEditing
/// </summary>
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;
+61
View File
@@ -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.
/// <summary>
/// Simple helper object to capture multiple IL edits to apply in a single
/// pass to avoid additional overhead.
/// </summary>
/// <remarks>
/// Since multiple edits are applied at once, a failure from one edit will
/// cause every edit to fail. Use with caution.
/// </remarks>
internal readonly ref struct ManipulatorBatch()
{
public required Action<ILContext.Manipulator> ManipulatorProvider { get; init; }
private readonly List<ILContext.Manipulator> manipulators = [];
/// <summary>
/// Adds a manipulator to be applied when this object is disposed.
/// </summary>
public void Add(ILContext.Manipulator manipulator)
{
manipulators.Add(manipulator);
}
/// <summary>
/// Applies the queued manipulators.
/// </summary>
public void Dispose()
{
var theManipulators = manipulators;
ManipulatorProvider?.Invoke(
il =>
{
foreach (var manipulator in theManipulators)
{
manipulator?.Invoke(il);
}
}
);
}
/// <summary>
/// Initializes a batch from
/// </summary>
/// <param name="manipulatorProvider"></param>
/// <returns></returns>
public static ManipulatorBatch From(Action<ILContext.Manipulator> manipulatorProvider)
{
ArgumentNullException.ThrowIfNull(manipulatorProvider);
return new ManipulatorBatch { ManipulatorProvider = manipulatorProvider };
}
}
@@ -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<Texture2D>::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