using System; using System.Runtime.CompilerServices; using UnityEngine; namespace Unity.Netcode { /// /// Utility class for packing values in serialization. /// to unpack packed values. /// public static class BytePacker { #if UNITY_NETCODE_DEBUG_NO_PACKING [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteValuePacked(FastBufferWriter writer, T value) where T: unmanaged => writer.WriteValueSafe(value); #else /// /// Write a packed enum value. /// /// The writer to write to /// The value to write /// An enum type [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void WriteValuePacked(FastBufferWriter writer, TEnum value) where TEnum : unmanaged, Enum { TEnum enumValue = value; switch (sizeof(TEnum)) { case sizeof(int): WriteValuePacked(writer, *(int*)&enumValue); break; case sizeof(byte): WriteValuePacked(writer, *(byte*)&enumValue); break; case sizeof(short): WriteValuePacked(writer, *(short*)&enumValue); break; case sizeof(long): WriteValuePacked(writer, *(long*)&enumValue); break; } } /// /// Write single-precision floating point value to the buffer as a varint /// /// The writer to write to /// Value to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, float value) { WriteValueBitPacked(writer, ToUint(value)); } /// /// Write double-precision floating point value to the buffer as a varint /// /// The writer to write to /// Value to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, double value) { WriteValueBitPacked(writer, ToUlong(value)); } /// /// Write a byte to the buffer. /// /// The writer to write to /// Value to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, byte value) => writer.WriteByteSafe(value); /// /// Write a signed byte to the buffer. /// /// The writer to write to /// Value to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, sbyte value) => writer.WriteByteSafe((byte)value); /// /// Write a bool to the buffer. /// /// The writer to write to /// Value to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, bool value) => writer.WriteValueSafe(value); /// /// Write a signed short (Int16) as a ZigZag encoded varint to the buffer. /// WARNING: If the value you're writing is > 2287, this will use MORE space /// (3 bytes instead of 2), and if your value is > 240 you'll get no savings at all. /// Only use this if you're certain your value will be small. /// /// The writer to write to /// Value to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, short value) => WriteValueBitPacked(writer, value); /// /// Write an unsigned short (UInt16) as a varint to the buffer. /// WARNING: If the value you're writing is > 2287, this will use MORE space /// (3 bytes instead of 2), and if your value is > 240 you'll get no savings at all. /// Only use this if you're certain your value will be small. /// /// The writer to write to /// Value to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, ushort value) => WriteValueBitPacked(writer, value); /// /// Write a two-byte character as a varint to the buffer. /// WARNING: If the value you're writing is > 2287, this will use MORE space /// (3 bytes instead of 2), and if your value is > 240 you'll get no savings at all. /// Only use this if you're certain your value will be small. /// /// The writer to write to /// Value to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, char c) => WriteValueBitPacked(writer, c); /// /// Write a signed int (Int32) as a ZigZag encoded varint to the buffer. /// /// The writer to write to /// Value to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, int value) => WriteValueBitPacked(writer, value); /// /// Write an unsigned int (UInt32) to the buffer. /// /// The writer to write to /// Value to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, uint value) => WriteValueBitPacked(writer, value); /// /// Write an unsigned long (UInt64) to the buffer. /// /// The writer to write to /// Value to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, ulong value) => WriteValueBitPacked(writer, value); /// /// Write a signed long (Int64) as a ZigZag encoded varint to the buffer. /// /// The writer to write to /// Value to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, long value) => WriteValueBitPacked(writer, value); /// /// Convenience method that writes two packed Vector3 from the ray to the buffer /// /// The writer to write to /// Ray to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, Ray ray) { WriteValuePacked(writer, ray.origin); WriteValuePacked(writer, ray.direction); } /// /// Convenience method that writes two packed Vector2 from the ray to the buffer /// /// The writer to write to /// Ray2D to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, Ray2D ray2d) { WriteValuePacked(writer, ray2d.origin); WriteValuePacked(writer, ray2d.direction); } /// /// Convenience method that writes four varint floats from the color to the buffer /// /// The writer to write to /// Color to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, Color color) { WriteValuePacked(writer, color.r); WriteValuePacked(writer, color.g); WriteValuePacked(writer, color.b); WriteValuePacked(writer, color.a); } /// /// Convenience method that writes four varint floats from the color to the buffer /// /// The writer to write to /// Color to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, Color32 color) { WriteValuePacked(writer, color.r); WriteValuePacked(writer, color.g); WriteValuePacked(writer, color.b); WriteValuePacked(writer, color.a); } /// /// Convenience method that writes two varint floats from the vector to the buffer /// /// The writer to write to /// Vector to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, Vector2 vector2) { WriteValuePacked(writer, vector2.x); WriteValuePacked(writer, vector2.y); } /// /// Convenience method that writes three varint floats from the vector to the buffer /// /// The writer to write to /// Vector to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, Vector3 vector3) { WriteValuePacked(writer, vector3.x); WriteValuePacked(writer, vector3.y); WriteValuePacked(writer, vector3.z); } /// /// Convenience method that writes four varint floats from the vector to the buffer /// /// The writer to write to /// Vector to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, Vector4 vector4) { WriteValuePacked(writer, vector4.x); WriteValuePacked(writer, vector4.y); WriteValuePacked(writer, vector4.z); WriteValuePacked(writer, vector4.w); } /// /// Writes the rotation to the buffer. /// /// The writer to write to /// Rotation to write [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, Quaternion rotation) { WriteValuePacked(writer, rotation.x); WriteValuePacked(writer, rotation.y); WriteValuePacked(writer, rotation.z); WriteValuePacked(writer, rotation.w); } /// /// Writes a string in a packed format /// /// The writer to write to /// The value to pack [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteValuePacked(FastBufferWriter writer, string s) { WriteValuePacked(writer, (uint)s.Length); int target = s.Length; for (int i = 0; i < target; ++i) { WriteValuePacked(writer, s[i]); } } #endif #if UNITY_NETCODE_DEBUG_NO_PACKING [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteValueBitPacked(FastBufferWriter writer, T value) where T: unmanaged => writer.WriteValueSafe(value); #else /// /// Obsolete value that no longer carries meaning. Do not use. /// public const ushort BitPackedUshortMax = (1 << 15) - 1; /// /// Obsolete value that no longer carries meaning. Do not use. /// public const short BitPackedShortMax = (1 << 14) - 1; /// /// Obsolete value that no longer carries meaning. Do not use. /// public const short BitPackedShortMin = -(1 << 14); /// /// Obsolete value that no longer carries meaning. Do not use. /// public const uint BitPackedUintMax = (1 << 30) - 1; /// /// Obsolete value that no longer carries meaning. Do not use. /// public const int BitPackedIntMax = (1 << 29) - 1; /// /// Obsolete value that no longer carries meaning. Do not use. /// public const int BitPackedIntMin = -(1 << 29); /// /// Obsolete value that no longer carries meaning. Do not use. /// public const ulong BitPackedULongMax = (1L << 61) - 1; /// /// Obsolete value that no longer carries meaning. Do not use. /// public const long BitPackedLongMax = (1L << 60) - 1; /// /// Obsolete value that no longer carries meaning. Do not use. /// public const long BitPackedLongMin = -(1L << 60); /// /// Writes a 16-bit signed short to the buffer in a bit-encoded packed format. /// Zig-zag encoding is used to move the sign bit to the least significant bit, so that negative values /// are still able to be compressed. /// The first two bits indicate whether the value is 1, 2, or 3 bytes. /// If the value uses 14 bits or less, the remaining 14 bits contain the value. /// For performance, reasons, if the value is 15 bits or more, there will be six 0 bits, followed /// by the original unmodified 16-bit value in the next 2 bytes. /// /// The writer to write to /// The value to pack public static void WriteValueBitPacked(FastBufferWriter writer, short value) => WriteValueBitPacked(writer, (ushort)Arithmetic.ZigZagEncode(value)); /// /// Writes a 16-bit unsigned short to the buffer in a bit-encoded packed format. /// The first two bits indicate whether the value is 1, 2, or 3 bytes. /// If the value uses 14 bits or less, the remaining 14 bits contain the value. /// For performance, reasons, if the value is 15 bits or more, there will be six 0 bits, followed /// by the original unmodified 16-bit value in the next 2 bytes. /// /// The writer to write to /// The value to pack public static void WriteValueBitPacked(FastBufferWriter writer, ushort value) { if (value > (1 << 14) - 1) { if (!writer.TryBeginWriteInternal(3)) { throw new OverflowException("Writing past the end of the buffer"); } writer.WriteByte(3); writer.WriteValue(value); return; } value <<= 2; var numBytes = BitCounter.GetUsedByteCount(value); if (!writer.TryBeginWriteInternal(numBytes)) { throw new OverflowException("Writing past the end of the buffer"); } writer.WritePartialValue(value | (ushort)(numBytes), numBytes); } /// /// Writes a 32-bit signed int to the buffer in a bit-encoded packed format. /// Zig-zag encoding is used to move the sign bit to the least significant bit, so that negative values /// are still able to be compressed. /// The first three bits indicate whether the value is 1, 2, 3, 4, or 5 bytes. /// If the value uses 29 bits or less, the remaining 29 bits contain the value. /// For performance, reasons, if the value is 30 bits or more, there will be five 0 bits, followed /// by the original unmodified 32-bit value in the next 4 bytes. /// /// The writer to write to /// The value to pack public static void WriteValueBitPacked(FastBufferWriter writer, int value) => WriteValueBitPacked(writer, (uint)Arithmetic.ZigZagEncode(value)); /// /// Writes a 32-bit unsigned int to the buffer in a bit-encoded packed format. /// The first three bits indicate whether the value is 1, 2, 3, 4, or 5 bytes. /// If the value uses 29 bits or less, the remaining 29 bits contain the value. /// For performance, reasons, if the value is 30 bits or more, there will be five 0 bits, followed /// by the original unmodified 32-bit value in the next 4 bytes. /// /// The writer to write to /// The value to pack public static void WriteValueBitPacked(FastBufferWriter writer, uint value) { if (value > (1 << 29) - 1) { if (!writer.TryBeginWriteInternal(5)) { throw new OverflowException("Writing past the end of the buffer"); } writer.WriteByte(5); writer.WriteValue(value); return; } value <<= 3; var numBytes = BitCounter.GetUsedByteCount(value); if (!writer.TryBeginWriteInternal(numBytes)) { throw new OverflowException("Writing past the end of the buffer"); } writer.WritePartialValue(value | (uint)(numBytes), numBytes); } /// /// Writes a 64-bit signed long to the buffer in a bit-encoded packed format. /// Zig-zag encoding is used to move the sign bit to the least significant bit, so that negative values /// are still able to be compressed. /// The first four bits indicate whether the value is 1, 2, 3, 4, 5, 6, 7, 8, or 9 bytes. /// If the value uses 60 bits or less, the remaining 60 bits contain the value. /// For performance, reasons, if the value is 61 bits or more, there will be four 0 bits, followed /// by the original unmodified 64-bit value in the next 8 bytes. /// /// The writer to write to /// The value to pack public static void WriteValueBitPacked(FastBufferWriter writer, long value) => WriteValueBitPacked(writer, Arithmetic.ZigZagEncode(value)); /// /// Writes a 64-bit unsigned long to the buffer in a bit-encoded packed format. /// The first four bits indicate whether the value is 1, 2, 3, 4, 5, 6, 7, 8, or 9 bytes. /// If the value uses 60 bits or less, the remaining 60 bits contain the value. /// For performance, reasons, if the value is 61 bits or more, there will be four 0 bits, followed /// by the original unmodified 64-bit value in the next 8 bytes. /// /// The writer to write to /// The value to pack public static void WriteValueBitPacked(FastBufferWriter writer, ulong value) { if (value > (1L << 60) - 1) { if (!writer.TryBeginWriteInternal(9)) { throw new OverflowException("Writing past the end of the buffer"); } writer.WriteByte(9); writer.WriteValue(value); return; } value <<= 4; var numBytes = BitCounter.GetUsedByteCount(value); if (!writer.TryBeginWriteInternal(numBytes)) { throw new OverflowException("Writing past the end of the buffer"); } writer.WritePartialValue(value | (uint)(numBytes), numBytes); } #endif [MethodImpl(MethodImplOptions.AggressiveInlining)] private static unsafe uint ToUint(T value) where T : unmanaged { uint* asUint = (uint*)&value; return *asUint; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static unsafe ulong ToUlong(T value) where T : unmanaged { ulong* asUlong = (ulong*)&value; return *asUlong; } } }