diff --git a/ILEditing/ILChangesLoading.cs b/ILEditing/ILChangesLoading.cs index 4406d78c01..9f19ff0a6d 100644 --- a/ILEditing/ILChangesLoading.cs +++ b/ILEditing/ILChangesLoading.cs @@ -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) + } + /// /// Loads all IL Editing changes in the mod. /// - 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 } - - /// - /// Unloads all IL Editing changes in the mod. - /// - 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; - } } } diff --git a/ILEditing/ManipulatorBatch.cs b/ILEditing/ManipulatorBatch.cs index 650a0d0b4f..e90509debe 100644 --- a/ILEditing/ManipulatorBatch.cs +++ b/ILEditing/ManipulatorBatch.cs @@ -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. /// -internal readonly ref struct ManipulatorBatch() : IDisposable +internal sealed class ManipulatorBatch : IDisposable { public required Action ManipulatorProvider { get; init; } diff --git a/ILEditing/ManipulatorManager.cs b/ILEditing/ManipulatorManager.cs new file mode 100644 index 0000000000..21d060030a --- /dev/null +++ b/ILEditing/ManipulatorManager.cs @@ -0,0 +1,51 @@ +#nullable enable + +using System; +using Terraria; +using Terraria.ModLoader; + +namespace CalamityMod.ILEditing; + +internal readonly record struct ManipulatorContext( + ManipulatorBatch PlayerUpdate +); + +/// +/// +/// +internal sealed class ManipulatorManager : ModSystem +{ + private static bool hasAppliedEdits; + private static event Action? WrappedApplyEdits; + + public static event Action 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); + } +} diff --git a/ILEditing/MechanicILChanges.cs b/ILEditing/MechanicILChanges.cs index c3dee41605..b5170c214c 100644 --- a/ILEditing/MechanicILChanges.cs +++ b/ILEditing/MechanicILChanges.cs @@ -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; diff --git a/ILEditing/VanillaBugfixingILChanges.cs b/ILEditing/VanillaBugfixingILChanges.cs index 0eeb2173ce..ec2e4b5902 100644 --- a/ILEditing/VanillaBugfixingILChanges.cs +++ b/ILEditing/VanillaBugfixingILChanges.cs @@ -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, diff --git a/ILEditing/WorldgenILChanges.cs b/ILEditing/WorldgenILChanges.cs index 0d58e9ade5..6a11c74bec 100644 --- a/ILEditing/WorldgenILChanges.cs +++ b/ILEditing/WorldgenILChanges.cs @@ -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); diff --git a/Systems/Graphic/LiquidSystem/ModLavaStyleSystem.cs b/Systems/Graphic/LiquidSystem/ModLavaStyleSystem.cs index 476f0a6ad2..9856b20c57 100644 --- a/Systems/Graphic/LiquidSystem/ModLavaStyleSystem.cs +++ b/Systems/Graphic/LiquidSystem/ModLavaStyleSystem.cs @@ -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; diff --git a/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_ILEdit.cs b/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_ILEdit.cs index adb5396756..3d799d1fc7 100644 --- a/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_ILEdit.cs +++ b/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_ILEdit.cs @@ -22,18 +22,13 @@ namespace CalamityMod.Systems.Graphic.LiquidSystem { private static readonly MethodInfo Tex2DAssetGetter = typeof(Asset).GetProperty(nameof(Asset.Value)).GetMethod; - private void LoadILEdits() + private static void ApplyEdits(ManipulatorContext ctx) { if (Tex2DAssetGetter == null) { 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; @@ -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