This repository has been archived on 2025-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariable/OwnerPermissionTests.cs
Unity Technologies eab996f3ac com.unity.netcode.gameobjects@2.0.0-pre.4
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).

## [2.0.0-pre.4] - 2024-08-21

### Added

- Added `NetworkVariable.CheckDirtyState` that is to be used in tandem with collections in order to detect whether the collection or an item within the collection has changed. (#3004)

### Fixed

- Fixed issue where nested `NetworkTransform` components were not getting updated. (#3016)
- Fixed issue by adding null checks in `NetworkVariableBase.CanClientRead` and `NetworkVariableBase.CanClientWrite` methods to ensure safe access to `NetworkBehaviour`. (#3012)
- Fixed issue where `FixedStringSerializer<T>` was using `NetworkVariableSerialization<byte>.AreEqual` to determine if two bytes were equal causes an exception to be thrown due to no byte serializer having been defined. (#3009)
- Fixed Issue where a state with dual triggers, inbound and outbound, could cause a false layer to layer state transition message to be sent to non-authority `NetworkAnimator` instances and cause a warning message to be logged. (#3008)
- Fixed issue using collections within `NetworkVariable` where the collection would not detect changes to items or nested items. (#3004)
- Fixed issue where `List`, `Dictionary`, and `HashSet` collections would not uniquely duplicate nested collections. (#3004)
- Fixed issue where `NotAuthorityTarget` would include the service observer in the list of targets to send the RPC to as opposed to excluding the service observer as it should. (#3000)
- Fixed issue where `ProxyRpcTargetGroup` could attempt to send a message if there were no targets to send to. (#3000)

### Changed

- Changed `NetworkAnimator` to automatically switch to owner authoritative mode when using a distributed authority network topology. (#3021)
- Changed permissions exception thrown in `NetworkList` to exiting early with a logged error that is now a unified permissions message within `NetworkVariableBase`. (#3004)
- Changed permissions exception thrown in `NetworkVariable.Value` to exiting early with a logged error that is now a unified permissions message within `NetworkVariableBase`. (#3004)
2024-08-21 00:00:00 +00:00

182 lines
7.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;
using UnityEngine.TestTools;
namespace Unity.Netcode.RuntimeTests
{
internal class OwnerPermissionObject : NetworkBehaviour
{
// indexed by [object, machine]
public static OwnerPermissionObject[,] Objects = new OwnerPermissionObject[3, 3];
public static int CurrentlySpawning = 0;
public static List<OwnerPermissionObject> ClientTargetedNetworkObjects = new List<OwnerPermissionObject>();
// a client-owned NetworkVariable
public NetworkVariable<int> MyNetworkVariableOwner;
// a server-owned NetworkVariable
public NetworkVariable<int> MyNetworkVariableServer;
// a client-owned NetworkVariable
public NetworkList<int> MyNetworkListOwner;
// a server-owned NetworkVariable
public NetworkList<int> MyNetworkListServer;
// verifies two lists are identical
public static void CheckLists(NetworkList<int> listA, NetworkList<int> listB)
{
Debug.Assert(listA.Count == listB.Count);
for (var i = 0; i < listA.Count; i++)
{
Debug.Assert(listA[i] == listB[i]);
}
}
// verifies all objects have consistent lists on all clients
public static void VerifyConsistency()
{
for (var objectIndex = 0; objectIndex < 3; objectIndex++)
{
CheckLists(Objects[objectIndex, 0].MyNetworkListOwner, Objects[objectIndex, 1].MyNetworkListOwner);
CheckLists(Objects[objectIndex, 0].MyNetworkListOwner, Objects[objectIndex, 2].MyNetworkListOwner);
CheckLists(Objects[objectIndex, 0].MyNetworkListServer, Objects[objectIndex, 1].MyNetworkListServer);
CheckLists(Objects[objectIndex, 0].MyNetworkListServer, Objects[objectIndex, 2].MyNetworkListServer);
}
}
public override void OnNetworkSpawn()
{
Objects[CurrentlySpawning, NetworkManager.LocalClientId] = GetComponent<OwnerPermissionObject>();
}
private void Awake()
{
MyNetworkVariableOwner = new NetworkVariable<int>(writePerm: NetworkVariableWritePermission.Owner);
MyNetworkVariableOwner.OnValueChanged += OwnerChanged;
MyNetworkVariableServer = new NetworkVariable<int>(writePerm: NetworkVariableWritePermission.Server);
MyNetworkVariableServer.OnValueChanged += ServerChanged;
MyNetworkListOwner = new NetworkList<int>(writePerm: NetworkVariableWritePermission.Owner);
MyNetworkListOwner.OnListChanged += ListOwnerChanged;
MyNetworkListServer = new NetworkList<int>(writePerm: NetworkVariableWritePermission.Server);
MyNetworkListServer.OnListChanged += ListServerChanged;
}
public void OwnerChanged(int before, int after)
{
}
public void ServerChanged(int before, int after)
{
}
public void ListOwnerChanged(NetworkListEvent<int> listEvent)
{
}
public void ListServerChanged(NetworkListEvent<int> listEvent)
{
}
}
internal class OwnerPermissionHideTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 2;
private GameObject m_PrefabToSpawn;
protected override void OnServerAndClientsCreated()
{
m_PrefabToSpawn = CreateNetworkObjectPrefab("OwnerPermissionObject");
m_PrefabToSpawn.AddComponent<OwnerPermissionObject>();
}
[UnityTest]
public IEnumerator OwnerPermissionTest()
{
// create 3 objects
for (var objectIndex = 0; objectIndex < 3; objectIndex++)
{
OwnerPermissionObject.CurrentlySpawning = objectIndex;
NetworkManager ownerManager = m_ServerNetworkManager;
if (objectIndex != 0)
{
ownerManager = m_ClientNetworkManagers[objectIndex - 1];
}
SpawnObject(m_PrefabToSpawn, ownerManager);
// wait for each object to spawn on each client
for (var clientIndex = 0; clientIndex < 3; clientIndex++)
{
while (OwnerPermissionObject.Objects[objectIndex, clientIndex] == null)
{
yield return new WaitForSeconds(0.0f);
}
}
}
var nextValueToWrite = 1;
var serverIndex = 0;
for (var objectIndex = 0; objectIndex < 3; objectIndex++)
{
for (var clientWriting = 0; clientWriting < 3; clientWriting++)
{
// ==== Server-writable NetworkVariable ====
VerboseDebug($"Writing to server-write variable on object {objectIndex} on client {clientWriting}");
nextValueToWrite++;
if (clientWriting != serverIndex)
{
LogAssert.Expect(LogType.Error, OwnerPermissionObject.Objects[objectIndex, clientWriting].MyNetworkVariableServer.GetWritePermissionError());
}
OwnerPermissionObject.Objects[objectIndex, clientWriting].MyNetworkVariableServer.Value = nextValueToWrite;
// ==== Owner-writable NetworkVariable ====
VerboseDebug($"Writing to owner-write variable on object {objectIndex} on client {clientWriting}");
nextValueToWrite++;
if (clientWriting != objectIndex)
{
LogAssert.Expect(LogType.Error, OwnerPermissionObject.Objects[objectIndex, clientWriting].MyNetworkVariableOwner.GetWritePermissionError());
}
OwnerPermissionObject.Objects[objectIndex, clientWriting].MyNetworkVariableOwner.Value = nextValueToWrite;
// ==== Server-writable NetworkList ====
VerboseDebug($"Writing to [Add] server-write NetworkList on object {objectIndex} on client {clientWriting}");
nextValueToWrite++;
if (clientWriting != serverIndex)
{
LogAssert.Expect(LogType.Error, OwnerPermissionObject.Objects[objectIndex, clientWriting].MyNetworkListServer.GetWritePermissionError());
}
OwnerPermissionObject.Objects[objectIndex, clientWriting].MyNetworkListServer.Add(nextValueToWrite);
// ==== Owner-writable NetworkList ====
VerboseDebug($"Writing to [Add] owner-write NetworkList on object {objectIndex} on client {clientWriting}");
nextValueToWrite++;
if (clientWriting != objectIndex)
{
LogAssert.Expect(LogType.Error, OwnerPermissionObject.Objects[objectIndex, clientWriting].MyNetworkListOwner.GetWritePermissionError());
}
OwnerPermissionObject.Objects[objectIndex, clientWriting].MyNetworkListOwner.Add(nextValueToWrite);
yield return WaitForTicks(m_ServerNetworkManager, 5);
yield return WaitForTicks(m_ClientNetworkManagers[0], 5);
yield return WaitForTicks(m_ClientNetworkManagers[1], 5);
OwnerPermissionObject.VerifyConsistency();
}
}
}
}
}