Skip to content

Commit

Permalink
Merge branch 'develop' into fix/anticipatednetworkvariable-not-updati…
Browse files Browse the repository at this point in the history
…ng-previousvalue-back-port
  • Loading branch information
NoelStephensUnity authored Feb 25, 2025
2 parents 432a54e + 66a7be4 commit 32fbd27
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Additional documentation and release notes are available at [Multiplayer Documen
## [Unreleased]

### Added
- Added `FastBufferReader(ArraySegment<byte> buffer, Allocator copyAllocator)` constructor that uses the `ArraySegment.Offset` as the `FastBufferReader` offset and the `ArraySegment.Count` as the `FastBufferReader` length. (#3320)
- Added `FastBufferReader(ArraySegment<byte> buffer, Allocator copyAllocator, int length = -1)` constructor that uses the `ArraySegment.Offset` as the `FastBufferReader` offset. (#3320)

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,59 @@ public unsafe FastBufferReader(ArraySegment<byte> buffer, Allocator copyAllocato
}
fixed (byte* data = buffer.Array)
{

Handle = CreateHandle(data, length == -1 ? buffer.Count : length, offset, copyAllocator, Allocator.Temp);
}
}

/// <summary>
/// Create a FastBufferReader from an ArraySegment that uses the ArraySegment.Offset for the reader's offset.
///
/// A new buffer will be created using the given allocator and the value will be copied in.
/// FastBufferReader will then own the data.
///
/// Allocator.None is not supported for byte[]. If you need this functionality, use a fixed() block
/// and ensure the FastBufferReader isn't used outside that block.
/// </summary>
/// <param name="buffer">The buffer to copy from</param>
/// <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>
/// <param name="length">The number of bytes to copy (all if this is -1)</param>
public unsafe FastBufferReader(ArraySegment<byte> buffer, Allocator copyAllocator, int length = -1)
{
if (copyAllocator == Allocator.None)
{
throw new NotSupportedException("Allocator.None cannot be used with managed source buffers.");
}
fixed (byte* data = buffer.Array)
{

Handle = CreateHandle(data, length == -1 ? buffer.Count : length, buffer.Offset, copyAllocator, Allocator.Temp);
}
}

/// <summary>
/// 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.
///
/// A new buffer will be created using the given allocator and the value will be copied in.
/// FastBufferReader will then own the data.
///
/// Allocator.None is not supported for byte[]. If you need this functionality, use a fixed() block
/// and ensure the FastBufferReader isn't used outside that block.
/// </summary>
/// <param name="buffer">The buffer to copy from</param>
/// <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>
public unsafe FastBufferReader(ArraySegment<byte> buffer, Allocator copyAllocator)
{
if (copyAllocator == Allocator.None)
{
throw new NotSupportedException("Allocator.None cannot be used with managed source buffers.");
}
fixed (byte* data = buffer.Array)
{
Handle = CreateHandle(data, buffer.Count, buffer.Offset, copyAllocator, Allocator.Temp);
}
}

/// <summary>
/// Create a FastBufferReader from an existing byte array.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1579,5 +1579,21 @@ public unsafe void WhenCallingTryBeginReadInternal_AllowedReadPositionDoesNotMov
Assert.AreEqual(reader.Handle->AllowedReadMark, 25);
}
}

[Test]
public unsafe void WhenUsingArraySegment_ConstructorHonorsArraySegmentConfiguration()
{
var bytes = new byte[] { 0, 1, 2, 3 };
var segment = new ArraySegment<byte>(bytes, 1, 3);
var reader = new FastBufferReader(segment, Allocator.Temp);

var readerArray = reader.ToArray();
Assert.True(readerArray.Length == bytes.Length - 1, $"Array of reader should have a length of {bytes.Length - 1} but was {readerArray.Length}!");
for (int i = 0; i < readerArray.Length; i++)
{
Assert.True(bytes[i + 1] == readerArray[i], $"Value of {nameof(readerArray)} at index {i} is {readerArray[i]} but should be {bytes[i + 1]}!");
}
reader.Dispose();
}
}
}
1 change: 1 addition & 0 deletions pvpExceptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@
"Unity.Netcode.EditorTests.FastBufferReaderTests: void WhenCallingTryBeginRead_TheAllowedReadPositionIsMarkedRelativeToCurrentPosition(): undocumented",
"Unity.Netcode.EditorTests.FastBufferReaderTests: void WhenReadingAfterSeeking_TheNewReadComesFromTheCorrectPosition(): undocumented",
"Unity.Netcode.EditorTests.FastBufferReaderTests: void WhenCallingTryBeginReadInternal_AllowedReadPositionDoesNotMoveBackward(): undocumented",
"Unity.Netcode.EditorTests.FastBufferReaderTests: void WhenUsingArraySegment_ConstructorHonorsArraySegmentConfiguration(): undocumented",
"Unity.Netcode.EditorTests.FastBufferWriterTests: undocumented",
"Unity.Netcode.EditorTests.FastBufferWriterTests: void RunTypeTest(T): undocumented",
"Unity.Netcode.EditorTests.FastBufferWriterTests: void RunTypeTestSafe(T): undocumented",
Expand Down

0 comments on commit 32fbd27

Please sign in to comment.