From 933fad7a93a9e0c79b042c4482a8a14e15c76d9c Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Tue, 14 Sep 2021 13:36:23 -0400 Subject: [PATCH 1/2] chore: removal of EnableNetworkVariable in NetworkConfig. It's always True now --- .../Editor/NetworkManagerEditor.cs | 9 +--- .../Runtime/Configuration/NetworkConfig.cs | 10 ---- .../Runtime/Core/NetworkManager.cs | 12 ++--- .../Runtime/Core/NetworkObject.cs | 54 +++++++++---------- .../Messaging/InternalMessageHandler.cs | 10 ---- .../Runtime/Spawning/NetworkSpawnManager.cs | 7 +-- 6 files changed, 30 insertions(+), 72 deletions(-) diff --git a/com.unity.netcode.gameobjects/Editor/NetworkManagerEditor.cs b/com.unity.netcode.gameobjects/Editor/NetworkManagerEditor.cs index 701fbe92a4..670cb974fe 100644 --- a/com.unity.netcode.gameobjects/Editor/NetworkManagerEditor.cs +++ b/com.unity.netcode.gameobjects/Editor/NetworkManagerEditor.cs @@ -26,7 +26,6 @@ public class NetworkManagerEditor : UnityEditor.Editor private SerializedProperty m_MaxObjectUpdatesPerTickProperty; private SerializedProperty m_ClientConnectionBufferTimeoutProperty; private SerializedProperty m_ConnectionApprovalProperty; - private SerializedProperty m_EnableNetworkVariableProperty; private SerializedProperty m_EnsureNetworkVariableLengthSafetyProperty; private SerializedProperty m_ForceSamePrefabsProperty; private SerializedProperty m_EnableSceneManagementProperty; @@ -94,7 +93,6 @@ private void Initialize() m_TickRateProperty = m_NetworkConfigProperty.FindPropertyRelative("TickRate"); m_ClientConnectionBufferTimeoutProperty = m_NetworkConfigProperty.FindPropertyRelative("ClientConnectionBufferTimeout"); m_ConnectionApprovalProperty = m_NetworkConfigProperty.FindPropertyRelative("ConnectionApproval"); - m_EnableNetworkVariableProperty = m_NetworkConfigProperty.FindPropertyRelative("EnableNetworkVariable"); m_EnsureNetworkVariableLengthSafetyProperty = m_NetworkConfigProperty.FindPropertyRelative("EnsureNetworkVariableLengthSafety"); m_ForceSamePrefabsProperty = m_NetworkConfigProperty.FindPropertyRelative("ForceSamePrefabs"); m_EnableSceneManagementProperty = m_NetworkConfigProperty.FindPropertyRelative("EnableSceneManagement"); @@ -122,7 +120,6 @@ private void CheckNullProperties() m_TickRateProperty = m_NetworkConfigProperty.FindPropertyRelative("TickRate"); m_ClientConnectionBufferTimeoutProperty = m_NetworkConfigProperty.FindPropertyRelative("ClientConnectionBufferTimeout"); m_ConnectionApprovalProperty = m_NetworkConfigProperty.FindPropertyRelative("ConnectionApproval"); - m_EnableNetworkVariableProperty = m_NetworkConfigProperty.FindPropertyRelative("EnableNetworkVariable"); m_EnsureNetworkVariableLengthSafetyProperty = m_NetworkConfigProperty.FindPropertyRelative("EnsureNetworkVariableLengthSafety"); m_ForceSamePrefabsProperty = m_NetworkConfigProperty.FindPropertyRelative("ForceSamePrefabs"); m_EnableSceneManagementProperty = m_NetworkConfigProperty.FindPropertyRelative("EnableSceneManagement"); @@ -260,12 +257,8 @@ public override void OnInspectorGUI() EditorGUILayout.PropertyField(m_TickRateProperty); EditorGUILayout.LabelField("Performance", EditorStyles.boldLabel); - EditorGUILayout.PropertyField(m_EnableNetworkVariableProperty); - using (new EditorGUI.DisabledScope(!m_NetworkManager.NetworkConfig.EnableNetworkVariable)) - { - EditorGUILayout.PropertyField(m_EnsureNetworkVariableLengthSafetyProperty); - } + EditorGUILayout.PropertyField(m_EnsureNetworkVariableLengthSafetyProperty); EditorGUILayout.LabelField("Connection", EditorStyles.boldLabel); EditorGUILayout.PropertyField(m_ConnectionApprovalProperty); diff --git a/com.unity.netcode.gameobjects/Runtime/Configuration/NetworkConfig.cs b/com.unity.netcode.gameobjects/Runtime/Configuration/NetworkConfig.cs index 74f8abefb5..8c05216bb0 100644 --- a/com.unity.netcode.gameobjects/Runtime/Configuration/NetworkConfig.cs +++ b/com.unity.netcode.gameobjects/Runtime/Configuration/NetworkConfig.cs @@ -81,13 +81,6 @@ public class NetworkConfig [Tooltip("The amount of seconds between re-syncs of NetworkTime, if enabled")] public int TimeResyncInterval = 30; - /// - /// Whether or not to enable the NetworkVariable system. This system runs in the Update loop and will degrade performance, but it can be a huge convenience. - /// Only turn it off if you have no need for the NetworkVariable system. - /// - [Tooltip("Whether or not to enable the NetworkVariable system")] - public bool EnableNetworkVariable = true; - /// /// Whether or not to ensure that NetworkVariables can be read even if a client accidentally writes where its not allowed to. This costs some CPU and bandwidth. /// @@ -180,7 +173,6 @@ public string ToBase64() writer.WriteBool(EnableSceneManagement); writer.WriteBool(RecycleNetworkIds); writer.WriteSinglePacked(NetworkIdRecycleDelay); - writer.WriteBool(EnableNetworkVariable); writer.WriteBool(EnableNetworkLogs); buffer.PadBuffer(); @@ -211,7 +203,6 @@ public void FromBase64(string base64) config.EnableSceneManagement = reader.ReadBool(); config.RecycleNetworkIds = reader.ReadBool(); config.NetworkIdRecycleDelay = reader.ReadSinglePacked(); - config.EnableNetworkVariable = reader.ReadBool(); config.EnableNetworkLogs = reader.ReadBool(); } @@ -246,7 +237,6 @@ public ulong GetConfig(bool cache = true) } } writer.WriteBool(ConnectionApproval); - writer.WriteBool(EnableNetworkVariable); writer.WriteBool(ForceSamePrefabs); writer.WriteBool(EnableSceneManagement); writer.WriteBool(EnsureNetworkVariableLengthSafety); diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs index 70833997f6..9b18418cbb 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs @@ -1040,11 +1040,8 @@ private void OnNetworkPreUpdate() /// private void OnNetworkManagerTick() { - if (NetworkConfig.EnableNetworkVariable) - { - // Do NetworkVariable updates - BehaviourUpdater.NetworkBehaviourUpdate(this); - } + // Do NetworkVariable updates + BehaviourUpdater.NetworkBehaviourUpdate(this); int timeSyncFrequencyTicks = (int)(k_TimeSyncFrequency * NetworkConfig.TickRate); if (IsServer && NetworkTickSystem.ServerTime.Tick % timeSyncFrequencyTicks == 0) @@ -1523,10 +1520,7 @@ internal void ApprovedPlayerSpawn(ulong clientId, uint playerPrefabHash) nonNullContext.NetworkWriter.WriteBool(false); //No payload data - if (NetworkConfig.EnableNetworkVariable) - { - ConnectedClients[clientId].PlayerObject.WriteNetworkVariableData(nonNullContext.NetworkWriter.GetStream(), clientPair.Key); - } + ConnectedClients[clientId].PlayerObject.WriteNetworkVariableData(nonNullContext.NetworkWriter.GetStream(), clientPair.Key); } } } diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs index 3eb83190f9..02e5dda832 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs @@ -968,44 +968,38 @@ internal void SerializeSceneObject(NetworkWriter writer, ulong targetClientId) WriteNetworkParenting(writer, isReparented, latestParent); } - // Write whether we are including network variable data - writer.WriteBool(NetworkManager.NetworkConfig.EnableNetworkVariable); + // NetworkVariable section + var buffer = writer.GetStream() as NetworkBuffer; - //If we are including NetworkVariable data - if (NetworkManager.NetworkConfig.EnableNetworkVariable) - { - var buffer = writer.GetStream() as NetworkBuffer; - - // Write placeholder size, NOT as a packed value, initially as zero (i.e. we do not know how much NetworkVariable data will be written yet) - writer.WriteUInt32(0); + // Write placeholder size, NOT as a packed value, initially as zero (i.e. we do not know how much NetworkVariable data will be written yet) + writer.WriteUInt32(0); - // Mark our current position before we potentially write any NetworkVariable data - var positionBeforeNetworkVariableData = buffer.Position; + // Mark our current position before we potentially write any NetworkVariable data + var positionBeforeNetworkVariableData = buffer.Position; - // Write network variable data - WriteNetworkVariableData(buffer, targetClientId); + // Write network variable data + WriteNetworkVariableData(buffer, targetClientId); - // If our current buffer position is greater than our positionBeforeNetworkVariableData then we wrote NetworkVariable data - // Part 1: This will include the total NetworkVariable data size, if there was NetworkVariable data written, to the stream - // in order to be able to skip past this entry on the deserialization side in the event this NetworkObject fails to be - // constructed (See Part 2 below in the DeserializeSceneObject method) - if (buffer.Position > positionBeforeNetworkVariableData) - { - // Store our current stream buffer position - var endOfNetworkVariableData = buffer.Position; + // If our current buffer position is greater than our positionBeforeNetworkVariableData then we wrote NetworkVariable data + // Part 1: This will include the total NetworkVariable data size, if there was NetworkVariable data written, to the stream + // in order to be able to skip past this entry on the deserialization side in the event this NetworkObject fails to be + // constructed (See Part 2 below in the DeserializeSceneObject method) + if (buffer.Position > positionBeforeNetworkVariableData) + { + // Store our current stream buffer position + var endOfNetworkVariableData = buffer.Position; - // Calculate the total NetworkVariable data size written - var networkVariableDataSize = endOfNetworkVariableData - positionBeforeNetworkVariableData; + // Calculate the total NetworkVariable data size written + var networkVariableDataSize = endOfNetworkVariableData - positionBeforeNetworkVariableData; - // Move the stream position back to just before we wrote our size (we include the unpacked UInt32 data size placeholder) - buffer.Position = positionBeforeNetworkVariableData - sizeof(uint); + // Move the stream position back to just before we wrote our size (we include the unpacked UInt32 data size placeholder) + buffer.Position = positionBeforeNetworkVariableData - sizeof(uint); - // Now write the actual data size written into our unpacked UInt32 placeholder position - writer.WriteUInt32((uint)(networkVariableDataSize)); + // Now write the actual data size written into our unpacked UInt32 placeholder position + writer.WriteUInt32((uint)(networkVariableDataSize)); - // Finally, revert the buffer position back to the end of the network variable data written - buffer.Position = endOfNetworkVariableData; - } + // Finally, revert the buffer position back to the end of the network variable data written + buffer.Position = endOfNetworkVariableData; } } diff --git a/com.unity.netcode.gameobjects/Runtime/Messaging/InternalMessageHandler.cs b/com.unity.netcode.gameobjects/Runtime/Messaging/InternalMessageHandler.cs index 10e30cdb11..1d61b31180 100644 --- a/com.unity.netcode.gameobjects/Runtime/Messaging/InternalMessageHandler.cs +++ b/com.unity.netcode.gameobjects/Runtime/Messaging/InternalMessageHandler.cs @@ -193,16 +193,6 @@ public void HandleTimeSync(ulong clientId, Stream stream) public void HandleNetworkVariableDelta(ulong clientId, Stream stream) { - if (!NetworkManager.NetworkConfig.EnableNetworkVariable) - { - if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) - { - NetworkLog.LogWarning($"Network variable delta received but {nameof(NetworkConfig.EnableNetworkVariable)} is false"); - } - - return; - } - using var reader = PooledNetworkReader.Get(stream); ulong networkObjectId = reader.ReadUInt64Packed(); ushort networkBehaviourIndex = reader.ReadUInt16Packed(); diff --git a/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs b/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs index 764ebf85a0..4527ec89d8 100644 --- a/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs @@ -296,7 +296,7 @@ internal void SpawnNetworkObjectLocally(NetworkObject networkObject, ulong netwo throw new SpawnStateException("Object is already spawned"); } - if (readNetworkVariable && NetworkManager.NetworkConfig.EnableNetworkVariable) + if (readNetworkVariable) { networkObject.SetNetworkVariableData(dataStream); } @@ -451,10 +451,7 @@ internal void WriteSpawnCallForObject(PooledNetworkWriter writer, ulong clientId var (isReparented, latestParent) = networkObject.GetNetworkParenting(); NetworkObject.WriteNetworkParenting(writer, isReparented, latestParent); } - if (NetworkManager.NetworkConfig.EnableNetworkVariable) - { - networkObject.WriteNetworkVariableData(writer.GetStream(), clientId); - } + networkObject.WriteNetworkVariableData(writer.GetStream(), clientId); } internal void DespawnObject(NetworkObject networkObject, bool destroyObject = false) From 5263f5f99410eb3d857f808bd6cc4286ec16aa79 Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Tue, 14 Sep 2021 16:17:32 -0400 Subject: [PATCH 2/2] fix: this bool write cannot be remoed yet --- com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs index 02e5dda832..019ab39301 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs @@ -969,6 +969,10 @@ internal void SerializeSceneObject(NetworkWriter writer, ulong targetClientId) } // NetworkVariable section + + // todo: remove this WriteBool and the matching read + writer.WriteBool(true); + var buffer = writer.GetStream() as NetworkBuffer; // Write placeholder size, NOT as a packed value, initially as zero (i.e. we do not know how much NetworkVariable data will be written yet)