Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.09k stars 429 forks source link

FastBufferReader does not respect ArraySegment.Offset #2885

Open Risord opened 1 month ago

Risord commented 1 month ago

Description

var bytes = new byte[] { 0, 1, 2, 3 };
var segment = new ArraySegment<byte>(bytes, 1, 3);
var reader = new Unity.Netcode.FastBufferReader(segment, Unity.Collections.Allocator.Temp);
Debug.Log(string.Join(',', segment.ToArray())); // => 1,2,3
Debug.Log(string.Join(',', reader.ToArray())); // => 0,1,2

Expected Outcome

Both prints output: 1,2,3

Environment

NoelStephensUnity commented 1 month ago

@Risord Yeah, this indeed does look like it does not honor the ArraySegment and a fix would be for us to create a new constructor that does not include the length or offset parameters in order to be 100% sure of the intended usage.

For now, the work around would be to add the offset in the constuctor:

            var bytes = new byte[] { 0, 1, 2, 3 };
            var segment = new System.ArraySegment<byte>(bytes, 1, 3);
            var reader = new Unity.Netcode.FastBufferReader(segment, Unity.Collections.Allocator.Temp, offset: 1);