This commit is contained in:
NotAKidoS 2023-03-24 19:50:57 -05:00
parent 7b5ad71060
commit 9cc62a961c
2 changed files with 28 additions and 29 deletions

View file

@ -19,19 +19,20 @@ public class BadAnimatorFix : MonoBehaviour
public void AttemptRewindAnimator() public void AttemptRewindAnimator()
{ {
if (animator == null) return;
bool rewound = false; bool rewound = false;
if (animator != null && animator.isActiveAndEnabled)
{
for (int layerIndex = 0; layerIndex < animator.layerCount; layerIndex++) for (int layerIndex = 0; layerIndex < animator.layerCount; layerIndex++)
{ {
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(layerIndex); AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(layerIndex);
AnimatorTransitionInfo transitionInfo = animator.GetAnimatorTransitionInfo(layerIndex); AnimatorTransitionInfo transitionInfo = animator.GetAnimatorTransitionInfo(layerIndex);
bool shouldSkipState = !stateInfo.loop || transitionInfo.fullPathHash != 0; // Skip if state doesn't loop or if mid-transition
if (shouldSkipState) continue; if (!stateInfo.loop || transitionInfo.fullPathHash != 0) continue;
bool shouldRewindState = stateInfo.normalizedTime >= StateLimit; // Skip if state hasn't looped enough
if (shouldRewindState) if (stateInfo.normalizedTime > StateLimit)
{ {
float rewindOffset = (stateInfo.normalizedTime % 1f) + 10f; float rewindOffset = (stateInfo.normalizedTime % 1f) + 10f;
animator.Play(stateInfo.fullPathHash, layerIndex, rewindOffset); animator.Play(stateInfo.fullPathHash, layerIndex, rewindOffset);
@ -41,17 +42,14 @@ public class BadAnimatorFix : MonoBehaviour
if (rewound) if (rewound)
{ {
var rootPlayable = animator.playableGraph.GetRootPlayable(0); PlayableExtensions.SetTime<Playable>(animator.playableGraph.GetRootPlayable(0), 0);
PlayableExtensions.SetTime<Playable>(rootPlayable, 0); }
}
if (BadAnimatorFixMod.EntryLogging.Value) if (BadAnimatorFixMod.EntryLogging.Value)
{ {
BadAnimatorFixMod.Logger.Msg($"Rewound animator and playable {animator}."); string message = rewound ? $"Rewound animator and playable {animator}." : $"Animator did not meet criteria to rewind {animator}.";
} BadAnimatorFixMod.Logger.Msg(message);
}
else if (BadAnimatorFixMod.EntryLogging.Value)
{
BadAnimatorFixMod.Logger.Msg($"Animator did not meet criteria to rewind {animator}.");
} }
} }
} }

View file

@ -1,8 +1,8 @@
using ABI.CCK.Components; using ABI.CCK.Components;
using ABI_RC.Core.InteractionSystem; using ABI_RC.Core.InteractionSystem;
using ABI_RC.Core.Player;
using HarmonyLib; using HarmonyLib;
using UnityEngine; using UnityEngine;
using ABI_RC.Core.Player;
namespace NAK.Melons.BadAnimatorFix.HarmonyPatches; namespace NAK.Melons.BadAnimatorFix.HarmonyPatches;
@ -54,9 +54,10 @@ internal static class AnimatorPatches
Animator[] animators = gameObject.GetComponentsInChildren<Animator>(true); Animator[] animators = gameObject.GetComponentsInChildren<Animator>(true);
foreach (Animator animator in animators) foreach (Animator animator in animators)
{ {
if (animator.gameObject.GetComponent<BadAnimatorFix>() != null) continue; if (!animator.TryGetComponent<BadAnimatorFix>(out _))
if (animator.runtimeAnimatorController != null) {
animator.gameObject.AddComponent<BadAnimatorFix>(); animator.gameObject.AddComponent<BadAnimatorFix>();
} }
} }
} }
}