diff --git a/Assets/Mirror/Core/NetworkReader.cs b/Assets/Mirror/Core/NetworkReader.cs index 141825ac6..0910f71b7 100644 --- a/Assets/Mirror/Core/NetworkReader.cs +++ b/Assets/Mirror/Core/NetworkReader.cs @@ -182,7 +182,7 @@ public ArraySegment ReadBytesSegment(int count) [MethodImpl(MethodImplOptions.AggressiveInlining)] public override string ToString() => - $"NetworkReader pos={Position} capacity={Capacity} buffer={BitConverter.ToString(buffer.Array, buffer.Offset, buffer.Count)}"; + $"[{buffer.ToHexString()} @ {Position}/{Capacity}]"; /// Reads any data type that mirror supports. Uses weaver populated Reader(T).read [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/Assets/Mirror/Tests/Editor/NetworkReaderTest.cs b/Assets/Mirror/Tests/Editor/NetworkReaderTest.cs index d54ff7613..c385df30e 100644 --- a/Assets/Mirror/Tests/Editor/NetworkReaderTest.cs +++ b/Assets/Mirror/Tests/Editor/NetworkReaderTest.cs @@ -102,5 +102,28 @@ public void ReadBytesSegment_CountNegative() ArraySegment result = reader.ReadBytesSegment(-1); }); } + // NetworkReader.ToString actually contained a bug once. + // ensure this never happens again. + [Test] + public void ToStringTest() + { + // byte[] with offset and count that's smaller than total length + byte[] data = {0xA1, 0xB2, 0xC3, 0xD4, 0xE5}; + ArraySegment segment = new ArraySegment(data, 1, 3); + NetworkReader reader = new NetworkReader(segment); + Assert.That(reader.ToString(), Is.EqualTo("[B2-C3-D4 @ 0/3]")); + + // different position + reader.Position = 1; + Assert.That(reader.ToString(), Is.EqualTo("[B2-C3-D4 @ 1/3]")); + + // byte[] with no offset and exact count + NetworkReader reader2 = new NetworkReader(data); + Assert.That(reader2.ToString(), Is.EqualTo("[A1-B2-C3-D4-E5 @ 0/5]")); + + // different position + reader2.Position = 1; + Assert.That(reader2.ToString(), Is.EqualTo("[A1-B2-C3-D4-E5 @ 1/5]")); + } } }