diff --git a/QSB/QSBNetworkManager.cs b/QSB/QSBNetworkManager.cs index 1e37c9f7..a1b6fb5a 100644 --- a/QSB/QSBNetworkManager.cs +++ b/QSB/QSBNetworkManager.cs @@ -116,6 +116,7 @@ namespace QSB public override void OnClientConnect(NetworkConnection connection) // Called on the client when connecting to a server { + DebugLog.ToConsole("Connecting to server...", OWML.Common.MessageType.Info); base.OnClientConnect(connection); gameObject.AddComponent(); @@ -142,13 +143,25 @@ namespace QSB public override void OnStopClient() // Called on the client when closing connection { + DebugLog.ToConsole("Disconnecting from server...", OWML.Common.MessageType.Info); Destroy(GetComponent()); Destroy(GetComponent()); Destroy(GetComponent()); EventList.Reset(); if (IsClientConnected()) { - PlayerTransformSync.LocalInstance.gameObject.GetComponent().Reset(); + PlayerTransformSync.LocalInstance?.gameObject.GetComponent().Reset(); + } + foreach (var player in PlayerRegistry.PlayerList) + { + if (player.HudMarker != null) + { + Destroy(player.HudMarker.transform.parent.gameObject); + } + } + foreach (var connection in NetworkServer.connections) + { + CleanupConnection(connection); } _canEditName = true; } @@ -176,20 +189,6 @@ namespace QSB base.OnStopServer(); } - public override void OnClientDisconnect(NetworkConnection conn) - { - DebugLog.ToConsole("Disconnected from server.", OWML.Common.MessageType.Info); - foreach (var player in PlayerRegistry.PlayerList) - { - Destroy(player.HudMarker.transform.parent.gameObject); - } - foreach (var connection in NetworkServer.connections.Where(x => x != conn)) - { - CleanupConnection(connection); - } - base.OnClientDisconnect(conn); - } - private void CleanupConnection(NetworkConnection connection) { var playerId = connection.playerControllers[0].gameObject.GetComponent().netId.Value; diff --git a/QSB/TimeSync/WakeUpSync.cs b/QSB/TimeSync/WakeUpSync.cs index 81ab381f..45c5391b 100644 --- a/QSB/TimeSync/WakeUpSync.cs +++ b/QSB/TimeSync/WakeUpSync.cs @@ -49,6 +49,12 @@ namespace QSB.TimeSync GlobalMessenger.AddListener(EventNames.RestartTimeLoop, OnLoopStart); } + private void OnDestroy() + { + QSBSceneManager.OnSceneLoaded -= OnSceneLoaded; + GlobalMessenger.RemoveListener(EventNames.RestartTimeLoop, OnLoopStart); + } + private void OnSceneLoaded(OWScene scene, bool isInUniverse) { if (isInUniverse) diff --git a/QSB/TransformSync/PlayerCameraSync.cs b/QSB/TransformSync/PlayerCameraSync.cs index 73a02602..1008f4fb 100644 --- a/QSB/TransformSync/PlayerCameraSync.cs +++ b/QSB/TransformSync/PlayerCameraSync.cs @@ -1,5 +1,7 @@ using QSB.Events; using QSB.Tools; +using QSB.Utility; +using System; using UnityEngine; namespace QSB.TransformSync @@ -13,7 +15,25 @@ namespace QSB.TransformSync LocalInstance = this; } - public override uint PlayerId => netId.Value - 2; + public override uint PlayerId + { + get + { + uint id = uint.MaxValue; + try + { + id = netId.Value - 2; + } + catch + { + DebugLog.ToConsole($"Error while geting netId of {GetType().Name}! " + + $"{Environment.NewLine} - Did you destroy the TransformSync without destroying the {GetType().Name}?" + + $"{Environment.NewLine} - Did a destroyed TransformSync/{GetType().Name} still have an active action/event listener?" + + $"{Environment.NewLine} If you are a user seeing this, please report this error.", OWML.Common.MessageType.Error); + } + return id; + } + } protected override Transform InitLocalTransform() { diff --git a/QSB/TransformSync/PlayerProbeSync.cs b/QSB/TransformSync/PlayerProbeSync.cs index b23f170c..caf1cb3f 100644 --- a/QSB/TransformSync/PlayerProbeSync.cs +++ b/QSB/TransformSync/PlayerProbeSync.cs @@ -1,4 +1,6 @@ using QSB.Tools; +using QSB.Utility; +using System; using UnityEngine; namespace QSB.TransformSync @@ -14,7 +16,25 @@ namespace QSB.TransformSync LocalInstance = this; } - public override uint PlayerId => netId.Value - 3; + public override uint PlayerId + { + get + { + uint id = uint.MaxValue; + try + { + id = netId.Value - 3; + } + catch + { + DebugLog.ToConsole($"Error while geting netId of {GetType().Name}! " + + $"{Environment.NewLine} - Did you destroy the TransformSync without destroying the {GetType().Name}?" + + $"{Environment.NewLine} - Did a destroyed TransformSync/{GetType().Name} still have an active action/event listener?" + + $"{Environment.NewLine} If you are a user seeing this, please report this error.", OWML.Common.MessageType.Error); + } + return id; + } + } private Transform GetProbe() { diff --git a/QSB/TransformSync/PlayerTransformSync.cs b/QSB/TransformSync/PlayerTransformSync.cs index 2a5970de..7d6ab40a 100644 --- a/QSB/TransformSync/PlayerTransformSync.cs +++ b/QSB/TransformSync/PlayerTransformSync.cs @@ -1,4 +1,6 @@ using QSB.Animation; +using QSB.Utility; +using System; using UnityEngine; namespace QSB.TransformSync @@ -17,7 +19,25 @@ namespace QSB.TransformSync LocalInstance = this; } - public override uint PlayerId => netId.Value - 0; + public override uint PlayerId + { + get + { + uint id = uint.MaxValue; + try + { + id = netId.Value - 0; + } + catch + { + DebugLog.ToConsole($"Error while geting netId of {GetType().Name}! " + + $"{Environment.NewLine} - Did you destroy the TransformSync without destroying the {GetType().Name}?" + + $"{Environment.NewLine} - Did a destroyed TransformSync/{GetType().Name} still have an active action/event listener?" + + $"{Environment.NewLine} If you are a user seeing this, please report this error.", OWML.Common.MessageType.Error); + } + return id; + } + } private Transform GetPlayerModel() { diff --git a/QSB/TransformSync/ShipTransformSync.cs b/QSB/TransformSync/ShipTransformSync.cs index 0e7acf6e..88ef277e 100644 --- a/QSB/TransformSync/ShipTransformSync.cs +++ b/QSB/TransformSync/ShipTransformSync.cs @@ -1,4 +1,6 @@ -using UnityEngine; +using QSB.Utility; +using System; +using UnityEngine; namespace QSB.TransformSync { @@ -11,7 +13,25 @@ namespace QSB.TransformSync LocalInstance = this; } - public override uint PlayerId => netId.Value - 1; + public override uint PlayerId + { + get + { + uint id = uint.MaxValue; + try + { + id = netId.Value - 1; + } + catch + { + DebugLog.ToConsole($"Error while geting netId of {GetType().Name}! " + + $"{Environment.NewLine} - Did you destroy the TransformSync without destroying the {GetType().Name}?" + + $"{Environment.NewLine} - Did a destroyed TransformSync/{GetType().Name} still have an active action/event listener?" + + $"{Environment.NewLine} If you are a user seeing this, please report this error.", OWML.Common.MessageType.Error); + } + return id; + } + } private Transform GetShipModel() {