Skip to content

Commit

Permalink
Fix AddBytes not resetting data segments
Browse files Browse the repository at this point in the history
Closes #124
  • Loading branch information
xzippyzachx committed Dec 10, 2023
1 parent 2e8691c commit 4352e8a
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions RiptideNetworking/RiptideNetworking/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -656,7 +663,7 @@ public Message AddBytes(byte[] array, bool includeLength = true)
writeBit += BitsPerByte;
}
}

return this;
}

Expand Down

0 comments on commit 4352e8a

Please sign in to comment.