feat: condense edit applications into a single callsite

This commit is contained in:
Stephen Thiebaud committed 2025-12-04 23:11:57 -06:00
1 parent 0d49a91bac
commit 925e2359b9
8 files changed
+86 -43

No files matched your search

+18 -24
View File
@@ -18,18 +18,24 @@ using Terraria.ModLoader;
namespace CalamityMod.ILEditing
{
public partial class ILChanges : ModSystem
public partial class ILChanges : ILoadable
{
void ILoadable.Load(Mod mod)
{
ManipulatorManager.ApplyEdits += ApplyEdits;
}
void ILoadable.Unload()
{
// (2023/07) Manually unloading IL hooks is no longer necessary as TML should automatically do it for you.
// https://discord.com/channels/103110554649894912/445276626352209920/1099856743820959855 (links to TML server)
}
/// <summary>
/// Loads all IL Editing changes in the mod.
/// </summary>
public override void OnModLoad()
private static void ApplyEdits(ManipulatorContext ctx)
{
// 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;
@@ -123,15 +129,15 @@ namespace CalamityMod.ILEditing
// Movement speed balance
IL_Player.UpdateJumpHeight += FixJumpHeightBoosts;
playerUpdate.Add(BaseJumpSpeedAdjustment);
playerUpdate.Add(RunSpeedAdjustments);
playerUpdate.Add(NerfOverpoweredRunAccelerationSources); // Soaring Insignia, Magiluminescence, and Shadow Armor
ctx.PlayerUpdate.Add(BaseJumpSpeedAdjustment);
ctx.PlayerUpdate.Add(RunSpeedAdjustments);
ctx.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;
playerUpdate.Add(ManaRegenDelayAdjustment);
ctx.PlayerUpdate.Add(ManaRegenDelayAdjustment);
// Debuff balancing
IL_Projectile.StatusPlayer += RemoveFrozenInflictionFromDeerclopsIceSpikes;
@@ -167,7 +173,7 @@ namespace CalamityMod.ILEditing
IL_Main.UpdateTime_StartNight += BloodMoonsRequire200MaxLife;
IL_WorldGen.AttemptFossilShattering += PreventFossilShattering;
On_Player.GetPickaxeDamage += RemoveHellforgePickaxeRequirement;
playerUpdate.Add(PreventUFODismountInWater);
ctx.PlayerUpdate.Add(PreventUFODismountInWater);
On_Player.GetAnglerReward_MainReward += AddMoreGuaranteedAnglerRewards;
On_Player.GetAnglerReward_Bait += ImproveAnglerBaitReward;
@@ -201,17 +207,5 @@ namespace CalamityMod.ILEditing
//Wulfrum armor hooks on Player.KeyDoubleTap and DrawPendingMouseText to activate its set bonus and spoof the mouse text to display the stats of the activated weapon if shift is held
//HeldOnlyItem detours Player.dropItemCheck, ItemSlot.Draw (Sb, itemarray, int, int, vector2, color) and ItemSlot.LeftClick_ItemArray to make its stuff work
}
/// <summary>
/// Unloads all IL Editing changes in the mod.
/// </summary>
public override void OnModUnload()
{
// (2023/07) Manually unloading IL hooks is no longer necessary as TML should automatically do it for you.
// https://discord.com/channels/103110554649894912/445276626352209920/1099856743820959855 (links to TML server)
VanillaSpawnTownNPCs = null;
labDoorOpen = labDoorClosed = aLabDoorOpen = aLabDoorClosed = exoDoorClosed = exoDoorOpen = -1;
}
}
}
+1 -1
View File
@@ -12,7 +12,7 @@ namespace CalamityMod.ILEditing;
/// 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() : IDisposable
internal sealed class ManipulatorBatch : IDisposable
{
public required Action<ILContext.Manipulator> ManipulatorProvider { get; init; }
+51
View File
@@ -0,0 +1,51 @@
#nullable enable
using System;
using Terraria;
using Terraria.ModLoader;
namespace CalamityMod.ILEditing;
internal readonly record struct ManipulatorContext(
ManipulatorBatch PlayerUpdate
);
/// <summary>
///
/// </summary>
internal sealed class ManipulatorManager : ModSystem
{
private static bool hasAppliedEdits;
private static event Action<ManipulatorContext>? WrappedApplyEdits;
public static event Action<ManipulatorContext> ApplyEdits
{
add
{
if (hasAppliedEdits)
{
throw new InvalidOperationException("Cannot add to ApplyEdits after edits have already been applied! Call it earlier.");
}
WrappedApplyEdits += value;
}
// No reason to actually use this, but we're contractually obligated to.
remove
{
WrappedApplyEdits -= value;
}
}
public override void OnModLoad()
{
base.OnModLoad();
hasAppliedEdits = true;
using var playerUpdate = ManipulatorBatch.From(manipulator => IL_Player.Update += manipulator);
var ctx = new ManipulatorContext(playerUpdate);
WrappedApplyEdits?.Invoke(ctx);
}
}
+4 -4
View File
@@ -2031,7 +2031,7 @@ namespace CalamityMod.ILEditing
#region Arena Collision for other things
private bool ArenaCollision_Vector2_int_int_bool(On_Collision.orig_SolidCollision_Vector2_int_int_bool orig, Vector2 Position, int Width, int Height, bool acceptTopSurfaces)
private static bool ArenaCollision_Vector2_int_int_bool(On_Collision.orig_SolidCollision_Vector2_int_int_bool orig, Vector2 Position, int Width, int Height, bool acceptTopSurfaces)
{
if (ArenaWallSystem.ActiveBoxes.Count > 0)
{
@@ -2044,7 +2044,7 @@ namespace CalamityMod.ILEditing
return orig(Position, Width, Height, acceptTopSurfaces);
}
private bool ArenaCollision_Vector2_int_int(On_Collision.orig_SolidCollision_Vector2_int_int orig, Vector2 Position, int Width, int Height)
private static bool ArenaCollision_Vector2_int_int(On_Collision.orig_SolidCollision_Vector2_int_int orig, Vector2 Position, int Width, int Height)
{
if (ArenaWallSystem.ActiveBoxes.Count > 0)
{
@@ -2058,7 +2058,7 @@ namespace CalamityMod.ILEditing
}
private Vector2 ArenaCollision_TileCollision(On_Collision.orig_TileCollision orig, Vector2 Position, Vector2 Velocity, int Width, int Height, bool fallThrough, bool fall2, int gravDir)
private static Vector2 ArenaCollision_TileCollision(On_Collision.orig_TileCollision orig, Vector2 Position, Vector2 Velocity, int Width, int Height, bool fallThrough, bool fall2, int gravDir)
{
Velocity = orig(Position, Velocity, Width, Height, fallThrough, fall2, gravDir);
if (ArenaWallSystem.ActiveBoxes.Count > 0 && Velocity != Vector2.Zero)
@@ -2074,7 +2074,7 @@ namespace CalamityMod.ILEditing
return Velocity;
}
Vector2 ArenaCollisionLogic(ArenaWallSystem.Box box, Vector2 Position, int Width, int Height, Vector2 Velocity)
private static Vector2 ArenaCollisionLogic(ArenaWallSystem.Box box, Vector2 Position, int Width, int Height, Vector2 Velocity)
{
var originalVelocity = Velocity;
var originalTopLeft = Position;
+3 -3
View File
@@ -14,7 +14,7 @@ namespace CalamityMod.ILEditing
{
#region Fixing Vanilla Not Accounting For Spritebatch Modification in Held Projectiles
private static bool HasLoggedHeldProjectileBlendStateCatch = false;
private void FixHeldProjectileBlendState(On_PlayerDrawLayers.orig_DrawHeldProj orig, PlayerDrawSet drawinfo, Projectile proj)
private static void FixHeldProjectileBlendState(On_PlayerDrawLayers.orig_DrawHeldProj orig, PlayerDrawSet drawinfo, Projectile proj)
{
orig(drawinfo, proj);
@@ -38,7 +38,7 @@ namespace CalamityMod.ILEditing
#endregion
#region Fix Vanilla Not Accounting For Multiple Bobbers When Fishing With Truffle Worm
private void FixTruffleWormFishing(ILContext il)
private static void FixTruffleWormFishing(ILContext il)
{
var cursor = new ILCursor(il);
@@ -85,7 +85,7 @@ namespace CalamityMod.ILEditing
#endregion
#region Fix Vanilla does not call CheckDead when NPC has realLife
private void EnsureCheckDeadOnSegments(ILContext il)
private static void EnsureCheckDeadOnSegments(ILContext il)
{
var cursor = new ILCursor(il);
if (!cursor.TryGotoNext(MoveType.After,
+1 -1
View File
@@ -159,7 +159,7 @@ namespace CalamityMod.ILEditing
#region Prevent Abyss/Dungeon Interactions
private void LimitDungeonEntranceXPosition(On_WorldGen.orig_MakeDungeon orig, int x, int y)
private static void LimitDungeonEntranceXPosition(On_WorldGen.orig_MakeDungeon orig, int x, int y)
{
// Ensure that the base X position stays within its required bounds.
x = Utils.Clamp(x, DungeonBaseXLimit, Main.maxTilesX - DungeonBaseXLimit);
@@ -1,4 +1,5 @@
using System;
using CalamityMod.ILEditing;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Content;
using Terraria;
@@ -54,11 +55,13 @@ namespace CalamityMod.Systems.Graphic.LiquidSystem
TextureArrayReady = true;
}
public override void OnModLoad()
public override void Load()
{
base.Load();
if (ExternalMods.biomeLava == null)
{
LoadILEdits();
ManipulatorManager.ApplyEdits += ApplyEdits;
Main.QueueMainThreadAction(PrepareRT);
Main.OnPreDraw += UpdateRT;
Initialized = true;
@@ -22,18 +22,13 @@ namespace CalamityMod.Systems.Graphic.LiquidSystem
{
private static readonly MethodInfo Tex2DAssetGetter = typeof(Asset<Texture2D>).GetProperty(nameof(Asset<Texture2D>.Value)).GetMethod;
private void LoadILEdits()
private static void ApplyEdits(ManipulatorContext ctx)
{
if (Tex2DAssetGetter == null)
{
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;
@@ -49,8 +44,8 @@ namespace CalamityMod.Systems.Graphic.LiquidSystem
IL_NPC.Collision_WaterCollision += SplashEntityLava;
IL_Projectile.Update += SplashEntityLava;
IL_Item.MoveInWorld += SplashEntityLava;
playerUpdate.Add(SplashEntityLava);
playerUpdate.Add(PlayerDebuffEdit);
ctx.PlayerUpdate.Add(SplashEntityLava);
ctx.PlayerUpdate.Add(PlayerDebuffEdit);
}
#region Drawing IL Edits