fix(core): Converts account packets to spanwriter (#381)

- [X] Converts account packets to spanwriter
This commit is contained in:
Kamron Batman 2021-01-04 18:47:20 -08:00 committed by GitHub
parent 3b413371dc
commit ac76c57c39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 157 additions and 117 deletions

View file

@ -125,6 +125,70 @@ namespace Server.Network
return outputIdx;
}
public static void Compress(ReadOnlySpan<byte> input, ref CircularBuffer<byte> output, out int length)
{
length = Compress(input, ref output);
}
public static int Compress(ReadOnlySpan<byte> input, ref CircularBuffer<byte> output)
{
if (input.Length > DefiniteOverflow)
{
return 0;
}
int bitCount = 0;
int bitValue = 0;
int inputIdx = 0;
int outputIdx = 0;
while (inputIdx < input.Length)
{
int i = input[inputIdx++] << 1;
bitCount += _huffmanTable[i];
bitValue = (bitValue << _huffmanTable[i]) | _huffmanTable[i + 1];
while (bitCount >= 8)
{
bitCount -= 8;
if (output.Length < outputIdx + 1)
{
return 0;
}
output[outputIdx++] = (byte)(bitValue >> bitCount);
}
}
// terminal code
bitCount += _huffmanTable[0x200];
bitValue = (bitValue << _huffmanTable[0x200]) | _huffmanTable[0x201];
// align on byte boundary
if ((bitCount & 7) != 0)
{
bitValue <<= 8 - (bitCount & 7);
bitCount += 8 - (bitCount & 7);
}
while (bitCount >= 8)
{
bitCount -= 8;
if (output.Length < outputIdx + 1)
{
return 0;
}
output[outputIdx++] = (byte)(bitValue >> bitCount);
}
return outputIdx;
}
public static int Compress(ReadOnlySpan<byte> input, Span<byte> output)
{
if (input.Length > DefiniteOverflow)