Skip to content

Commit 84d2b65

Browse files
fix
Fixing issue where FastBufferReader did not provide constructors to honor an ArraySegment's configuration.
1 parent 6ecfcc1 commit 84d2b65

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

com.unity.netcode.gameobjects/Runtime/Serialization/FastBufferReader.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,54 @@ public unsafe FastBufferReader(ArraySegment<byte> buffer, Allocator copyAllocato
140140
}
141141
}
142142

143+
/// <summary>
144+
/// Create a FastBufferReader from an ArraySegment that uses the ArraySegment.Offset for the reader's offset.
145+
///
146+
/// A new buffer will be created using the given allocator and the value will be copied in.
147+
/// FastBufferReader will then own the data.
148+
///
149+
/// Allocator.None is not supported for byte[]. If you need this functionality, use a fixed() block
150+
/// and ensure the FastBufferReader isn't used outside that block.
151+
/// </summary>
152+
/// <param name="buffer">The buffer to copy from</param>
153+
/// <param name="copyAllocator">The allocator type used for internal data when copying an existing buffer if other than Allocator.None is specified, that memory will be owned by this FastBufferReader instance</param>
154+
/// <param name="length">The number of bytes to copy (all if this is -1)</param>
155+
public unsafe FastBufferReader(ArraySegment<byte> buffer, Allocator copyAllocator, int length = -1)
156+
{
157+
if (copyAllocator == Allocator.None)
158+
{
159+
throw new NotSupportedException("Allocator.None cannot be used with managed source buffers.");
160+
}
161+
fixed (byte* data = buffer.Array)
162+
{
163+
164+
Handle = CreateHandle(data, length == -1 ? buffer.Count : length, buffer.Offset, copyAllocator, Allocator.Temp);
165+
}
166+
}
167+
168+
/// <summary>
169+
/// Create a FastBufferReader from an ArraySegment that uses the ArraySegment.Offset for the reader's offset and the ArraySegment.Count for the reader's length.
170+
///
171+
/// A new buffer will be created using the given allocator and the value will be copied in.
172+
/// FastBufferReader will then own the data.
173+
///
174+
/// Allocator.None is not supported for byte[]. If you need this functionality, use a fixed() block
175+
/// and ensure the FastBufferReader isn't used outside that block.
176+
/// </summary>
177+
/// <param name="buffer">The buffer to copy from</param>
178+
/// <param name="copyAllocator">The allocator type used for internal data when copying an existing buffer if other than Allocator.None is specified, that memory will be owned by this FastBufferReader instance</param>
179+
public unsafe FastBufferReader(ArraySegment<byte> buffer, Allocator copyAllocator)
180+
{
181+
if (copyAllocator == Allocator.None)
182+
{
183+
throw new NotSupportedException("Allocator.None cannot be used with managed source buffers.");
184+
}
185+
fixed (byte* data = buffer.Array)
186+
{
187+
Handle = CreateHandle(data, buffer.Count, buffer.Offset, copyAllocator, Allocator.Temp);
188+
}
189+
}
190+
143191
/// <summary>
144192
/// Create a FastBufferReader from an existing byte array.
145193
///

0 commit comments

Comments
 (0)