diff --git a/ml_amt/GameEvents.cs b/ml_amt/GameEvents.cs index 00cb0f8..8400096 100644 --- a/ml_amt/GameEvents.cs +++ b/ml_amt/GameEvents.cs @@ -15,8 +15,6 @@ namespace ml_amt public void Invoke() => m_action?.Invoke(); } - public static readonly GameEvent OnAvatarSetup = new GameEvent(); - public static readonly GameEvent OnAvatarClear = new GameEvent(); public static readonly GameEvent OnAvatarReuse = new GameEvent(); public static readonly GameEvent OnPlayspaceScale = new GameEvent(); @@ -24,18 +22,6 @@ namespace ml_amt { try { - p_instance.Patch( - typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.ClearAvatar), BindingFlags.Instance | BindingFlags.Public), - null, - new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnAvatarClear_Postfix), BindingFlags.Static | BindingFlags.NonPublic)) - ); - - p_instance.Patch( - typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.SetupAvatar), BindingFlags.Instance | BindingFlags.Public), - null, - new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnSetupAvatar_Postfix), BindingFlags.Static | BindingFlags.NonPublic)) - ); - p_instance.Patch( typeof(IKSystem).GetMethod(nameof(IKSystem.ReinitializeAvatar), BindingFlags.Instance | BindingFlags.Public), null, @@ -54,30 +40,6 @@ namespace ml_amt } } - static void OnAvatarClear_Postfix() - { - try - { - OnAvatarClear.Invoke(); - } - catch(Exception e) - { - MelonLoader.MelonLogger.Error(e); - } - } - - static void OnSetupAvatar_Postfix() - { - try - { - OnAvatarSetup.Invoke(); - } - catch(Exception l_exception) - { - MelonLoader.MelonLogger.Error(l_exception); - } - } - static void OnAvatarReinitialize_Postfix() { try diff --git a/ml_amt/MotionTweaker.cs b/ml_amt/MotionTweaker.cs index cc881c6..e81097f 100644 --- a/ml_amt/MotionTweaker.cs +++ b/ml_amt/MotionTweaker.cs @@ -1,5 +1,7 @@ -using ABI_RC.Core.Player; +using ABI.CCK.Components; +using ABI_RC.Core.Player; using ABI_RC.Core.Util.AnimatorManager; +using ABI_RC.Systems.GameEventSystem; using ABI_RC.Systems.IK.SubSystems; using ABI_RC.Systems.Movement; using RootMotion.FinalIK; @@ -59,8 +61,8 @@ namespace ml_amt Settings.OnProneLimitChanged.AddListener(this.OnProneLimitChanged); Settings.OnMassCenterChanged.AddListener(this.OnMassCenterChanged); - GameEvents.OnAvatarSetup.AddListener(this.OnAvatarSetup); - GameEvents.OnAvatarClear.AddListener(this.OnAvatarClear); + CVRGameEventSystem.Avatar.OnLocalAvatarLoad.AddListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarClear.AddListener(this.OnAvatarClear); GameEvents.OnAvatarReuse.AddListener(this.OnAvatarReuse); GameEvents.OnPlayspaceScale.AddListener(this.OnPlayspaceScale); } @@ -78,8 +80,8 @@ namespace ml_amt Settings.OnProneLimitChanged.RemoveListener(this.OnProneLimitChanged); Settings.OnMassCenterChanged.RemoveListener(this.OnMassCenterChanged); - GameEvents.OnAvatarSetup.RemoveListener(this.OnAvatarSetup); - GameEvents.OnAvatarClear.RemoveListener(this.OnAvatarClear); + CVRGameEventSystem.Avatar.OnLocalAvatarLoad.RemoveListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarClear.RemoveListener(this.OnAvatarClear); GameEvents.OnAvatarReuse.RemoveListener(this.OnAvatarReuse); GameEvents.OnPlayspaceScale.RemoveListener(this.OnPlayspaceScale); } @@ -96,68 +98,82 @@ namespace ml_amt } // Game events - void OnAvatarClear() + void OnAvatarClear(CVRAvatar p_avatar) { - m_vrIk = null; - m_avatarReady = false; - m_avatarScale = 1f; - m_locomotionOffset = Vector3.zero; - m_massCenter = Vector3.zero; - m_ikLimits = null; - m_parameters.Clear(); + try + { + m_vrIk = null; + m_avatarReady = false; + m_avatarScale = 1f; + m_locomotionOffset = Vector3.zero; + m_massCenter = Vector3.zero; + m_ikLimits = null; + m_parameters.Clear(); - BetterBetterCharacterController.Instance.avatarCrouchLimit = Mathf.Clamp01(Settings.CrouchLimit); - BetterBetterCharacterController.Instance.avatarProneLimit = Mathf.Clamp01(Settings.ProneLimit); + BetterBetterCharacterController.Instance.avatarCrouchLimit = Mathf.Clamp01(Settings.CrouchLimit); + BetterBetterCharacterController.Instance.avatarProneLimit = Mathf.Clamp01(Settings.ProneLimit); + } + catch(System.Exception e) + { + MelonLoader.MelonLogger.Error(e); + } } - void OnAvatarSetup() + void OnAvatarSetup(CVRAvatar p_avatar) { - Utils.SetAvatarTPose(); - - m_vrIk = PlayerSetup.Instance.AvatarObject.GetComponent(); - m_avatarScale = Mathf.Abs(PlayerSetup.Instance.AvatarTransform.localScale.y); - - // Parse animator parameters - m_parameters.Add(new AvatarParameter(AvatarParameter.ParameterType.Moving, PlayerSetup.Instance.AnimatorManager)); - m_parameters.Add(new AvatarParameter(AvatarParameter.ParameterType.MovementSpeed, PlayerSetup.Instance.AnimatorManager)); - m_parameters.Add(new AvatarParameter(AvatarParameter.ParameterType.Velocity, PlayerSetup.Instance.AnimatorManager)); - m_parameters.RemoveAll(p => !p.IsValid()); - - // Avatar custom IK limits - m_ikLimits = PlayerSetup.Instance.AvatarTransform.Find("[IKLimits]"); - UpdateIKLimits(); - - // Apply VRIK tweaks - if(m_vrIk != null) + try { - m_locomotionOffset = m_vrIk.solver.locomotion.offset; - m_massCenter = m_locomotionOffset; + Utils.SetAvatarTPose(); - if(m_vrIk.solver.HasToes()) + m_vrIk = PlayerSetup.Instance.AvatarObject.GetComponent(); + m_avatarScale = Mathf.Abs(PlayerSetup.Instance.AvatarTransform.localScale.y); + + // Parse animator parameters + m_parameters.Add(new AvatarParameter(AvatarParameter.ParameterType.Moving, PlayerSetup.Instance.AnimatorManager)); + m_parameters.Add(new AvatarParameter(AvatarParameter.ParameterType.MovementSpeed, PlayerSetup.Instance.AnimatorManager)); + m_parameters.Add(new AvatarParameter(AvatarParameter.ParameterType.Velocity, PlayerSetup.Instance.AnimatorManager)); + m_parameters.RemoveAll(p => !p.IsValid()); + + // Avatar custom IK limits + m_ikLimits = PlayerSetup.Instance.AvatarTransform.Find("[IKLimits]"); + UpdateIKLimits(); + + // Apply VRIK tweaks + if(m_vrIk != null) { - Transform l_foot = PlayerSetup.Instance.Animator.GetBoneTransform(HumanBodyBones.LeftFoot); - if(l_foot == null) - l_foot = PlayerSetup.Instance.Animator.GetBoneTransform(HumanBodyBones.RightFoot); + m_locomotionOffset = m_vrIk.solver.locomotion.offset; + m_massCenter = m_locomotionOffset; - Transform l_toe = PlayerSetup.Instance.Animator.GetBoneTransform(HumanBodyBones.LeftToes); - if(l_toe == null) - l_toe = PlayerSetup.Instance.Animator.GetBoneTransform(HumanBodyBones.RightToes); - - if((l_foot != null) && (l_toe != null)) + if(m_vrIk.solver.HasToes()) { - Vector3 l_footPos = (PlayerSetup.Instance.AvatarTransform.GetMatrix().inverse * l_foot.GetMatrix()).GetPosition(); - Vector3 l_toePos = (PlayerSetup.Instance.AvatarTransform.GetMatrix().inverse * l_toe.GetMatrix()).GetPosition(); - m_massCenter = new Vector3(0f, 0f, l_toePos.z - l_footPos.z); + Transform l_foot = PlayerSetup.Instance.Animator.GetBoneTransform(HumanBodyBones.LeftFoot); + if(l_foot == null) + l_foot = PlayerSetup.Instance.Animator.GetBoneTransform(HumanBodyBones.RightFoot); + + Transform l_toe = PlayerSetup.Instance.Animator.GetBoneTransform(HumanBodyBones.LeftToes); + if(l_toe == null) + l_toe = PlayerSetup.Instance.Animator.GetBoneTransform(HumanBodyBones.RightToes); + + if((l_foot != null) && (l_toe != null)) + { + Vector3 l_footPos = (PlayerSetup.Instance.AvatarTransform.GetMatrix().inverse * l_foot.GetMatrix()).GetPosition(); + Vector3 l_toePos = (PlayerSetup.Instance.AvatarTransform.GetMatrix().inverse * l_toe.GetMatrix()).GetPosition(); + m_massCenter = new Vector3(0f, 0f, l_toePos.z - l_footPos.z); + } } + + m_vrIk.solver.locomotion.offset = (Settings.MassCenter ? m_massCenter : m_locomotionOffset); + + m_vrIk.onPreSolverUpdate.AddListener(this.OnIKPreSolverUpdate); + m_vrIk.onPostSolverUpdate.AddListener(this.OnIKPostSolverUpdate); } - m_vrIk.solver.locomotion.offset = (Settings.MassCenter ? m_massCenter : m_locomotionOffset); - - m_vrIk.onPreSolverUpdate.AddListener(this.OnIKPreSolverUpdate); - m_vrIk.onPostSolverUpdate.AddListener(this.OnIKPostSolverUpdate); + m_avatarReady = true; + } + catch(System.Exception e) + { + MelonLoader.MelonLogger.Error(e); } - - m_avatarReady = true; } void OnPlayspaceScale() diff --git a/ml_bft/FingerSystem.cs b/ml_bft/FingerSystem.cs index 6899264..8946ef3 100644 --- a/ml_bft/FingerSystem.cs +++ b/ml_bft/FingerSystem.cs @@ -1,5 +1,7 @@ -using ABI_RC.Core.Player; +using ABI.CCK.Components; +using ABI_RC.Core.Player; using ABI_RC.Core.Savior; +using ABI_RC.Systems.GameEventSystem; using ABI_RC.Systems.IK; using ABI_RC.Systems.InputManagement; using System.Collections.Generic; @@ -92,8 +94,8 @@ namespace ml_bft m_pose = new HumanPose(); m_lastValues = new float[40]; - GameEvents.OnAvatarSetup.AddListener(this.OnAvatarSetup); - GameEvents.OnAvatarClear.AddListener(this.OnAvatarClear); + CVRGameEventSystem.Avatar.OnLocalAvatarLoad.AddListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarClear.AddListener(this.OnAvatarClear); GameEvents.OnAvatarReuse.AddListener(this.OnAvatarReuse); GameEvents.OnIKSystemLateUpdate.AddListener(this.OnIKSystemLateUpdate); } @@ -106,99 +108,113 @@ namespace ml_bft m_rightFingerOffsets.Clear(); m_ready = false; - GameEvents.OnAvatarSetup.RemoveListener(this.OnAvatarSetup); - GameEvents.OnAvatarClear.RemoveListener(this.OnAvatarClear); + CVRGameEventSystem.Avatar.OnLocalAvatarLoad.RemoveListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarClear.RemoveListener(this.OnAvatarClear); GameEvents.OnAvatarReuse.RemoveListener(this.OnAvatarReuse); GameEvents.OnIKSystemLateUpdate.RemoveListener(this.OnIKSystemLateUpdate); } - internal void OnAvatarSetup() + internal void OnAvatarSetup(CVRAvatar p_avatar) { - Animator l_animator = PlayerSetup.Instance.Animator; - if(l_animator.isHuman) + try { - Utils.SetAvatarTPose(); - InputHandler.Instance.Rebind(PlayerSetup.Instance.transform.rotation); - - foreach(var l_tuple in ms_fingersChains) + Animator l_animator = PlayerSetup.Instance.Animator; + if(l_animator.isHuman) { - ReorientateTowards( - PlayerSetup.Instance.transform, - l_animator.GetBoneTransform(l_tuple.Item1), - (l_tuple.Item2 != HumanBodyBones.LastBone) ? l_animator.GetBoneTransform(l_tuple.Item2) : null, - InputHandler.Instance.GetSourceForBone(l_tuple.Item1, l_tuple.Item3), - InputHandler.Instance.GetSourceForBone(l_tuple.Item2, l_tuple.Item3), - PlaneType.OXZ - ); - ReorientateTowards( - PlayerSetup.Instance.transform, - l_animator.GetBoneTransform(l_tuple.Item1), - (l_tuple.Item2 != HumanBodyBones.LastBone) ? l_animator.GetBoneTransform(l_tuple.Item2) : null, - InputHandler.Instance.GetSourceForBone(l_tuple.Item1, l_tuple.Item3), - InputHandler.Instance.GetSourceForBone(l_tuple.Item2, l_tuple.Item3), - PlaneType.OYX - ); - } + Utils.SetAvatarTPose(); + InputHandler.Instance.Rebind(PlayerSetup.Instance.transform.rotation); - // Bind hands - m_leftHandOffset.m_source = l_animator.GetBoneTransform(HumanBodyBones.LeftHand); - m_leftHandOffset.m_target = InputHandler.Instance.GetSourceForBone(HumanBodyBones.LeftHand, true); - if((m_leftHandOffset.m_source != null) && (m_leftHandOffset.m_target != null)) - m_leftHandOffset.m_offset = Quaternion.Inverse(m_leftHandOffset.m_source.rotation) * m_leftHandOffset.m_target.rotation; - - m_rightHandOffset.m_source = l_animator.GetBoneTransform(HumanBodyBones.RightHand); - m_rightHandOffset.m_target = InputHandler.Instance.GetSourceForBone(HumanBodyBones.RightHand, false); - if((m_rightHandOffset.m_source != null) && (m_rightHandOffset.m_target != null)) - m_rightHandOffset.m_offset = Quaternion.Inverse(m_rightHandOffset.m_source.rotation) * m_rightHandOffset.m_target.rotation; - - // Bind fingers - foreach(HumanBodyBones p_bone in ms_leftFingerBones) - { - Transform l_avatarBone = l_animator.GetBoneTransform(p_bone); - Transform l_controllerBone = InputHandler.Instance.GetSourceForBone(p_bone, true); - if((l_avatarBone != null) && (l_controllerBone != null)) + foreach(var l_tuple in ms_fingersChains) { - RotationOffset l_offset = new RotationOffset(); - l_offset.m_source = l_controllerBone; - l_offset.m_target = l_avatarBone; - l_offset.m_offset = Quaternion.Inverse(l_controllerBone.rotation) * l_avatarBone.rotation; - m_leftFingerOffsets.Add(l_offset); + ReorientateTowards( + PlayerSetup.Instance.transform, + l_animator.GetBoneTransform(l_tuple.Item1), + (l_tuple.Item2 != HumanBodyBones.LastBone) ? l_animator.GetBoneTransform(l_tuple.Item2) : null, + InputHandler.Instance.GetSourceForBone(l_tuple.Item1, l_tuple.Item3), + InputHandler.Instance.GetSourceForBone(l_tuple.Item2, l_tuple.Item3), + PlaneType.OXZ + ); + ReorientateTowards( + PlayerSetup.Instance.transform, + l_animator.GetBoneTransform(l_tuple.Item1), + (l_tuple.Item2 != HumanBodyBones.LastBone) ? l_animator.GetBoneTransform(l_tuple.Item2) : null, + InputHandler.Instance.GetSourceForBone(l_tuple.Item1, l_tuple.Item3), + InputHandler.Instance.GetSourceForBone(l_tuple.Item2, l_tuple.Item3), + PlaneType.OYX + ); } - } - foreach(HumanBodyBones p_bone in ms_rightFingerBones) - { - Transform l_avatarBone = l_animator.GetBoneTransform(p_bone); - Transform l_controllerBone = InputHandler.Instance.GetSourceForBone(p_bone, false); - if((l_avatarBone != null) && (l_controllerBone != null)) - { - RotationOffset l_offset = new RotationOffset(); - l_offset.m_source = l_controllerBone; - l_offset.m_target = l_avatarBone; - l_offset.m_offset = Quaternion.Inverse(l_controllerBone.rotation) * l_avatarBone.rotation; - m_rightFingerOffsets.Add(l_offset); - } - } - m_ready = ((m_leftFingerOffsets.Count > 0) || (m_rightFingerOffsets.Count > 0)); + // Bind hands + m_leftHandOffset.m_source = l_animator.GetBoneTransform(HumanBodyBones.LeftHand); + m_leftHandOffset.m_target = InputHandler.Instance.GetSourceForBone(HumanBodyBones.LeftHand, true); + if((m_leftHandOffset.m_source != null) && (m_leftHandOffset.m_target != null)) + m_leftHandOffset.m_offset = Quaternion.Inverse(m_leftHandOffset.m_source.rotation) * m_leftHandOffset.m_target.rotation; + + m_rightHandOffset.m_source = l_animator.GetBoneTransform(HumanBodyBones.RightHand); + m_rightHandOffset.m_target = InputHandler.Instance.GetSourceForBone(HumanBodyBones.RightHand, false); + if((m_rightHandOffset.m_source != null) && (m_rightHandOffset.m_target != null)) + m_rightHandOffset.m_offset = Quaternion.Inverse(m_rightHandOffset.m_source.rotation) * m_rightHandOffset.m_target.rotation; + + // Bind fingers + foreach(HumanBodyBones p_bone in ms_leftFingerBones) + { + Transform l_avatarBone = l_animator.GetBoneTransform(p_bone); + Transform l_controllerBone = InputHandler.Instance.GetSourceForBone(p_bone, true); + if((l_avatarBone != null) && (l_controllerBone != null)) + { + RotationOffset l_offset = new RotationOffset(); + l_offset.m_source = l_controllerBone; + l_offset.m_target = l_avatarBone; + l_offset.m_offset = Quaternion.Inverse(l_controllerBone.rotation) * l_avatarBone.rotation; + m_leftFingerOffsets.Add(l_offset); + } + } + foreach(HumanBodyBones p_bone in ms_rightFingerBones) + { + Transform l_avatarBone = l_animator.GetBoneTransform(p_bone); + Transform l_controllerBone = InputHandler.Instance.GetSourceForBone(p_bone, false); + if((l_avatarBone != null) && (l_controllerBone != null)) + { + RotationOffset l_offset = new RotationOffset(); + l_offset.m_source = l_controllerBone; + l_offset.m_target = l_avatarBone; + l_offset.m_offset = Quaternion.Inverse(l_controllerBone.rotation) * l_avatarBone.rotation; + m_rightFingerOffsets.Add(l_offset); + } + } + + m_ready = ((m_leftFingerOffsets.Count > 0) || (m_rightFingerOffsets.Count > 0)); + } + } + catch(System.Exception e) + { + MelonLoader.MelonLogger.Error(e); } } - internal void OnAvatarClear() + internal void OnAvatarClear(CVRAvatar p_avatar) { - m_ready = false; - m_pose = new HumanPose(); + try + { + m_ready = false; + m_pose = new HumanPose(); - m_leftHandOffset.Reset(); - m_rightHandOffset.Reset(); + m_leftHandOffset.Reset(); + m_rightHandOffset.Reset(); - m_leftFingerOffsets.Clear(); - m_rightFingerOffsets.Clear(); + m_leftFingerOffsets.Clear(); + m_rightFingerOffsets.Clear(); + } + catch(System.Exception e) + { + MelonLoader.MelonLogger.Error(e); + } } internal void OnAvatarReuse() { - OnAvatarClear(); - OnAvatarSetup(); + OnAvatarClear(PlayerSetup.Instance.AvatarDescriptor); + OnAvatarSetup(PlayerSetup.Instance.AvatarDescriptor); } internal void OnIKSystemLateUpdate(HumanPoseHandler p_handler, Transform p_hips) diff --git a/ml_bft/GameEvents.cs b/ml_bft/GameEvents.cs index 58ea46a..0b3d7a6 100644 --- a/ml_bft/GameEvents.cs +++ b/ml_bft/GameEvents.cs @@ -24,8 +24,6 @@ namespace ml_bft public void Invoke(T1 p_objA, T2 p_objB) => m_action?.Invoke(p_objA, p_objB); } - public static readonly GameEvent OnAvatarSetup = new GameEvent(); - public static readonly GameEvent OnAvatarClear = new GameEvent(); public static readonly GameEvent OnAvatarReuse = new GameEvent(); public static readonly GameEvent OnInputUpdate = new GameEvent(); public static readonly GameEvent OnIKSystemLateUpdate = new GameEvent(); @@ -34,18 +32,6 @@ namespace ml_bft { try { - p_instance.Patch( - typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.SetupAvatar), BindingFlags.Instance | BindingFlags.Public), - null, - new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnSetupAvatar_Postfix), BindingFlags.Static | BindingFlags.NonPublic)) - ); - - p_instance.Patch( - typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.ClearAvatar), BindingFlags.Instance | BindingFlags.Public), - null, - new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnAvatarClear_Postfix), BindingFlags.Static | BindingFlags.NonPublic)) - ); - p_instance.Patch( typeof(IKSystem).GetMethod(nameof(IKSystem.ReinitializeAvatar), BindingFlags.Instance | BindingFlags.Public), null, @@ -70,30 +56,6 @@ namespace ml_bft } } - static void OnAvatarClear_Postfix() - { - try - { - OnAvatarClear.Invoke(); - } - catch(Exception e) - { - MelonLoader.MelonLogger.Error(e); - } - } - - static void OnSetupAvatar_Postfix() - { - try - { - OnAvatarSetup.Invoke(); - } - catch(Exception l_exception) - { - MelonLoader.MelonLogger.Error(l_exception); - } - } - static void OnAvatarReinitialize_Postfix() { try diff --git a/ml_dht/GameEvents.cs b/ml_dht/GameEvents.cs index 56e8043..03f2d3d 100644 --- a/ml_dht/GameEvents.cs +++ b/ml_dht/GameEvents.cs @@ -35,8 +35,6 @@ namespace ml_dht public void Invoke(T1 p_objA, T2 p_objB) => m_action?.Invoke(p_objA, p_objB); } - public static readonly GameEvent OnAvatarSetup = new GameEvent(); - public static readonly GameEvent OnAvatarClear = new GameEvent(); public static readonly GameEvent OnAvatarReuse = new GameEvent(); public static readonly GameEvent OnEyeControllerUpdate = new GameEvent(); public static readonly GameEvent OnFaceTrackingUpdate = new GameEvent(); @@ -47,18 +45,6 @@ namespace ml_dht { try { - p_instance.Patch( - typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.ClearAvatar), BindingFlags.Instance | BindingFlags.Public), - null, - new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnAvatarClear_Postfix), BindingFlags.Static | BindingFlags.NonPublic)) - ); - - p_instance.Patch( - typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.SetupAvatar), BindingFlags.Instance | BindingFlags.Public), - null, - new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnSetupAvatar_Postfix), BindingFlags.Static | BindingFlags.NonPublic)) - ); - p_instance.Patch( typeof(IKSystem).GetMethod(nameof(IKSystem.ReinitializeAvatar), BindingFlags.Instance | BindingFlags.Public), null, @@ -92,30 +78,6 @@ namespace ml_dht } } - static void OnSetupAvatar_Postfix() - { - try - { - OnAvatarSetup.Invoke(); - } - catch(Exception e) - { - MelonLoader.MelonLogger.Error(e); - } - } - - static void OnAvatarClear_Postfix() - { - try - { - OnAvatarClear.Invoke(); - } - catch(Exception e) - { - MelonLoader.MelonLogger.Error(e); - } - } - static void OnAvatarReinitialize_Postfix() { try diff --git a/ml_dht/HeadTracked.cs b/ml_dht/HeadTracked.cs index 424c326..7ed33b2 100644 --- a/ml_dht/HeadTracked.cs +++ b/ml_dht/HeadTracked.cs @@ -2,6 +2,7 @@ using ABI_RC.Core.Player; using ABI_RC.Core.Player.EyeMovement; using ABI_RC.Systems.FaceTracking; +using ABI_RC.Systems.GameEventSystem; using ABI_RC.Systems.IK; using ABI_RC.Systems.VRModeSwitch; using RootMotion.FinalIK; @@ -69,8 +70,8 @@ namespace ml_dht Settings.OnHeadTrackingChanged.AddListener(this.OnEnabledOrHeadTrackingChanged); Settings.OnSmoothingChanged.AddListener(this.OnSmoothingChanged); - GameEvents.OnAvatarClear.AddListener(this.OnAvatarClear); - GameEvents.OnAvatarSetup.AddListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarLoad.AddListener(this.OnAvatarClear); + CVRGameEventSystem.Avatar.OnLocalAvatarClear.AddListener(this.OnAvatarSetup); GameEvents.OnAvatarReuse.AddListener(this.OnAvatarReuse); GameEvents.OnEyeControllerUpdate.AddListener(this.OnEyeControllerUpdate); GameEvents.OnFaceTrackingUpdate.AddListener(this.UpdateFaceTracking); @@ -89,8 +90,8 @@ namespace ml_dht Settings.OnHeadTrackingChanged.RemoveListener(this.OnEnabledOrHeadTrackingChanged); Settings.OnSmoothingChanged.RemoveListener(this.OnSmoothingChanged); - GameEvents.OnAvatarClear.RemoveListener(this.OnAvatarClear); - GameEvents.OnAvatarSetup.RemoveListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarLoad.RemoveListener(this.OnAvatarClear); + CVRGameEventSystem.Avatar.OnLocalAvatarClear.RemoveListener(this.OnAvatarSetup); GameEvents.OnAvatarReuse.RemoveListener(this.OnAvatarReuse); GameEvents.OnEyeControllerUpdate.RemoveListener(this.OnEyeControllerUpdate); GameEvents.OnFaceTrackingUpdate.RemoveListener(this.UpdateFaceTracking); @@ -137,33 +138,47 @@ namespace ml_dht } // Game events - internal void OnAvatarSetup() + internal void OnAvatarSetup(CVRAvatar p_avatar) { - m_camera = PlayerSetup.Instance.activeCam.transform; - m_avatarDescriptor = PlayerSetup.Instance.AvatarObject.GetComponent(); - - if(PlayerSetup.Instance.Animator.isHuman) + try { - IKSystem.Instance.SetAvatarPose(IKSystem.AvatarPose.TPose); - PlayerSetup.Instance.AvatarTransform.localPosition = Vector3.zero; - PlayerSetup.Instance.AvatarTransform.localRotation = Quaternion.identity; + m_camera = PlayerSetup.Instance.activeCam.transform; + m_avatarDescriptor = PlayerSetup.Instance.AvatarObject.GetComponent(); - m_headBone = PlayerSetup.Instance.Animator.GetBoneTransform(HumanBodyBones.Head); - if(m_headBone != null) - m_bindRotation = Quaternion.Inverse(m_avatarDescriptor.transform.rotation) * m_headBone.rotation; + if(PlayerSetup.Instance.Animator.isHuman) + { + IKSystem.Instance.SetAvatarPose(IKSystem.AvatarPose.TPose); + PlayerSetup.Instance.AvatarTransform.localPosition = Vector3.zero; + PlayerSetup.Instance.AvatarTransform.localRotation = Quaternion.identity; - m_lookIK = PlayerSetup.Instance.AvatarObject.GetComponent(); - if(m_lookIK != null) - m_lookIK.onPostSolverUpdate.AddListener(this.OnLookIKPostUpdate); + m_headBone = PlayerSetup.Instance.Animator.GetBoneTransform(HumanBodyBones.Head); + if(m_headBone != null) + m_bindRotation = Quaternion.Inverse(m_avatarDescriptor.transform.rotation) * m_headBone.rotation; + + m_lookIK = PlayerSetup.Instance.AvatarObject.GetComponent(); + if(m_lookIK != null) + m_lookIK.onPostSolverUpdate.AddListener(this.OnLookIKPostUpdate); + } + } + catch(Exception e) + { + MelonLoader.MelonLogger.Error(e); } } - void OnAvatarClear() + void OnAvatarClear(CVRAvatar p_avatar) { - m_avatarDescriptor = null; - m_lookIK = null; - m_headBone = null; - m_lastHeadRotation = Quaternion.identity; - m_bindRotation = Quaternion.identity; + try + { + m_avatarDescriptor = null; + m_lookIK = null; + m_headBone = null; + m_lastHeadRotation = Quaternion.identity; + m_bindRotation = Quaternion.identity; + } + catch(Exception e) + { + MelonLoader.MelonLogger.Error(e); + } } void OnAvatarReuse() { diff --git a/ml_lme/GameEvents.cs b/ml_lme/GameEvents.cs index 54c77b3..281b9eb 100644 --- a/ml_lme/GameEvents.cs +++ b/ml_lme/GameEvents.cs @@ -23,8 +23,6 @@ namespace ml_lme public void Invoke(T1 p_obj) => m_action?.Invoke(p_obj); } - public static readonly GameEvent OnAvatarSetup = new GameEvent(); - public static readonly GameEvent OnAvatarClear = new GameEvent(); public static readonly GameEvent OnAvatarReuse = new GameEvent(); public static readonly GameEvent OnRayScale = new GameEvent(); public static readonly GameEvent OnPlayspaceScale = new GameEvent(); @@ -34,18 +32,6 @@ namespace ml_lme { try { - p_instance.Patch( - typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.ClearAvatar), BindingFlags.Instance | BindingFlags.Public), - null, - new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnAvatarClear_Postfix), BindingFlags.Static | BindingFlags.NonPublic)) - ); - - p_instance.Patch( - typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.SetupAvatar), BindingFlags.Instance | BindingFlags.Public), - null, - new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnSetupAvatar_Postfix), BindingFlags.Static | BindingFlags.NonPublic)) - ); - p_instance.Patch( typeof(IKSystem).GetMethod(nameof(IKSystem.ReinitializeAvatar), BindingFlags.Instance | BindingFlags.Public), null, @@ -76,30 +62,6 @@ namespace ml_lme } } - static void OnAvatarClear_Postfix() - { - try - { - OnAvatarClear.Invoke(); - } - catch(Exception e) - { - MelonLoader.MelonLogger.Error(e); - } - } - - static void OnSetupAvatar_Postfix() - { - try - { - OnAvatarSetup.Invoke(); - } - catch(Exception e) - { - MelonLoader.MelonLogger.Error(e); - } - } - static void OnAvatarReinitialize_Postfix() { try diff --git a/ml_lme/LeapTracked.cs b/ml_lme/LeapTracked.cs index e9bace1..211fc4c 100644 --- a/ml_lme/LeapTracked.cs +++ b/ml_lme/LeapTracked.cs @@ -1,4 +1,6 @@ -using ABI_RC.Core.Player; +using ABI.CCK.Components; +using ABI_RC.Core.Player; +using ABI_RC.Systems.GameEventSystem; using ABI_RC.Systems.IK; using RootMotion.FinalIK; using System.Collections.Generic; @@ -148,8 +150,8 @@ namespace ml_lme Settings.OnFingersOnlyChanged.AddListener(this.OnEnabledOrFingersOnlyChanged); Settings.OnTrackElbowsChanged.AddListener(this.OnTrackElbowsChanged); - GameEvents.OnAvatarClear.AddListener(this.OnAvatarClear); - GameEvents.OnAvatarSetup.AddListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarLoad.AddListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarClear.AddListener(this.OnAvatarClear); GameEvents.OnAvatarReuse.AddListener(this.OnAvatarReuse); } @@ -174,8 +176,8 @@ namespace ml_lme Settings.OnFingersOnlyChanged.RemoveListener(this.OnEnabledOrFingersOnlyChanged); Settings.OnTrackElbowsChanged.RemoveListener(this.OnTrackElbowsChanged); - GameEvents.OnAvatarClear.RemoveListener(this.OnAvatarClear); - GameEvents.OnAvatarSetup.RemoveListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarLoad.RemoveListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarClear.RemoveListener(this.OnAvatarClear); GameEvents.OnAvatarReuse.RemoveListener(this.OnAvatarReuse); } @@ -255,58 +257,72 @@ namespace ml_lme } // Game events - void OnAvatarClear() + void OnAvatarClear(CVRAvatar p_avatar) { - m_vrIK = null; - m_hips = null; - m_leftArmIK = null; - m_rightArmIK = null; - m_leftTargetActive = false; - m_rightTargetActive = false; + try + { + m_vrIK = null; + m_hips = null; + m_leftArmIK = null; + m_rightArmIK = null; + m_leftTargetActive = false; + m_rightTargetActive = false; - m_poseHandler?.Dispose(); - m_poseHandler = null; + m_poseHandler?.Dispose(); + m_poseHandler = null; - m_leftHandTarget.localPosition = Vector3.zero; - m_leftHandTarget.localRotation = Quaternion.identity; - m_rightHandTarget.localPosition = Vector3.zero; - m_rightHandTarget.localRotation = Quaternion.identity; + m_leftHandTarget.localPosition = Vector3.zero; + m_leftHandTarget.localRotation = Quaternion.identity; + m_rightHandTarget.localPosition = Vector3.zero; + m_rightHandTarget.localRotation = Quaternion.identity; - m_leftHandOffset.Reset(); - m_rightHandOffset.Reset(); + m_leftHandOffset.Reset(); + m_rightHandOffset.Reset(); - m_leftFingerOffsets.Clear(); - m_rightFingerOffsets.Clear(); + m_leftFingerOffsets.Clear(); + m_rightFingerOffsets.Clear(); + } + catch(System.Exception e) + { + MelonLoader.MelonLogger.Error(e); + } } - void OnAvatarSetup() + void OnAvatarSetup(CVRAvatar p_avatar) { - Animator l_animator = PlayerSetup.Instance.Animator; - if(l_animator.isHuman) + try { - Utils.SetAvatarTPose(); - - m_poseHandler = new HumanPoseHandler(l_animator.avatar, l_animator.transform); - m_poseHandler.GetHumanPose(ref m_pose); - - m_hips = l_animator.GetBoneTransform(HumanBodyBones.Hips); - - m_leftHandOffset.m_source = l_animator.GetBoneTransform(HumanBodyBones.LeftHand); - m_leftHandTarget.localRotation = ms_offsetLeft * (Quaternion.Inverse(l_animator.transform.rotation) * m_leftHandOffset.m_source.rotation); - - m_rightHandOffset.m_source = l_animator.GetBoneTransform(HumanBodyBones.RightHand); - m_rightHandTarget.localRotation = ms_offsetRight * (Quaternion.Inverse(l_animator.transform.rotation) * m_rightHandOffset.m_source.rotation); - - ParseFingersBones(); - - m_vrIK = l_animator.GetComponent(); - if(m_vrIK != null) + Animator l_animator = PlayerSetup.Instance.Animator; + if(l_animator.isHuman) { - m_vrIK.onPreSolverUpdate.AddListener(this.OnIKPreSolverUpdate); - m_vrIK.onPostSolverUpdate.AddListener(this.OnIKPostSolverUpdate); + Utils.SetAvatarTPose(); + + m_poseHandler = new HumanPoseHandler(l_animator.avatar, l_animator.transform); + m_poseHandler.GetHumanPose(ref m_pose); + + m_hips = l_animator.GetBoneTransform(HumanBodyBones.Hips); + + m_leftHandOffset.m_source = l_animator.GetBoneTransform(HumanBodyBones.LeftHand); + m_leftHandTarget.localRotation = ms_offsetLeft * (Quaternion.Inverse(l_animator.transform.rotation) * m_leftHandOffset.m_source.rotation); + + m_rightHandOffset.m_source = l_animator.GetBoneTransform(HumanBodyBones.RightHand); + m_rightHandTarget.localRotation = ms_offsetRight * (Quaternion.Inverse(l_animator.transform.rotation) * m_rightHandOffset.m_source.rotation); + + ParseFingersBones(); + + m_vrIK = l_animator.GetComponent(); + if(m_vrIK != null) + { + m_vrIK.onPreSolverUpdate.AddListener(this.OnIKPreSolverUpdate); + m_vrIK.onPostSolverUpdate.AddListener(this.OnIKPostSolverUpdate); + } + else + SetupArmIK(); } - else - SetupArmIK(); + } + catch(System.Exception e) + { + MelonLoader.MelonLogger.Error(e); } } diff --git a/ml_pam/ArmMover.cs b/ml_pam/ArmMover.cs index a4aebf8..7b2d567 100644 --- a/ml_pam/ArmMover.cs +++ b/ml_pam/ArmMover.cs @@ -1,6 +1,7 @@ using ABI.CCK.Components; using ABI_RC.Core.InteractionSystem; using ABI_RC.Core.Player; +using ABI_RC.Systems.GameEventSystem; using ABI_RC.Systems.VRModeSwitch; using RootMotion.FinalIK; using System.Collections; @@ -100,8 +101,8 @@ namespace ml_pam Settings.OnLeadingHandChanged.AddListener(this.OnLeadingHandChanged); Settings.OnHandsExtensionChanged.AddListener(this.OnHandsExtensionChanged); - GameEvents.OnAvatarClear.AddListener(this.OnAvatarClear); - GameEvents.OnAvatarSetup.AddListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarClear.AddListener(this.OnAvatarClear); + CVRGameEventSystem.Avatar.OnLocalAvatarLoad.AddListener(this.OnAvatarSetup); GameEvents.OnAvatarReuse.AddListener(this.OnAvatarReuse); GameEvents.OnIKScaling.AddListener(this.OnIKScaling); GameEvents.OnPickupGrab.AddListener(this.OnPickupGrab); @@ -149,10 +150,10 @@ namespace ml_pam Settings.OnLeadingHandChanged.RemoveListener(this.OnLeadingHandChanged); Settings.OnHandsExtensionChanged.RemoveListener(this.OnHandsExtensionChanged); - GameEvents.OnAvatarClear.RemoveListener(this.OnAvatarClear); - GameEvents.OnAvatarSetup.RemoveListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarClear.RemoveListener(this.OnAvatarClear); + CVRGameEventSystem.Avatar.OnLocalAvatarLoad.RemoveListener(this.OnAvatarSetup); GameEvents.OnAvatarReuse.RemoveListener(this.OnAvatarReuse); - GameEvents.OnIKScaling.AddListener(this.OnIKScaling); + GameEvents.OnIKScaling.RemoveListener(this.OnIKScaling); GameEvents.OnPickupGrab.RemoveListener(this.OnPickupGrab); GameEvents.OnPickupDrop.RemoveListener(this.OnPickupDrop); @@ -341,66 +342,80 @@ namespace ml_pam } // Game events - void OnAvatarClear() + void OnAvatarClear(CVRAvatar p_avatar) { - m_vrIK = null; - m_armIKLeft = null; - m_armIKRight = null; - m_armsLength.Set(0f, 0f, 0f, 0f); - m_leftHandParameter = null; - m_rightHandParameter = null; + try + { + m_vrIK = null; + m_armIKLeft = null; + m_armIKRight = null; + m_armsLength.Set(0f, 0f, 0f, 0f); + m_leftHandParameter = null; + m_rightHandParameter = null; + } + catch(System.Exception e) + { + MelonLoader.MelonLogger.Error(e); + } } - void OnAvatarSetup() + void OnAvatarSetup(CVRAvatar p_avatar) { - m_camera = PlayerSetup.Instance.activeCam.transform; - - if(PlayerSetup.Instance.Animator.isHuman) + try { - m_vrIK = PlayerSetup.Instance.Animator.GetComponent(); - Utils.SetAvatarTPose(); + m_camera = PlayerSetup.Instance.activeCam.transform; - Animator l_animator = PlayerSetup.Instance.Animator; - Matrix4x4 l_avatarMatrixInv = l_animator.transform.GetMatrix().inverse; // Animator and avatar are on same game object + if(PlayerSetup.Instance.Animator.isHuman) + { + m_vrIK = PlayerSetup.Instance.Animator.GetComponent(); + Utils.SetAvatarTPose(); - Transform l_leftHand = l_animator.GetBoneTransform(HumanBodyBones.LeftHand); - if(l_leftHand != null) - m_leftRotationTarget.localRotation = ms_offsetLeft * (l_avatarMatrixInv * l_leftHand.GetMatrix()).rotation; - Transform l_rightHand = l_animator.GetBoneTransform(HumanBodyBones.RightHand); - if(l_rightHand != null) - m_rightRotationTarget.localRotation = ms_offsetRight * (l_avatarMatrixInv * l_rightHand.GetMatrix()).rotation; + Animator l_animator = PlayerSetup.Instance.Animator; + Matrix4x4 l_avatarMatrixInv = l_animator.transform.GetMatrix().inverse; // Animator and avatar are on same game object - m_armsLength.x = GetChainLength(new Transform[]{ + Transform l_leftHand = l_animator.GetBoneTransform(HumanBodyBones.LeftHand); + if(l_leftHand != null) + m_leftRotationTarget.localRotation = ms_offsetLeft * (l_avatarMatrixInv * l_leftHand.GetMatrix()).rotation; + Transform l_rightHand = l_animator.GetBoneTransform(HumanBodyBones.RightHand); + if(l_rightHand != null) + m_rightRotationTarget.localRotation = ms_offsetRight * (l_avatarMatrixInv * l_rightHand.GetMatrix()).rotation; + + m_armsLength.x = GetChainLength(new Transform[]{ l_animator.GetBoneTransform(HumanBodyBones.LeftUpperArm), l_animator.GetBoneTransform(HumanBodyBones.LeftLowerArm), l_animator.GetBoneTransform(HumanBodyBones.LeftHand) }); - m_armsLength.y = GetChainLength(new Transform[]{ + m_armsLength.y = GetChainLength(new Transform[]{ l_animator.GetBoneTransform(HumanBodyBones.RightUpperArm), l_animator.GetBoneTransform(HumanBodyBones.RightLowerArm), l_animator.GetBoneTransform(HumanBodyBones.RightHand) }); - m_armsLength.z = Mathf.Abs((l_avatarMatrixInv * l_animator.GetBoneTransform(HumanBodyBones.LeftUpperArm).GetMatrix()).GetPosition().x); - m_armsLength.w = Mathf.Abs((l_avatarMatrixInv * l_animator.GetBoneTransform(HumanBodyBones.RightUpperArm).GetMatrix()).GetPosition().x); + m_armsLength.z = Mathf.Abs((l_avatarMatrixInv * l_animator.GetBoneTransform(HumanBodyBones.LeftUpperArm).GetMatrix()).GetPosition().x); + m_armsLength.w = Mathf.Abs((l_avatarMatrixInv * l_animator.GetBoneTransform(HumanBodyBones.RightUpperArm).GetMatrix()).GetPosition().x); - if(!Utils.IsInVR()) - { - if(m_vrIK != null) + if(!Utils.IsInVR()) { - m_vrIK.onPreSolverUpdate.AddListener(this.OnIKPreUpdate); - m_vrIK.onPostSolverUpdate.AddListener(this.OnIKPostUpdate); + if(m_vrIK != null) + { + m_vrIK.onPreSolverUpdate.AddListener(this.OnIKPreUpdate); + m_vrIK.onPostSolverUpdate.AddListener(this.OnIKPostUpdate); + } + else + SetupArmIK(); } - else - SetupArmIK(); } + + m_leftHandParameter = new AvatarBoolParameter("LeftHandExtended", PlayerSetup.Instance.AnimatorManager); + m_rightHandParameter = new AvatarBoolParameter("RightHandExtended", PlayerSetup.Instance.AnimatorManager); + + OnEnabledChanged(Settings.Enabled); + OnGrabOffsetChanged(Settings.GrabOffset); + OnIKScaling(1f); // Reset scaling, game doesn't do this anymore on avatar switch + } + catch(System.Exception e) + { + MelonLoader.MelonLogger.Error(e); } - - m_leftHandParameter = new AvatarBoolParameter("LeftHandExtended", PlayerSetup.Instance.AnimatorManager); - m_rightHandParameter = new AvatarBoolParameter("RightHandExtended", PlayerSetup.Instance.AnimatorManager); - - OnEnabledChanged(Settings.Enabled); - OnGrabOffsetChanged(Settings.GrabOffset); - OnIKScaling(1f); // Reset scaling, game doesn't do this anymore on avatar switch } void OnAvatarReuse() diff --git a/ml_pam/GameEvents.cs b/ml_pam/GameEvents.cs index 188e773..64c2f63 100644 --- a/ml_pam/GameEvents.cs +++ b/ml_pam/GameEvents.cs @@ -31,8 +31,6 @@ namespace ml_pam public void Invoke(T1 p_objA, T2 p_objB) => m_action?.Invoke(p_objA, p_objB); } - public static readonly GameEvent OnAvatarSetup = new GameEvent(); - public static readonly GameEvent OnAvatarClear = new GameEvent(); public static readonly GameEvent OnAvatarReuse = new GameEvent(); public static readonly GameEvent OnIKScaling = new GameEvent(); public static readonly GameEvent OnPickupGrab = new GameEvent(); @@ -42,18 +40,6 @@ namespace ml_pam { try { - p_instance.Patch( - typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.ClearAvatar), BindingFlags.Instance | BindingFlags.Public), - null, - new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnAvatarClear_Postfix), BindingFlags.Static | BindingFlags.NonPublic)) - ); - - p_instance.Patch( - typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.SetupAvatar), BindingFlags.Instance | BindingFlags.Public), - null, - new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnSetupAvatar_Postfix), BindingFlags.Static | BindingFlags.NonPublic)) - ); - p_instance.Patch( typeof(IKSystem).GetMethod(nameof(IKSystem.ReinitializeAvatar), BindingFlags.Instance | BindingFlags.Public), null, @@ -84,30 +70,6 @@ namespace ml_pam } } - static void OnAvatarClear_Postfix() - { - try - { - OnAvatarClear.Invoke(); - } - catch(Exception e) - { - MelonLoader.MelonLogger.Error(e); - } - } - - static void OnSetupAvatar_Postfix() - { - try - { - OnAvatarSetup.Invoke(); - } - catch(Exception e) - { - MelonLoader.MelonLogger.Error(e); - } - } - static void OnAvatarReinitialize_Postfix() { try diff --git a/ml_pmc/GameEvents.cs b/ml_pmc/GameEvents.cs index 389065b..3f61aaf 100644 --- a/ml_pmc/GameEvents.cs +++ b/ml_pmc/GameEvents.cs @@ -15,8 +15,6 @@ namespace ml_pmc public void Invoke() => m_action?.Invoke(); } - public static readonly GameEvent OnAvatarSetup = new GameEvent(); - public static readonly GameEvent OnAvatarClear = new GameEvent(); public static readonly GameEvent OnAvatarPreReuse = new GameEvent(); public static readonly GameEvent OnAvatarPostReuse = new GameEvent(); @@ -24,18 +22,6 @@ namespace ml_pmc { try { - p_instance.Patch( - typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.ClearAvatar), BindingFlags.Instance | BindingFlags.Public), - null, - new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnAvatarClear_Postfix), BindingFlags.Static | BindingFlags.NonPublic)) - ); - - p_instance.Patch( - typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.SetupAvatar), BindingFlags.Instance | BindingFlags.Public), - null, - new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnSetupAvatar_Postfix), BindingFlags.Static | BindingFlags.NonPublic)) - ); - p_instance.Patch( typeof(IKSystem).GetMethod(nameof(IKSystem.ReinitializeAvatar), BindingFlags.Instance | BindingFlags.Public), new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnAvatarReinitialize_Prefix), BindingFlags.Static | BindingFlags.NonPublic)), @@ -48,30 +34,6 @@ namespace ml_pmc } } - static void OnAvatarClear_Postfix() - { - try - { - OnAvatarClear.Invoke(); - } - catch(Exception e) - { - MelonLoader.MelonLogger.Error(e); - } - } - - static void OnSetupAvatar_Postfix() - { - try - { - OnAvatarSetup.Invoke(); - } - catch(Exception e) - { - MelonLoader.MelonLogger.Error(e); - } - } - static void OnAvatarReinitialize_Prefix() { try diff --git a/ml_pmc/PoseCopycat.cs b/ml_pmc/PoseCopycat.cs index 6aba068..929d5f6 100644 --- a/ml_pmc/PoseCopycat.cs +++ b/ml_pmc/PoseCopycat.cs @@ -1,5 +1,7 @@ -using ABI_RC.Core.Networking.IO.Social; +using ABI.CCK.Components; +using ABI_RC.Core.Networking.IO.Social; using ABI_RC.Core.Player; +using ABI_RC.Systems.GameEventSystem; using ABI_RC.Systems.IK; using ABI_RC.Systems.IK.SubSystems; using ABI_RC.Systems.InputManagement; @@ -55,8 +57,8 @@ namespace ml_pmc void Start() { - GameEvents.OnAvatarClear.AddListener(this.OnAvatarClear); - GameEvents.OnAvatarSetup.AddListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarClear.AddListener(this.OnAvatarClear); + CVRGameEventSystem.Avatar.OnLocalAvatarLoad.AddListener(this.OnAvatarSetup); GameEvents.OnAvatarPreReuse.AddListener(this.OnAvatarPreReuse); GameEvents.OnAvatarPostReuse.AddListener(this.OnAvatarPostReuse); @@ -78,8 +80,8 @@ namespace ml_pmc m_vrIk = null; m_lookAtIk = null; - GameEvents.OnAvatarClear.RemoveListener(this.OnAvatarClear); - GameEvents.OnAvatarSetup.RemoveListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarClear.RemoveListener(this.OnAvatarClear); + CVRGameEventSystem.Avatar.OnLocalAvatarLoad.RemoveListener(this.OnAvatarSetup); GameEvents.OnAvatarPreReuse.RemoveListener(this.OnAvatarPreReuse); GameEvents.OnAvatarPostReuse.RemoveListener(this.OnAvatarPostReuse); @@ -229,58 +231,72 @@ namespace ml_pmc } // Game events - void OnAvatarClear() + void OnAvatarClear(CVRAvatar p_avatar) { - if(m_active) + try { - RestoreIK(); - RestoreFingerTracking(); - OnCopycatChanged.Invoke(false); + if(m_active) + { + RestoreIK(); + RestoreFingerTracking(); + OnCopycatChanged.Invoke(false); + } + m_active = false; + + if(m_puppetParser != null) + Object.Destroy(m_puppetParser); + m_puppetParser = null; + + m_animator = null; + m_vrIk = null; + m_lookAtIk = null; + + m_poseHandler?.Dispose(); + m_poseHandler = null; + + m_distanceLimit = float.MaxValue; + m_fingerTracking = false; + m_pose = new HumanPose(); + } + catch(System.Exception e) + { + MelonLoader.MelonLogger.Error(e); } - m_active = false; - - if(m_puppetParser != null) - Object.Destroy(m_puppetParser); - m_puppetParser = null; - - m_animator = null; - m_vrIk = null; - m_lookAtIk = null; - - m_poseHandler?.Dispose(); - m_poseHandler = null; - - m_distanceLimit = float.MaxValue; - m_fingerTracking = false; - m_pose = new HumanPose(); } - void OnAvatarSetup() + void OnAvatarSetup(CVRAvatar p_avatar) { - m_inVr = Utils.IsInVR(); - m_animator = PlayerSetup.Instance.Animator; - m_vrIk = PlayerSetup.Instance.AvatarObject.GetComponent(); - m_lookAtIk = PlayerSetup.Instance.AvatarObject.GetComponent(); - - if((m_animator != null) && m_animator.isHuman) + try { - m_poseHandler = new HumanPoseHandler(m_animator.avatar, m_animator.transform); - m_poseHandler.GetHumanPose(ref m_pose); + m_inVr = Utils.IsInVR(); + m_animator = PlayerSetup.Instance.Animator; + m_vrIk = PlayerSetup.Instance.AvatarObject.GetComponent(); + m_lookAtIk = PlayerSetup.Instance.AvatarObject.GetComponent(); - if(m_vrIk != null) + if((m_animator != null) && m_animator.isHuman) { - m_vrIk.onPreSolverUpdate.AddListener(this.OnVRIKPreUpdate); - m_vrIk.onPostSolverUpdate.AddListener(this.OnVRIKPostUpdate); - } + m_poseHandler = new HumanPoseHandler(m_animator.avatar, m_animator.transform); + m_poseHandler.GetHumanPose(ref m_pose); - if(m_lookAtIk != null) - { - m_lookAtIk.onPreSolverUpdate.AddListener(this.OnLookAtIKPreUpdate); - m_lookAtIk.onPostSolverUpdate.AddListener(this.OnLookAtIKPostUpdate); + if(m_vrIk != null) + { + m_vrIk.onPreSolverUpdate.AddListener(this.OnVRIKPreUpdate); + m_vrIk.onPostSolverUpdate.AddListener(this.OnVRIKPostUpdate); + } + + if(m_lookAtIk != null) + { + m_lookAtIk.onPreSolverUpdate.AddListener(this.OnLookAtIKPreUpdate); + m_lookAtIk.onPostSolverUpdate.AddListener(this.OnLookAtIKPostUpdate); + } } + else + m_animator = null; + } + catch(System.Exception e) + { + MelonLoader.MelonLogger.Error(e); } - else - m_animator = null; } void OnAvatarPreReuse() diff --git a/ml_ppu/GameEvents.cs b/ml_ppu/GameEvents.cs index 3ab7057..b8b70dd 100644 --- a/ml_ppu/GameEvents.cs +++ b/ml_ppu/GameEvents.cs @@ -23,8 +23,6 @@ namespace ml_ppu public void Invoke(T1 p_obj) => m_action?.Invoke(p_obj); } - public static readonly GameEvent OnAvatarSetup = new GameEvent(); - public static readonly GameEvent OnAvatarClear = new GameEvent(); public static readonly GameEvent OnIKScaling = new GameEvent(); public static readonly GameEvent OnWorldPreSpawn = new GameEvent(); public static readonly GameEvent OnSeatPreSit = new GameEvent(); @@ -33,18 +31,6 @@ namespace ml_ppu { try { - p_instance.Patch( - typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.ClearAvatar), BindingFlags.Instance | BindingFlags.Public), - null, - new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnAvatarClear_Postfix), BindingFlags.Static |BindingFlags.NonPublic)) - ); - - p_instance.Patch( - typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.SetupAvatar), BindingFlags.Instance | BindingFlags.Public), - null, - new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnSetupAvatar_Postfix), BindingFlags.Static | BindingFlags.NonPublic)) - ); - p_instance.Patch( typeof(PlayerSetup).GetMethod("SetupIKScaling", BindingFlags.Instance | BindingFlags.NonPublic), null, @@ -69,30 +55,6 @@ namespace ml_ppu } } - static void OnAvatarClear_Postfix() - { - try - { - OnAvatarClear.Invoke(); - } - catch(Exception e) - { - MelonLoader.MelonLogger.Error(e); - } - } - - static void OnSetupAvatar_Postfix() - { - try - { - OnAvatarSetup.Invoke(); - } - catch(Exception e) - { - MelonLoader.MelonLogger.Error(e); - } - } - static void OnSetupIKScaling_Postfix(ref UnityEngine.Vector3 ___scaleDifference) { try diff --git a/ml_ppu/PickUpManager.cs b/ml_ppu/PickUpManager.cs index 9030588..9ee2ab1 100644 --- a/ml_ppu/PickUpManager.cs +++ b/ml_ppu/PickUpManager.cs @@ -2,6 +2,7 @@ using ABI_RC.Core; using ABI_RC.Core.InteractionSystem; using ABI_RC.Core.Player; +using ABI_RC.Systems.GameEventSystem; using ABI_RC.Systems.IK; using ABI_RC.Systems.Movement; using UnityEngine; @@ -46,8 +47,8 @@ namespace ml_ppu void Start() { - GameEvents.OnAvatarSetup.AddListener(this.OnAvatarSetup); - GameEvents.OnAvatarClear.AddListener(this.OnAvatarClear); + CVRGameEventSystem.Avatar.OnLocalAvatarLoad.AddListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarClear.AddListener(this.OnAvatarClear); GameEvents.OnIKScaling.AddListener(this.OnIKScaling); GameEvents.OnWorldPreSpawn.AddListener(this.OnWorldPreSpawn); GameEvents.OnSeatPreSit.AddListener(this.OnSeatPreSit); @@ -60,8 +61,8 @@ namespace ml_ppu if(Instance == this) Instance = null; - GameEvents.OnAvatarSetup.RemoveListener(this.OnAvatarSetup); - GameEvents.OnAvatarClear.RemoveListener(this.OnAvatarClear); + CVRGameEventSystem.Avatar.OnLocalAvatarLoad.RemoveListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarClear.RemoveListener(this.OnAvatarClear); GameEvents.OnIKScaling.RemoveListener(this.OnIKScaling); GameEvents.OnWorldPreSpawn.RemoveListener(this.OnWorldPreSpawn); GameEvents.OnSeatPreSit.RemoveListener(this.OnSeatPreSit); @@ -126,53 +127,67 @@ namespace ml_ppu } } - void OnAvatarSetup() + void OnAvatarSetup(CVRAvatar p_avatar) { - Animator l_animator = PlayerSetup.Instance.Animator; - if((l_animator != null) && l_animator.isHuman) + try { - IKSystem.Instance.SetAvatarPose(IKSystem.AvatarPose.TPose); - PlayerSetup.Instance.AvatarTransform.localPosition = Vector3.zero; - PlayerSetup.Instance.AvatarTransform.localRotation = Quaternion.identity; - - m_hips = l_animator.GetBoneTransform(HumanBodyBones.Hips); - m_armLeft = l_animator.GetBoneTransform(HumanBodyBones.LeftUpperArm); - m_armRight = l_animator.GetBoneTransform(HumanBodyBones.RightUpperArm); - - if((m_hips != null) && (m_armLeft != null) && (m_armRight != null)) + Animator l_animator = PlayerSetup.Instance.Animator; + if((l_animator != null) && l_animator.isHuman) { - Matrix4x4 l_avatarMatInv = PlayerSetup.Instance.AvatarTransform.GetMatrix().inverse; - Vector3 l_hipsPos = (l_avatarMatInv * m_hips.GetMatrix()).GetPosition(); - Vector3 l_armPos = (l_avatarMatInv * m_armLeft.GetMatrix()).GetPosition(); + IKSystem.Instance.SetAvatarPose(IKSystem.AvatarPose.TPose); + PlayerSetup.Instance.AvatarTransform.localPosition = Vector3.zero; + PlayerSetup.Instance.AvatarTransform.localRotation = Quaternion.identity; - m_collider = new GameObject("[Collider]").AddComponent(); - m_collider.gameObject.layer = CVRLayers.PlayerClone; - m_collider.transform.parent = this.transform; - m_collider.isTrigger = true; - m_collider.height = Vector3.Distance(l_hipsPos, new Vector3(0f, l_armPos.y, l_armPos.z)); - m_collider.radius = new Vector2(l_armPos.x, l_armPos.z).magnitude; - m_collider.center = new Vector3(0f, m_collider.height * 0.5f, 0f); - m_collider.gameObject.AddComponent(); + m_hips = l_animator.GetBoneTransform(HumanBodyBones.Hips); + m_armLeft = l_animator.GetBoneTransform(HumanBodyBones.LeftUpperArm); + m_armRight = l_animator.GetBoneTransform(HumanBodyBones.RightUpperArm); - m_ready = true; + if((m_hips != null) && (m_armLeft != null) && (m_armRight != null)) + { + Matrix4x4 l_avatarMatInv = PlayerSetup.Instance.AvatarTransform.GetMatrix().inverse; + Vector3 l_hipsPos = (l_avatarMatInv * m_hips.GetMatrix()).GetPosition(); + Vector3 l_armPos = (l_avatarMatInv * m_armLeft.GetMatrix()).GetPosition(); + + m_collider = new GameObject("[Collider]").AddComponent(); + m_collider.gameObject.layer = CVRLayers.PlayerClone; + m_collider.transform.parent = this.transform; + m_collider.isTrigger = true; + m_collider.height = Vector3.Distance(l_hipsPos, new Vector3(0f, l_armPos.y, l_armPos.z)); + m_collider.radius = new Vector2(l_armPos.x, l_armPos.z).magnitude; + m_collider.center = new Vector3(0f, m_collider.height * 0.5f, 0f); + m_collider.gameObject.AddComponent(); + + m_ready = true; + } } } + catch(System.Exception e) + { + MelonLoader.MelonLogger.Error(e); + } } - void OnAvatarClear() + void OnAvatarClear(CVRAvatar p_avatar) { - m_ready = false; - m_held = false; - - if(m_collider != null) + try { - Destroy(m_collider.gameObject); - m_collider = null; + m_ready = false; + m_held = false; + + if(m_collider != null) + { + Destroy(m_collider.gameObject); + m_collider = null; + } + m_holderPointA = null; + m_holderPointerA = null; + m_holderPointB = null; + m_holderPointerB = null; + } + catch(System.Exception e) + { + MelonLoader.MelonLogger.Error(e); } - m_holderPointA = null; - m_holderPointerA = null; - m_holderPointB = null; - m_holderPointerB = null; } void OnIKScaling(float p_scale) diff --git a/ml_prm/GameEvents.cs b/ml_prm/GameEvents.cs index 4e48819..ceb534d 100644 --- a/ml_prm/GameEvents.cs +++ b/ml_prm/GameEvents.cs @@ -32,8 +32,6 @@ namespace ml_prm public void Invoke(T1 p_obj) => m_action?.Invoke(p_obj); } - public static readonly GameEvent OnAvatarSetup = new GameEvent(); - public static readonly GameEvent OnAvatarClear = new GameEvent(); public static readonly GameEvent OnAvatarPreReuse = new GameEvent(); public static readonly GameEvent OnAvatarPostReuse = new GameEvent(); public static readonly GameEvent OnIKScaling = new GameEvent(); @@ -50,18 +48,6 @@ namespace ml_prm { try { - p_instance.Patch( - typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.ClearAvatar), BindingFlags.Instance | BindingFlags.Public), - null, - new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnAvatarClear_Postfix), BindingFlags.Static |BindingFlags.NonPublic)) - ); - - p_instance.Patch( - typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.SetupAvatar), BindingFlags.Instance | BindingFlags.Public), - null, - new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnSetupAvatar_Postfix), BindingFlags.Static | BindingFlags.NonPublic)) - ); - p_instance.Patch( typeof(IKSystem).GetMethod(nameof(IKSystem.ReinitializeAvatar), BindingFlags.Instance | BindingFlags.Public), new HarmonyLib.HarmonyMethod(typeof(GameEvents).GetMethod(nameof(OnAvatarReinitialize_Prefix), BindingFlags.Static | BindingFlags.NonPublic)), @@ -115,30 +101,6 @@ namespace ml_prm } } - static void OnAvatarClear_Postfix() - { - try - { - OnAvatarClear.Invoke(); - } - catch(Exception e) - { - MelonLoader.MelonLogger.Error(e); - } - } - - static void OnSetupAvatar_Postfix() - { - try - { - OnAvatarSetup.Invoke(); - } - catch(Exception e) - { - MelonLoader.MelonLogger.Error(e); - } - } - static void OnAvatarReinitialize_Prefix() { try diff --git a/ml_prm/RagdollController.cs b/ml_prm/RagdollController.cs index 0ff7f07..ea98869 100644 --- a/ml_prm/RagdollController.cs +++ b/ml_prm/RagdollController.cs @@ -2,6 +2,7 @@ using ABI_RC.Core; using ABI_RC.Core.InteractionSystem; using ABI_RC.Core.Player; +using ABI_RC.Systems.GameEventSystem; using ABI_RC.Systems.IK; using ABI_RC.Systems.IK.SubSystems; using ABI_RC.Systems.InputManagement; @@ -99,8 +100,8 @@ namespace ml_prm Settings.OnFallDamageChanged.AddListener(this.OnFallDamageChanged); Settings.OnGestureGrabChanged.AddListener(this.OnGestureGrabChanged); - GameEvents.OnAvatarClear.AddListener(this.OnAvatarClear); - GameEvents.OnAvatarSetup.AddListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarClear.AddListener(this.OnAvatarClear); + CVRGameEventSystem.Avatar.OnLocalAvatarLoad.AddListener(this.OnAvatarSetup); GameEvents.OnAvatarPreReuse.AddListener(this.OnAvatarPreReuse); GameEvents.OnAvatarPostReuse.AddListener(this.OnAvatarPostReuse); GameEvents.OnIKScaling.AddListener(this.OnAvatarScaling); @@ -148,8 +149,8 @@ namespace ml_prm Settings.OnFallDamageChanged.RemoveListener(this.OnFallDamageChanged); Settings.OnGestureGrabChanged.RemoveListener(this.OnGestureGrabChanged); - GameEvents.OnAvatarClear.RemoveListener(this.OnAvatarClear); - GameEvents.OnAvatarSetup.RemoveListener(this.OnAvatarSetup); + CVRGameEventSystem.Avatar.OnLocalAvatarClear.RemoveListener(this.OnAvatarClear); + CVRGameEventSystem.Avatar.OnLocalAvatarLoad.RemoveListener(this.OnAvatarSetup); GameEvents.OnAvatarPreReuse.RemoveListener(this.OnAvatarPreReuse); GameEvents.OnAvatarPostReuse.RemoveListener(this.OnAvatarPostReuse); GameEvents.OnIKScaling.RemoveListener(this.OnAvatarScaling); @@ -252,129 +253,143 @@ namespace ml_prm } // Game events - void OnAvatarClear() + void OnAvatarClear(CVRAvatar p_avatar) { - if(m_initTask != null) + try { - StopCoroutine(m_initTask); - m_initTask = null; - } - - if(m_ragdolled) - { - TryRestoreMovement(); - BodySystem.TrackingPositionWeight = 1f; - } - - if(m_puppetRoot != null) - Object.Destroy(m_puppetRoot.gameObject); - m_puppetRoot = null; - - m_avatarTransform = null; - m_hips = null; - m_vrIK = null; - m_applyHipsPosition = false; - m_ragdolled = false; - m_avatarReady = false; - m_avatarRagdollToggle = null; - m_ragdolledParameter = null; - m_puppetReferences = new BipedRagdollReferences(); - m_ragdollBodyHandlers.Clear(); - m_boneLinks.Clear(); - m_jointAnchors.Clear(); - m_reachedGround = true; - m_groundedTime = 0f; - m_downTime = float.MinValue; - m_puppet.localScale = Vector3.one; - m_inAir = false; - } - - void OnAvatarSetup() - { - if(PlayerSetup.Instance.Animator.isHuman) - { - m_avatarTransform = PlayerSetup.Instance.AvatarTransform; - m_hips = PlayerSetup.Instance.Animator.GetBoneTransform(HumanBodyBones.Hips); - Utils.SetAvatarTPose(); - - BipedRagdollReferences l_avatarReferences = BipedRagdollReferences.FromAvatar(PlayerSetup.Instance.Animator); - - m_puppetRoot = new GameObject("Root").transform; - m_puppetRoot.gameObject.layer = CVRLayers.PlayerClone; - m_puppetRoot.parent = m_puppet; - m_puppetRoot.position = m_avatarTransform.position; - m_puppetRoot.rotation = m_avatarTransform.rotation; - - m_puppetReferences.root = m_puppetRoot; - m_puppetReferences.hips = CloneTransform(l_avatarReferences.hips, m_puppetReferences.root, "Hips"); - m_puppetReferences.spine = CloneTransform(l_avatarReferences.spine, m_puppetReferences.hips, "Spine"); - - if(l_avatarReferences.chest != null) - m_puppetReferences.chest = CloneTransform(l_avatarReferences.chest, m_puppetReferences.spine, "Chest"); - - m_puppetReferences.head = CloneTransform(l_avatarReferences.head, (m_puppetReferences.chest != null) ? m_puppetReferences.chest : m_puppetReferences.spine, "Head"); - - m_puppetReferences.leftUpperArm = CloneTransform(l_avatarReferences.leftUpperArm, (m_puppetReferences.chest != null) ? m_puppetReferences.chest : m_puppetReferences.spine, "LeftUpperArm"); - m_puppetReferences.leftLowerArm = CloneTransform(l_avatarReferences.leftLowerArm, m_puppetReferences.leftUpperArm, "LeftLowerArm"); - m_puppetReferences.leftHand = CloneTransform(l_avatarReferences.leftHand, m_puppetReferences.leftLowerArm, "LeftHand"); - - m_puppetReferences.rightUpperArm = CloneTransform(l_avatarReferences.rightUpperArm, (m_puppetReferences.chest != null) ? m_puppetReferences.chest : m_puppetReferences.spine, "RightUpperArm"); - m_puppetReferences.rightLowerArm = CloneTransform(l_avatarReferences.rightLowerArm, m_puppetReferences.rightUpperArm, "RightLowerArm"); - m_puppetReferences.rightHand = CloneTransform(l_avatarReferences.rightHand, m_puppetReferences.rightLowerArm, "RightHand"); - - m_puppetReferences.leftUpperLeg = CloneTransform(l_avatarReferences.leftUpperLeg, m_puppetReferences.hips, "LeftUpperLeg"); - m_puppetReferences.leftLowerLeg = CloneTransform(l_avatarReferences.leftLowerLeg, m_puppetReferences.leftUpperLeg, "LeftLowerLeg"); - m_puppetReferences.leftFoot = CloneTransform(l_avatarReferences.leftFoot, m_puppetReferences.leftLowerLeg, "LeftFoot"); - - m_puppetReferences.rightUpperLeg = CloneTransform(l_avatarReferences.rightUpperLeg, m_puppetReferences.hips, "RightUpperLeg"); - m_puppetReferences.rightLowerLeg = CloneTransform(l_avatarReferences.rightLowerLeg, m_puppetReferences.rightUpperLeg, "RightLowerLeg"); - m_puppetReferences.rightFoot = CloneTransform(l_avatarReferences.rightFoot, m_puppetReferences.rightLowerLeg, "RightFoot"); - - // Move to world origin to overcome possible issues - m_puppetRoot.position = Vector3.zero; - m_puppetRoot.rotation = Quaternion.identity; - - BipedRagdollCreator.Options l_options = BipedRagdollCreator.AutodetectOptions(m_puppetReferences); - l_options.joints = RagdollCreator.JointType.Character; - BipedRagdollCreator.Create(m_puppetReferences, l_options); - - Transform[] l_puppetTransforms = m_puppetReferences.GetRagdollTransforms(); - Transform[] l_avatarTransforms = l_avatarReferences.GetRagdollTransforms(); - Transform[] l_influencedTransforms = new Transform[] { m_puppetReferences.hips, m_puppetReferences.spine, m_puppetReferences.chest }; - for(int i = 0; i < l_puppetTransforms.Length; i++) + if(m_initTask != null) { - if(l_puppetTransforms[i] != null) - { - CharacterJoint l_joint = l_puppetTransforms[i].GetComponent(); - if(l_joint != null) - { - l_joint.enablePreprocessing = false; - l_joint.enableProjection = true; - m_jointAnchors.Add(System.Tuple.Create(l_joint, l_joint.connectedAnchor)); - } - - Rigidbody l_body = l_puppetTransforms[i].GetComponent(); - Collider l_collider = l_puppetTransforms[i].GetComponent(); - if((l_body != null) && (l_collider != null)) - { - RagdollBodypartHandler l_handler = l_puppetTransforms[i].gameObject.AddComponent(); - l_handler.SetInfuencerUsage(Utils.IsInEnumeration(l_puppetTransforms[i], l_influencedTransforms)); - m_ragdollBodyHandlers.Add(l_handler); - } - - if(l_avatarTransforms[i] != null) - m_boneLinks.Add(System.Tuple.Create(l_puppetTransforms[i], l_avatarTransforms[i])); - } + StopCoroutine(m_initTask); + m_initTask = null; } - m_vrIK = PlayerSetup.Instance.AvatarObject.GetComponent(); - if(m_vrIK != null) - m_vrIK.onPostSolverUpdate.AddListener(this.OnIKPostSolverUpdate); + if(m_ragdolled) + { + TryRestoreMovement(); + BodySystem.TrackingPositionWeight = 1f; + } - m_avatarRagdollToggle = PlayerSetup.Instance.AvatarObject.GetComponentInChildren(true); - m_ragdolledParameter = new AvatarParameter("Ragdolled", PlayerSetup.Instance.AnimatorManager); + if(m_puppetRoot != null) + Object.Destroy(m_puppetRoot.gameObject); + m_puppetRoot = null; - m_initTask = StartCoroutine(WaitForBodyHandlers()); + m_avatarTransform = null; + m_hips = null; + m_vrIK = null; + m_applyHipsPosition = false; + m_ragdolled = false; + m_avatarReady = false; + m_avatarRagdollToggle = null; + m_ragdolledParameter = null; + m_puppetReferences = new BipedRagdollReferences(); + m_ragdollBodyHandlers.Clear(); + m_boneLinks.Clear(); + m_jointAnchors.Clear(); + m_reachedGround = true; + m_groundedTime = 0f; + m_downTime = float.MinValue; + m_puppet.localScale = Vector3.one; + m_inAir = false; + } + catch(System.Exception e) + { + MelonLoader.MelonLogger.Error(e); + } + } + + void OnAvatarSetup(CVRAvatar p_avatar) + { + try + { + if(PlayerSetup.Instance.Animator.isHuman) + { + m_avatarTransform = PlayerSetup.Instance.AvatarTransform; + m_hips = PlayerSetup.Instance.Animator.GetBoneTransform(HumanBodyBones.Hips); + Utils.SetAvatarTPose(); + + BipedRagdollReferences l_avatarReferences = BipedRagdollReferences.FromAvatar(PlayerSetup.Instance.Animator); + + m_puppetRoot = new GameObject("Root").transform; + m_puppetRoot.gameObject.layer = CVRLayers.PlayerClone; + m_puppetRoot.parent = m_puppet; + m_puppetRoot.position = m_avatarTransform.position; + m_puppetRoot.rotation = m_avatarTransform.rotation; + + m_puppetReferences.root = m_puppetRoot; + m_puppetReferences.hips = CloneTransform(l_avatarReferences.hips, m_puppetReferences.root, "Hips"); + m_puppetReferences.spine = CloneTransform(l_avatarReferences.spine, m_puppetReferences.hips, "Spine"); + + if(l_avatarReferences.chest != null) + m_puppetReferences.chest = CloneTransform(l_avatarReferences.chest, m_puppetReferences.spine, "Chest"); + + m_puppetReferences.head = CloneTransform(l_avatarReferences.head, (m_puppetReferences.chest != null) ? m_puppetReferences.chest : m_puppetReferences.spine, "Head"); + + m_puppetReferences.leftUpperArm = CloneTransform(l_avatarReferences.leftUpperArm, (m_puppetReferences.chest != null) ? m_puppetReferences.chest : m_puppetReferences.spine, "LeftUpperArm"); + m_puppetReferences.leftLowerArm = CloneTransform(l_avatarReferences.leftLowerArm, m_puppetReferences.leftUpperArm, "LeftLowerArm"); + m_puppetReferences.leftHand = CloneTransform(l_avatarReferences.leftHand, m_puppetReferences.leftLowerArm, "LeftHand"); + + m_puppetReferences.rightUpperArm = CloneTransform(l_avatarReferences.rightUpperArm, (m_puppetReferences.chest != null) ? m_puppetReferences.chest : m_puppetReferences.spine, "RightUpperArm"); + m_puppetReferences.rightLowerArm = CloneTransform(l_avatarReferences.rightLowerArm, m_puppetReferences.rightUpperArm, "RightLowerArm"); + m_puppetReferences.rightHand = CloneTransform(l_avatarReferences.rightHand, m_puppetReferences.rightLowerArm, "RightHand"); + + m_puppetReferences.leftUpperLeg = CloneTransform(l_avatarReferences.leftUpperLeg, m_puppetReferences.hips, "LeftUpperLeg"); + m_puppetReferences.leftLowerLeg = CloneTransform(l_avatarReferences.leftLowerLeg, m_puppetReferences.leftUpperLeg, "LeftLowerLeg"); + m_puppetReferences.leftFoot = CloneTransform(l_avatarReferences.leftFoot, m_puppetReferences.leftLowerLeg, "LeftFoot"); + + m_puppetReferences.rightUpperLeg = CloneTransform(l_avatarReferences.rightUpperLeg, m_puppetReferences.hips, "RightUpperLeg"); + m_puppetReferences.rightLowerLeg = CloneTransform(l_avatarReferences.rightLowerLeg, m_puppetReferences.rightUpperLeg, "RightLowerLeg"); + m_puppetReferences.rightFoot = CloneTransform(l_avatarReferences.rightFoot, m_puppetReferences.rightLowerLeg, "RightFoot"); + + // Move to world origin to overcome possible issues + m_puppetRoot.position = Vector3.zero; + m_puppetRoot.rotation = Quaternion.identity; + + BipedRagdollCreator.Options l_options = BipedRagdollCreator.AutodetectOptions(m_puppetReferences); + l_options.joints = RagdollCreator.JointType.Character; + BipedRagdollCreator.Create(m_puppetReferences, l_options); + + Transform[] l_puppetTransforms = m_puppetReferences.GetRagdollTransforms(); + Transform[] l_avatarTransforms = l_avatarReferences.GetRagdollTransforms(); + Transform[] l_influencedTransforms = new Transform[] { m_puppetReferences.hips, m_puppetReferences.spine, m_puppetReferences.chest }; + for(int i = 0; i < l_puppetTransforms.Length; i++) + { + if(l_puppetTransforms[i] != null) + { + CharacterJoint l_joint = l_puppetTransforms[i].GetComponent(); + if(l_joint != null) + { + l_joint.enablePreprocessing = false; + l_joint.enableProjection = true; + m_jointAnchors.Add(System.Tuple.Create(l_joint, l_joint.connectedAnchor)); + } + + Rigidbody l_body = l_puppetTransforms[i].GetComponent(); + Collider l_collider = l_puppetTransforms[i].GetComponent(); + if((l_body != null) && (l_collider != null)) + { + RagdollBodypartHandler l_handler = l_puppetTransforms[i].gameObject.AddComponent(); + l_handler.SetInfuencerUsage(Utils.IsInEnumeration(l_puppetTransforms[i], l_influencedTransforms)); + m_ragdollBodyHandlers.Add(l_handler); + } + + if(l_avatarTransforms[i] != null) + m_boneLinks.Add(System.Tuple.Create(l_puppetTransforms[i], l_avatarTransforms[i])); + } + } + + m_vrIK = PlayerSetup.Instance.AvatarObject.GetComponent(); + if(m_vrIK != null) + m_vrIK.onPostSolverUpdate.AddListener(this.OnIKPostSolverUpdate); + + m_avatarRagdollToggle = PlayerSetup.Instance.AvatarObject.GetComponentInChildren(true); + m_ragdolledParameter = new AvatarParameter("Ragdolled", PlayerSetup.Instance.AnimatorManager); + + m_initTask = StartCoroutine(WaitForBodyHandlers()); + } + } + catch(System.Exception e) + { + MelonLoader.MelonLogger.Error(e); } }