From 5a5d48f05ddd4055df19c34b4245ff29c2a2a9ba Mon Sep 17 00:00:00 2001 From: Emma Date: Thu, 30 Oct 2025 12:51:16 -0400 Subject: [PATCH] fix: Ensure DisconnectReason remains parsable --- .../Connection/NetworkConnectionManager.cs | 38 ++++++------------- .../{ => Messages}/DisconnectReasonMessage.cs | 2 +- .../DisconnectReasonMessage.cs.meta | 0 3 files changed, 13 insertions(+), 27 deletions(-) rename com.unity.netcode.gameobjects/Runtime/Messaging/{ => Messages}/DisconnectReasonMessage.cs (97%) rename com.unity.netcode.gameobjects/Runtime/Messaging/{ => Messages}/DisconnectReasonMessage.cs.meta (100%) diff --git a/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs b/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs index 1c60bfa1c9..51f5dc2aeb 100644 --- a/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs @@ -113,36 +113,21 @@ public sealed class NetworkConnectionManager /// client that disconnected. It is recommended to copy the message to some other property or field when /// is invoked. /// - public string DisconnectReason - { - get - { - // For in-frequent event driven invocations, a method within a getter - // is "generally ok". - return GetDisconnectReason(); - } - internal set - { - m_DisconnectReason = value; - } - } + public string DisconnectReason => GetDisconnectReason(); // fine as function because this call is infrequent /// - /// Returns the conbined result of the locally applied and the - /// server applied . - /// - If both values are empty or null, then it returns . - /// - If either value is valid, then it returns that value. - /// - If both values are valid, then it returns followed by a - /// new line and then . + /// Gets the reason for why this client was disconnected if exists. /// - /// A disconnect reason, if any. + /// disconnect reason if it exists, otherwise . [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal string GetDisconnectReason(string header = null) + internal string GetDisconnectReason() { - var disconnectReason = string.IsNullOrEmpty(m_DisconnectReason) ? string.Empty : m_DisconnectReason; - var serverDisconnectReason = string.IsNullOrEmpty(ServerDisconnectReason) ? string.Empty : $"\n{ServerDisconnectReason}"; - var headerInfo = string.IsNullOrEmpty(header) ? string.Empty : header; - return $"{headerInfo}{disconnectReason}{serverDisconnectReason}"; + // TODO: fix this properly + if (!string.IsNullOrEmpty(ServerDisconnectReason)) + { + return ServerDisconnectReason; + } + return m_DisconnectReason; } /// @@ -590,7 +575,8 @@ private void GenerateDisconnectInformation(ulong clientId, ulong transportClient if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) { - NetworkLog.LogInfo($"{DisconnectReason}"); + var serverDisconnectReason = string.IsNullOrEmpty(ServerDisconnectReason) ? string.Empty : $"\n{ServerDisconnectReason}"; + NetworkLog.LogInfo($"{m_DisconnectReason}{serverDisconnectReason}"); } } diff --git a/com.unity.netcode.gameobjects/Runtime/Messaging/DisconnectReasonMessage.cs b/com.unity.netcode.gameobjects/Runtime/Messaging/Messages/DisconnectReasonMessage.cs similarity index 97% rename from com.unity.netcode.gameobjects/Runtime/Messaging/DisconnectReasonMessage.cs rename to com.unity.netcode.gameobjects/Runtime/Messaging/Messages/DisconnectReasonMessage.cs index 1e698df9a4..1a8bf88dcc 100644 --- a/com.unity.netcode.gameobjects/Runtime/Messaging/DisconnectReasonMessage.cs +++ b/com.unity.netcode.gameobjects/Runtime/Messaging/Messages/DisconnectReasonMessage.cs @@ -10,7 +10,7 @@ public void Serialize(FastBufferWriter writer, int targetVersion) { string reasonSent = Reason ?? string.Empty; - // Since we don't send a ConnectionApprovedMessage, the version for this message is encded with the message itself. + // Since we don't send a ConnectionApprovedMessage, the version for this message is encoded with the message itself. // However, note that we HAVE received a ConnectionRequestMessage, so we DO have a valid targetVersion on this side of things. // We just have to make sure the receiving side knows what version we sent it, since whoever has the higher version number is responsible for versioning and they may be the one with the higher version number. BytePacker.WriteValueBitPacked(writer, Version); diff --git a/com.unity.netcode.gameobjects/Runtime/Messaging/DisconnectReasonMessage.cs.meta b/com.unity.netcode.gameobjects/Runtime/Messaging/Messages/DisconnectReasonMessage.cs.meta similarity index 100% rename from com.unity.netcode.gameobjects/Runtime/Messaging/DisconnectReasonMessage.cs.meta rename to com.unity.netcode.gameobjects/Runtime/Messaging/Messages/DisconnectReasonMessage.cs.meta