diff --git a/BadAnimatorFix/BadAnimatorFixManager.cs b/BadAnimatorFix/BadAnimatorFixManager.cs index 41bc8bf..df307e2 100644 --- a/BadAnimatorFix/BadAnimatorFixManager.cs +++ b/BadAnimatorFix/BadAnimatorFixManager.cs @@ -6,20 +6,20 @@ namespace NAK.BadAnimatorFix; public static class BadAnimatorFixManager { - private static List badAnimatorFixes = new List(); - private static int currentIndex = 0; - private static float checkInterval = 5f; + static List _animatorFixers = new List(); + static int _currentIndex = 0; + static float _checkInterval = 5f; public static void Add(BadAnimatorFixer bad) { - if (!badAnimatorFixes.Contains(bad)) - badAnimatorFixes.Add(bad); + if (!_animatorFixers.Contains(bad)) + _animatorFixers.Add(bad); } public static void Remove(BadAnimatorFixer bad) { - if (badAnimatorFixes.Contains(bad)) - badAnimatorFixes.Remove(bad); + if (_animatorFixers.Contains(bad)) + _animatorFixers.Remove(bad); } public static void OnPlayerLoaded() @@ -43,25 +43,25 @@ public static class BadAnimatorFixManager } } - private static void CheckNextAnimator() - { - if (badAnimatorFixes.Count == 0) return; - currentIndex = (currentIndex + 1) % badAnimatorFixes.Count; - - BadAnimatorFixer currentAnimatorFix = badAnimatorFixes[currentIndex]; - currentAnimatorFix.AttemptRewindAnimator(); - } - public static void ToggleJob(bool enable) { - var job = SchedulerSystem.Instance.activeJobs.FirstOrDefault(pair => pair.Job.Method.Name == "CheckNextAnimator").Job; + var job = SchedulerSystem.Instance.activeJobs.FirstOrDefault(pair => pair.Job.Method.Name == nameof(CheckNextAnimator)).Job; if (enable && job == null) { - SchedulerSystem.AddJob(new SchedulerSystem.Job(CheckNextAnimator), 0f, checkInterval, -1); + SchedulerSystem.AddJob(new SchedulerSystem.Job(CheckNextAnimator), 0f, _checkInterval, -1); } else if (!enable && job != null) { SchedulerSystem.RemoveJob(job); } } + + static void CheckNextAnimator() + { + if (_animatorFixers.Count == 0) return; + _currentIndex = (_currentIndex + 1) % _animatorFixers.Count; + + BadAnimatorFixer currentAnimatorFix = _animatorFixers[_currentIndex]; + currentAnimatorFix.AttemptRewindAnimator(); + } } diff --git a/BadAnimatorFix/BadAnimatorFixer.cs b/BadAnimatorFix/BadAnimatorFixer.cs index 4bcbe7e..9b43036 100644 --- a/BadAnimatorFix/BadAnimatorFixer.cs +++ b/BadAnimatorFix/BadAnimatorFixer.cs @@ -9,18 +9,17 @@ public class BadAnimatorFixer : MonoBehaviour private Animator animator; - private void Start() + void Start() { animator = GetComponent(); } - private void OnEnable() => BadAnimatorFixManager.Add(this); - private void OnDisable() => BadAnimatorFixManager.Remove(this); + void OnEnable() => BadAnimatorFixManager.Add(this); + void OnDisable() => BadAnimatorFixManager.Remove(this); public void AttemptRewindAnimator() { bool rewound = false; - if (animator != null && animator.isActiveAndEnabled) { for (int layerIndex = 0; layerIndex < animator.layerCount; layerIndex++) @@ -38,9 +37,7 @@ public class BadAnimatorFixer : MonoBehaviour } if (rewound) - { PlayableExtensions.SetTime(animator.playableGraph.GetRootPlayable(0), 0); - } } if (BadAnimatorFix.EntryLogging.Value) diff --git a/BadAnimatorFix/HarmonyPatches.cs b/BadAnimatorFix/HarmonyPatches.cs index 3d620cf..bb3d645 100644 --- a/BadAnimatorFix/HarmonyPatches.cs +++ b/BadAnimatorFix/HarmonyPatches.cs @@ -6,17 +6,17 @@ using UnityEngine; namespace NAK.BadAnimatorFix.HarmonyPatches; -internal static class AnimatorPatches +static class AnimatorPatches { [HarmonyPostfix] - [HarmonyPatch(typeof(PlayerSetup), "Start")] + [HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.Start))] private static void Postfix_PlayerSetup_Start() { BadAnimatorFixManager.OnPlayerLoaded(); } [HarmonyPostfix] - [HarmonyPatch(typeof(CVRAvatar), "Start")] + [HarmonyPatch(typeof(CVRAvatar), nameof(CVRAvatar.Start))] private static void Postfix_CVRAvatar_Start(CVRAvatar __instance) { if (!BadAnimatorFix.EntryCVRAvatar.Value) return; @@ -24,7 +24,7 @@ internal static class AnimatorPatches } [HarmonyPostfix] - [HarmonyPatch(typeof(CVRSpawnable), "Start")] + [HarmonyPatch(typeof(CVRSpawnable), nameof(CVRSpawnable.Start))] private static void Postfix_CVRSpawnable_Start(CVRSpawnable __instance) { if (!BadAnimatorFix.EntryCVRSpawnable.Value) return; @@ -33,7 +33,7 @@ internal static class AnimatorPatches // Set QM stuff [HarmonyPostfix] - [HarmonyPatch(typeof(CVR_MenuManager), "Start")] + [HarmonyPatch(typeof(CVR_MenuManager), nameof(CVR_MenuManager.Start))] private static void Postfix_CVR_MenuManager_Start(ref CVR_MenuManager __instance) { if (!BadAnimatorFix.EntryMenus.Value) return; @@ -42,7 +42,7 @@ internal static class AnimatorPatches // Set MM stuff [HarmonyPostfix] - [HarmonyPatch(typeof(ViewManager), "Start")] + [HarmonyPatch(typeof(ViewManager), nameof(ViewManager.Start))] private static void Postfix_ViewManager_Start(ref ViewManager __instance) { if (!BadAnimatorFix.EntryMenus.Value) return; diff --git a/BadAnimatorFix/Main.cs b/BadAnimatorFix/Main.cs index bb70438..9c17481 100644 --- a/BadAnimatorFix/Main.cs +++ b/BadAnimatorFix/Main.cs @@ -6,46 +6,48 @@ public class BadAnimatorFix : MelonMod { internal static MelonLogger.Instance Logger; - public static readonly MelonPreferences_Category CategoryBadAnimatorFix = + public static readonly MelonPreferences_Category Category = MelonPreferences.CreateCategory(nameof(BadAnimatorFix)); public static readonly MelonPreferences_Entry EntryEnabled = - CategoryBadAnimatorFix.CreateEntry("Enabled", true, description: "Toggle BadAnimatorFix entirely. Requires avatar/spawnable/world reload."); + Category.CreateEntry("Enabled", true, description: "Toggle BadAnimatorFix entirely. Requires avatar/spawnable/world reload."); public static readonly MelonPreferences_Entry EntryCVRAvatar = - CategoryBadAnimatorFix.CreateEntry("Add to CVRAvatar", true, description: "Should BadAnimatorFix run for CVRAvatar? Requires avatar reload."); + Category.CreateEntry("Add to CVRAvatar", true, description: "Should BadAnimatorFix run for CVRAvatar? Requires avatar reload."); public static readonly MelonPreferences_Entry EntryCVRSpawnable = - CategoryBadAnimatorFix.CreateEntry("Add to CVRSpawnable", true, description: "Should BadAnimatorFix run for CVRSpawnable? Requires spawnable reload."); + Category.CreateEntry("Add to CVRSpawnable", true, description: "Should BadAnimatorFix run for CVRSpawnable? Requires spawnable reload."); public static readonly MelonPreferences_Entry EntryCVRWorld = - CategoryBadAnimatorFix.CreateEntry("Add to CVRWorld", true, description: "Should BadAnimatorFix run for CVRWorld? Requires world reload."); + Category.CreateEntry("Add to CVRWorld", true, description: "Should BadAnimatorFix run for CVRWorld? Requires world reload."); public static readonly MelonPreferences_Entry EntryMenus = - CategoryBadAnimatorFix.CreateEntry("Add to Menus", true, description: "Should BadAnimatorFix run for QM & MM? Requires game restart."); + Category.CreateEntry("Add to Menus", true, description: "Should BadAnimatorFix run for QM & MM? Requires game restart."); public static readonly MelonPreferences_Entry EntryLogging = - CategoryBadAnimatorFix.CreateEntry("Debugging", false, description: "Toggle to log each rewind if successful. Only needed for debugging."); + Category.CreateEntry("Debugging", false, description: "Toggle to log each rewind if successful. Only needed for debugging."); public override void OnInitializeMelon() { Logger = LoggerInstance; - EntryEnabled.OnEntryValueChangedUntyped.Subscribe(OnEnabled); + EntryEnabled.OnEntryValueChanged.Subscribe(OnEntryEnabledChanged); ApplyPatches(typeof(HarmonyPatches.AnimatorPatches)); } public override void OnSceneWasInitialized(int buildIndex, string sceneName) { if (!EntryCVRWorld.Value) return; - BadAnimatorFixManager.OnSceneInitialized(sceneName); + + if (buildIndex < 0) // -1 is custom world: 0 to 3 is game login, init, hq + BadAnimatorFixManager.OnSceneInitialized(sceneName); } - private void OnEnabled(object arg1, object arg2) + void OnEntryEnabledChanged(bool newValue, bool oldValue) { - BadAnimatorFixManager.ToggleJob(EntryEnabled.Value); + BadAnimatorFixManager.ToggleJob(newValue); } - private void ApplyPatches(Type type) + void ApplyPatches(Type type) { try { diff --git a/BadAnimatorFix/Properties/AssemblyInfo.cs b/BadAnimatorFix/Properties/AssemblyInfo.cs index 080e016..e441e76 100644 --- a/BadAnimatorFix/Properties/AssemblyInfo.cs +++ b/BadAnimatorFix/Properties/AssemblyInfo.cs @@ -10,7 +10,7 @@ using System.Reflection; [assembly: AssemblyProduct(nameof(NAK.BadAnimatorFix))] [assembly: MelonInfo( - typeof(NAK.BadAnimatorFix.BadAnimatorFixer), + typeof(NAK.BadAnimatorFix.BadAnimatorFix), nameof(NAK.BadAnimatorFix), AssemblyInfoParams.Version, AssemblyInfoParams.Author, @@ -25,6 +25,6 @@ using System.Reflection; namespace NAK.BadAnimatorFix.Properties; internal static class AssemblyInfoParams { - public const string Version = "1.0.0"; + public const string Version = "1.0.1"; public const string Author = "NotAKidoS"; } \ No newline at end of file diff --git a/BadAnimatorFix/format.json b/BadAnimatorFix/format.json index bb18a39..df9b753 100644 --- a/BadAnimatorFix/format.json +++ b/BadAnimatorFix/format.json @@ -1,9 +1,9 @@ { - "_id": -1, + "_id": 152, "name": "BadAnimatorFix", - "modversion": "1.0.0", + "modversion": "1.0.1", "gameversion": "2022r170", - "loaderversion": "0.5.7", + "loaderversion": "0.6.1", "modtype": "Mod", "author": "NotAKidoS", "description": "This mod occasionally rewinds animation states that have loop enabled.\n\nUnity seems to have a weird quirk where *sometimes* animations with loop cause performance issues after running for a long time.\nYou'll only start to notice this after a few hours to a few days of idling.\n\nIf you don't happen to be AFK for long periods of time, you probably don't need this mod. This issue seems to be primarily caused by one-two frame animation clips meant for toggles with loop needlessly enabled.", @@ -16,8 +16,8 @@ "requirements": [ "None" ], - "downloadlink": "https://github.com/NotAKidOnSteam/NAK_CVR_Mods/releases/download/r3/BadAnimatorFix.dll", + "downloadlink": "https://github.com/NotAKidOnSteam/NAK_CVR_Mods/releases/download/r10/BadAnimatorFix.dll", "sourcelink": "https://github.com/NotAKidOnSteam/NAK_CVR_Mods/tree/main/BadAnimatorFix/", - "changelog": "Initial Release", - "embedcolor": "7F3F99" + "changelog": "- Speculative fix for possible error while loading default cvr scenes", + "embedcolor": "e25352" } \ No newline at end of file