From 892ac6f6263f00c85d0e358d6efaa4ee646f6c3f Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Thu, 21 Jan 2021 17:57:40 -0500 Subject: [PATCH] Accept changes to compression treshold on the fly. Vanilla allows this for some reason, and there has been one case where Velocity's strict behavior has caused a bug. Fix this. --- .../proxy/connection/MinecraftConnection.java | 27 ++++++++++++------- .../netty/MinecraftCompressDecoder.java | 6 ++++- .../netty/MinecraftCompressEncoder.java | 6 ++++- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java index 3571f7a0f..42b3a766f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java @@ -382,16 +382,25 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { if (threshold == -1) { channel.pipeline().remove(COMPRESSION_DECODER); channel.pipeline().remove(COMPRESSION_ENCODER); - return; + } else { + MinecraftCompressDecoder decoder = (MinecraftCompressDecoder) channel.pipeline() + .get(COMPRESSION_DECODER); + MinecraftCompressEncoder encoder = (MinecraftCompressEncoder) channel.pipeline() + .get(COMPRESSION_ENCODER); + if (decoder != null && encoder != null) { + decoder.setThreshold(threshold); + encoder.setThreshold(threshold); + } else { + int level = server.getConfiguration().getCompressionLevel(); + VelocityCompressor compressor = Natives.compress.get().create(level); + + encoder = new MinecraftCompressEncoder(threshold, compressor); + decoder = new MinecraftCompressDecoder(threshold, compressor); + + channel.pipeline().addBefore(MINECRAFT_DECODER, COMPRESSION_DECODER, decoder); + channel.pipeline().addBefore(MINECRAFT_ENCODER, COMPRESSION_ENCODER, encoder); + } } - - int level = server.getConfiguration().getCompressionLevel(); - VelocityCompressor compressor = Natives.compress.get().create(level); - MinecraftCompressEncoder encoder = new MinecraftCompressEncoder(threshold, compressor); - MinecraftCompressDecoder decoder = new MinecraftCompressDecoder(threshold, compressor); - - channel.pipeline().addBefore(MINECRAFT_DECODER, COMPRESSION_DECODER, decoder); - channel.pipeline().addBefore(MINECRAFT_ENCODER, COMPRESSION_ENCODER, encoder); } /** diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressDecoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressDecoder.java index a083a9291..21b958b05 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressDecoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressDecoder.java @@ -20,7 +20,7 @@ public class MinecraftCompressDecoder extends MessageToMessageDecoder { Boolean.getBoolean("velocity.increased-compression-cap") ? HARD_MAXIMUM_UNCOMPRESSED_SIZE : VANILLA_MAXIMUM_UNCOMPRESSED_SIZE; - private final int threshold; + private int threshold; private final VelocityCompressor compressor; public MinecraftCompressDecoder(int threshold, VelocityCompressor compressor) { @@ -60,4 +60,8 @@ public class MinecraftCompressDecoder extends MessageToMessageDecoder { public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { compressor.close(); } + + public void setThreshold(int threshold) { + this.threshold = threshold; + } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressEncoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressEncoder.java index 23c67c024..aad607602 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressEncoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressEncoder.java @@ -9,7 +9,7 @@ import io.netty.handler.codec.MessageToByteEncoder; public class MinecraftCompressEncoder extends MessageToByteEncoder { - private final int threshold; + private int threshold; private final VelocityCompressor compressor; public MinecraftCompressEncoder(int threshold, VelocityCompressor compressor) { @@ -54,4 +54,8 @@ public class MinecraftCompressEncoder extends MessageToByteEncoder { public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { compressor.close(); } + + public void setThreshold(int threshold) { + this.threshold = threshold; + } }