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,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<Playable>(animator.playableGraph.GetRootPlayable(0), 0);
}
}
if (rewound)
if (BadAnimatorFixMod.EntryLogging.Value)
{
var rootPlayable = animator.playableGraph.GetRootPlayable(0);
PlayableExtensions.SetTime<Playable>(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);
}
}
}

View file

@ -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<Animator>(true);
foreach (Animator animator in animators)
{
if (animator.gameObject.GetComponent<BadAnimatorFix>() != null) continue;
if (animator.runtimeAnimatorController != null)
if (!animator.TryGetComponent<BadAnimatorFix>(out _))
{
animator.gameObject.AddComponent<BadAnimatorFix>();
}
}
}
}