From 339e44cac7bf85c08bc2709c4b1f0092b057bb35 Mon Sep 17 00:00:00 2001 From: Zachary Ross Date: Sat, 9 Dec 2023 21:53:47 -0500 Subject: [PATCH] Fix AddBytes not resetting data segments --- RiptideNetworking/RiptideNetworking/Message.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/RiptideNetworking/RiptideNetworking/Message.cs b/RiptideNetworking/RiptideNetworking/Message.cs index ad164327..0b5b32eb 100644 --- a/RiptideNetworking/RiptideNetworking/Message.cs +++ b/RiptideNetworking/RiptideNetworking/Message.cs @@ -640,13 +640,20 @@ public Message AddBytes(byte[] array, bool includeLength = true) if (includeLength) AddVarULong((uint)array.Length); - if (UnwrittenBits < array.Length * BitsPerByte) + int writeAmount = array.Length * BitsPerByte; + if (UnwrittenBits < writeAmount) throw new InsufficientCapacityException(this, array.Length, ByteName, BitsPerByte); if (writeBit % BitsPerByte == 0) { + int bit = writeBit % BitsPerSegment; + if (bit + writeAmount > BitsPerSegment) // Range reaches into subsequent segment(s) + data[(writeBit + writeAmount) / BitsPerSegment] = 0; + else if (bit == 0) // Range doesn't fill the current segment, but begins the segment + data[writeBit / BitsPerSegment] = 0; + Buffer.BlockCopy(array, 0, data, writeBit / BitsPerByte, array.Length); - writeBit += array.Length * BitsPerByte; + writeBit += writeAmount; } else { @@ -656,7 +663,7 @@ public Message AddBytes(byte[] array, bool includeLength = true) writeBit += BitsPerByte; } } - + return this; }