Skip to content

Commit

Permalink
Fix support for new bungee compression
Browse files Browse the repository at this point in the history
  • Loading branch information
booky10 committed Sep 17, 2024
1 parent 99acbe4 commit 909492c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageEncoder;
import net.md_5.bungee.api.connection.ProxiedPlayer;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

// Thanks to ViaVersion for the compression method.
@ChannelHandler.Sharable
public class PacketEventsEncoder extends MessageToByteEncoder<ByteBuf> {
public class PacketEventsEncoder extends MessageToMessageEncoder<ByteBuf> {
public ProxiedPlayer player;
public User user;
public boolean handledCompression;
Expand All @@ -44,7 +45,7 @@ public PacketEventsEncoder(User user) {
this.user = user;
}

public void read(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
public void read(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
boolean doCompression = handleCompressionOrder(ctx, buffer);
int firstReaderIndex = buffer.readerIndex();
PacketSendEvent packetSendEvent = EventCreationUtil.createSendEvent(ctx.channel(), user, player,
Expand All @@ -61,7 +62,9 @@ public void read(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
buffer.readerIndex(firstReaderIndex);
}
if (doCompression) {
recompress(ctx, buffer);
recompress(ctx, buffer, out);
} else {
out.add(buffer.retain());
}
} else {
ByteBufHelper.clear(packetSendEvent.getByteBuf());
Expand All @@ -74,12 +77,11 @@ public void read(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
}

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
if (!msg.isReadable()) {
return;
}
read(ctx, msg);
out.writeBytes(msg);
read(ctx, msg, out);
}

@Override
Expand Down Expand Up @@ -123,18 +125,12 @@ private boolean handleCompressionOrder(ChannelHandlerContext ctx, ByteBuf buffer
return false;
}

private void recompress(ChannelHandlerContext ctx, ByteBuf buffer) {
private void recompress(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) {
ChannelHandler compressor = ctx.pipeline().get("compress");
ByteBuf compressed = ctx.alloc().buffer();
try {
CustomPipelineUtil.callPacketEncodeByteBuf(compressor, ctx, buffer, compressed);
CustomPipelineUtil.callPacketEncodeByteBuf(compressor, ctx, buffer, out);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
try {
buffer.clear().writeBytes(compressed);
} finally {
compressed.release();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ public static List<Object> callMTMDecode(Object decoder, Object ctx, Object msg)
return output;
}

public static void callPacketEncodeByteBuf(Object encoder, Object ctx, Object msg, Object output) throws InvocationTargetException {
public static void callPacketEncodeByteBuf(Object encoder, Object ctx, Object msg, List<Object> output) throws InvocationTargetException {
if (BUNGEE_PACKET_ENCODE_BYTEBUF == null) {
try {
BUNGEE_PACKET_ENCODE_BYTEBUF = encoder.getClass()
.getDeclaredMethod("encode", ChannelHandlerContext.class, ByteBuf.class,
ByteBuf.class);
List.class);
BUNGEE_PACKET_ENCODE_BYTEBUF.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
Expand Down

0 comments on commit 909492c

Please sign in to comment.