Don't rely on this data always being the same here

This commit is contained in:
RednedEpic 2022-05-29 19:51:36 -05:00
parent 3a51f530c6
commit 8b4a2aaac8
3 changed files with 15 additions and 19 deletions

View file

@ -14,14 +14,10 @@ import java.util.List;
public class TcpPacketCodec extends ByteToMessageCodec<Packet> {
private final Session session;
private final PacketProtocol packetProtocol;
private final PacketCodecHelper codecHelper;
private final boolean client;
public TcpPacketCodec(Session session, boolean client) {
this.session = session;
this.packetProtocol = session.getPacketProtocol();
this.codecHelper = session.getCodecHelper();
this.client = client;
}
@ -30,12 +26,14 @@ public class TcpPacketCodec extends ByteToMessageCodec<Packet> {
public void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf buf) throws Exception {
int initial = buf.writerIndex();
PacketProtocol packetProtocol = this.session.getPacketProtocol();
PacketCodecHelper codecHelper = this.session.getCodecHelper();
try {
int packetId = this.client ? this.packetProtocol.getServerboundId(packet) : this.packetProtocol.getClientboundId(packet);
PacketDefinition definition = this.client ? this.packetProtocol.getServerboundDefinition(packetId) : this.packetProtocol.getClientboundDefinition(packetId);
int packetId = this.client ? packetProtocol.getServerboundId(packet) : packetProtocol.getClientboundId(packet);
PacketDefinition definition = this.client ? packetProtocol.getServerboundDefinition(packetId) : packetProtocol.getClientboundDefinition(packetId);
this.packetProtocol.getPacketHeader().writePacketId(buf, this.codecHelper, packetId);
definition.getSerializer().serialize(buf, this.codecHelper, packet);
packetProtocol.getPacketHeader().writePacketId(buf, codecHelper, packetId);
definition.getSerializer().serialize(buf, codecHelper, packet);
} catch (Throwable t) {
// Reset writer index to make sure incomplete data is not written out.
buf.writerIndex(initial);
@ -52,14 +50,16 @@ public class TcpPacketCodec extends ByteToMessageCodec<Packet> {
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
int initial = buf.readerIndex();
PacketProtocol packetProtocol = this.session.getPacketProtocol();
PacketCodecHelper codecHelper = this.session.getCodecHelper();
try {
int id = this.packetProtocol.getPacketHeader().readPacketId(buf, this.codecHelper);
int id = packetProtocol.getPacketHeader().readPacketId(buf, codecHelper);
if (id == -1) {
buf.readerIndex(initial);
return;
}
Packet packet = this.client ? this.packetProtocol.createClientboundPacket(id, buf, this.codecHelper) : this.packetProtocol.createServerboundPacket(id, buf, this.codecHelper);
Packet packet = this.client ? packetProtocol.createClientboundPacket(id, buf, codecHelper) : packetProtocol.createServerboundPacket(id, buf, codecHelper);
if (buf.readableBytes() > 0) {
throw new IllegalStateException("Packet \"" + packet.getClass().getSimpleName() + "\" not fully read.");

View file

@ -16,7 +16,6 @@ public class TcpPacketCompression extends ByteToMessageCodec<ByteBuf> {
private static final int MAX_COMPRESSED_SIZE = 2097152;
private final Session session;
private final PacketCodecHelper codecHelper;
private final Deflater deflater = new Deflater();
private final Inflater inflater = new Inflater();
private final byte[] buf = new byte[8192];
@ -24,7 +23,6 @@ public class TcpPacketCompression extends ByteToMessageCodec<ByteBuf> {
public TcpPacketCompression(Session session, boolean validateDecompression) {
this.session = session;
this.codecHelper = session.getCodecHelper();
this.validateDecompression = validateDecompression;
}
@ -40,12 +38,12 @@ public class TcpPacketCompression extends ByteToMessageCodec<ByteBuf> {
public void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
int readable = in.readableBytes();
if(readable < this.session.getCompressionThreshold()) {
this.codecHelper.writeVarInt(out, 0);
this.session.getCodecHelper().writeVarInt(out, 0);
out.writeBytes(in);
} else {
byte[] bytes = new byte[readable];
in.readBytes(bytes);
this.codecHelper.writeVarInt(out, bytes.length);
this.session.getCodecHelper().writeVarInt(out, bytes.length);
this.deflater.setInput(bytes, 0, readable);
this.deflater.finish();
while(!this.deflater.finished()) {
@ -60,7 +58,7 @@ public class TcpPacketCompression extends ByteToMessageCodec<ByteBuf> {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
if(buf.readableBytes() != 0) {
int size = this.codecHelper.readVarInt(buf);
int size = this.session.getCodecHelper().readVarInt(buf);
if(size == 0) {
out.add(buf.readBytes(buf.readableBytes()));
} else {

View file

@ -12,12 +12,10 @@ import java.util.List;
public class TcpPacketSizer extends ByteToMessageCodec<ByteBuf> {
private final Session session;
private final PacketCodecHelper codecHelper;
private final int size;
public TcpPacketSizer(Session session, int size) {
this.session = session;
this.codecHelper = session.getCodecHelper();
this.size = size;
}
@ -25,7 +23,7 @@ public class TcpPacketSizer extends ByteToMessageCodec<ByteBuf> {
public void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
int length = in.readableBytes();
out.ensureWritable(this.session.getPacketProtocol().getPacketHeader().getLengthSize(length) + length);
this.session.getPacketProtocol().getPacketHeader().writeLength(out, this.codecHelper, length);
this.session.getPacketProtocol().getPacketHeader().writeLength(out, this.session.getCodecHelper(), length);
out.writeBytes(in);
}
@ -41,7 +39,7 @@ public class TcpPacketSizer extends ByteToMessageCodec<ByteBuf> {
lengthBytes[index] = buf.readByte();
if ((this.session.getPacketProtocol().getPacketHeader().isLengthVariable() && lengthBytes[index] >= 0) || index == size - 1) {
int length = this.session.getPacketProtocol().getPacketHeader().readLength(Unpooled.wrappedBuffer(lengthBytes), this.codecHelper, buf.readableBytes());
int length = this.session.getPacketProtocol().getPacketHeader().readLength(Unpooled.wrappedBuffer(lengthBytes), this.session.getCodecHelper(), buf.readableBytes());
if (buf.readableBytes() < length) {
buf.resetReaderIndex();
return;