From ea9bf162ea82e0043ee33412fd8484cce8b36a05 Mon Sep 17 00:00:00 2001 From: XtraCube <72575280+XtraCube@users.noreply.github.com> Date: Sat, 29 Mar 2025 13:12:12 -0400 Subject: [PATCH] rework modifier component caching --- MiraAPI/Modifiers/ModifierComponent.cs | 29 ++++++++++++----- MiraAPI/Patches/PlayerControlPatches.cs | 41 ++++--------------------- 2 files changed, 28 insertions(+), 42 deletions(-) diff --git a/MiraAPI/Modifiers/ModifierComponent.cs b/MiraAPI/Modifiers/ModifierComponent.cs index 53a8ac4..f8859e3 100644 --- a/MiraAPI/Modifiers/ModifierComponent.cs +++ b/MiraAPI/Modifiers/ModifierComponent.cs @@ -29,9 +29,9 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr) [HideFromIl2Cpp] private List Modifiers { get; set; } = []; - private PlayerControl? _player; + private PlayerControl _player = null!; - private TextMeshPro? _modifierText; + private TextMeshPro _modifierText = null!; private readonly List _toRemove = []; @@ -42,9 +42,18 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr) _toRemove.AddRange(Modifiers); } - private void Start() + private void Awake() { _player = GetComponent(); + if (_player == null) + { + Logger.Error("ModifierComponent could not find PlayerControl on the same GameObject."); + Destroy(this); + return; + } + + ModifierExtensions.ModifierComponents.Add(_player, this); + Modifiers = []; if (!_player.AmOwner) @@ -78,9 +87,9 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr) if (_toAdd.Count > 0 || _toRemove.Count > 0) { - if (_player?.AmOwner == true) + if (_player.AmOwner && HudManager.InstanceExists) { - HudManager.Instance?.SetHudActive(_player, _player.Data.Role, HudManager.Instance.TaskPanel.isActiveAndEnabled); + HudManager.Instance.SetHudActive(_player, _player.Data.Role, HudManager.Instance.TaskPanel.isActiveAndEnabled); } _toAdd.Clear(); _toRemove.Clear(); @@ -101,7 +110,7 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr) modifier.Update(); } - if (!_modifierText || _player?.AmOwner == false) + if (!_modifierText || !_player.AmOwner) { return; } @@ -125,7 +134,13 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr) builder.AppendLine(modifier.GetHudString()); } - _modifierText!.text = showText ? builder.ToString() : string.Empty; + _modifierText.text = showText ? builder.ToString() : string.Empty; + } + + private void OnDestroy() + { + // Remove this component from the global list when destroyed + ModifierExtensions.ModifierComponents.Remove(_player); } /// diff --git a/MiraAPI/Patches/PlayerControlPatches.cs b/MiraAPI/Patches/PlayerControlPatches.cs index feb2921..0f53f9f 100644 --- a/MiraAPI/Patches/PlayerControlPatches.cs +++ b/MiraAPI/Patches/PlayerControlPatches.cs @@ -15,14 +15,11 @@ namespace MiraAPI.Patches; /// General patches for the PlayerControl class. /// [HarmonyPatch(typeof(PlayerControl))] -public static class PlayerControlPatches +internal static class PlayerControlPatches { - /// - /// Adds the modifier component to the player on start. - /// - /// PlayerControl instance. [HarmonyPostfix] [HarmonyPatch(nameof(PlayerControl.Start))] + // ReSharper disable once InconsistentNaming public static void PlayerControlStartPostfix(PlayerControl __instance) { if (__instance.gameObject.TryGetComponent(out var modifierComp)) @@ -39,12 +36,9 @@ public static class PlayerControlPatches __instance.gameObject.AddComponent(); } - /// - /// Calls the OnDeath method for all active modifiers. - /// - /// PlayerControl instance. [HarmonyPostfix] [HarmonyPatch(nameof(PlayerControl.Die))] + // ReSharper disable once InconsistentNaming public static void PlayerControlDiePostfix(PlayerControl __instance, DeathReason reason) { var deathEvent = new PlayerDeathEvent(__instance, reason); @@ -58,13 +52,9 @@ public static class PlayerControlPatches } } - /// - /// Used to trigger the . - /// - /// PlayerControl instance. - /// The task id. [HarmonyPostfix] [HarmonyPatch(nameof(PlayerControl.CompleteTask))] + // ReSharper disable once InconsistentNaming public static void PlayerCompleteTaskPostfix(PlayerControl __instance, uint idx) { var playerTask = __instance.myTasks.ToArray().First(playerTask => playerTask.Id == idx); @@ -75,14 +65,9 @@ public static class PlayerControlPatches } } - /// - /// Used to trigger the . - /// - /// The source player. - /// The target. - /// Whether the kill succeeded. [HarmonyPrefix] [HarmonyPatch(nameof(PlayerControl.RpcMurderPlayer))] + // ReSharper disable once InconsistentNaming public static void PlayerControlMurderPrefix(PlayerControl __instance, PlayerControl target, ref bool didSucceed) { var beforeMurderEvent = new BeforeMurderEvent(__instance, target); @@ -91,12 +76,9 @@ public static class PlayerControlPatches didSucceed = beforeMurderEvent.IsCancelled; } - /// - /// FixedUpdate handler for custom roles and custom buttons. - /// - /// PlayerControl instance. [HarmonyPostfix] [HarmonyPatch(nameof(PlayerControl.FixedUpdate))] + // ReSharper disable once InconsistentNaming public static void PlayerControlFixedUpdatePostfix(PlayerControl __instance) { if (__instance.Data?.Role is ICustomRole customRole) @@ -124,15 +106,4 @@ public static class PlayerControlPatches button.FixedUpdateHandler(__instance); } } - - /// - /// Clear from modifier component cache. - /// - /// PlayerControl instance. - [HarmonyPrefix] - [HarmonyPatch(nameof(PlayerControl.OnDestroy))] - public static void PlayerControlOnDestroyPrefix(PlayerControl __instance) - { - ModifierExtensions.ModifierComponents.Remove(__instance); - } }