Fix packet compression

This commit is contained in:
Pokechu22 2017-09-08 17:36:22 -07:00 committed by ORelio
parent 7ba0c3c8fc
commit c85352f819
2 changed files with 10 additions and 13 deletions

View file

@ -1421,12 +1421,10 @@ namespace MinecraftClient.Protocol.Handlers
if (compression_treshold > 0) //Compression enabled? if (compression_treshold > 0) //Compression enabled?
{ {
if (the_packet.Length > compression_treshold) //Packet long enough for compressing? if (the_packet.Length >= compression_treshold) //Packet long enough for compressing?
{ {
byte[] uncompressed_length = getVarInt(the_packet.Length);
byte[] compressed_packet = ZlibUtils.Compress(the_packet); byte[] compressed_packet = ZlibUtils.Compress(the_packet);
byte[] compressed_packet_length = getVarInt(compressed_packet.Length); the_packet = concatBytes(getVarInt(the_packet.Length), compressed_packet);
the_packet = concatBytes(compressed_packet_length, compressed_packet);
} }
else else
{ {

View file

@ -21,17 +21,16 @@ namespace MinecraftClient.Protocol.Handlers
/// <returns>Compressed data as a byte array</returns> /// <returns>Compressed data as a byte array</returns>
public static byte[] Compress(byte[] to_compress) public static byte[] Compress(byte[] to_compress)
{ {
ZlibStream stream = new ZlibStream(new System.IO.MemoryStream(to_compress, false), CompressionMode.Compress); byte[] data;
List<byte> temp_compression_list = new List<byte>(); using (System.IO.MemoryStream memstream = new System.IO.MemoryStream())
byte[] b = new byte[1];
while (true)
{ {
int read = stream.Read(b, 0, 1); using (ZlibStream stream = new ZlibStream(memstream, CompressionMode.Compress))
if (read > 0) { temp_compression_list.Add(b[0]); } {
else break; stream.Write(to_compress, 0, to_compress.Length);
}
data = memstream.ToArray();
} }
stream.Close(); return data;
return temp_compression_list.ToArray();
} }
/// <summary> /// <summary>