Skip to content

Commit f978841

Browse files
Implement play state compression and handle compression being set twice (#607)
* Implement play state compression and handle compression being set twice * Fix 0 threshold; Code cleanup --------- Co-authored-by: FlorianMichael <[email protected]>
1 parent 1e2fcb9 commit f978841

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed

common/src/main/java/com/viaversion/viarewind/protocol/v1_8to1_7_6_10/Protocol1_8To1_7_6_10.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,8 @@ public void register() {
8181
map(Types.BYTE_ARRAY_PRIMITIVE, Types.SHORT_BYTE_ARRAY); // verification token
8282
}
8383
});
84-
this.registerClientbound(State.LOGIN, ClientboundLoginPackets.LOGIN_COMPRESSION, new PacketHandlers() {
85-
@Override
86-
public void register() {
87-
handler(wrapper -> {
88-
final int threshold = wrapper.read(Types.VAR_INT);
89-
90-
Via.getManager().getProviders().get(CompressionHandlerProvider.class).onHandleLoginCompressionPacket(wrapper.user(), threshold);
91-
wrapper.cancel();
92-
});
93-
}
94-
});
95-
this.cancelClientbound(ClientboundPackets1_8.SET_COMPRESSION); // unused
84+
this.registerClientbound(State.LOGIN, ClientboundLoginPackets.LOGIN_COMPRESSION, this::handleCompression);
85+
this.registerClientbound(ClientboundPackets1_8.SET_COMPRESSION, null, this::handleCompression);
9686
this.registerClientbound(ClientboundPackets1_8.KEEP_ALIVE, new PacketHandlers() {
9787
@Override
9888
public void register() {
@@ -116,6 +106,13 @@ public void register() {
116106
});
117107
}
118108

109+
private void handleCompression(final PacketWrapper wrapper) {
110+
wrapper.cancel();
111+
final int threshold = wrapper.read(Types.VAR_INT);
112+
113+
Via.getManager().getProviders().get(CompressionHandlerProvider.class).setCompressionThreshold(wrapper.user(), threshold);
114+
}
115+
119116
@Override
120117
public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws CancelException {
121118
Via.getManager().getProviders().get(CompressionHandlerProvider.class).onTransformPacket(packetWrapper.user());

common/src/main/java/com/viaversion/viarewind/protocol/v1_8to1_7_6_10/provider/CompressionHandlerProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@
2424

2525
public abstract class CompressionHandlerProvider implements Provider {
2626

27-
public abstract void onHandleLoginCompressionPacket(UserConnection user, int threshold);
27+
public abstract void setCompressionThreshold(UserConnection user, int threshold);
2828

2929
public abstract void onTransformPacket(UserConnection user);
3030

3131
public abstract ChannelHandler getEncoder(int threshold);
3232

3333
public abstract ChannelHandler getDecoder(int threshold);
3434

35-
public boolean isCompressionEnabled(UserConnection user) {
35+
public boolean isRemoveCompression(UserConnection user) {
3636
return user.get(CompressionStatusTracker.class).removeCompression;
3737
}
3838

39-
public void setCompressionEnabled(UserConnection user, boolean enabled) {
39+
public void setRemoveCompression(UserConnection user, boolean enabled) {
4040
user.get(CompressionStatusTracker.class).removeCompression = enabled;
4141
}
4242
}

common/src/main/java/com/viaversion/viarewind/protocol/v1_8to1_7_6_10/provider/compression/CompressionDecoder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@
2929
public class CompressionDecoder extends MessageToMessageDecoder<ByteBuf> {
3030
private final Inflater inflater = new Inflater();
3131

32-
private final int threshold;
32+
private int threshold;
3333

3434
public CompressionDecoder(final int threshold) {
3535
this.threshold = threshold;
3636
}
3737

38+
public void setThreshold(final int threshold) {
39+
this.threshold = threshold;
40+
}
41+
3842
@Override
3943
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
4044
if (!in.isReadable()) return;

common/src/main/java/com/viaversion/viarewind/protocol/v1_8to1_7_6_10/provider/compression/CompressionEncoder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@
2727
public class CompressionEncoder extends MessageToByteEncoder<ByteBuf> {
2828
private final Deflater deflater = new Deflater();
2929

30-
private final int threshold;
30+
private int threshold;
3131

3232
public CompressionEncoder(final int threshold) {
3333
this.threshold = threshold;
3434
}
3535

36+
public void setThreshold(final int threshold) {
37+
this.threshold = threshold;
38+
}
39+
3640
@Override
3741
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
3842
int frameLength = in.readableBytes();

common/src/main/java/com/viaversion/viarewind/protocol/v1_8to1_7_6_10/provider/compression/TrackingCompressionHandlerProvider.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,33 @@
2929
public class TrackingCompressionHandlerProvider extends CompressionHandlerProvider {
3030

3131
@Override
32-
public void onHandleLoginCompressionPacket(UserConnection user, int threshold) {
32+
public void setCompressionThreshold(UserConnection user, int threshold) {
3333
final ChannelPipeline pipeline = user.getChannel().pipeline();
34-
if (user.isClientSide()) {
35-
pipeline.addBefore(Via.getManager().getInjector().getEncoderName(), ViaRewind.getPlatform().compressHandlerName(), getEncoder(threshold));
36-
pipeline.addBefore(Via.getManager().getInjector().getDecoderName(), ViaRewind.getPlatform().decompressHandlerName(), getDecoder(threshold));
34+
if (!user.isClientSide() || threshold < 0) {
35+
setRemoveCompression(user, true); // We need to remove compression for 1.7 clients
36+
return;
37+
}
38+
39+
final String compressHandlerName = ViaRewind.getPlatform().compressHandlerName();
40+
final CompressionEncoder encoder = (CompressionEncoder) pipeline.get(compressHandlerName);
41+
if (encoder != null) {
42+
encoder.setThreshold(threshold);
43+
} else {
44+
pipeline.addBefore(Via.getManager().getInjector().getEncoderName(), compressHandlerName, getEncoder(threshold));
45+
}
46+
47+
final String decompressHandlerName = ViaRewind.getPlatform().decompressHandlerName();
48+
final CompressionDecoder decoder = (CompressionDecoder) pipeline.get(decompressHandlerName);
49+
if (decoder != null) {
50+
decoder.setThreshold(threshold);
3751
} else {
38-
setCompressionEnabled(user, true); // We need to remove compression for 1.7 clients
52+
pipeline.addBefore(Via.getManager().getInjector().getDecoderName(), decompressHandlerName, getDecoder(threshold));
3953
}
4054
}
4155

4256
@Override
4357
public void onTransformPacket(UserConnection user) {
44-
if (isCompressionEnabled(user)) {
58+
if (isRemoveCompression(user)) {
4559
final ChannelPipeline pipeline = user.getChannel().pipeline();
4660

4761
String compressor = null;
@@ -58,7 +72,7 @@ public void onTransformPacket(UserConnection user) {
5872
throw new IllegalStateException("Couldn't remove compression for 1.7!");
5973
}
6074

61-
setCompressionEnabled(user, false);
75+
setRemoveCompression(user, false);
6276
}
6377
}
6478

0 commit comments

Comments
 (0)