Merge branch 'proto_1.12.2' into world-loader-anvil
# Conflicts: # proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/PacketEncoder.java
This commit is contained in:
@@ -10,21 +10,21 @@ import mc.core.network.proto_1_12_2.State;
|
||||
import mc.core.network.proto_1_12_2.netty.wrappers.WrapperNetInputStream;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static mc.core.network.proto_1_12_2.netty.NettyServer.ATTR_STATE;
|
||||
|
||||
@Slf4j
|
||||
public class PacketDecoder extends ReplayingDecoder<CSPacket> {
|
||||
private int[] countReadBytes = new int[]{0};
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
public void channelActive(ChannelHandlerContext ctx) {
|
||||
ctx.channel().attr(ATTR_STATE).set(State.HANDSHAKE);
|
||||
ctx.fireChannelActive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
public void channelInactive(ChannelHandlerContext ctx) {
|
||||
ctx.channel().attr(ATTR_STATE).set(null);
|
||||
ctx.fireChannelInactive();
|
||||
}
|
||||
@@ -38,11 +38,12 @@ public class PacketDecoder extends ReplayingDecoder<CSPacket> {
|
||||
log.debug("Packet size: {}", packetSize);
|
||||
int leftDataPacket = packetSize;
|
||||
|
||||
final AtomicInteger countReadBytes = new AtomicInteger(0);
|
||||
int packetId = netStream.readVarInt(countReadBytes);
|
||||
String hexPacketId = Integer.toHexString(packetId).toUpperCase();
|
||||
if (hexPacketId.length() == 1) hexPacketId = "0" + hexPacketId;
|
||||
log.debug("Packet id: 0x{}", hexPacketId);
|
||||
leftDataPacket = leftDataPacket - countReadBytes[0];
|
||||
leftDataPacket = leftDataPacket - countReadBytes.get();
|
||||
|
||||
Class<? extends CSPacket> packetClass = state.getClientSidePacket(packetId);
|
||||
if (packetClass == null) {
|
||||
|
||||
@@ -10,27 +10,14 @@ import io.netty.handler.codec.MessageToByteEncoder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.network.NetOutputStream;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream;
|
||||
import mc.core.network.proto_1_12_2.State;
|
||||
import mc.core.network.proto_1_12_2.netty.wrappers.WrapperNetOutputStream;
|
||||
import org.slf4j.helpers.MessageFormatter;
|
||||
|
||||
import static mc.core.network.proto_1_12_2.netty.NettyServer.ATTR_STATE;
|
||||
import static org.slf4j.helpers.MessageFormatter.format;
|
||||
|
||||
@Slf4j
|
||||
public class PacketEncoder extends MessageToByteEncoder<SCPacket> {
|
||||
private static int sizeVarInt(final int value) {
|
||||
byte size = 0;
|
||||
int v = value;
|
||||
|
||||
do {
|
||||
v >>>= 7;
|
||||
size++;
|
||||
} while (v != 0);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, SCPacket packet, ByteBuf out) {
|
||||
@@ -44,14 +31,9 @@ public class PacketEncoder extends MessageToByteEncoder<SCPacket> {
|
||||
log.debug("Send {}:{}", state, packet);
|
||||
|
||||
try {
|
||||
NetOutputStream netStream = new ByteArrayOutputNetStream();
|
||||
packet.writeSelf(netStream);
|
||||
byte[] bytes = ((ByteArrayOutputNetStream) netStream).toByteArray();
|
||||
netStream = new WrapperNetOutputStream(out);
|
||||
|
||||
netStream.writeVarInt(bytes.length + sizeVarInt(id));
|
||||
NetOutputStream netStream = new WrapperNetOutputStream(out);
|
||||
netStream.writeVarInt(id);
|
||||
netStream.writeBytes(bytes);
|
||||
packet.writeSelf(netStream);
|
||||
} catch (Throwable t) {
|
||||
log.error(format("Error encoding packet {}:{}", state, packet).getMessage(), t);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package mc.core.network.proto_1_12_2.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.network.proto_1_12_2.netty.wrappers.WrapperNetOutputStream;
|
||||
|
||||
/**
|
||||
* Подсчет размера отправляемого пакета
|
||||
*/
|
||||
@Slf4j
|
||||
public class PacketPostEncoder extends MessageToByteEncoder<ByteBuf> {
|
||||
|
||||
private static int getVarIntSize(int input) {
|
||||
for (int i = 1; i < 5; ++i) {
|
||||
if ((input & -1 << i * 7) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return 5;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) {
|
||||
final int pktSize = msg.readableBytes();
|
||||
final int sizeOfPktSize = getVarIntSize(pktSize);
|
||||
|
||||
if (sizeOfPktSize > 3) {
|
||||
log.warn("unable to fit {} into {}", pktSize, 3);
|
||||
}
|
||||
|
||||
out.ensureWritable(sizeOfPktSize + pktSize);
|
||||
(new WrapperNetOutputStream(out)).writeVarInt(pktSize);
|
||||
out.writeBytes(msg);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user