using System; using Unity.Collections; using UnityEngine; namespace Unity.Netcode { /// /// Two-way serializer wrapping FastBufferReader or FastBufferWriter. /// /// Implemented as a ref struct for two reasons: /// 1. The BufferSerializer cannot outlive the FBR/FBW it wraps or using it will cause a crash /// 2. The BufferSerializer must always be passed by reference and can't be copied /// /// Ref structs help enforce both of those rules: they can't ref live the stack context in which they were /// created, and they're always passed by reference no matter what. /// /// BufferSerializer doesn't wrapp FastBufferReader or FastBufferWriter directly because it can't. /// ref structs can't implement interfaces, and in order to be able to have two different implementations with /// the same interface (which allows us to avoid an "if(IsReader)" on every call), the thing directly wrapping /// the struct has to implement an interface. So IReaderWriter exists as the interface, /// which is implemented by a normal struct, while the ref struct wraps the normal one to enforce the two above /// requirements. (Allowing direct access to the IReaderWriter struct would allow dangerous /// things to happen because the struct's lifetime could outlive the Reader/Writer's.) /// /// The implementation struct public ref struct BufferSerializer where TReaderWriter : IReaderWriter { private TReaderWriter m_Implementation; /// /// Check if the contained implementation is a reader /// public bool IsReader => m_Implementation.IsReader; /// /// Check if the contained implementation is a writer /// public bool IsWriter => m_Implementation.IsWriter; internal BufferSerializer(TReaderWriter implementation) { m_Implementation = implementation; } /// /// Retrieves the FastBufferReader instance. Only valid if IsReader = true, throws /// InvalidOperationException otherwise. /// /// Reader instance public FastBufferReader GetFastBufferReader() { return m_Implementation.GetFastBufferReader(); } /// /// Retrieves the FastBufferWriter instance. Only valid if IsWriter = true, throws /// InvalidOperationException otherwise. /// /// Writer instance public FastBufferWriter GetFastBufferWriter() { return m_Implementation.GetFastBufferWriter(); } /// /// Read or write a string /// /// The value to read/write /// If true, characters will be limited to one-byte ASCII characters public void SerializeValue(ref string s, bool oneByteChars = false) => m_Implementation.SerializeValue(ref s, oneByteChars); /// /// Read or write a single byte /// /// The value to read/write public void SerializeValue(ref byte value) => m_Implementation.SerializeValue(ref value); /// /// Read or write a primitive value (int, bool, etc) /// Accepts any value that implements the given interfaces, but is not guaranteed to work correctly /// on values that are not primitives. /// /// The value to read/write /// An unused parameter used for enabling overload resolution based on generic constraints /// The type being serialized public void SerializeValue(ref T value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable, IEquatable => m_Implementation.SerializeValue(ref value); /// /// Read or write an array of primitive values (int, bool, etc) /// Accepts any value that implements the given interfaces, but is not guaranteed to work correctly /// on values that are not primitives. /// /// The values to read/write /// An unused parameter used for enabling overload resolution based on generic constraints /// The type being serialized public void SerializeValue(ref T[] value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable, IEquatable => m_Implementation.SerializeValue(ref value); /// /// Read or write an enum value /// /// The value to read/write /// An unused parameter used for enabling overload resolution based on generic constraints /// The type being serialized public void SerializeValue(ref T value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => m_Implementation.SerializeValue(ref value); /// /// Read or write an array of enum values /// /// The value to read/write /// An unused parameter used for enabling overload resolution based on generic constraints /// The type being serialized public void SerializeValue(ref T[] value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => m_Implementation.SerializeValue(ref value); /// /// Read or write a struct value implementing ISerializeByMemcpy /// /// The value to read/write /// An unused parameter used for enabling overload resolution based on generic constraints /// The type being serialized public void SerializeValue(ref T value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => m_Implementation.SerializeValue(ref value); /// /// Read or write an array of struct values implementing ISerializeByMemcpy /// /// The values to read/write /// An unused parameter used for enabling overload resolution based on generic constraints /// The type being serialized public void SerializeValue(ref T[] value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => m_Implementation.SerializeValue(ref value); /// /// Read or write a struct or class value implementing INetworkSerializable /// /// The value to read/write /// An unused parameter used for enabling overload resolution based on generic constraints /// The type being serialized public void SerializeValue(ref T value, FastBufferWriter.ForNetworkSerializable unused = default) where T : INetworkSerializable, new() => m_Implementation.SerializeValue(ref value); /// /// Read or write an array of struct or class values implementing INetworkSerializable /// /// The values to read/write /// An unused parameter used for enabling overload resolution based on generic constraints /// The type being serialized public void SerializeValue(ref T[] value, FastBufferWriter.ForNetworkSerializable unused = default) where T : INetworkSerializable, new() => m_Implementation.SerializeValue(ref value); /// /// Read or write a Vector2 value /// /// The value to read/write public void SerializeValue(ref Vector2 value) => m_Implementation.SerializeValue(ref value); /// /// Read or write an array of Vector2 values /// /// The values to read/write public void SerializeValue(ref Vector2[] value) => m_Implementation.SerializeValue(ref value); /// /// Read or write a Vector3 value /// /// The value to read/write public void SerializeValue(ref Vector3 value) => m_Implementation.SerializeValue(ref value); /// /// Read or write an array of Vector3 values /// /// The values to read/write public void SerializeValue(ref Vector3[] value) => m_Implementation.SerializeValue(ref value); /// /// Read or write a Vector2Int value /// /// The value to read/write public void SerializeValue(ref Vector2Int value) => m_Implementation.SerializeValue(ref value); /// /// Read or write an array of Vector2Int values /// /// The values to read/write public void SerializeValue(ref Vector2Int[] value) => m_Implementation.SerializeValue(ref value); /// /// Read or write a Vector3Int value /// /// The value to read/write public void SerializeValue(ref Vector3Int value) => m_Implementation.SerializeValue(ref value); /// /// Read or write an array of Vector3Int values /// /// The values to read/write public void SerializeValue(ref Vector3Int[] value) => m_Implementation.SerializeValue(ref value); /// /// Read or write a Vector4 value /// /// The value to read/write public void SerializeValue(ref Vector4 value) => m_Implementation.SerializeValue(ref value); /// /// Read or write an array of Vector4 values /// /// The values to read/write public void SerializeValue(ref Vector4[] value) => m_Implementation.SerializeValue(ref value); /// /// Read or write a Quaternion value /// /// The value to read/write public void SerializeValue(ref Quaternion value) => m_Implementation.SerializeValue(ref value); /// /// Read or write an array of Quaternion values /// /// The values to read/write public void SerializeValue(ref Quaternion[] value) => m_Implementation.SerializeValue(ref value); /// /// Read or write a Color value /// /// The value to read/write public void SerializeValue(ref Color value) => m_Implementation.SerializeValue(ref value); /// /// Read or write an array of Color values /// /// The values to read/write public void SerializeValue(ref Color[] value) => m_Implementation.SerializeValue(ref value); /// /// Read or write a Color32 value /// /// The value to read/write public void SerializeValue(ref Color32 value) => m_Implementation.SerializeValue(ref value); /// /// Read or write an array of Color32 values /// /// The values to read/write public void SerializeValue(ref Color32[] value) => m_Implementation.SerializeValue(ref value); /// /// Read or write a Ray value /// /// The value to read/write public void SerializeValue(ref Ray value) => m_Implementation.SerializeValue(ref value); /// /// Read or write an array of Ray values /// /// The values to read/write public void SerializeValue(ref Ray[] value) => m_Implementation.SerializeValue(ref value); /// /// Read or write a Ray2D value /// /// The value to read/write public void SerializeValue(ref Ray2D value) => m_Implementation.SerializeValue(ref value); /// /// Read or write an array of Ray2D values /// /// The values to read/write public void SerializeValue(ref Ray2D[] value) => m_Implementation.SerializeValue(ref value); // There are many FixedString types, but all of them share the interfaces INativeList and IUTF8Bytes. // INativeList provides the Length property // IUTF8Bytes provides GetUnsafePtr() // Those two are necessary to serialize FixedStrings efficiently // - otherwise we'd just be memcpy'ing the whole thing even if // most of it isn't used. /// /// Read or write a FixedString value /// /// The network serializable type /// The values to read/write /// An unused parameter used for enabling overload resolution of FixedStrings public void SerializeValue(ref T value, FastBufferWriter.ForFixedStrings unused = default) where T : unmanaged, INativeList, IUTF8Bytes => m_Implementation.SerializeValue(ref value); /// /// Read or write a NetworkSerializable value. /// SerializeValue() is the preferred method to do this - this is provided for backward compatibility only. /// /// The network serializable type /// The values to read/write public void SerializeNetworkSerializable(ref T value) where T : INetworkSerializable, new() => m_Implementation.SerializeNetworkSerializable(ref value); /// /// Performs an advance check to ensure space is available to read/write one or more values. /// This provides a performance benefit for serializing multiple values using the /// SerializeValuePreChecked methods. But note that the benefit is small and only likely to be /// noticeable if serializing a very large number of items. /// /// /// public bool PreCheck(int amount) { return m_Implementation.PreCheck(amount); } /// /// Serialize a string, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write /// If true, characters will be limited to one-byte ASCII characters public void SerializeValuePreChecked(ref string s, bool oneByteChars = false) => m_Implementation.SerializeValuePreChecked(ref s, oneByteChars); /// /// Serialize a byte, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref byte value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a primitive, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The network serializable type /// The value to read/write /// An unused parameter used for enabling overload resolution based on generic constraints public void SerializeValuePreChecked(ref T value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable, IEquatable => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize an array of primitives, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The network serializable types in an array /// The values to read/write /// An unused parameter used for enabling overload resolution of primitives public void SerializeValuePreChecked(ref T[] value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable, IEquatable => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize an enum, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The network serializable type /// The values to read/write /// An unused parameter used for enabling overload resolution of enums public void SerializeValuePreChecked(ref T value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize an array of enums, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The network serializable types in an array /// The values to read/write /// An unused parameter used for enabling overload resolution of enums public void SerializeValuePreChecked(ref T[] value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a struct, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The network serializable type /// The values to read/write /// An unused parameter used for enabling overload resolution of structs public void SerializeValuePreChecked(ref T value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize an array of structs, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The network serializable types in an array /// The values to read/write /// An unused parameter used for enabling overload resolution of structs public void SerializeValuePreChecked(ref T[] value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Vector2, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Vector2 value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Vector2 array, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The values to read/write public void SerializeValuePreChecked(ref Vector2[] value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Vector3, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Vector3 value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Vector3 array, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The values to read/write public void SerializeValuePreChecked(ref Vector3[] value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Vector2Int, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Vector2Int value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Vector2Int array, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The values to read/write public void SerializeValuePreChecked(ref Vector2Int[] value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Vector3Int, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Vector3Int value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Vector3Int array, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Vector3Int[] value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Vector4, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Vector4 value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Vector4Array, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Vector4[] value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Quaternion, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Quaternion value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Quaternion array, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Quaternion[] value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Color, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Color value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Color array, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Color[] value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Color32, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Color32 value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Color32 array, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Color32[] value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Ray, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Ray value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Ray array, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Ray[] value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Ray2D, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Ray2D value) => m_Implementation.SerializeValuePreChecked(ref value); /// /// Serialize a Ray2D array, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The value to read/write public void SerializeValuePreChecked(ref Ray2D[] value) => m_Implementation.SerializeValuePreChecked(ref value); // There are many FixedString types, but all of them share the interfaces INativeList and IUTF8Bytes. // INativeList provides the Length property // IUTF8Bytes provides GetUnsafePtr() // Those two are necessary to serialize FixedStrings efficiently // - otherwise we'd just be memcpying the whole thing even if // most of it isn't used. /// /// Serialize a FixedString, "pre-checked", which skips buffer checks. /// In debug and editor builds, a check is made to ensure you've called "PreCheck" before /// calling this. In release builds, calling this without calling "PreCheck" may read or write /// past the end of the buffer, which will cause memory corruption and undefined behavior. /// /// The network serializable type /// The value to read/write /// An unused parameter that can be used for enabling overload resolution for fixed strings public void SerializeValuePreChecked(ref T value, FastBufferWriter.ForFixedStrings unused = default) where T : unmanaged, INativeList, IUTF8Bytes => m_Implementation.SerializeValuePreChecked(ref value); } }