From 18f87b0b35b4c63dd70e94b0cf8ed9c6a73a5ae4 Mon Sep 17 00:00:00 2001 From: Mister_Nebula <41904486+misternebula@users.noreply.github.com> Date: Mon, 9 Nov 2020 21:05:50 +0000 Subject: [PATCH] fix animation syncing --- QSB/Animation/AnimationSync.cs | 77 +++++++++++++-------- QSB/Animation/AnimatorMirror.cs | 5 -- QSB/Animation/Events/ChangeAnimTypeEvent.cs | 12 ++-- QSB/Animation/Events/PlayerSuitEvent.cs | 3 + QSB/Animation/QSBNetworkAnimator.cs | 39 ++++++----- QSB/Instruments/InstrumentsManager.cs | 2 + QSB/QSBNetworkManager.cs | 11 ++- 7 files changed, 85 insertions(+), 64 deletions(-) diff --git a/QSB/Animation/AnimationSync.cs b/QSB/Animation/AnimationSync.cs index e349fee8..5d3895fa 100644 --- a/QSB/Animation/AnimationSync.cs +++ b/QSB/Animation/AnimationSync.cs @@ -3,7 +3,6 @@ using OWML.ModHelper.Events; using QSB.EventsCore; using QSB.Player; using QSB.Utility; -using System; using System.Linq; using UnityEngine; @@ -37,7 +36,7 @@ namespace QSB.Animation _netAnim.enabled = false; _netAnim.animator = _anim; - QSBSceneManager.OnSceneLoaded += OnSceneLoaded; + QSBSceneManager.OnSceneLoaded += (OWScene scene, bool universe) => LoadControllers(universe); } private void OnDestroy() @@ -51,11 +50,15 @@ namespace QSB.Animation _playerController.OnBecomeGrounded -= OnBecomeGrounded; _playerController.OnBecomeUngrounded -= OnBecomeUngrounded; - QSBSceneManager.OnSceneLoaded -= OnSceneLoaded; + QSBSceneManager.OnSceneLoaded -= (OWScene scene, bool universe) => LoadControllers(universe); } - private void OnSceneLoaded(OWScene scene, bool inUniverse) + private void LoadControllers(bool universe) { + if (!universe) + { + return; + } var reibeckRoot = GameObject.Find("Traveller_HEA_Riebeck_ANIM_Talking"); _riebeckController = reibeckRoot.GetComponent().runtimeAnimatorController; var chertRoot = GameObject.Find("Traveller_HEA_Chert_ANIM_Chatter_Chipper"); @@ -68,6 +71,10 @@ namespace QSB.Animation private void InitCommon(Transform body) { + if (QSBSceneManager.IsInUniverse) + { + LoadControllers(true); + } _netAnim.enabled = true; _bodyAnim = body.GetComponent(); Mirror = body.gameObject.AddComponent(); @@ -86,10 +93,17 @@ namespace QSB.Animation { _netAnim.SetParameterAutoSend(i, true); } + + var playerAnimController = body.GetComponent(); + _suitedAnimController = AnimControllerPatch.SuitedAnimController; + _unsuitedAnimController = playerAnimController.GetValue("_unsuitedAnimOverride"); + _suitedGraphics = playerAnimController.GetValue("_suitedGroup"); + _unsuitedGraphics = playerAnimController.GetValue("_unsuitedGroup"); } public void InitLocal(Transform body) { + DebugLog.DebugWrite($"InitLocal ({PlayerId})"); InitCommon(body); _playerController = body.parent.GetComponent(); @@ -102,16 +116,12 @@ namespace QSB.Animation public void InitRemote(Transform body) { + DebugLog.DebugWrite($"InitRemote ({PlayerId})"); InitCommon(body); var playerAnimController = body.GetComponent(); playerAnimController.enabled = false; - _suitedAnimController = AnimControllerPatch.SuitedAnimController; - _unsuitedAnimController = playerAnimController.GetValue("_unsuitedAnimOverride"); - _suitedGraphics = playerAnimController.GetValue("_suitedGroup"); - _unsuitedGraphics = playerAnimController.GetValue("_unsuitedGroup"); - playerAnimController.SetValue("_suitedGroup", new GameObject()); playerAnimController.SetValue("_unsuitedGroup", new GameObject()); playerAnimController.SetValue("_baseAnimController", null); @@ -149,16 +159,14 @@ namespace QSB.Animation private void SuitUp() { + GlobalMessenger.FireEvent(EventNames.QSBChangeAnimType, PlayerId, AnimationType.PlayerSuited); SetAnimationType(AnimationType.PlayerSuited); - _unsuitedGraphics?.SetActive(false); - _suitedGraphics?.SetActive(true); } private void SuitDown() { + GlobalMessenger.FireEvent(EventNames.QSBChangeAnimType, PlayerId, AnimationType.PlayerUnsuited); SetAnimationType(AnimationType.PlayerUnsuited); - _unsuitedGraphics?.SetActive(true); - _suitedGraphics?.SetActive(false); } public void SetSuitState(bool state) @@ -177,33 +185,46 @@ namespace QSB.Animation { return; } - DebugLog.DebugWrite($"{_bodyAnim.name} Changing animtype to {Enum.GetName(typeof(AnimationType), type)}"); - GlobalMessenger.FireEvent(EventNames.QSBChangeAnimType, type); CurrentType = type; + if (_unsuitedAnimController == null) + { + DebugLog.DebugWrite($"Error - Unsuited controller is null. ({PlayerId})", MessageType.Error); + } + if (_suitedAnimController == null) + { + DebugLog.DebugWrite($"Error - Suited controller is null. ({PlayerId})", MessageType.Error); + } + RuntimeAnimatorController controller = default; switch (type) { case AnimationType.PlayerSuited: - _bodyAnim.runtimeAnimatorController = _suitedAnimController; - _anim.runtimeAnimatorController = _suitedAnimController; - DebugLog.DebugWrite("done suited"); + controller = _suitedAnimController; + _unsuitedGraphics?.SetActive(false); + _suitedGraphics?.SetActive(true); break; case AnimationType.PlayerUnsuited: - _bodyAnim.runtimeAnimatorController = _unsuitedAnimController; - _anim.runtimeAnimatorController = _unsuitedAnimController; - DebugLog.DebugWrite("done unsuited"); + controller = _unsuitedAnimController; + _unsuitedGraphics?.SetActive(true); + _suitedGraphics?.SetActive(false); break; case AnimationType.Chert: - _bodyAnim.runtimeAnimatorController = _chertController; - _bodyAnim.SetTrigger("Playing"); - _anim.runtimeAnimatorController = _chertController; - _anim.SetTrigger("Playing"); + controller = _chertController; break; } - if (_bodyAnim.runtimeAnimatorController == null) + _anim.runtimeAnimatorController = controller; + _bodyAnim.runtimeAnimatorController = controller; + if (type != AnimationType.PlayerSuited && type != AnimationType.PlayerUnsuited) { - DebugLog.ToConsole("Error - Somehow set RAC of bodyAnim to null?", MessageType.Error); + _bodyAnim.SetTrigger("Playing"); + _anim.SetTrigger("Playing"); } - _netAnim.animator = _anim; + else + { + // Avoids "jumping" look when exiting instrument + _bodyAnim.SetTrigger("Grounded"); + _anim.SetTrigger("Grounded"); + } + _netAnim.animator = _anim; // Probably not needed. Mirror.RebuildFloatParams(); for (var i = 0; i < _anim.parameterCount; i++) { diff --git a/QSB/Animation/AnimatorMirror.cs b/QSB/Animation/AnimatorMirror.cs index ac0a1b4d..3fb87fd0 100644 --- a/QSB/Animation/AnimatorMirror.cs +++ b/QSB/Animation/AnimatorMirror.cs @@ -88,12 +88,7 @@ namespace QSB.Animation public void RebuildFloatParams() { - if (_from.runtimeAnimatorController == null) - { - DebugLog.ToConsole($"Error - Controller of \"from\" for player {GetPlayer().PlayerId} is null! Current animtype is {GetPlayer().Animator.CurrentType}.", MessageType.Error); - } _floatParams.Clear(); - DebugLog.DebugWrite($"REBUILD FLOAT PARAMS id {GetPlayer().PlayerId}"); foreach (var param in _from.parameters.Where(p => p.type == AnimatorControllerParameterType.Float)) { _floatParams.Add(param.name, new AnimFloatParam()); diff --git a/QSB/Animation/Events/ChangeAnimTypeEvent.cs b/QSB/Animation/Events/ChangeAnimTypeEvent.cs index bd2fdd1b..53d75246 100644 --- a/QSB/Animation/Events/ChangeAnimTypeEvent.cs +++ b/QSB/Animation/Events/ChangeAnimTypeEvent.cs @@ -8,21 +8,21 @@ namespace QSB.Animation.Events { public override EventType Type => EventType.PlayInstrument; - public override void SetupListener() => GlobalMessenger.AddListener(EventNames.QSBChangeAnimType, Handler); + public override void SetupListener() => GlobalMessenger.AddListener(EventNames.QSBChangeAnimType, Handler); - public override void CloseListener() => GlobalMessenger.RemoveListener(EventNames.QSBChangeAnimType, Handler); + public override void CloseListener() => GlobalMessenger.RemoveListener(EventNames.QSBChangeAnimType, Handler); - private void Handler(AnimationType type) => SendEvent(CreateMessage(type)); + private void Handler(uint player, AnimationType type) => SendEvent(CreateMessage(player, type)); - private ChangeAnimTypeMessage CreateMessage(AnimationType type) => new ChangeAnimTypeMessage + private ChangeAnimTypeMessage CreateMessage(uint player, AnimationType type) => new ChangeAnimTypeMessage { - AboutId = LocalPlayerId, + AboutId = player, Type = type }; public override void OnReceiveRemote(ChangeAnimTypeMessage message) { - DebugLog.DebugWrite("Receive remote ChangeAnimType - " + message.Type); + DebugLog.DebugWrite($"ChangeAnimType for {message.AboutId} - {message.Type}"); QSBPlayerManager.GetPlayer(message.AboutId).Animator.SetAnimationType(message.Type); } } diff --git a/QSB/Animation/Events/PlayerSuitEvent.cs b/QSB/Animation/Events/PlayerSuitEvent.cs index f91679d3..32427d60 100644 --- a/QSB/Animation/Events/PlayerSuitEvent.cs +++ b/QSB/Animation/Events/PlayerSuitEvent.cs @@ -1,6 +1,7 @@ using QSB.EventsCore; using QSB.Messaging; using QSB.Player; +using QSB.Utility; namespace QSB.Animation { @@ -31,12 +32,14 @@ namespace QSB.Animation public override void OnReceiveRemote(ToggleMessage message) { + DebugLog.DebugWrite($"remote suit {(message.ToggleValue ? "on" : "off")} player {message.AboutId}"); var player = QSBPlayerManager.GetPlayer(message.AboutId); player?.UpdateState(State.Suit, message.ToggleValue); } public override void OnReceiveLocal(ToggleMessage message) { + DebugLog.DebugWrite($"local suit {(message.ToggleValue ? "on" : "off")}"); QSBPlayerManager.LocalPlayer.UpdateState(State.Suit, message.ToggleValue); var animator = QSBPlayerManager.LocalPlayer.Animator; if (message.ToggleValue) diff --git a/QSB/Animation/QSBNetworkAnimator.cs b/QSB/Animation/QSBNetworkAnimator.cs index 117014e4..ac5cd087 100644 --- a/QSB/Animation/QSBNetworkAnimator.cs +++ b/QSB/Animation/QSBNetworkAnimator.cs @@ -1,4 +1,5 @@ using QSB.Animation.Events; +using QSB.Utility; using UnityEngine; using UnityEngine.Networking; using Object = UnityEngine.Object; @@ -338,39 +339,39 @@ namespace QSB.Animation internal static void OnAnimationClientMessage(NetworkMessage netMsg) { netMsg.ReadMessage(AnimationMessage); - GameObject localObject = ClientScene.FindLocalObject(QSBNetworkAnimator.AnimationMessage.netId); - if ((Object)localObject == (Object)null) + var localObject = ClientScene.FindLocalObject(AnimationMessage.netId); + if (localObject == null) return; - QSBNetworkAnimator component = localObject.GetComponent(); - if (!((Object)component != (Object)null)) + var component = localObject.GetComponent(); + if (component == null) return; - NetworkReader reader = new NetworkReader(QSBNetworkAnimator.AnimationMessage.parameters); - component.HandleAnimMsg(QSBNetworkAnimator.AnimationMessage, reader); + var reader = new NetworkReader(AnimationMessage.parameters); + component.HandleAnimMsg(AnimationMessage, reader); } internal static void OnAnimationParametersClientMessage(NetworkMessage netMsg) { - netMsg.ReadMessage(QSBNetworkAnimator.ParametersMessage); - GameObject localObject = ClientScene.FindLocalObject(QSBNetworkAnimator.ParametersMessage.netId); - if ((Object)localObject == (Object)null) + netMsg.ReadMessage(ParametersMessage); + var localObject = ClientScene.FindLocalObject(ParametersMessage.netId); + if (localObject == null) return; - QSBNetworkAnimator component = localObject.GetComponent(); - if (!((Object)component != (Object)null)) + var component = localObject.GetComponent(); + if (component == null) return; - NetworkReader reader = new NetworkReader(QSBNetworkAnimator.ParametersMessage.parameters); - component.HandleAnimParamsMsg(QSBNetworkAnimator.ParametersMessage, reader); + var reader = new NetworkReader(ParametersMessage.parameters); + component.HandleAnimParamsMsg(ParametersMessage, reader); } internal static void OnAnimationTriggerClientMessage(NetworkMessage netMsg) { - netMsg.ReadMessage(QSBNetworkAnimator.TriggersMessage); - GameObject localObject = ClientScene.FindLocalObject(QSBNetworkAnimator.TriggersMessage.netId); - if ((Object)localObject == (Object)null) + netMsg.ReadMessage(TriggersMessage); + var localObject = ClientScene.FindLocalObject(TriggersMessage.netId); + if (localObject == null) return; - QSBNetworkAnimator component = localObject.GetComponent(); - if (!((Object)component != (Object)null)) + var component = localObject.GetComponent(); + if (component == null) return; - component.HandleAnimTriggerMsg(QSBNetworkAnimator.TriggersMessage.hash); + component.HandleAnimTriggerMsg(TriggersMessage.hash); } } } \ No newline at end of file diff --git a/QSB/Instruments/InstrumentsManager.cs b/QSB/Instruments/InstrumentsManager.cs index 3461579a..0b8dd768 100644 --- a/QSB/Instruments/InstrumentsManager.cs +++ b/QSB/Instruments/InstrumentsManager.cs @@ -1,4 +1,5 @@ using QSB.Animation; +using QSB.EventsCore; using QSB.Instruments.QSBCamera; using QSB.Player; using UnityEngine; @@ -36,6 +37,7 @@ namespace QSB.Instruments public void SwitchToType(AnimationType type) { + GlobalMessenger.FireEvent(EventNames.QSBChangeAnimType, QSBPlayerManager.LocalPlayerId, type); QSBPlayerManager.LocalPlayer.Animator.SetAnimationType(type); } } diff --git a/QSB/QSBNetworkManager.cs b/QSB/QSBNetworkManager.cs index 231e0ea1..7941437a 100644 --- a/QSB/QSBNetworkManager.cs +++ b/QSB/QSBNetworkManager.cs @@ -148,16 +148,15 @@ namespace QSB QSBSectorManager.Instance.RebuildSectors(); OrbManager.Instance.QueueBuildSlots(); - if (NetworkClient.active && !NetworkServer.active) + if (!NetworkServer.localClientActive) { QSBPatchManager.DoPatchType(QSBPatchTypes.OnNonServerClientConnect); + singleton.client.UnregisterHandler(40); + singleton.client.UnregisterHandler(41); + singleton.client.RegisterHandlerSafe(40, new NetworkMessageDelegate(QSBNetworkAnimator.OnAnimationClientMessage)); + singleton.client.RegisterHandlerSafe(41, new NetworkMessageDelegate(QSBNetworkAnimator.OnAnimationParametersClientMessage)); } - - singleton.client.UnregisterHandler(40); - singleton.client.UnregisterHandler(41); singleton.client.UnregisterHandler(42); - singleton.client.RegisterHandlerSafe(40, new NetworkMessageDelegate(QSBNetworkAnimator.OnAnimationClientMessage)); - singleton.client.RegisterHandlerSafe(41, new NetworkMessageDelegate(QSBNetworkAnimator.OnAnimationParametersClientMessage)); singleton.client.RegisterHandlerSafe(42, new NetworkMessageDelegate(QSBNetworkAnimator.OnAnimationTriggerClientMessage)); QSBPatchManager.DoPatchType(QSBPatchTypes.OnClientConnect);