Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for new bungee compression #996

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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