2using System.Runtime.CompilerServices;
3using Unity.Collections.LowLevel.Unsafe;
11 public const int MaxStringLength = 1024 * 32;
16 byte[] buffer =
new byte[1500];
24 [MethodImpl(MethodImplOptions.AggressiveInlining)]
33 [MethodImpl(MethodImplOptions.AggressiveInlining)]
34 void EnsureCapacity(
int value)
36 if (buffer.Length < value)
38 int capacity = Math.Max(value, buffer.Length * 2);
39 Array.Resize(ref buffer, capacity);
44 [MethodImpl(MethodImplOptions.AggressiveInlining)]
48 Array.ConstrainedCopy(buffer, 0, data, 0,
Position);
53 [MethodImpl(MethodImplOptions.AggressiveInlining)]
56 return new ArraySegment<byte>(buffer, 0,
Position);
105 [MethodImpl(MethodImplOptions.AggressiveInlining)]
106 internal unsafe
void WriteBlittable<T>(T value)
111 if (!UnsafeUtility.IsBlittable(typeof(T)))
113 Debug.LogError($
"{typeof(T)} is not blittable!");
123 int size =
sizeof(T);
132 fixed (
byte* ptr = &buffer[
Position])
148 T* valueBuffer = stackalloc T[1]{value};
149 UnsafeUtility.MemCpy(ptr, valueBuffer, size);
159 [MethodImpl(MethodImplOptions.AggressiveInlining)]
160 internal void WriteBlittableNullable<T>(T? value)
164 WriteByte((
byte)(value.HasValue ? 0x01 : 0x00));
168 WriteBlittable(value.Value);
171 public void WriteByte(
byte value) => WriteBlittable(value);
175 public void WriteBytes(
byte[] buffer,
int offset,
int count)
178 Array.ConstrainedCopy(buffer, offset, this.buffer,
Position, count);
183 [MethodImpl(MethodImplOptions.AggressiveInlining)]
186 Action<NetworkWriter, T> writeDelegate =
Writer<T>.write;
187 if (writeDelegate ==
null)
189 Debug.LogError($
"No writer found for {typeof(T)}. This happens either if you are missing a NetworkWriter extension for your custom type, or if weaving failed. Try to reimport a script to weave again.");
193 writeDelegate(
this, value);
203 public static Action<NetworkWriter, T> write;
Network Writer for most simple types like floats, ints, buffers, structs, etc. Use NetworkWriterPool....
void Reset()
Reset both the position and length of the stream
void Write< T >(T value)
Writes any type that mirror supports. Uses weaver populated Writer(T).write.
ArraySegment< byte > ToArraySegment()
Returns allocation-free ArraySegment until 'Position'.
byte[] ToArray()
Copies buffer until 'Position' to a new array.
int Position
Next position to write to the buffer
Helper class that weaver populates with all writer types.