From 1b717709f624f271efa0752339b64addc6af0b33 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Tue, 28 Dec 2021 04:37:39 -0800 Subject: [PATCH] buh --- QSB/Animation/Player/AnimationSync.cs | 3 +- .../Messages/AnimationTriggerMessage.cs | 12 ++-- .../Player/Messages/ChangeAnimTypeMessage.cs | 5 +- .../Player/Patches/PlayerAnimationPatches.cs | 8 ++- QSB/Instruments/InstrumentsManager.cs | 3 +- QSB/Player/PlayerInfo.cs | 14 ++--- QSB/Player/PlayerSyncObject.cs | 6 +- QSB/Player/QSBPlayerManager.cs | 42 ++++++++++++-- .../TransformSync/PlayerTransformSync.cs | 16 +++--- QSB/Syncs/SyncBase.cs | 56 +++++++++++++++---- QSB/Utility/Extensions.cs | 17 +++++- QSB/debugsettings.json | 6 +- QuantumUNET/Components/QNetworkIdentity.cs | 19 +++++++ 13 files changed, 158 insertions(+), 49 deletions(-) diff --git a/QSB/Animation/Player/AnimationSync.cs b/QSB/Animation/Player/AnimationSync.cs index 8edf96be..95773279 100644 --- a/QSB/Animation/Player/AnimationSync.cs +++ b/QSB/Animation/Player/AnimationSync.cs @@ -41,8 +41,9 @@ namespace QSB.Animation.Player QSBSceneManager.OnUniverseSceneLoaded += OnUniverseSceneLoaded; } - protected void OnDestroy() + protected override void OnDestroy() { + base.OnDestroy(); Destroy(InvisibleAnimator); Destroy(NetworkAnimator); QSBSceneManager.OnUniverseSceneLoaded -= OnUniverseSceneLoaded; diff --git a/QSB/Animation/Player/Messages/AnimationTriggerMessage.cs b/QSB/Animation/Player/Messages/AnimationTriggerMessage.cs index a42709e9..0000c1a2 100644 --- a/QSB/Animation/Player/Messages/AnimationTriggerMessage.cs +++ b/QSB/Animation/Player/Messages/AnimationTriggerMessage.cs @@ -7,26 +7,26 @@ namespace QSB.Animation.Player.Messages { internal class AnimationTriggerMessage : QSBMessage { - private uint PlayerId; + private uint AttachedNetId; private string Name; - public AnimationTriggerMessage(uint playerId, string name) + public AnimationTriggerMessage(uint attachedNetId, string name) { - PlayerId = playerId; + AttachedNetId = attachedNetId; Name = name; } public override void Serialize(QNetworkWriter writer) { base.Serialize(writer); - writer.Write(PlayerId); + writer.Write(AttachedNetId); writer.Write(Name); } public override void Deserialize(QNetworkReader reader) { base.Deserialize(reader); - PlayerId = reader.ReadUInt32(); + AttachedNetId = reader.ReadUInt32(); Name = reader.ReadString(); } @@ -34,7 +34,7 @@ namespace QSB.Animation.Player.Messages public override void OnReceiveRemote() { - var animationSync = QSBPlayerManager.GetPlayer(PlayerId).AnimationSync; + var animationSync = QSBPlayerManager.GetSyncObject(AttachedNetId); if (animationSync == null) { return; diff --git a/QSB/Animation/Player/Messages/ChangeAnimTypeMessage.cs b/QSB/Animation/Player/Messages/ChangeAnimTypeMessage.cs index 38f18ba6..89d32f1e 100644 --- a/QSB/Animation/Player/Messages/ChangeAnimTypeMessage.cs +++ b/QSB/Animation/Player/Messages/ChangeAnimTypeMessage.cs @@ -1,4 +1,5 @@ -using QSB.Messaging; +using QSB.Instruments; +using QSB.Messaging; using QSB.Player; using QSB.WorldSync; using QuantumUNET.Transport; @@ -38,7 +39,7 @@ namespace QSB.Animation.Player.Messages } player.AnimationSync.SetAnimationType(Value); - player.InstrumentsManager.CheckInstrumentProps(Value); + QSBPlayerManager.GetSyncObject(PlayerId).CheckInstrumentProps(Value); } } } \ No newline at end of file diff --git a/QSB/Animation/Player/Patches/PlayerAnimationPatches.cs b/QSB/Animation/Player/Patches/PlayerAnimationPatches.cs index 84795864..d2d0beb4 100644 --- a/QSB/Animation/Player/Patches/PlayerAnimationPatches.cs +++ b/QSB/Animation/Player/Patches/PlayerAnimationPatches.cs @@ -62,15 +62,16 @@ namespace QSB.Animation.Player.Patches __instance._animator.SetBool("UsingJetpack", isInZeroG && PlayerState.IsWearingSuit()); if (__instance._justBecameGrounded) { + var playerAnimationSync = QSBPlayerManager.LocalPlayer.AnimationSync; if (__instance._justTookFallDamage) { __instance._animator.SetTrigger("LandHard"); - new AnimationTriggerMessage(QSBPlayerManager.LocalPlayerId, "LandHard").Send(); + new AnimationTriggerMessage(playerAnimationSync.AttachedNetId, "LandHard").Send(); } else { __instance._animator.SetTrigger("Land"); - new AnimationTriggerMessage(QSBPlayerManager.LocalPlayerId, "Land").Send(); + new AnimationTriggerMessage(playerAnimationSync.AttachedNetId, "Land").Send(); } } @@ -127,7 +128,8 @@ namespace QSB.Animation.Player.Patches } __instance._animator.SetTrigger("Jump"); - new AnimationTriggerMessage(QSBPlayerManager.LocalPlayerId, "Jump").Send(); + var playerAnimationSync = QSBPlayerManager.LocalPlayer.AnimationSync; + new AnimationTriggerMessage(playerAnimationSync.AttachedNetId, "Jump").Send(); return false; } } diff --git a/QSB/Instruments/InstrumentsManager.cs b/QSB/Instruments/InstrumentsManager.cs index 427d836e..3df9df6f 100644 --- a/QSB/Instruments/InstrumentsManager.cs +++ b/QSB/Instruments/InstrumentsManager.cs @@ -36,8 +36,9 @@ namespace QSB.Instruments QSBCore.UnityEvents.RunWhen(() => Locator.GetPlayerBody() != null, SetupInstruments); } - protected void OnDestroy() + protected override void OnDestroy() { + base.OnDestroy(); if (!IsLocalPlayer) { return; diff --git a/QSB/Player/PlayerInfo.cs b/QSB/Player/PlayerInfo.cs index 149f72bd..a94bbc0d 100644 --- a/QSB/Player/PlayerInfo.cs +++ b/QSB/Player/PlayerInfo.cs @@ -4,7 +4,6 @@ using QSB.Animation.Player.Thrusters; using QSB.Audio; using QSB.CampfireSync.WorldObjects; using QSB.ClientServerStateSync; -using QSB.Instruments; using QSB.ItemSync.WorldObjects.Items; using QSB.Messaging; using QSB.Player.Messages; @@ -24,9 +23,9 @@ namespace QSB.Player public class PlayerInfo { public uint PlayerId { get; } - public PlayerTransformSync TransformSync { get; } public string Name { get; set; } public PlayerHUDMarker HudMarker { get; set; } + public PlayerTransformSync TransformSync { get; set; } // Body Objects public OWCamera Camera @@ -115,8 +114,7 @@ namespace QSB.Player public GameObject CurrentDialogueBox { get; set; } // Animation - public AnimationSync AnimationSync => TransformSync.GetComponent(); - public InstrumentsManager InstrumentsManager => TransformSync.GetComponent(); + public AnimationSync AnimationSync => QSBPlayerManager.GetSyncObject(PlayerId); public bool PlayingInstrument => AnimationSync.CurrentType is not AnimationType.PlayerSuited and not AnimationType.PlayerUnsuited; public JetpackAccelerationSync JetpackAcceleration { get; set; } @@ -195,10 +193,9 @@ namespace QSB.Player } } - public PlayerInfo(PlayerTransformSync transformSync) + public PlayerInfo(uint id) { - PlayerId = transformSync.NetId.Value; - TransformSync = transformSync; + PlayerId = id; CurrentCharacterDialogueTreeId = -1; } @@ -219,7 +216,8 @@ namespace QSB.Player Translator?.ChangeEquipState(TranslatorEquipped); ProbeLauncher?.ChangeEquipState(ProbeLauncherEquipped); Signalscope?.ChangeEquipState(SignalscopeEquipped); - AnimationSync.SetSuitState(SuitedUp); + QSBCore.UnityEvents.RunWhen(() => QSBPlayerManager.GetSyncObject(PlayerId) != null, + () => QSBPlayerManager.GetSyncObject(PlayerId).SetSuitState(SuitedUp)); } public void UpdateStatesFromObjects() diff --git a/QSB/Player/PlayerSyncObject.cs b/QSB/Player/PlayerSyncObject.cs index c6d98205..d8ad57b8 100644 --- a/QSB/Player/PlayerSyncObject.cs +++ b/QSB/Player/PlayerSyncObject.cs @@ -4,7 +4,11 @@ namespace QSB.Player { public abstract class PlayerSyncObject : QNetworkBehaviour { - public uint PlayerId => NetIdentity.NetId.Value; + public uint AttachedNetId => NetIdentity?.NetId.Value ?? uint.MaxValue; + public uint PlayerId => NetIdentity.RootIdentity?.NetId.Value ?? NetIdentity.NetId.Value; public PlayerInfo Player => QSBPlayerManager.GetPlayer(PlayerId); + + protected virtual void Start() => QSBPlayerManager.AddSyncObject(this); + protected virtual void OnDestroy() => QSBPlayerManager.RemoveSyncObject(this); } } \ No newline at end of file diff --git a/QSB/Player/QSBPlayerManager.cs b/QSB/Player/QSBPlayerManager.cs index f937eee0..e9c637d1 100644 --- a/QSB/Player/QSBPlayerManager.cs +++ b/QSB/Player/QSBPlayerManager.cs @@ -24,19 +24,27 @@ namespace QSB.Player return uint.MaxValue; } - return localInstance.NetId.Value; + if (localInstance.NetIdentity == null) + { + DebugLog.ToConsole($"Error - Trying to get LocalPlayerId when the local PlayerTransformSync instance's QNetworkIdentity is null.", MessageType.Error); + return uint.MaxValue; + } + + return localInstance.NetIdentity.NetId.Value; } } - public static Action OnAddPlayer; public static Action OnRemovePlayer; + public static Action OnAddPlayer; public static PlayerInfo LocalPlayer => GetPlayer(LocalPlayerId); - public static List PlayerList { get; } = new(); + public static List PlayerList { get; } = new List(); + + private static readonly List PlayerSyncObjects = new(); public static PlayerInfo GetPlayer(uint id) { - if (id is uint.MaxValue or 0) + if (id is uint.MaxValue or 0U) { return default; } @@ -51,8 +59,32 @@ namespace QSB.Player return player; } + public static void AddPlayer(uint id) + { + DebugLog.DebugWrite($"Create Player : id<{id}>", MessageType.Info); + var player = new PlayerInfo(id); + PlayerList.Add(player); + OnAddPlayer?.Invoke(id); + } + + public static void RemovePlayer(uint id) + { + DebugLog.DebugWrite($"Remove Player : id<{id}>", MessageType.Info); + PlayerList.RemoveAll(x => x.PlayerId == id); + } + public static bool PlayerExists(uint id) => - id is not (uint.MaxValue or 0) && PlayerList.Any(x => x.PlayerId == id); + id != uint.MaxValue && PlayerList.Any(x => x.PlayerId == id); + + public static IEnumerable GetSyncObjects() where T : PlayerSyncObject => + PlayerSyncObjects.OfType().Where(x => x != null); + + public static T GetSyncObject(uint id) where T : PlayerSyncObject => + GetSyncObjects().FirstOrDefault(x => x != null && x.AttachedNetId == id); + + public static void AddSyncObject(PlayerSyncObject obj) => PlayerSyncObjects.Add(obj); + + public static void RemoveSyncObject(PlayerSyncObject obj) => PlayerSyncObjects.Remove(obj); public static List GetPlayersWithCameras(bool includeLocalCamera = true) { diff --git a/QSB/Player/TransformSync/PlayerTransformSync.cs b/QSB/Player/TransformSync/PlayerTransformSync.cs index 86ed29c8..191ed6a5 100644 --- a/QSB/Player/TransformSync/PlayerTransformSync.cs +++ b/QSB/Player/TransformSync/PlayerTransformSync.cs @@ -1,5 +1,4 @@ -using OWML.Common; -using QSB.Animation.Player; +using QSB.Animation.Player; using QSB.Audio; using QSB.Instruments; using QSB.Messaging; @@ -50,9 +49,8 @@ namespace QSB.Player.TransformSync public override void Start() { base.Start(); - QSBPlayerManager.PlayerList.Add(new PlayerInfo(this)); - QSBPlayerManager.OnAddPlayer?.Invoke(PlayerId); - DebugLog.DebugWrite($"Create Player : id<{PlayerId}>", MessageType.Info); + QSBPlayerManager.AddPlayer(PlayerId); + Player.TransformSync = this; } protected override void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool isInUniverse) @@ -90,9 +88,11 @@ namespace QSB.Player.TransformSync // TODO : Maybe move this to a leave event...? Would ensure everything could finish up before removing the player QSBPlayerManager.OnRemovePlayer?.Invoke(PlayerId); base.OnDestroy(); - Player.HudMarker?.Remove(); - QSBPlayerManager.PlayerList.RemoveAll(x => x.PlayerId == PlayerId); - DebugLog.DebugWrite($"Remove Player : id<{PlayerId}>", MessageType.Info); + if (QSBPlayerManager.PlayerExists(PlayerId)) + { + Player.HudMarker?.Remove(); + QSBPlayerManager.RemovePlayer(PlayerId); + } } protected override Transform InitLocalTransform() diff --git a/QSB/Syncs/SyncBase.cs b/QSB/Syncs/SyncBase.cs index a8134de3..2de19ff7 100644 --- a/QSB/Syncs/SyncBase.cs +++ b/QSB/Syncs/SyncBase.cs @@ -17,14 +17,48 @@ namespace QSB.Syncs public abstract class SyncBase : QNetworkTransform where T : Component { - public uint PlayerId { get; private set; } = uint.MaxValue; + public uint AttachedNetId + { + get + { + if (NetIdentity == null) + { + DebugLog.ToConsole($"Error - Trying to get AttachedNetId with null NetIdentity! Type:{GetType().Name} GrandType:{GetType().GetType().Name}", MessageType.Error); + return uint.MaxValue; + } + + return NetIdentity.NetId.Value; + } + } + + public uint PlayerId + { + get + { + if (!IsPlayerObject) + { + return uint.MaxValue; + } + + if (NetIdentity == null) + { + DebugLog.ToConsole($"Error - Trying to get PlayerId with null NetIdentity! Type:{GetType().Name} GrandType:{GetType().GetType().Name}", MessageType.Error); + return uint.MaxValue; + } + + return NetIdentity.RootIdentity != null + ? NetIdentity.RootIdentity.NetId.Value + : AttachedNetId; + } + } + public PlayerInfo Player => QSBPlayerManager.GetPlayer(PlayerId); private bool _baseIsReady { get { - if (NetId.Value is uint.MaxValue or 0) + if (NetId.Value is uint.MaxValue or 0U) { return false; } @@ -41,7 +75,12 @@ namespace QSB.Syncs return false; } - if (!IsLocalPlayer && !Player.IsReady) + if (Player == null) + { + return false; + } + + if (!Player.IsReady && !IsLocalPlayer) { return false; } @@ -56,7 +95,7 @@ namespace QSB.Syncs public abstract bool IgnoreDisabledAttachedObject { get; } public abstract bool IgnoreNullReferenceTransform { get; } public abstract bool DestroyAttachedObject { get; } - public abstract bool IsPlayerObject { get; } + public abstract bool IsPlayerObject { get; } public T AttachedObject { get; set; } public Transform ReferenceTransform { get; set; } @@ -79,12 +118,9 @@ namespace QSB.Syncs { if (IsPlayerObject) { - // get player objects spawned before this object (or is this one) - // and use the most recently spawned one - PlayerId = QSBWorldSync.GetUnityObjects() - .Select(x => x.NetId.Value) - .Where(x => x <= NetId.Value) - .Max(); + var lowestBound = QSBWorldSync.GetUnityObjects() + .Where(x => x.NetId.Value <= NetId.Value).OrderBy(x => x.NetId.Value).Last(); + NetIdentity.SetRootIdentity(lowestBound.NetIdentity); } DontDestroyOnLoad(gameObject); diff --git a/QSB/Utility/Extensions.cs b/QSB/Utility/Extensions.cs index fc481237..72829c87 100644 --- a/QSB/Utility/Extensions.cs +++ b/QSB/Utility/Extensions.cs @@ -1,5 +1,6 @@ using OWML.Common; using QSB.Player; +using QSB.Player.TransformSync; using QuantumUNET; using System; using System.Collections.Generic; @@ -47,7 +48,21 @@ namespace QSB.Utility return uint.MaxValue; } - return playerController.UnetView.NetId.Value; + var go = playerController.Gameobject; + if (go == null) + { + DebugLog.ToConsole($"Error - GameObject of {playerController.UnetView.NetId.Value} is null.", MessageType.Error); + return uint.MaxValue; + } + + var controller = go.GetComponent(); + if (controller == null) + { + DebugLog.ToConsole($"Error - No PlayerTransformSync found on {go.name}", MessageType.Error); + return uint.MaxValue; + } + + return controller.NetId.Value; } public static void SpawnWithServerAuthority(this GameObject go) diff --git a/QSB/debugsettings.json b/QSB/debugsettings.json index 12aece60..e8d2e410 100644 --- a/QSB/debugsettings.json +++ b/QSB/debugsettings.json @@ -1,8 +1,8 @@ { - "debugMode": true, - "drawLines": true, + "debugMode": false, + "drawLines": false, "showQuantumVisibilityObjects": true, "showQuantumDebugBoxes": true, "avoidTimeSync": false, - "skipTitleScreen": true + "skipTitleScreen": false } \ No newline at end of file diff --git a/QuantumUNET/Components/QNetworkIdentity.cs b/QuantumUNET/Components/QNetworkIdentity.cs index 041d52bc..c6401e33 100644 --- a/QuantumUNET/Components/QNetworkIdentity.cs +++ b/QuantumUNET/Components/QNetworkIdentity.cs @@ -21,6 +21,8 @@ namespace QuantumUNET.Components public short PlayerControllerId { get; private set; } = -1; public QNetworkConnection ConnectionToServer { get; private set; } public QNetworkConnection ConnectionToClient { get; private set; } + public QNetworkIdentity RootIdentity { get; private set; } + public List SubIdentities { get; private set; } = new List(); public bool ServerOnly { @@ -37,6 +39,23 @@ namespace QuantumUNET.Components public QNetworkBehaviour[] GetNetworkBehaviours() => m_NetworkBehaviours; + public void SetRootIdentity(QNetworkIdentity newRoot) + { + if (RootIdentity != null) + { + RootIdentity.RemoveSubIdentity(this); + } + + RootIdentity = newRoot; + RootIdentity.AddSubIndentity(this); + } + + internal void AddSubIndentity(QNetworkIdentity identityToAdd) + => SubIdentities.Add(identityToAdd); + + internal void RemoveSubIdentity(QNetworkIdentity identityToRemove) + => SubIdentities.Remove(identityToRemove); + internal void SetDynamicAssetId(int newAssetId) { if (m_AssetId == 0 || m_AssetId.Equals(newAssetId))