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/NetworkVarBufferCopyTest.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

183 lines
7.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine.TestTools;
namespace Unity.Netcode.RuntimeTests
{
[TestFixture(HostOrServer.DAHost)]
[TestFixture(HostOrServer.Host)]
internal class NetworkVarBufferCopyTest : NetcodeIntegrationTest
{
internal class DummyNetVar : NetworkVariableBase
{
private const int k_DummyValue = 0x13579BDF;
public bool DeltaWritten;
public bool FieldWritten;
public bool DeltaRead;
public bool FieldRead;
public override void WriteDelta(FastBufferWriter writer)
{
writer.TryBeginWrite(FastBufferWriter.GetWriteSize(k_DummyValue) + 1);
using (var bitWriter = writer.EnterBitwiseContext())
{
bitWriter.WriteBits(1, 1);
}
writer.WriteValue(k_DummyValue);
DeltaWritten = true;
}
public override void WriteField(FastBufferWriter writer)
{
writer.TryBeginWrite(FastBufferWriter.GetWriteSize(k_DummyValue) + 1);
using (var bitWriter = writer.EnterBitwiseContext())
{
bitWriter.WriteBits(1, 1);
}
writer.WriteValue(k_DummyValue);
FieldWritten = true;
}
public override void ReadField(FastBufferReader reader)
{
reader.TryBeginRead(FastBufferWriter.GetWriteSize(k_DummyValue) + 1);
using (var bitReader = reader.EnterBitwiseContext())
{
bitReader.ReadBits(out byte b, 1);
}
reader.ReadValue(out int i);
Assert.AreEqual(k_DummyValue, i);
FieldRead = true;
}
public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
{
reader.TryBeginRead(FastBufferWriter.GetWriteSize(k_DummyValue) + 1);
using (var bitReader = reader.EnterBitwiseContext())
{
bitReader.ReadBits(out byte b, 1);
}
reader.ReadValue(out int i);
Assert.AreEqual(k_DummyValue, i);
DeltaRead = true;
}
public DummyNetVar(
NetworkVariableReadPermission readPerm = DefaultReadPerm,
NetworkVariableWritePermission writePerm = DefaultWritePerm) : base(readPerm, writePerm) { }
}
internal class DummyNetBehaviour : NetworkBehaviour
{
public static bool DistributedAuthority;
public DummyNetVar NetVar;
private void Awake()
{
if (DistributedAuthority)
{
NetVar = new DummyNetVar(writePerm: NetworkVariableWritePermission.Owner);
}
else
{
NetVar = new DummyNetVar();
}
}
public override void OnNetworkSpawn()
{
if ((NetworkManager.DistributedAuthorityMode && !IsOwner) || (!NetworkManager.DistributedAuthorityMode && !IsServer))
{
ClientDummyNetBehaviourSpawned(this);
}
base.OnNetworkSpawn();
}
}
protected override int NumberOfClients => 1;
public NetworkVarBufferCopyTest(HostOrServer hostOrServer) : base(hostOrServer) { }
private static List<DummyNetBehaviour> s_ClientDummyNetBehavioursSpawned = new List<DummyNetBehaviour>();
public static void ClientDummyNetBehaviourSpawned(DummyNetBehaviour dummyNetBehaviour)
{
s_ClientDummyNetBehavioursSpawned.Add(dummyNetBehaviour);
}
protected override IEnumerator OnSetup()
{
s_ClientDummyNetBehavioursSpawned.Clear();
return base.OnSetup();
}
protected override void OnCreatePlayerPrefab()
{
DummyNetBehaviour.DistributedAuthority = m_DistributedAuthority;
m_PlayerPrefab.AddComponent<DummyNetBehaviour>();
}
[UnityTest]
public IEnumerator TestEntireBufferIsCopiedOnNetworkVariableDelta()
{
// This is the *SERVER VERSION* of the *CLIENT PLAYER*
var serverClientPlayerResult = new NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject>();
yield return NetcodeIntegrationTestHelpers.GetNetworkObjectByRepresentation(
x => x.IsPlayerObject && x.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId,
m_ServerNetworkManager, serverClientPlayerResult);
var serverSideClientPlayer = serverClientPlayerResult.Result;
var serverComponent = serverSideClientPlayer.GetComponent<DummyNetBehaviour>();
// This is the *CLIENT VERSION* of the *CLIENT PLAYER*
var clientClientPlayerResult = new NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject>();
yield return NetcodeIntegrationTestHelpers.GetNetworkObjectByRepresentation(
x => x.IsPlayerObject && x.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId,
m_ClientNetworkManagers[0], clientClientPlayerResult);
var clientSideClientPlayer = clientClientPlayerResult.Result;
var clientComponent = clientSideClientPlayer.GetComponent<DummyNetBehaviour>();
// Wait for the DummyNetBehaviours on the client side to notify they have been initialized and spawned
yield return WaitForConditionOrTimeOut(() => s_ClientDummyNetBehavioursSpawned.Count >= 1);
Assert.IsFalse(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for client side DummyNetBehaviour to register it was spawned!");
// Check that FieldWritten is written when dirty
var authorityComponent = m_DistributedAuthority ? clientComponent : serverComponent;
var nonAuthorityComponent = m_DistributedAuthority ? serverComponent : clientComponent;
authorityComponent.NetVar.SetDirty(true);
yield return s_DefaultWaitForTick;
Assert.True(authorityComponent.NetVar.FieldWritten);
// Check that DeltaWritten is written when dirty
authorityComponent.NetVar.SetDirty(true);
yield return s_DefaultWaitForTick;
Assert.True(authorityComponent.NetVar.DeltaWritten);
// Check that both FieldRead and DeltaRead were invoked on the client side
yield return WaitForConditionOrTimeOut(() => nonAuthorityComponent.NetVar.FieldRead == true && nonAuthorityComponent.NetVar.DeltaRead == true);
var timedOutMessage = "Timed out waiting for client reads: ";
if (s_GlobalTimeoutHelper.TimedOut)
{
if (!nonAuthorityComponent.NetVar.FieldRead)
{
timedOutMessage += "[FieldRead]";
}
if (!nonAuthorityComponent.NetVar.DeltaRead)
{
timedOutMessage += "[DeltaRead]";
}
}
Assert.IsFalse(s_GlobalTimeoutHelper.TimedOut, timedOutMessage);
}
}
}