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-exp.4] - 2024-05-31 ### Added - Added `NetworkRigidbodyBase.AttachToFixedJoint` and `NetworkRigidbodyBase.DetachFromFixedJoint` to replace parenting for rigid bodies that have `NetworkRigidbodyBase.UseRigidBodyForMotion` enabled. (#2933) - Added `NetworkBehaviour.OnNetworkPreSpawn` and `NetworkBehaviour.OnNetworkPostSpawn` methods that provide the ability to handle pre and post spawning actions during the `NetworkObject` spawn sequence. (#2912) - Added a client-side only `NetworkBehaviour.OnNetworkSessionSynchronized` convenience method that is invoked on all `NetworkBehaviour`s after a newly joined client has finished synchronizing with the network session in progress. (#2912) - Added `NetworkBehaviour.OnInSceneObjectsSpawned` convenience method that is invoked when all in-scene `NetworkObject`s have been spawned after a scene has been loaded or upon a host or server starting. (#2912) ### Fixed - Fixed issue where non-authoritative rigid bodies with `NetworkRigidbodyBase.UseRigidBodyForMotion` enabled would constantly log errors about the renderTime being before `StartTimeConsumed`. (#2933) - Fixed issue where in-scene placed NetworkObjects could be destroyed if a client disconnects early and/or before approval. (#2924) - Fixed issue where a `NetworkObject` component's associated `NetworkBehaviour` components would not be detected if scene loading is disabled in the editor and the currently loaded scene has in-scene placed `NetworkObject`s. (#2912) - Fixed issue where an in-scene placed `NetworkObject` with `NetworkTransform` that is also parented under a `GameObject` would not properly synchronize when the parent `GameObject` had a world space position other than 0,0,0. (#2898) ### Changed - Change all the access modifiers of test class from Public to Internal (#2930) - Changed messages are now sorted by enum values as opposed to ordinally sorting the messages by their type name. (#2929) - Changed `NetworkClient.SessionModeTypes` to `NetworkClient.NetworkTopologyTypes`. (#2875) - Changed `NetworkClient.SessionModeType` to `NetworkClient.NetworkTopologyType`. (#2875) - Changed `NetworkConfig.SessionMode` to `NeworkConfig.NetworkTopology`. (#2875)
372 lines
13 KiB
C#
372 lines
13 KiB
C#
using System;
|
|
using NUnit.Framework;
|
|
using Unity.Collections;
|
|
|
|
namespace Unity.Netcode.EditorTests
|
|
{
|
|
internal class BitReaderTests
|
|
{
|
|
[Test]
|
|
public void TestReadingOneBit()
|
|
{
|
|
var writer = new FastBufferWriter(4, Allocator.Temp);
|
|
using (writer)
|
|
{
|
|
Assert.IsTrue(writer.TryBeginWrite(3));
|
|
using (var bitWriter = writer.EnterBitwiseContext())
|
|
{
|
|
bitWriter.WriteBit(true);
|
|
|
|
bitWriter.WriteBit(true);
|
|
|
|
bitWriter.WriteBit(false);
|
|
bitWriter.WriteBit(true);
|
|
|
|
bitWriter.WriteBit(false);
|
|
bitWriter.WriteBit(false);
|
|
bitWriter.WriteBit(false);
|
|
bitWriter.WriteBit(true);
|
|
|
|
bitWriter.WriteBit(false);
|
|
bitWriter.WriteBit(true);
|
|
bitWriter.WriteBit(false);
|
|
bitWriter.WriteBit(true);
|
|
}
|
|
|
|
writer.WriteByte(0b11111111);
|
|
|
|
var reader = new FastBufferReader(writer, Allocator.Temp);
|
|
using (reader)
|
|
{
|
|
Assert.IsTrue(reader.TryBeginRead(3));
|
|
using (var bitReader = reader.EnterBitwiseContext())
|
|
{
|
|
bool b;
|
|
bitReader.ReadBit(out b);
|
|
Assert.IsTrue(b);
|
|
bitReader.ReadBit(out b);
|
|
Assert.IsTrue(b);
|
|
bitReader.ReadBit(out b);
|
|
Assert.IsFalse(b);
|
|
bitReader.ReadBit(out b);
|
|
Assert.IsTrue(b);
|
|
|
|
bitReader.ReadBit(out b);
|
|
Assert.IsFalse(b);
|
|
bitReader.ReadBit(out b);
|
|
Assert.IsFalse(b);
|
|
bitReader.ReadBit(out b);
|
|
Assert.IsFalse(b);
|
|
bitReader.ReadBit(out b);
|
|
Assert.IsTrue(b);
|
|
|
|
bitReader.ReadBit(out b);
|
|
Assert.IsFalse(b);
|
|
bitReader.ReadBit(out b);
|
|
Assert.IsTrue(b);
|
|
bitReader.ReadBit(out b);
|
|
Assert.IsFalse(b);
|
|
bitReader.ReadBit(out b);
|
|
Assert.IsTrue(b);
|
|
}
|
|
|
|
reader.ReadByte(out byte lastByte);
|
|
Assert.AreEqual(0b11111111, lastByte);
|
|
}
|
|
}
|
|
}
|
|
[Test]
|
|
public unsafe void TestTryBeginReadBits()
|
|
{
|
|
var nativeArray = new NativeArray<byte>(4, Allocator.Temp);
|
|
var reader = new FastBufferReader(nativeArray, Allocator.Temp);
|
|
nativeArray.Dispose();
|
|
using (reader)
|
|
{
|
|
int* asInt = (int*)reader.GetUnsafePtr();
|
|
*asInt = 0b11111111_00001010_10101011;
|
|
|
|
using (var bitReader = reader.EnterBitwiseContext())
|
|
{
|
|
Assert.Throws<InvalidOperationException>(() => reader.TryBeginRead(1));
|
|
Assert.Throws<InvalidOperationException>(() => reader.TryBeginReadValue(1));
|
|
Assert.IsTrue(bitReader.TryBeginReadBits(1));
|
|
bitReader.ReadBit(out bool b);
|
|
Assert.IsTrue(b);
|
|
|
|
// Can't use Assert.Throws() because ref struct BitWriter can't be captured in a lambda
|
|
try
|
|
{
|
|
bitReader.ReadBit(out b);
|
|
}
|
|
catch (OverflowException)
|
|
{
|
|
// Should get called here.
|
|
}
|
|
Assert.IsTrue(bitReader.TryBeginReadBits(3));
|
|
bitReader.ReadBit(out b);
|
|
Assert.IsTrue(b);
|
|
bitReader.ReadBit(out b);
|
|
Assert.IsFalse(b);
|
|
bitReader.ReadBit(out b);
|
|
Assert.IsTrue(b);
|
|
|
|
byte byteVal;
|
|
try
|
|
{
|
|
bitReader.ReadBits(out byteVal, 4);
|
|
}
|
|
catch (OverflowException)
|
|
{
|
|
// Should get called here.
|
|
}
|
|
|
|
try
|
|
{
|
|
bitReader.ReadBits(out byteVal, 1);
|
|
}
|
|
catch (OverflowException)
|
|
{
|
|
// Should get called here.
|
|
}
|
|
Assert.IsTrue(bitReader.TryBeginReadBits(3));
|
|
|
|
try
|
|
{
|
|
bitReader.ReadBits(out byteVal, 4);
|
|
}
|
|
catch (OverflowException)
|
|
{
|
|
// Should get called here.
|
|
}
|
|
Assert.IsTrue(bitReader.TryBeginReadBits(4));
|
|
bitReader.ReadBits(out byteVal, 3);
|
|
Assert.AreEqual(0b010, byteVal);
|
|
|
|
Assert.IsTrue(bitReader.TryBeginReadBits(5));
|
|
|
|
bitReader.ReadBits(out byteVal, 5);
|
|
Assert.AreEqual(0b10101, byteVal);
|
|
}
|
|
|
|
Assert.AreEqual(2, reader.Position);
|
|
|
|
Assert.IsTrue(reader.TryBeginRead(1));
|
|
reader.ReadByte(out byte nextByte);
|
|
Assert.AreEqual(0b11111111, nextByte);
|
|
|
|
Assert.IsTrue(reader.TryBeginRead(1));
|
|
reader.ReadByte(out nextByte);
|
|
Assert.AreEqual(0b00000000, nextByte);
|
|
|
|
Assert.IsFalse(reader.TryBeginRead(1));
|
|
using (var bitReader = reader.EnterBitwiseContext())
|
|
{
|
|
Assert.IsFalse(bitReader.TryBeginReadBits(1));
|
|
}
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void TestReadingMultipleBits()
|
|
{
|
|
var writer = new FastBufferWriter(4, Allocator.Temp);
|
|
using (writer)
|
|
{
|
|
Assert.IsTrue(writer.TryBeginWrite(3));
|
|
using (var bitWriter = writer.EnterBitwiseContext())
|
|
{
|
|
bitWriter.WriteBits(0b11111111, 1);
|
|
bitWriter.WriteBits(0b11111111, 1);
|
|
bitWriter.WriteBits(0b11111110, 2);
|
|
bitWriter.WriteBits(0b11111000, 4);
|
|
bitWriter.WriteBits(0b11111010, 4);
|
|
}
|
|
writer.WriteByte(0b11111111);
|
|
|
|
|
|
var reader = new FastBufferReader(writer, Allocator.Temp);
|
|
using (reader)
|
|
{
|
|
Assert.IsTrue(reader.TryBeginRead(3));
|
|
using (var bitReader = reader.EnterBitwiseContext())
|
|
{
|
|
byte b;
|
|
bitReader.ReadBits(out b, 1);
|
|
Assert.AreEqual(0b1, b);
|
|
|
|
bitReader.ReadBits(out b, 1);
|
|
Assert.AreEqual(0b1, b);
|
|
|
|
bitReader.ReadBits(out b, 2);
|
|
Assert.AreEqual(0b10, b);
|
|
|
|
bitReader.ReadBits(out b, 4);
|
|
Assert.AreEqual(0b1000, b);
|
|
|
|
bitReader.ReadBits(out b, 4);
|
|
Assert.AreEqual(0b1010, b);
|
|
}
|
|
|
|
reader.ReadByte(out byte lastByte);
|
|
Assert.AreEqual(0b11111111, lastByte);
|
|
}
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void TestReadingMultipleBitsToLongs()
|
|
{
|
|
var writer = new FastBufferWriter(4, Allocator.Temp);
|
|
using (writer)
|
|
{
|
|
Assert.IsTrue(writer.TryBeginWrite(3));
|
|
using (var bitWriter = writer.EnterBitwiseContext())
|
|
{
|
|
bitWriter.WriteBits(0b11111111UL, 1);
|
|
bitWriter.WriteBits(0b11111111UL, 1);
|
|
bitWriter.WriteBits(0b11111110UL, 2);
|
|
bitWriter.WriteBits(0b11111000UL, 4);
|
|
bitWriter.WriteBits(0b11111010UL, 4);
|
|
}
|
|
|
|
writer.WriteByte(0b11111111);
|
|
|
|
var reader = new FastBufferReader(writer, Allocator.Temp);
|
|
using (reader)
|
|
{
|
|
Assert.IsTrue(reader.TryBeginRead(3));
|
|
using (var bitReader = reader.EnterBitwiseContext())
|
|
{
|
|
ulong ul;
|
|
bitReader.ReadBits(out ul, 1);
|
|
Assert.AreEqual(0b1, ul);
|
|
|
|
bitReader.ReadBits(out ul, 1);
|
|
Assert.AreEqual(0b1, ul);
|
|
|
|
bitReader.ReadBits(out ul, 2);
|
|
Assert.AreEqual(0b10, ul);
|
|
|
|
bitReader.ReadBits(out ul, 4);
|
|
Assert.AreEqual(0b1000, ul);
|
|
|
|
bitReader.ReadBits(out ul, 4);
|
|
Assert.AreEqual(0b1010, ul);
|
|
}
|
|
|
|
reader.ReadByte(out byte lastByte);
|
|
Assert.AreEqual(0b11111111, lastByte);
|
|
}
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public unsafe void TestReadingMultipleBytesToLongs([Range(1U, 64U)] uint numBits)
|
|
{
|
|
ulong value = 0xFFFFFFFFFFFFFFFF;
|
|
var reader = new FastBufferReader((byte*)&value, Allocator.Temp, sizeof(ulong));
|
|
using (reader)
|
|
{
|
|
ulong* asUlong = (ulong*)reader.GetUnsafePtr();
|
|
|
|
Assert.AreEqual(value, *asUlong);
|
|
var mask = 0UL;
|
|
for (var i = 0; i < numBits; ++i)
|
|
{
|
|
mask |= (1UL << i);
|
|
}
|
|
|
|
ulong readValue;
|
|
|
|
Assert.IsTrue(reader.TryBeginRead(sizeof(ulong)));
|
|
using (var bitReader = reader.EnterBitwiseContext())
|
|
{
|
|
bitReader.ReadBits(out readValue, numBits);
|
|
}
|
|
Assert.AreEqual(value & mask, readValue);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public unsafe void TestReadingMultipleBytesToLongsMisaligned([Range(1U, 63U)] uint numBits)
|
|
{
|
|
ulong value = 0b01010101_10101010_01010101_10101010_01010101_10101010_01010101_10101010;
|
|
var reader = new FastBufferReader((byte*)&value, Allocator.Temp, sizeof(ulong));
|
|
using (reader)
|
|
{
|
|
ulong* asUlong = (ulong*)reader.GetUnsafePtr();
|
|
|
|
Assert.AreEqual(value, *asUlong);
|
|
var mask = 0UL;
|
|
for (var i = 0; i < numBits; ++i)
|
|
{
|
|
mask |= (1UL << i);
|
|
}
|
|
|
|
ulong readValue;
|
|
|
|
Assert.IsTrue(reader.TryBeginRead(sizeof(ulong)));
|
|
using (var bitReader = reader.EnterBitwiseContext())
|
|
{
|
|
bitReader.ReadBit(out bool unused);
|
|
bitReader.ReadBits(out readValue, numBits);
|
|
}
|
|
Assert.AreEqual((value >> 1) & mask, readValue);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public unsafe void TestReadingBitsThrowsIfTryBeginReadNotCalled()
|
|
{
|
|
var nativeArray = new NativeArray<byte>(4, Allocator.Temp);
|
|
var reader = new FastBufferReader(nativeArray, Allocator.Temp);
|
|
nativeArray.Dispose();
|
|
using (reader)
|
|
{
|
|
int* asInt = (int*)reader.GetUnsafePtr();
|
|
*asInt = 0b11111111_00001010_10101011;
|
|
|
|
Assert.Throws<OverflowException>(() =>
|
|
{
|
|
using var bitReader = reader.EnterBitwiseContext();
|
|
bitReader.ReadBit(out bool b);
|
|
});
|
|
|
|
Assert.Throws<OverflowException>(() =>
|
|
{
|
|
using var bitReader = reader.EnterBitwiseContext();
|
|
bitReader.ReadBits(out byte b, 1);
|
|
});
|
|
|
|
Assert.Throws<OverflowException>(() =>
|
|
{
|
|
using var bitReader = reader.EnterBitwiseContext();
|
|
bitReader.ReadBits(out ulong ul, 1);
|
|
});
|
|
|
|
Assert.AreEqual(0, reader.Position);
|
|
|
|
Assert.Throws<OverflowException>(() =>
|
|
{
|
|
Assert.IsTrue(reader.TryBeginRead(1));
|
|
using var bitReader = reader.EnterBitwiseContext();
|
|
ulong ul;
|
|
try
|
|
{
|
|
bitReader.ReadBits(out ul, 4);
|
|
bitReader.ReadBits(out ul, 4);
|
|
}
|
|
catch (OverflowException)
|
|
{
|
|
Assert.Fail("Overflow exception was thrown too early.");
|
|
throw;
|
|
}
|
|
bitReader.ReadBits(out ul, 4);
|
|
});
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|