Skip to content
Open
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 @@ -23,6 +23,7 @@
import com.github.retrooper.packetevents.exception.InvalidDisconnectPacketSend;
import com.github.retrooper.packetevents.exception.PacketProcessException;
import com.github.retrooper.packetevents.protocol.ConnectionState;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.util.ExceptionUtil;
import com.github.retrooper.packetevents.util.PacketEventsImplHelper;
Expand All @@ -45,6 +46,7 @@

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

public class PacketEventsEncoder extends ChannelOutboundHandlerAdapter {
Expand All @@ -66,6 +68,7 @@ public class PacketEventsEncoder extends ChannelOutboundHandlerAdapter {

public User user;
public Player player;
private boolean handleCompression;
private boolean handledCompression = COMPRESSION_ENABLED_EVENT != null;

private final Queue<QueuedMessage> queuedMessages = new ArrayDeque<>();
Expand All @@ -78,6 +81,7 @@ public PacketEventsEncoder(User user) {
public PacketEventsEncoder(ChannelHandler encoder) {
user = ((PacketEventsEncoder) encoder).user;
player = ((PacketEventsEncoder) encoder).player;
handleCompression = ((PacketEventsEncoder) encoder).handleCompression;
handledCompression = ((PacketEventsEncoder) encoder).handledCompression;
}

Expand Down Expand Up @@ -106,8 +110,16 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)

PacketSendEvent packetSendEvent = null;
if (msg instanceof ByteBuf) {
boolean needsRecompression = !this.handledCompression && this.handleCompression(ctx, (ByteBuf) msg);
boolean needsRecompression = this.handleCompression(ctx, (ByteBuf) msg);
packetSendEvent = PacketEventsImplHelper.handleClientBoundPacket(ctx.channel(), this.user, this.player, msg, true);
if (!handledCompression && packetSendEvent != null) {
if (packetSendEvent.getConnectionState() == ConnectionState.PLAY) {
// Late injection or server doesn't have compression enabled
handledCompression = true;
} else if (packetSendEvent.getPacketType() == PacketType.Login.Server.SET_COMPRESSION) {
handleCompression = true;
}
}

// check if the packet got cancelled
if (!((ByteBuf) msg).isReadable()) {
Expand Down Expand Up @@ -222,11 +234,12 @@ private void decompress(ChannelHandlerContext ctx, ByteBuf input, ByteBuf output
}

private boolean handleCompression(ChannelHandlerContext ctx, ByteBuf buffer) throws InvocationTargetException {
if (handledCompression) return false;
int compressIndex = ctx.pipeline().names().indexOf("compress");
if (!handleCompression || handledCompression) return false;
List<String> handlerNames = ctx.pipeline().names();
int compressIndex = handlerNames.indexOf("compress");
if (compressIndex == -1) return false;
handledCompression = true;
int peEncoderIndex = ctx.pipeline().names().indexOf(PacketEvents.ENCODER_NAME);
int peEncoderIndex = handlerNames.indexOf(PacketEvents.ENCODER_NAME);
if (peEncoderIndex == -1) return false;
if (compressIndex > peEncoderIndex) {
//We are ahead of the decompression handler (they are added dynamically) so let us relocate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.github.retrooper.packetevents.exception.InvalidDisconnectPacketSend;
import com.github.retrooper.packetevents.exception.PacketProcessException;
import com.github.retrooper.packetevents.protocol.ConnectionState;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.util.ExceptionUtil;
import com.github.retrooper.packetevents.util.PacketEventsImplHelper;
Expand All @@ -37,12 +38,14 @@
import org.spongepowered.api.Sponge;

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

public class PacketEventsEncoder extends ChannelOutboundHandlerAdapter {

public User user;
public UUID player;
private boolean handleCompression;
private boolean handledCompression;

public PacketEventsEncoder(User user) {
Expand All @@ -52,6 +55,7 @@ public PacketEventsEncoder(User user) {
public PacketEventsEncoder(ChannelHandler encoder) {
user = ((PacketEventsEncoder) encoder).user;
player = ((PacketEventsEncoder) encoder).player;
handleCompression = ((PacketEventsEncoder) encoder).handleCompression;
handledCompression = ((PacketEventsEncoder) encoder).handledCompression;
}

Expand All @@ -61,9 +65,17 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
super.write(ctx, msg, promise);
return;
}
boolean needsRecompression = !handledCompression && handleCompression(ctx, byteBuf);
boolean needsRecompression = handleCompression(ctx, byteBuf);
Object player = this.player == null ? null : Sponge.server().player(this.player).orElse(null);
PacketSendEvent event = PacketEventsImplHelper.handleClientBoundPacket(ctx.channel(), user, player, byteBuf, true);
if (!handledCompression && event != null) {
if (event.getConnectionState() == ConnectionState.PLAY) {
// Late injection or server doesn't have compression enabled
handledCompression = true;
} else if (event.getPacketType() == PacketType.Login.Server.SET_COMPRESSION) {
handleCompression = true;
}
}

if (!byteBuf.isReadable()) {
byteBuf.release();
Expand Down Expand Up @@ -136,11 +148,12 @@ private void decompress(ChannelHandlerContext ctx, ByteBuf input, ByteBuf output
}

private boolean handleCompression(ChannelHandlerContext ctx, ByteBuf buffer) throws InvocationTargetException {
if (handledCompression) return false;
int compressIndex = ctx.pipeline().names().indexOf("compress");
if (!handleCompression || handledCompression) return false;
List<String> handlerNames = ctx.pipeline().names();
int compressIndex = handlerNames.indexOf("compress");
if (compressIndex == -1) return false;
handledCompression = true;
int peEncoderIndex = ctx.pipeline().names().indexOf(PacketEvents.ENCODER_NAME);
int peEncoderIndex = handlerNames.indexOf(PacketEvents.ENCODER_NAME);
if (peEncoderIndex == -1) return false;
if (compressIndex > peEncoderIndex) {
//We are ahead of the decompression handler (they are added dynamically) so let us relocate.
Expand Down