From 1acf58d16143c9f004703bb158d050de2fd04a30 Mon Sep 17 00:00:00 2001 From: NotAKidoS <37721153+NotAKidOnSteam@users.noreply.github.com> Date: Fri, 24 Mar 2023 17:17:53 -0500 Subject: [PATCH] idk --- BadAnimatorFix/BadAnimatorFix.cs | 47 ++++++--------------- BadAnimatorFix/BadAnimatorFix.csproj | 3 +- BadAnimatorFix/BadAnimatorFixManager.cs | 54 +++++++++++++++++++++++++ BadAnimatorFix/HarmonyPatches.cs | 6 +-- BadAnimatorFix/Main.cs | 16 ++++++++ 5 files changed, 88 insertions(+), 38 deletions(-) create mode 100644 BadAnimatorFix/BadAnimatorFixManager.cs diff --git a/BadAnimatorFix/BadAnimatorFix.cs b/BadAnimatorFix/BadAnimatorFix.cs index 15a4bb2..5fb5e76 100644 --- a/BadAnimatorFix/BadAnimatorFix.cs +++ b/BadAnimatorFix/BadAnimatorFix.cs @@ -5,34 +5,30 @@ namespace NAK.Melons.BadAnimatorFix; public class BadAnimatorFix : MonoBehaviour { - private float stateLimit = 20f; + private float stateLimit = 50f; private Animator animator; private Playable playable; - private void Start() + void Start() { animator = GetComponent(); playable = animator.playableGraph.GetRootPlayable(0); + BadAnimatorFixManager.Add(this); } - private void Update() + void OnDestroy() { - if (!BadAnimatorFixMod.EntryEnabled.Value) return; - if (playable.IsValid() && GetTime() > BadAnimatorFixMod.EntryPlayableTimeLimit.Value) - { - RewindAnimator(); - BadAnimatorFixMod.Logger.Msg($"Rewound animator and playable {animator}."); - } + BadAnimatorFixManager.Remove(this); } - private double GetTime() + public double GetTime() { return PlayableExtensions.IsValid(playable) ? PlayableExtensions.GetTime(playable) : -1; } - private void RewindAnimator() + public void AttemptRewindAnimator() { - PlayableExtensions.SetTime(playable, 0); + bool rewound = false; for (int i = 0; i < animator.layerCount; i++) { AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(i); @@ -42,31 +38,14 @@ public class BadAnimatorFix : MonoBehaviour // Skip if anim doesn't loop, or hasn't looped enough if (stateInfo.normalizedTime <= stateLimit) continue; // Rewind state, with 10f as buffer, to account for reasonable use of ExitTime + rewound = true; float offset = 10f + (stateInfo.normalizedTime % 1f); animator.Play(stateInfo.fullPathHash, i, offset); } - } - - private float GetNormalizedTime() - { - float time = 0f; - for (int i = 0; i < animator.layerCount; i++) + if (rewound) { - AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(i); - time += stateInfo.normalizedTime; + PlayableExtensions.SetTime(playable, 0); + BadAnimatorFixMod.Logger.Msg($"Rewound animator and playable {animator}."); } - return time; } - - private float GetMaxNormalizedTime() - { - float time = 0f; - for (int i = 0; i < animator.layerCount; i++) - { - AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(i); - if (time < stateInfo.normalizedTime) - time = stateInfo.normalizedTime; - } - return time; - } -} +} \ No newline at end of file diff --git a/BadAnimatorFix/BadAnimatorFix.csproj b/BadAnimatorFix/BadAnimatorFix.csproj index e4c8455..040f29a 100644 --- a/BadAnimatorFix/BadAnimatorFix.csproj +++ b/BadAnimatorFix/BadAnimatorFix.csproj @@ -6,6 +6,7 @@ enable latest false + True @@ -13,7 +14,7 @@ C:\Program Files (x86)\Steam\steamapps\common\ChilloutVR\MelonLoader\0Harmony.dll - C:\Program Files (x86)\Steam\steamapps\common\ChilloutVR\ChilloutVR_Data\Managed\Assembly-CSharp.dll + ..\..\FuckCohtml\FuckMetrics\ManagedLibs\Assembly-CSharp.dll C:\Program Files (x86)\Steam\steamapps\common\ChilloutVR\ChilloutVR_Data\Managed\Assembly-CSharp-firstpass.dll diff --git a/BadAnimatorFix/BadAnimatorFixManager.cs b/BadAnimatorFix/BadAnimatorFixManager.cs new file mode 100644 index 0000000..b4c4d66 --- /dev/null +++ b/BadAnimatorFix/BadAnimatorFixManager.cs @@ -0,0 +1,54 @@ +using ABI_RC.Core.IO; + +namespace NAK.Melons.BadAnimatorFix; + +public static class BadAnimatorFixManager +{ + public static List badAnimatorFixes = new List(); + private static int currentIndex = 0; + + public static void Add(BadAnimatorFix bad) + { + if (!badAnimatorFixes.Contains(bad)) + { + badAnimatorFixes.Add(bad); + } + } + + public static void Remove(BadAnimatorFix bad) + { + if (badAnimatorFixes.Contains(bad)) + { + badAnimatorFixes.Remove(bad); + } + } + + // Runs every 5 seconds to see if an animator has played longer than PlayableTimeLimit + public static void CheckNextAnimator() + { + if (badAnimatorFixes.Count == 0) return; + + if (currentIndex >= badAnimatorFixes.Count) currentIndex = 0; + + BadAnimatorFix currentAnimatorFix = badAnimatorFixes[currentIndex]; + if (currentAnimatorFix.GetTime() > BadAnimatorFixMod.EntryPlayableTimeLimit.Value) + { + currentAnimatorFix.AttemptRewindAnimator(); + } + + currentIndex++; + } + + public static void ToggleJob(bool enable) + { + var job = SchedulerSystem.Instance.activeJobs.FirstOrDefault(pair => pair.Job.Method.Name == "CheckNextAnimator").Job; + if (enable && job == null) + { + SchedulerSystem.AddJob(new SchedulerSystem.Job(BadAnimatorFixManager.CheckNextAnimator), 0f, 5f, -1); + } + else if (!enable && job != null) + { + SchedulerSystem.RemoveJob(job); + } + } +} diff --git a/BadAnimatorFix/HarmonyPatches.cs b/BadAnimatorFix/HarmonyPatches.cs index 11bac03..c281f64 100644 --- a/BadAnimatorFix/HarmonyPatches.cs +++ b/BadAnimatorFix/HarmonyPatches.cs @@ -2,6 +2,7 @@ using ABI_RC.Core.InteractionSystem; using HarmonyLib; using UnityEngine; +using ABI_RC.Core.IO; namespace NAK.Melons.BadAnimatorFix.HarmonyPatches; @@ -51,7 +52,7 @@ internal class AnimatorPatches private static void AddBadAnimatorFixComponentIfAnimatorExists(GameObject gameObject) { - if (!BadAnimatorFixMod.EntryEnabled.Value) return; + //if (!BadAnimatorFixMod.EntryEnabled.Value) return; Animator[] animators = gameObject.GetComponentsInChildren(); foreach (Animator animator in animators.Where(a => a.gameObject.GetComponent() == null)) { @@ -59,5 +60,4 @@ internal class AnimatorPatches animator.gameObject.AddComponent(); } } -} - +} \ No newline at end of file diff --git a/BadAnimatorFix/Main.cs b/BadAnimatorFix/Main.cs index 14463ac..eba99a7 100644 --- a/BadAnimatorFix/Main.cs +++ b/BadAnimatorFix/Main.cs @@ -1,4 +1,6 @@ using MelonLoader; +using System.Collections; +using ABI_RC.Core.Player; namespace NAK.Melons.BadAnimatorFix; @@ -29,7 +31,21 @@ public class BadAnimatorFixMod : MelonMod public override void OnInitializeMelon() { Logger = LoggerInstance; + EntryEnabled.OnEntryValueChangedUntyped.Subscribe(OnEnabled); ApplyPatches(typeof(HarmonyPatches.AnimatorPatches)); + MelonCoroutines.Start(WaitForLocalPlayer()); + } + + private IEnumerator WaitForLocalPlayer() + { + while (PlayerSetup.Instance == null) + yield return null; + BadAnimatorFixManager.ToggleJob(EntryEnabled.Value); + } + + private void OnEnabled(object arg1, object arg2) + { + BadAnimatorFixManager.ToggleJob(EntryEnabled.Value); } private void ApplyPatches(Type type)