mirror of
https://github.com/NotAKidoS/NAK_CVR_Mods.git
synced 2025-09-02 06:19:22 +00:00
not really sure
This commit is contained in:
parent
1acf58d161
commit
38445efae3
5 changed files with 77 additions and 83 deletions
|
@ -2,50 +2,49 @@
|
||||||
using UnityEngine.Playables;
|
using UnityEngine.Playables;
|
||||||
|
|
||||||
namespace NAK.Melons.BadAnimatorFix;
|
namespace NAK.Melons.BadAnimatorFix;
|
||||||
|
|
||||||
public class BadAnimatorFix : MonoBehaviour
|
public class BadAnimatorFix : MonoBehaviour
|
||||||
{
|
{
|
||||||
private float stateLimit = 50f;
|
private const float StateLimit = 20f;
|
||||||
private Animator animator;
|
|
||||||
private Playable playable;
|
|
||||||
|
|
||||||
void Start()
|
private Animator animator;
|
||||||
|
|
||||||
|
private void Start()
|
||||||
{
|
{
|
||||||
animator = GetComponent<Animator>();
|
animator = GetComponent<Animator>();
|
||||||
playable = animator.playableGraph.GetRootPlayable(0);
|
|
||||||
BadAnimatorFixManager.Add(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnDestroy()
|
private void OnEnable() => BadAnimatorFixManager.Add(this);
|
||||||
{
|
private void OnDisable() => BadAnimatorFixManager.Remove(this);
|
||||||
BadAnimatorFixManager.Remove(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public double GetTime()
|
|
||||||
{
|
|
||||||
return PlayableExtensions.IsValid<Playable>(playable) ? PlayableExtensions.GetTime<Playable>(playable) : -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AttemptRewindAnimator()
|
public void AttemptRewindAnimator()
|
||||||
{
|
{
|
||||||
|
if (animator == null) return;
|
||||||
|
|
||||||
bool rewound = false;
|
bool rewound = false;
|
||||||
for (int i = 0; i < animator.layerCount; i++)
|
for (int layerIndex = 0; layerIndex < animator.layerCount; layerIndex++)
|
||||||
{
|
{
|
||||||
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(i);
|
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(layerIndex);
|
||||||
AnimatorTransitionInfo transitionInfo = animator.GetAnimatorTransitionInfo(i);
|
AnimatorTransitionInfo transitionInfo = animator.GetAnimatorTransitionInfo(layerIndex);
|
||||||
// Skip if mid-transition
|
|
||||||
if (transitionInfo.fullPathHash != 0) continue;
|
bool shouldSkipState = !stateInfo.loop || transitionInfo.fullPathHash != 0;
|
||||||
// Skip if anim doesn't loop, or hasn't looped enough
|
if (shouldSkipState) continue;
|
||||||
if (stateInfo.normalizedTime <= stateLimit) continue;
|
|
||||||
// Rewind state, with 10f as buffer, to account for reasonable use of ExitTime
|
bool shouldRewindState = stateInfo.normalizedTime >= StateLimit;
|
||||||
rewound = true;
|
if (shouldRewindState)
|
||||||
float offset = 10f + (stateInfo.normalizedTime % 1f);
|
{
|
||||||
animator.Play(stateInfo.fullPathHash, i, offset);
|
float rewindOffset = (stateInfo.normalizedTime % 1f) + 10f;
|
||||||
|
animator.Play(stateInfo.fullPathHash, layerIndex, rewindOffset);
|
||||||
|
rewound = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rewound)
|
if (rewound)
|
||||||
{
|
{
|
||||||
PlayableExtensions.SetTime<Playable>(playable, 0);
|
var rootPlayable = animator.playableGraph.GetRootPlayable(0);
|
||||||
BadAnimatorFixMod.Logger.Msg($"Rewound animator and playable {animator}.");
|
PlayableExtensions.SetTime<Playable>(rootPlayable, 0);
|
||||||
|
|
||||||
|
if (BadAnimatorFixMod.EntryLogging.Value)
|
||||||
|
BadAnimatorFixMod.Logger.Msg($"Rewound animator and playable {animator}.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,50 +1,58 @@
|
||||||
using ABI_RC.Core.IO;
|
using ABI_RC.Core.IO;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.SceneManagement;
|
||||||
|
|
||||||
namespace NAK.Melons.BadAnimatorFix;
|
namespace NAK.Melons.BadAnimatorFix;
|
||||||
|
|
||||||
public static class BadAnimatorFixManager
|
public static class BadAnimatorFixManager
|
||||||
{
|
{
|
||||||
public static List<BadAnimatorFix> badAnimatorFixes = new List<BadAnimatorFix>();
|
private static List<BadAnimatorFix> badAnimatorFixes = new List<BadAnimatorFix>();
|
||||||
private static int currentIndex = 0;
|
private static int currentIndex = 0;
|
||||||
|
private static float checkInterval = 5f;
|
||||||
|
|
||||||
public static void Add(BadAnimatorFix bad)
|
public static void Add(BadAnimatorFix bad)
|
||||||
{
|
{
|
||||||
if (!badAnimatorFixes.Contains(bad))
|
if (!badAnimatorFixes.Contains(bad))
|
||||||
{
|
|
||||||
badAnimatorFixes.Add(bad);
|
badAnimatorFixes.Add(bad);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Remove(BadAnimatorFix bad)
|
public static void Remove(BadAnimatorFix bad)
|
||||||
{
|
{
|
||||||
if (badAnimatorFixes.Contains(bad))
|
if (badAnimatorFixes.Contains(bad))
|
||||||
{
|
|
||||||
badAnimatorFixes.Remove(bad);
|
badAnimatorFixes.Remove(bad);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void OnSceneInitialized(string sceneName)
|
||||||
|
{
|
||||||
|
// Get all the animators in the loaded world
|
||||||
|
var allAnimators = SceneManager.GetSceneByName(sceneName).GetRootGameObjects()
|
||||||
|
.SelectMany(x => x.GetComponentsInChildren<Animator>(true));
|
||||||
|
|
||||||
|
foreach (var animator in allAnimators)
|
||||||
|
{
|
||||||
|
// Ignore objects that have our "fix", this shouldn't be needed but eh
|
||||||
|
if (!animator.TryGetComponent<BadAnimatorFix>(out _))
|
||||||
|
{
|
||||||
|
animator.gameObject.AddComponent<BadAnimatorFix>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Runs every 5 seconds to see if an animator has played longer than PlayableTimeLimit
|
private static void CheckNextAnimator()
|
||||||
public static void CheckNextAnimator()
|
|
||||||
{
|
{
|
||||||
if (badAnimatorFixes.Count == 0) return;
|
if (badAnimatorFixes.Count == 0) return;
|
||||||
|
currentIndex = (currentIndex + 1) % badAnimatorFixes.Count;
|
||||||
if (currentIndex >= badAnimatorFixes.Count) currentIndex = 0;
|
|
||||||
|
|
||||||
BadAnimatorFix currentAnimatorFix = badAnimatorFixes[currentIndex];
|
BadAnimatorFix currentAnimatorFix = badAnimatorFixes[currentIndex];
|
||||||
if (currentAnimatorFix.GetTime() > BadAnimatorFixMod.EntryPlayableTimeLimit.Value)
|
currentAnimatorFix.AttemptRewindAnimator();
|
||||||
{
|
}
|
||||||
currentAnimatorFix.AttemptRewindAnimator();
|
|
||||||
}
|
|
||||||
|
|
||||||
currentIndex++;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void ToggleJob(bool enable)
|
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 == "CheckNextAnimator").Job;
|
||||||
if (enable && job == null)
|
if (enable && job == null)
|
||||||
{
|
{
|
||||||
SchedulerSystem.AddJob(new SchedulerSystem.Job(BadAnimatorFixManager.CheckNextAnimator), 0f, 5f, -1);
|
SchedulerSystem.AddJob(new SchedulerSystem.Job(CheckNextAnimator), 0f, checkInterval, -1);
|
||||||
}
|
}
|
||||||
else if (!enable && job != null)
|
else if (!enable && job != null)
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,11 +2,10 @@
|
||||||
using ABI_RC.Core.InteractionSystem;
|
using ABI_RC.Core.InteractionSystem;
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using ABI_RC.Core.IO;
|
|
||||||
|
|
||||||
namespace NAK.Melons.BadAnimatorFix.HarmonyPatches;
|
namespace NAK.Melons.BadAnimatorFix.HarmonyPatches;
|
||||||
|
|
||||||
internal class AnimatorPatches
|
internal static class AnimatorPatches
|
||||||
{
|
{
|
||||||
[HarmonyPostfix]
|
[HarmonyPostfix]
|
||||||
[HarmonyPatch(typeof(CVRAvatar), "Start")]
|
[HarmonyPatch(typeof(CVRAvatar), "Start")]
|
||||||
|
@ -24,24 +23,16 @@ internal class AnimatorPatches
|
||||||
AddBadAnimatorFixComponentIfAnimatorExists(__instance.gameObject);
|
AddBadAnimatorFixComponentIfAnimatorExists(__instance.gameObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HarmonyPostfix]
|
// Set QM stuff
|
||||||
[HarmonyPatch(typeof(CVRWorld), "Start")]
|
|
||||||
private static void Post_CVRWorld_Start(CVRWorld __instance)
|
|
||||||
{
|
|
||||||
if (!BadAnimatorFixMod.EntryCVRWorld.Value) return;
|
|
||||||
AddBadAnimatorFixComponentIfAnimatorExists(__instance.gameObject);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Set QM stuff
|
|
||||||
[HarmonyPostfix]
|
[HarmonyPostfix]
|
||||||
[HarmonyPatch(typeof(CVR_MenuManager), "Start")]
|
[HarmonyPatch(typeof(CVR_MenuManager), "Start")]
|
||||||
private static void Postfix_CVR_MenuManager_Start(ref CVR_MenuManager __instance, ref GameObject ____leftVrAnchor)
|
private static void Postfix_CVR_MenuManager_Start(ref CVR_MenuManager __instance)
|
||||||
{
|
{
|
||||||
if (!BadAnimatorFixMod.EntryMenus.Value) return;
|
if (!BadAnimatorFixMod.EntryMenus.Value) return;
|
||||||
AddBadAnimatorFixComponentIfAnimatorExists(__instance.gameObject);
|
AddBadAnimatorFixComponentIfAnimatorExists(__instance.gameObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Set MM stuff
|
// Set MM stuff
|
||||||
[HarmonyPostfix]
|
[HarmonyPostfix]
|
||||||
[HarmonyPatch(typeof(ViewManager), "Start")]
|
[HarmonyPatch(typeof(ViewManager), "Start")]
|
||||||
private static void Postfix_ViewManager_Start(ref ViewManager __instance)
|
private static void Postfix_ViewManager_Start(ref ViewManager __instance)
|
||||||
|
@ -52,12 +43,12 @@ internal class AnimatorPatches
|
||||||
|
|
||||||
private static void AddBadAnimatorFixComponentIfAnimatorExists(GameObject gameObject)
|
private static void AddBadAnimatorFixComponentIfAnimatorExists(GameObject gameObject)
|
||||||
{
|
{
|
||||||
//if (!BadAnimatorFixMod.EntryEnabled.Value) return;
|
Animator[] animators = gameObject.GetComponentsInChildren<Animator>(true);
|
||||||
Animator[] animators = gameObject.GetComponentsInChildren<Animator>();
|
foreach (Animator animator in animators)
|
||||||
foreach (Animator animator in animators.Where(a => a.gameObject.GetComponent<BadAnimatorFix>() == null))
|
|
||||||
{
|
{
|
||||||
|
if (animator.gameObject.GetComponent<BadAnimatorFix>() != null) continue;
|
||||||
if (animator.runtimeAnimatorController != null)
|
if (animator.runtimeAnimatorController != null)
|
||||||
animator.gameObject.AddComponent<BadAnimatorFix>();
|
animator.gameObject.AddComponent<BadAnimatorFix>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
using MelonLoader;
|
using MelonLoader;
|
||||||
using System.Collections;
|
|
||||||
using ABI_RC.Core.Player;
|
|
||||||
|
|
||||||
namespace NAK.Melons.BadAnimatorFix;
|
namespace NAK.Melons.BadAnimatorFix;
|
||||||
|
|
||||||
|
@ -17,30 +15,28 @@ public class BadAnimatorFixMod : MelonMod
|
||||||
CategoryBadAnimatorFix.CreateEntry("Add to CVRAvatar", true, description: "Should BadAnimatorFix run for CVRAvatar? Requires avatar reload.");
|
CategoryBadAnimatorFix.CreateEntry("Add to CVRAvatar", true, description: "Should BadAnimatorFix run for CVRAvatar? Requires avatar reload.");
|
||||||
|
|
||||||
public static readonly MelonPreferences_Entry<bool> EntryCVRSpawnable =
|
public static readonly MelonPreferences_Entry<bool> EntryCVRSpawnable =
|
||||||
CategoryBadAnimatorFix.CreateEntry("Add to CVRSpawnable", false, description: "Should BadAnimatorFix run for CVRSpawnable? Requires spawnable reload.");
|
CategoryBadAnimatorFix.CreateEntry("Add to CVRSpawnable", true, description: "Should BadAnimatorFix run for CVRSpawnable? Requires spawnable reload.");
|
||||||
|
|
||||||
public static readonly MelonPreferences_Entry<bool> EntryCVRWorld =
|
public static readonly MelonPreferences_Entry<bool> EntryCVRWorld =
|
||||||
CategoryBadAnimatorFix.CreateEntry("Add to CVRWorld", false, description: "Should BadAnimatorFix run for CVRWorld? Requires world reload.");
|
CategoryBadAnimatorFix.CreateEntry("Add to CVRWorld", true, description: "Should BadAnimatorFix run for CVRWorld? Requires world reload.");
|
||||||
|
|
||||||
public static readonly MelonPreferences_Entry<bool> EntryMenus =
|
public static readonly MelonPreferences_Entry<bool> EntryMenus =
|
||||||
CategoryBadAnimatorFix.CreateEntry("Add to Menus", false, description: "Should BadAnimatorFix run for QM & MM? Requires game restart.");
|
CategoryBadAnimatorFix.CreateEntry("Add to Menus", true, description: "Should BadAnimatorFix run for QM & MM? Requires game restart.");
|
||||||
|
|
||||||
public static readonly MelonPreferences_Entry<float> EntryPlayableTimeLimit =
|
public static readonly MelonPreferences_Entry<bool> EntryLogging =
|
||||||
CategoryBadAnimatorFix.CreateEntry("Playable Time Limit", 600f, description: "How long in seconds can a Playable play for before rewinding its states.");
|
CategoryBadAnimatorFix.CreateEntry("Logging", false, description: "Toggle to log each rewind if successful. Only needed for debugging.");
|
||||||
|
|
||||||
public override void OnInitializeMelon()
|
public override void OnInitializeMelon()
|
||||||
{
|
{
|
||||||
Logger = LoggerInstance;
|
Logger = LoggerInstance;
|
||||||
EntryEnabled.OnEntryValueChangedUntyped.Subscribe(OnEnabled);
|
EntryEnabled.OnEntryValueChangedUntyped.Subscribe(OnEnabled);
|
||||||
ApplyPatches(typeof(HarmonyPatches.AnimatorPatches));
|
ApplyPatches(typeof(HarmonyPatches.AnimatorPatches));
|
||||||
MelonCoroutines.Start(WaitForLocalPlayer());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerator WaitForLocalPlayer()
|
public override void OnSceneWasInitialized(int buildIndex, string sceneName)
|
||||||
{
|
{
|
||||||
while (PlayerSetup.Instance == null)
|
if (!EntryCVRWorld.Value) return;
|
||||||
yield return null;
|
BadAnimatorFixManager.OnSceneInitialized(sceneName);
|
||||||
BadAnimatorFixManager.ToggleJob(EntryEnabled.Value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnEnabled(object arg1, object arg2)
|
private void OnEnabled(object arg1, object arg2)
|
||||||
|
|
|
@ -1,23 +1,23 @@
|
||||||
{
|
{
|
||||||
"_id": -1,
|
"_id": -1,
|
||||||
"name": "TrackedControllerFix",
|
"name": "BadAnimatorFix",
|
||||||
"modversion": "1.0.0",
|
"modversion": "1.0.0",
|
||||||
"gameversion": "2022r170",
|
"gameversion": "2022r170",
|
||||||
"loaderversion": "0.5.7",
|
"loaderversion": "0.5.7",
|
||||||
"modtype": "Mod",
|
"modtype": "Mod",
|
||||||
"author": "NotAKidoS",
|
"author": "NotAKidoS",
|
||||||
"description": "Allows your controllers to track while the SteamVR overlay is open. This also fixes Quest/Touch controllers feeling slow during fast movements.",
|
"description": "This mod occasionally rewinds animation states that have loop enabled.\n\nUnity seems to have a weird quirk where animations with loop cause performance issues after running for a long long time.\nYou'll only start to notice this after a few hours to a few days of idling.\n\nDisable loop on your 2-frame toggle clips! They cycle insanely fast and heavily contribute to this issue.",
|
||||||
"searchtags": [
|
"searchtags": [
|
||||||
"vr",
|
"bad",
|
||||||
"quest",
|
"fix",
|
||||||
"controller",
|
"animator",
|
||||||
"tracking"
|
"loop"
|
||||||
],
|
],
|
||||||
"requirements": [
|
"requirements": [
|
||||||
"None"
|
"None"
|
||||||
],
|
],
|
||||||
"downloadlink": "https://github.com/NotAKidOnSteam/TrackedControllerFix/releases/download/v1.0.0/TrackedControllerFix.dll",
|
"downloadlink": "https://github.com/NotAKidOnSteam/BadAnimatorFix/releases/download/v1.0.0/BadAnimatorFix.dll",
|
||||||
"sourcelink": "https://github.com/NotAKidOnSteam/TrackedControllerFix/",
|
"sourcelink": "https://github.com/NotAKidOnSteam/BadAnimatorFix/",
|
||||||
"changelog": "Initial Release",
|
"changelog": "Initial Release",
|
||||||
"embedcolor": "3498db"
|
"embedcolor": "7F3F99"
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue