Skip to content

Commit c384053

Browse files
authored
feat: FastBufferReader/Writer IsInitialized property (#1859)
* feat: FastBufferReader/Writer IsInitialized property
1 parent db94033 commit c384053

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1414
- Added `UnityTransport` implementation and `com.unity.transport` package dependency (#1823)
1515
- Added `NetworkVariableWritePermission` to `NetworkVariableBase` and implemented `Owner` client writable netvars. (#1762)
1616
- `UnityTransport` settings can now be set programmatically. (#1845)
17+
- `FastBufferWriter` and Reader IsInitialized property. (#1859)
1718

1819
### Changed
1920

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal struct ReaderHandle
1919
#endif
2020
}
2121

22-
internal readonly unsafe ReaderHandle* Handle;
22+
internal unsafe ReaderHandle* Handle;
2323

2424
/// <summary>
2525
/// Get the current read position
@@ -39,6 +39,11 @@ public unsafe int Length
3939
get => Handle->Length;
4040
}
4141

42+
/// <summary>
43+
/// Gets a value indicating whether the reader has been initialized and a handle allocated.
44+
/// </summary>
45+
public unsafe bool IsInitialized => Handle != null;
46+
4247
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4348
internal unsafe void CommitBitwiseReads(int amount)
4449
{
@@ -196,6 +201,7 @@ public unsafe FastBufferReader(FastBufferWriter writer, Allocator allocator, int
196201
public unsafe void Dispose()
197202
{
198203
UnsafeUtility.Free(Handle, Handle->Allocator);
204+
Handle = null;
199205
}
200206

201207
/// <summary>

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal struct WriterHandle
2222
#endif
2323
}
2424

25-
internal readonly unsafe WriterHandle* Handle;
25+
internal unsafe WriterHandle* Handle;
2626

2727
private static byte[] s_ByteArrayCache = new byte[65535];
2828

@@ -62,6 +62,11 @@ public unsafe int Length
6262
get => Handle->Position > Handle->Length ? Handle->Position : Handle->Length;
6363
}
6464

65+
/// <summary>
66+
/// Gets a value indicating whether the writer has been initialized and a handle allocated.
67+
/// </summary>
68+
public unsafe bool IsInitialized => Handle != null;
69+
6570

6671
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6772
internal unsafe void CommitBitwiseWrites(int amount)
@@ -111,6 +116,7 @@ public unsafe void Dispose()
111116
UnsafeUtility.Free(Handle->BufferPointer, Handle->Allocator);
112117
}
113118
UnsafeUtility.Free(Handle, Handle->Allocator);
119+
Handle = null;
114120
}
115121

116122
/// <summary>
@@ -207,7 +213,7 @@ internal unsafe void Grow(int additionalSizeRequired)
207213
/// When you know you will be writing multiple fields back-to-back and you know the total size,
208214
/// you can call TryBeginWrite() once on the total size, and then follow it with calls to
209215
/// WriteValue() instead of WriteValueSafe() for faster serialization.
210-
///
216+
///
211217
/// Unsafe write operations will throw OverflowException in editor and development builds if you
212218
/// go past the point you've marked using TryBeginWrite(). In release builds, OverflowException will not be thrown
213219
/// for performance reasons, since the point of using TryBeginWrite is to avoid bounds checking in the following
@@ -253,7 +259,7 @@ public unsafe bool TryBeginWrite(int bytes)
253259
/// When you know you will be writing multiple fields back-to-back and you know the total size,
254260
/// you can call TryBeginWrite() once on the total size, and then follow it with calls to
255261
/// WriteValue() instead of WriteValueSafe() for faster serialization.
256-
///
262+
///
257263
/// Unsafe write operations will throw OverflowException in editor and development builds if you
258264
/// go past the point you've marked using TryBeginWrite(). In release builds, OverflowException will not be thrown
259265
/// for performance reasons, since the point of using TryBeginWrite is to avoid bounds checking in the following

com.unity.netcode.gameobjects/Tests/Editor/Serialization/FastBufferReaderTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,34 @@ public void WhenCreatingAReaderFromAnEmptyBuffer_LengthIsZero()
393393
}
394394
}
395395

396+
[Test]
397+
public void WhenCreatingNewFastBufferReader_IsInitializedIsTrue()
398+
{
399+
var array = new NativeArray<byte>(100, Allocator.Temp);
400+
var reader = new FastBufferReader(array, Allocator.Temp);
401+
Assert.AreEqual(true, reader.IsInitialized);
402+
reader.Dispose();
403+
array.Dispose();
404+
}
405+
406+
[Test]
407+
public void WhenDisposingFastBufferReader_IsInitializedIsFalse()
408+
{
409+
var array = new NativeArray<byte>(100, Allocator.Temp);
410+
var reader = new FastBufferReader(array, Allocator.Temp);
411+
reader.Dispose();
412+
Assert.AreEqual(false, reader.IsInitialized);
413+
array.Dispose();
414+
Assert.AreEqual(false, reader.IsInitialized);
415+
}
416+
417+
[Test]
418+
public void WhenUsingDefaultFastBufferReader_IsInitializedIsFalse()
419+
{
420+
FastBufferReader writer = default;
421+
Assert.AreEqual(false, writer.IsInitialized);
422+
}
423+
396424
[Test]
397425
public void WhenCallingReadByteWithoutCallingTryBeingReadFirst_OverflowExceptionIsThrown()
398426
{

com.unity.netcode.gameobjects/Tests/Editor/Serialization/FastBufferWriterTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,29 @@ public void WhenCreatingNewFastBufferWriter_MaxCapacityIsCorrect()
891891
writer.Dispose();
892892
}
893893

894+
[Test]
895+
public void WhenCreatingNewFastBufferWriter_IsInitializedIsTrue()
896+
{
897+
var writer = new FastBufferWriter(100, Allocator.Temp);
898+
Assert.AreEqual(true, writer.IsInitialized);
899+
writer.Dispose();
900+
}
901+
902+
[Test]
903+
public void WhenDisposingFastBufferWriter_IsInitializedIsFalse()
904+
{
905+
var writer = new FastBufferWriter(100, Allocator.Temp);
906+
writer.Dispose();
907+
Assert.AreEqual(false, writer.IsInitialized);
908+
}
909+
910+
[Test]
911+
public void WhenUsingDefaultFastBufferWriter_IsInitializedIsFalse()
912+
{
913+
FastBufferWriter writer = default;
914+
Assert.AreEqual(false, writer.IsInitialized);
915+
}
916+
894917
[Test]
895918
public void WhenRequestingWritePastBoundsForNonGrowingWriter_TryBeginWriteReturnsFalse()
896919
{

0 commit comments

Comments
 (0)