From bda8b5d8db4e79c0473c5d98f6548ca6f29c7c30 Mon Sep 17 00:00:00 2001 From: "M. Fatih MAR" Date: Fri, 10 Sep 2021 19:49:08 +0100 Subject: [PATCH] chore: remove `ClientNetworkVariable` --- .../NetworkVariable/ClientNetworkVariable.cs | 52 --------------- .../ClientNetworkVariable.cs.meta | 11 ---- .../Tests/Runtime/NetworkVariableTests.cs | 66 +------------------ 3 files changed, 1 insertion(+), 128 deletions(-) delete mode 100644 com.unity.netcode.gameobjects/Runtime/NetworkVariable/ClientNetworkVariable.cs delete mode 100644 com.unity.netcode.gameobjects/Runtime/NetworkVariable/ClientNetworkVariable.cs.meta diff --git a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/ClientNetworkVariable.cs b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/ClientNetworkVariable.cs deleted file mode 100644 index e187741189..0000000000 --- a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/ClientNetworkVariable.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System; - -namespace Unity.Netcode -{ - /// - /// A ClientNetworkVariable is special in that: - /// - only the owner of the variable can write to it - /// - not even the server can write to it - /// - it is not snapshotted - /// - /// (This class may be removed in the future when integrated into NetworkVariable natively) - /// - [Serializable] - public class ClientNetworkVariable : NetworkVariable where T : unmanaged - { - public ClientNetworkVariable() { } - - public ClientNetworkVariable(NetworkVariableReadPermission readPerm) : base(readPerm) { } - - public override bool CanClientWrite(ulong clientId) - { - return m_NetworkBehaviour.OwnerClientId == clientId; - } - - public override bool ShouldWrite(ulong clientId, bool isServer) - { - return m_IsDirty && !isServer && m_NetworkBehaviour.IsOwner; - } - - /// - /// The value of the NetworkVariable container - /// - public override T Value - { - get => m_InternalValue; - set - { - // this could be improved. The Networking Manager is not always initialized here - // Good place to decouple network manager from the network variable - - // Also, note this is not really very water-tight, if you are running as a host - // we cannot tell if a ClientNetworkVariable write is happening inside server-ish code - if (m_NetworkBehaviour.NetworkManager.IsServer) - { - throw new InvalidOperationException("Server not allowed to write to ClientNetworkVariables"); - } - - Set(value); - } - } - } -} diff --git a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/ClientNetworkVariable.cs.meta b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/ClientNetworkVariable.cs.meta deleted file mode 100644 index 85deda8734..0000000000 --- a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/ClientNetworkVariable.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 2eede058d58f4493ba93dc19a82131fd -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariableTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariableTests.cs index c77bc80e07..22ef29e77c 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariableTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariableTests.cs @@ -5,7 +5,6 @@ using NUnit.Framework; using Unity.Collections; - namespace Unity.Netcode.RuntimeTests { public struct FixedString32Struct : INetworkSerializable @@ -43,13 +42,9 @@ public void NetworkSerialize(NetworkSerializer serializer) serializer.Serialize(ref SomeBool); } } + public class NetworkVariableTest : NetworkBehaviour { - public readonly ClientNetworkVariable ClientVar = new ClientNetworkVariable(); - - public readonly ClientNetworkVariable ClientVarPrivate = - new ClientNetworkVariable(NetworkVariableReadPermission.OwnerOnly); - public readonly NetworkVariable TheScalar = new NetworkVariable(); public readonly NetworkList TheList = new NetworkList(); public readonly NetworkSet TheSet = new NetworkSet(); @@ -232,38 +227,6 @@ public void ClientWritePermissionTest([Values(true, false)] bool useHost) Assert.Throws(() => m_Player1OnClient1.TheScalar.Value = k_TestVal1); } - [Test] - public void ServerWritePermissionTest([Values(true, false)] bool useHost) - { - m_TestWithHost = useHost; - - // server must not be allowed to write to a client auth variable - Assert.Throws(() => m_Player1OnServer.ClientVar.Value = k_TestVal1); - } - - [UnityTest] - public IEnumerator ClientTest([Values(true, false)] bool useHost) - { - m_TestWithHost = useHost; - - yield return MultiInstanceHelpers.RunAndWaitForCondition( - () => - { - m_Player1OnClient1.ClientVar.Value = k_TestVal2; - m_Player2OnClient2.ClientVar.Value = k_TestVal3; - }, - () => - { - // the client's values should win on the objects it owns - return - m_Player1OnServer.ClientVar.Value == k_TestVal2 && - m_Player2OnServer.ClientVar.Value == k_TestVal3 && - m_Player1OnClient1.ClientVar.Value == k_TestVal2 && - m_Player2OnClient2.ClientVar.Value == k_TestVal3; - } - ); - } - [UnityTest] public IEnumerator FixedString32StructTest([Values(true, false)] bool useHost) { @@ -289,33 +252,6 @@ public IEnumerator FixedString32StructTest([Values(true, false)] bool useHost) ); } - [UnityTest] - public IEnumerator PrivateClientTest([Values(true, false)] bool useHost) - { - m_TestWithHost = useHost; - - yield return MultiInstanceHelpers.RunAndWaitForCondition( - () => - { - // we are writing to the private and public variables on player 1's object... - m_Player1OnClient1.ClientVarPrivate.Value = k_TestVal1; - m_Player1OnClient1.ClientVar.Value = k_TestVal2; - }, - () => - { - // ...and we should see the writes to the private var only on the server & the owner, - // but the public variable everywhere - return - m_Player1OnClient2.ClientVarPrivate.Value != k_TestVal1 && - m_Player1OnClient1.ClientVarPrivate.Value == k_TestVal1 && - m_Player1OnClient2.ClientVar.Value != k_TestVal2 && - m_Player1OnClient1.ClientVar.Value == k_TestVal2 && - m_Player1OnServer.ClientVarPrivate.Value == k_TestVal1 && - m_Player1OnServer.ClientVar.Value == k_TestVal2; - } - ); - } - [UnityTest] public IEnumerator NetworkListAdd([Values(true, false)] bool useHost) {