From 9cc62a961cfdcff1998b18515b42061417a4242c Mon Sep 17 00:00:00 2001 From: NotAKidoS <37721153+NotAKidOnSteam@users.noreply.github.com> Date: Fri, 24 Mar 2023 19:50:57 -0500 Subject: [PATCH] touch --- BadAnimatorFix/BadAnimatorFix.cs | 50 +++++++++++++++----------------- BadAnimatorFix/HarmonyPatches.cs | 7 +++-- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/BadAnimatorFix/BadAnimatorFix.cs b/BadAnimatorFix/BadAnimatorFix.cs index e021481..7d47f5f 100644 --- a/BadAnimatorFix/BadAnimatorFix.cs +++ b/BadAnimatorFix/BadAnimatorFix.cs @@ -19,39 +19,37 @@ public class BadAnimatorFix : MonoBehaviour public void AttemptRewindAnimator() { - if (animator == null) return; - bool rewound = false; - for (int layerIndex = 0; layerIndex < animator.layerCount; layerIndex++) + + if (animator != null && animator.isActiveAndEnabled) { - AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(layerIndex); - AnimatorTransitionInfo transitionInfo = animator.GetAnimatorTransitionInfo(layerIndex); - - bool shouldSkipState = !stateInfo.loop || transitionInfo.fullPathHash != 0; - if (shouldSkipState) continue; - - bool shouldRewindState = stateInfo.normalizedTime >= StateLimit; - if (shouldRewindState) + for (int layerIndex = 0; layerIndex < animator.layerCount; layerIndex++) { - float rewindOffset = (stateInfo.normalizedTime % 1f) + 10f; - animator.Play(stateInfo.fullPathHash, layerIndex, rewindOffset); - rewound = true; + AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(layerIndex); + AnimatorTransitionInfo transitionInfo = animator.GetAnimatorTransitionInfo(layerIndex); + + // Skip if state doesn't loop or if mid-transition + if (!stateInfo.loop || transitionInfo.fullPathHash != 0) continue; + + // Skip if state hasn't looped enough + if (stateInfo.normalizedTime > StateLimit) + { + float rewindOffset = (stateInfo.normalizedTime % 1f) + 10f; + animator.Play(stateInfo.fullPathHash, layerIndex, rewindOffset); + rewound = true; + } + } + + if (rewound) + { + PlayableExtensions.SetTime(animator.playableGraph.GetRootPlayable(0), 0); } } - if (rewound) + if (BadAnimatorFixMod.EntryLogging.Value) { - var rootPlayable = animator.playableGraph.GetRootPlayable(0); - PlayableExtensions.SetTime(rootPlayable, 0); - - if (BadAnimatorFixMod.EntryLogging.Value) - { - BadAnimatorFixMod.Logger.Msg($"Rewound animator and playable {animator}."); - } - } - else if (BadAnimatorFixMod.EntryLogging.Value) - { - BadAnimatorFixMod.Logger.Msg($"Animator did not meet criteria to rewind {animator}."); + string message = rewound ? $"Rewound animator and playable {animator}." : $"Animator did not meet criteria to rewind {animator}."; + BadAnimatorFixMod.Logger.Msg(message); } } } \ No newline at end of file diff --git a/BadAnimatorFix/HarmonyPatches.cs b/BadAnimatorFix/HarmonyPatches.cs index 293aa0c..617e773 100644 --- a/BadAnimatorFix/HarmonyPatches.cs +++ b/BadAnimatorFix/HarmonyPatches.cs @@ -1,8 +1,8 @@ using ABI.CCK.Components; using ABI_RC.Core.InteractionSystem; +using ABI_RC.Core.Player; using HarmonyLib; using UnityEngine; -using ABI_RC.Core.Player; namespace NAK.Melons.BadAnimatorFix.HarmonyPatches; @@ -54,9 +54,10 @@ internal static class AnimatorPatches Animator[] animators = gameObject.GetComponentsInChildren(true); foreach (Animator animator in animators) { - if (animator.gameObject.GetComponent() != null) continue; - if (animator.runtimeAnimatorController != null) + if (!animator.TryGetComponent(out _)) + { animator.gameObject.AddComponent(); + } } } }