|
|
|
|
@@ -20,6 +20,8 @@ namespace Unity.Netcode
|
|
|
|
|
// of it to pass it as a ref parameter.
|
|
|
|
|
public void Write(FastBufferWriter writer, ref T value);
|
|
|
|
|
public void Read(FastBufferReader reader, ref T value);
|
|
|
|
|
internal void ReadWithAllocator(FastBufferReader reader, out T value, Allocator allocator);
|
|
|
|
|
public void Duplicate(in T value, ref T duplicatedValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
@@ -35,6 +37,16 @@ namespace Unity.Netcode
|
|
|
|
|
{
|
|
|
|
|
ByteUnpacker.ReadValueBitPacked(reader, out value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<short>.ReadWithAllocator(FastBufferReader reader, out short value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in short value, ref short duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
@@ -50,6 +62,16 @@ namespace Unity.Netcode
|
|
|
|
|
{
|
|
|
|
|
ByteUnpacker.ReadValueBitPacked(reader, out value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<ushort>.ReadWithAllocator(FastBufferReader reader, out ushort value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in ushort value, ref ushort duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
@@ -65,6 +87,16 @@ namespace Unity.Netcode
|
|
|
|
|
{
|
|
|
|
|
ByteUnpacker.ReadValueBitPacked(reader, out value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<int>.ReadWithAllocator(FastBufferReader reader, out int value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in int value, ref int duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
@@ -80,6 +112,16 @@ namespace Unity.Netcode
|
|
|
|
|
{
|
|
|
|
|
ByteUnpacker.ReadValueBitPacked(reader, out value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<uint>.ReadWithAllocator(FastBufferReader reader, out uint value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in uint value, ref uint duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
@@ -95,6 +137,16 @@ namespace Unity.Netcode
|
|
|
|
|
{
|
|
|
|
|
ByteUnpacker.ReadValueBitPacked(reader, out value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<long>.ReadWithAllocator(FastBufferReader reader, out long value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in long value, ref long duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
@@ -110,6 +162,16 @@ namespace Unity.Netcode
|
|
|
|
|
{
|
|
|
|
|
ByteUnpacker.ReadValueBitPacked(reader, out value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<ulong>.ReadWithAllocator(FastBufferReader reader, out ulong value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in ulong value, ref ulong duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
@@ -130,8 +192,84 @@ namespace Unity.Netcode
|
|
|
|
|
{
|
|
|
|
|
reader.ReadUnmanagedSafe(out value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<T>.ReadWithAllocator(FastBufferReader reader, out T value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in T value, ref T duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class UnmanagedArraySerializer<T> : INetworkVariableSerializer<NativeArray<T>> where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
public void Write(FastBufferWriter writer, ref NativeArray<T> value)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteUnmanagedSafe(value);
|
|
|
|
|
}
|
|
|
|
|
public void Read(FastBufferReader reader, ref NativeArray<T> value)
|
|
|
|
|
{
|
|
|
|
|
value.Dispose();
|
|
|
|
|
reader.ReadUnmanagedSafe(out value, Allocator.Persistent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<NativeArray<T>>.ReadWithAllocator(FastBufferReader reader, out NativeArray<T> value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
reader.ReadUnmanagedSafe(out value, allocator);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in NativeArray<T> value, ref NativeArray<T> duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
if (!duplicatedValue.IsCreated || duplicatedValue.Length != value.Length)
|
|
|
|
|
{
|
|
|
|
|
if (duplicatedValue.IsCreated)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
duplicatedValue = new NativeArray<T>(value.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
duplicatedValue.CopyFrom(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
|
|
|
|
|
internal class UnmanagedListSerializer<T> : INetworkVariableSerializer<NativeList<T>> where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
public void Write(FastBufferWriter writer, ref NativeList<T> value)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteUnmanagedSafe(value);
|
|
|
|
|
}
|
|
|
|
|
public void Read(FastBufferReader reader, ref NativeList<T> value)
|
|
|
|
|
{
|
|
|
|
|
reader.ReadUnmanagedSafeInPlace(ref value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<NativeList<T>>.ReadWithAllocator(FastBufferReader reader, out NativeList<T> value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in NativeList<T> value, ref NativeList<T> duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
if (!duplicatedValue.IsCreated)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue = new NativeList<T>(value.Length, Allocator.Persistent);
|
|
|
|
|
}
|
|
|
|
|
else if (value.Length != duplicatedValue.Length)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue.ResizeUninitialized(value.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
duplicatedValue.CopyFrom(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializer for FixedStrings
|
|
|
|
|
/// </summary>
|
|
|
|
|
@@ -146,8 +284,92 @@ namespace Unity.Netcode
|
|
|
|
|
{
|
|
|
|
|
reader.ReadValueSafeInPlace(ref value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<T>.ReadWithAllocator(FastBufferReader reader, out T value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in T value, ref T duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializer for FixedStrings
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
internal class FixedStringArraySerializer<T> : INetworkVariableSerializer<NativeArray<T>> where T : unmanaged, INativeList<byte>, IUTF8Bytes
|
|
|
|
|
{
|
|
|
|
|
public void Write(FastBufferWriter writer, ref NativeArray<T> value)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteValueSafe(value);
|
|
|
|
|
}
|
|
|
|
|
public void Read(FastBufferReader reader, ref NativeArray<T> value)
|
|
|
|
|
{
|
|
|
|
|
value.Dispose();
|
|
|
|
|
reader.ReadValueSafe(out value, Allocator.Persistent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<NativeArray<T>>.ReadWithAllocator(FastBufferReader reader, out NativeArray<T> value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
reader.ReadValueSafe(out value, allocator);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in NativeArray<T> value, ref NativeArray<T> duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
if (!duplicatedValue.IsCreated || duplicatedValue.Length != value.Length)
|
|
|
|
|
{
|
|
|
|
|
if (duplicatedValue.IsCreated)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
duplicatedValue = new NativeArray<T>(value.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
duplicatedValue.CopyFrom(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializer for FixedStrings
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
internal class FixedStringListSerializer<T> : INetworkVariableSerializer<NativeList<T>> where T : unmanaged, INativeList<byte>, IUTF8Bytes
|
|
|
|
|
{
|
|
|
|
|
public void Write(FastBufferWriter writer, ref NativeList<T> value)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteValueSafe(value);
|
|
|
|
|
}
|
|
|
|
|
public void Read(FastBufferReader reader, ref NativeList<T> value)
|
|
|
|
|
{
|
|
|
|
|
reader.ReadValueSafeInPlace(ref value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<NativeList<T>>.ReadWithAllocator(FastBufferReader reader, out NativeList<T> value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in NativeList<T> value, ref NativeList<T> duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
if (!duplicatedValue.IsCreated)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue = new NativeList<T>(value.Length, Allocator.Persistent);
|
|
|
|
|
}
|
|
|
|
|
else if (value.Length != duplicatedValue.Length)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue.ResizeUninitialized(value.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
duplicatedValue.CopyFrom(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializer for unmanaged INetworkSerializable types
|
|
|
|
|
/// </summary>
|
|
|
|
|
@@ -163,10 +385,93 @@ namespace Unity.Netcode
|
|
|
|
|
{
|
|
|
|
|
var bufferSerializer = new BufferSerializer<BufferSerializerReader>(new BufferSerializerReader(reader));
|
|
|
|
|
value.NetworkSerialize(bufferSerializer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<T>.ReadWithAllocator(FastBufferReader reader, out T value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in T value, ref T duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializer for unmanaged INetworkSerializable types
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
internal class UnmanagedNetworkSerializableArraySerializer<T> : INetworkVariableSerializer<NativeArray<T>> where T : unmanaged, INetworkSerializable
|
|
|
|
|
{
|
|
|
|
|
public void Write(FastBufferWriter writer, ref NativeArray<T> value)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteNetworkSerializable(value);
|
|
|
|
|
}
|
|
|
|
|
public void Read(FastBufferReader reader, ref NativeArray<T> value)
|
|
|
|
|
{
|
|
|
|
|
value.Dispose();
|
|
|
|
|
reader.ReadNetworkSerializable(out value, Allocator.Persistent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<NativeArray<T>>.ReadWithAllocator(FastBufferReader reader, out NativeArray<T> value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
reader.ReadNetworkSerializable(out value, allocator);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in NativeArray<T> value, ref NativeArray<T> duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
if (!duplicatedValue.IsCreated || duplicatedValue.Length != value.Length)
|
|
|
|
|
{
|
|
|
|
|
if (duplicatedValue.IsCreated)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
duplicatedValue = new NativeArray<T>(value.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
duplicatedValue.CopyFrom(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializer for unmanaged INetworkSerializable types
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
internal class UnmanagedNetworkSerializableListSerializer<T> : INetworkVariableSerializer<NativeList<T>> where T : unmanaged, INetworkSerializable
|
|
|
|
|
{
|
|
|
|
|
public void Write(FastBufferWriter writer, ref NativeList<T> value)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteNetworkSerializable(value);
|
|
|
|
|
}
|
|
|
|
|
public void Read(FastBufferReader reader, ref NativeList<T> value)
|
|
|
|
|
{
|
|
|
|
|
reader.ReadNetworkSerializableInPlace(ref value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<NativeList<T>>.ReadWithAllocator(FastBufferReader reader, out NativeList<T> value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in NativeList<T> value, ref NativeList<T> duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
if (!duplicatedValue.IsCreated)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue = new NativeList<T>(value.Length, Allocator.Persistent);
|
|
|
|
|
}
|
|
|
|
|
else if (value.Length != duplicatedValue.Length)
|
|
|
|
|
{
|
|
|
|
|
duplicatedValue.ResizeUninitialized(value.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
duplicatedValue.CopyFrom(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializer for managed INetworkSerializable types, which differs from the unmanaged implementation in that it
|
|
|
|
|
/// has to be null-aware
|
|
|
|
|
@@ -201,6 +506,21 @@ namespace Unity.Netcode
|
|
|
|
|
value.NetworkSerialize(bufferSerializer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<T>.ReadWithAllocator(FastBufferReader reader, out T value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in T value, ref T duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
using var writer = new FastBufferWriter(256, Allocator.Temp);
|
|
|
|
|
var refValue = value;
|
|
|
|
|
Write(writer, ref refValue);
|
|
|
|
|
|
|
|
|
|
using var reader = new FastBufferReader(writer, Allocator.None);
|
|
|
|
|
Read(reader, ref duplicatedValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
@@ -227,14 +547,26 @@ namespace Unity.Netcode
|
|
|
|
|
public delegate void ReadValueDelegate(FastBufferReader reader, out T value);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The <see cref="WriteValueDelegate"/> delegate handler declaration
|
|
|
|
|
/// The read value delegate handler definition
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="reader">The <see cref="FastBufferReader"/> to read the value of type `T`</param>
|
|
|
|
|
/// <param name="value">The value of type `T` to be read</param>
|
|
|
|
|
public delegate void DuplicateValueDelegate(in T value, ref T duplicatedValue);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Callback to write a value
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static WriteValueDelegate WriteValue;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The <see cref="ReadValueDelegate"/> delegate handler declaration
|
|
|
|
|
/// Callback to read a value
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static ReadValueDelegate ReadValue;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Callback to create a duplicate of a value, used to check for dirty status.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static DuplicateValueDelegate DuplicateValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
@@ -250,20 +582,34 @@ namespace Unity.Netcode
|
|
|
|
|
{
|
|
|
|
|
public void Write(FastBufferWriter writer, ref T value)
|
|
|
|
|
{
|
|
|
|
|
if (UserNetworkVariableSerialization<T>.ReadValue == null || UserNetworkVariableSerialization<T>.WriteValue == null)
|
|
|
|
|
if (UserNetworkVariableSerialization<T>.ReadValue == null || UserNetworkVariableSerialization<T>.WriteValue == null || UserNetworkVariableSerialization<T>.DuplicateValue == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException($"Type {typeof(T).FullName} is not supported by {typeof(NetworkVariable<>).Name}. If this is a type you can change, then either implement {nameof(INetworkSerializable)} or mark it as serializable by memcpy by adding {nameof(INetworkSerializeByMemcpy)} to its interface list. If not, assign serialization code to {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.WriteValue)} and {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.ReadValue)}, or if it's serializable by memcpy (contains no pointers), wrap it in {typeof(ForceNetworkSerializeByMemcpy<>).Name}.");
|
|
|
|
|
throw new ArgumentException($"Type {typeof(T).FullName} is not supported by {typeof(NetworkVariable<>).Name}. If this is a type you can change, then either implement {nameof(INetworkSerializable)} or mark it as serializable by memcpy by adding {nameof(INetworkSerializeByMemcpy)} to its interface list. If not, assign serialization code to {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.WriteValue)}, {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.ReadValue)}, and {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.DuplicateValue)}, or if it's serializable by memcpy (contains no pointers), wrap it in {typeof(ForceNetworkSerializeByMemcpy<>).Name}.");
|
|
|
|
|
}
|
|
|
|
|
UserNetworkVariableSerialization<T>.WriteValue(writer, value);
|
|
|
|
|
}
|
|
|
|
|
public void Read(FastBufferReader reader, ref T value)
|
|
|
|
|
{
|
|
|
|
|
if (UserNetworkVariableSerialization<T>.ReadValue == null || UserNetworkVariableSerialization<T>.WriteValue == null)
|
|
|
|
|
if (UserNetworkVariableSerialization<T>.ReadValue == null || UserNetworkVariableSerialization<T>.WriteValue == null || UserNetworkVariableSerialization<T>.DuplicateValue == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException($"Type {typeof(T).FullName} is not supported by {typeof(NetworkVariable<>).Name}. If this is a type you can change, then either implement {nameof(INetworkSerializable)} or mark it as serializable by memcpy by adding {nameof(INetworkSerializeByMemcpy)} to its interface list. If not, assign serialization code to {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.WriteValue)} and {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.ReadValue)}, or if it's serializable by memcpy (contains no pointers), wrap it in {typeof(ForceNetworkSerializeByMemcpy<>).Name}.");
|
|
|
|
|
throw new ArgumentException($"Type {typeof(T).FullName} is not supported by {typeof(NetworkVariable<>).Name}. If this is a type you can change, then either implement {nameof(INetworkSerializable)} or mark it as serializable by memcpy by adding {nameof(INetworkSerializeByMemcpy)} to its interface list. If not, assign serialization code to {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.WriteValue)}, {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.ReadValue)}, and {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.DuplicateValue)}, or if it's serializable by memcpy (contains no pointers), wrap it in {typeof(ForceNetworkSerializeByMemcpy<>).Name}.");
|
|
|
|
|
}
|
|
|
|
|
UserNetworkVariableSerialization<T>.ReadValue(reader, out value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void INetworkVariableSerializer<T>.ReadWithAllocator(FastBufferReader reader, out T value, Allocator allocator)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Duplicate(in T value, ref T duplicatedValue)
|
|
|
|
|
{
|
|
|
|
|
if (UserNetworkVariableSerialization<T>.ReadValue == null || UserNetworkVariableSerialization<T>.WriteValue == null || UserNetworkVariableSerialization<T>.DuplicateValue == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException($"Type {typeof(T).FullName} is not supported by {typeof(NetworkVariable<>).Name}. If this is a type you can change, then either implement {nameof(INetworkSerializable)} or mark it as serializable by memcpy by adding {nameof(INetworkSerializeByMemcpy)} to its interface list. If not, assign serialization code to {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.WriteValue)}, {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.ReadValue)}, and {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.DuplicateValue)}, or if it's serializable by memcpy (contains no pointers), wrap it in {typeof(ForceNetworkSerializeByMemcpy<>).Name}.");
|
|
|
|
|
}
|
|
|
|
|
UserNetworkVariableSerialization<T>.DuplicateValue(value, ref duplicatedValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
@@ -309,6 +655,26 @@ namespace Unity.Netcode
|
|
|
|
|
NetworkVariableSerialization<T>.Serializer = new UnmanagedTypeSerializer<T>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registeres an unmanaged type that will be serialized by a direct memcpy into a buffer
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public static void InitializeSerializer_UnmanagedByMemcpyArray<T>() where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
NetworkVariableSerialization<NativeArray<T>>.Serializer = new UnmanagedArraySerializer<T>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registeres an unmanaged type that will be serialized by a direct memcpy into a buffer
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public static void InitializeSerializer_UnmanagedByMemcpyList<T>() where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
NetworkVariableSerialization<NativeList<T>>.Serializer = new UnmanagedListSerializer<T>();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers an unmanaged type that implements INetworkSerializable and will be serialized through a call to
|
|
|
|
|
/// NetworkSerialize
|
|
|
|
|
@@ -319,6 +685,28 @@ namespace Unity.Netcode
|
|
|
|
|
NetworkVariableSerialization<T>.Serializer = new UnmanagedNetworkSerializableSerializer<T>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers an unmanaged type that implements INetworkSerializable and will be serialized through a call to
|
|
|
|
|
/// NetworkSerialize
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public static void InitializeSerializer_UnmanagedINetworkSerializableArray<T>() where T : unmanaged, INetworkSerializable
|
|
|
|
|
{
|
|
|
|
|
NetworkVariableSerialization<NativeArray<T>>.Serializer = new UnmanagedNetworkSerializableArraySerializer<T>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers an unmanaged type that implements INetworkSerializable and will be serialized through a call to
|
|
|
|
|
/// NetworkSerialize
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public static void InitializeSerializer_UnmanagedINetworkSerializableList<T>() where T : unmanaged, INetworkSerializable
|
|
|
|
|
{
|
|
|
|
|
NetworkVariableSerialization<NativeList<T>>.Serializer = new UnmanagedNetworkSerializableListSerializer<T>();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers a managed type that implements INetworkSerializable and will be serialized through a call to
|
|
|
|
|
/// NetworkSerialize
|
|
|
|
|
@@ -339,6 +727,28 @@ namespace Unity.Netcode
|
|
|
|
|
NetworkVariableSerialization<T>.Serializer = new FixedStringSerializer<T>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers a FixedString type that will be serialized through FastBufferReader/FastBufferWriter's FixedString
|
|
|
|
|
/// serializers
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public static void InitializeSerializer_FixedStringArray<T>() where T : unmanaged, INativeList<byte>, IUTF8Bytes
|
|
|
|
|
{
|
|
|
|
|
NetworkVariableSerialization<NativeArray<T>>.Serializer = new FixedStringArraySerializer<T>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers a FixedString type that will be serialized through FastBufferReader/FastBufferWriter's FixedString
|
|
|
|
|
/// serializers
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public static void InitializeSerializer_FixedStringList<T>() where T : unmanaged, INativeList<byte>, IUTF8Bytes
|
|
|
|
|
{
|
|
|
|
|
NetworkVariableSerialization<NativeList<T>>.Serializer = new FixedStringListSerializer<T>();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers a managed type that will be checked for equality using T.Equals()
|
|
|
|
|
/// </summary>
|
|
|
|
|
@@ -357,6 +767,26 @@ namespace Unity.Netcode
|
|
|
|
|
NetworkVariableSerialization<T>.AreEqual = NetworkVariableSerialization<T>.EqualityEquals;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers an unmanaged type that will be checked for equality using T.Equals()
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public static void InitializeEqualityChecker_UnmanagedIEquatableArray<T>() where T : unmanaged, IEquatable<T>
|
|
|
|
|
{
|
|
|
|
|
NetworkVariableSerialization<NativeArray<T>>.AreEqual = NetworkVariableSerialization<T>.EqualityEqualsArray;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers an unmanaged type that will be checked for equality using T.Equals()
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public static void InitializeEqualityChecker_UnmanagedIEquatableList<T>() where T : unmanaged, IEquatable<T>
|
|
|
|
|
{
|
|
|
|
|
NetworkVariableSerialization<NativeList<T>>.AreEqual = NetworkVariableSerialization<T>.EqualityEqualsList;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers an unmanaged type that will be checked for equality using memcmp and only considered
|
|
|
|
|
/// equal if they are bitwise equivalent in memory
|
|
|
|
|
@@ -367,6 +797,28 @@ namespace Unity.Netcode
|
|
|
|
|
NetworkVariableSerialization<T>.AreEqual = NetworkVariableSerialization<T>.ValueEquals;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers an unmanaged type that will be checked for equality using memcmp and only considered
|
|
|
|
|
/// equal if they are bitwise equivalent in memory
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public static void InitializeEqualityChecker_UnmanagedValueEqualsArray<T>() where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
NetworkVariableSerialization<NativeArray<T>>.AreEqual = NetworkVariableSerialization<T>.ValueEqualsArray;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers an unmanaged type that will be checked for equality using memcmp and only considered
|
|
|
|
|
/// equal if they are bitwise equivalent in memory
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public static void InitializeEqualityChecker_UnmanagedValueEqualsList<T>() where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
NetworkVariableSerialization<NativeList<T>>.AreEqual = NetworkVariableSerialization<T>.ValueEqualsList;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers a managed type that will be checked for equality using the == operator
|
|
|
|
|
/// </summary>
|
|
|
|
|
@@ -405,6 +857,58 @@ namespace Unity.Netcode
|
|
|
|
|
return UnsafeUtility.MemCmp(aptr, bptr, sizeof(TValueType)) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
|
|
|
|
|
// Compares two values of the same unmanaged type by underlying memory
|
|
|
|
|
// Ignoring any overridden value checks
|
|
|
|
|
// Size is fixed
|
|
|
|
|
internal static unsafe bool ValueEqualsList<TValueType>(ref NativeList<TValueType> a, ref NativeList<TValueType> b) where TValueType : unmanaged
|
|
|
|
|
{
|
|
|
|
|
if (a.IsCreated != b.IsCreated)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!a.IsCreated)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (a.Length != b.Length)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var aptr = (TValueType*)a.GetUnsafePtr();
|
|
|
|
|
var bptr = (TValueType*)b.GetUnsafePtr();
|
|
|
|
|
return UnsafeUtility.MemCmp(aptr, bptr, sizeof(TValueType) * a.Length) == 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Compares two values of the same unmanaged type by underlying memory
|
|
|
|
|
// Ignoring any overridden value checks
|
|
|
|
|
// Size is fixed
|
|
|
|
|
internal static unsafe bool ValueEqualsArray<TValueType>(ref NativeArray<TValueType> a, ref NativeArray<TValueType> b) where TValueType : unmanaged
|
|
|
|
|
{
|
|
|
|
|
if (a.IsCreated != b.IsCreated)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!a.IsCreated)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (a.Length != b.Length)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var aptr = (TValueType*)a.GetUnsafePtr();
|
|
|
|
|
var bptr = (TValueType*)b.GetUnsafePtr();
|
|
|
|
|
return UnsafeUtility.MemCmp(aptr, bptr, sizeof(TValueType) * a.Length) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static bool EqualityEqualsObject<TValueType>(ref TValueType a, ref TValueType b) where TValueType : class, IEquatable<TValueType>
|
|
|
|
|
{
|
|
|
|
|
if (a == null)
|
|
|
|
|
@@ -425,6 +929,74 @@ namespace Unity.Netcode
|
|
|
|
|
return a.Equals(b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
|
|
|
|
|
// Compares two values of the same unmanaged type by underlying memory
|
|
|
|
|
// Ignoring any overridden value checks
|
|
|
|
|
// Size is fixed
|
|
|
|
|
internal static unsafe bool EqualityEqualsList<TValueType>(ref NativeList<TValueType> a, ref NativeList<TValueType> b) where TValueType : unmanaged, IEquatable<TValueType>
|
|
|
|
|
{
|
|
|
|
|
if (a.IsCreated != b.IsCreated)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!a.IsCreated)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (a.Length != b.Length)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var aptr = (TValueType*)a.GetUnsafePtr();
|
|
|
|
|
var bptr = (TValueType*)b.GetUnsafePtr();
|
|
|
|
|
for (var i = 0; i < a.Length; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (!EqualityEquals(ref aptr[i], ref bptr[i]))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Compares two values of the same unmanaged type by underlying memory
|
|
|
|
|
// Ignoring any overridden value checks
|
|
|
|
|
// Size is fixed
|
|
|
|
|
internal static unsafe bool EqualityEqualsArray<TValueType>(ref NativeArray<TValueType> a, ref NativeArray<TValueType> b) where TValueType : unmanaged, IEquatable<TValueType>
|
|
|
|
|
{
|
|
|
|
|
if (a.IsCreated != b.IsCreated)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!a.IsCreated)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (a.Length != b.Length)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var aptr = (TValueType*)a.GetUnsafePtr();
|
|
|
|
|
var bptr = (TValueType*)b.GetUnsafePtr();
|
|
|
|
|
for (var i = 0; i < a.Length; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (!EqualityEquals(ref aptr[i], ref bptr[i]))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static bool ClassEquals<TValueType>(ref TValueType a, ref TValueType b) where TValueType : class
|
|
|
|
|
{
|
|
|
|
|
return a == b;
|
|
|
|
|
|