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:
@@ -54,6 +54,8 @@ subprojects {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
compile (group: 'org.jetbrains', name: 'annotations', version: '16.0.3')
|
||||||
|
|
||||||
/* Logger */
|
/* Logger */
|
||||||
compile (group: 'org.slf4j', name: 'slf4j-api', version: slf4j_version)
|
compile (group: 'org.slf4j', name: 'slf4j-api', version: slf4j_version)
|
||||||
compile (group: 'org.slf4j', name: 'jcl-over-slf4j', version: slf4j_version)
|
compile (group: 'org.slf4j', name: 'jcl-over-slf4j', version: slf4j_version)
|
||||||
|
|||||||
@@ -3,10 +3,11 @@ package mc.core.network;
|
|||||||
import com.flowpowered.nbt.Tag;
|
import com.flowpowered.nbt.Tag;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public abstract class NetInputStream extends InputStream {
|
public abstract class NetInputStream extends InputStream {
|
||||||
@Getter
|
@Getter
|
||||||
@@ -24,7 +25,7 @@ public abstract class NetInputStream extends InputStream {
|
|||||||
public abstract short readShort();
|
public abstract short readShort();
|
||||||
public abstract int readInt();
|
public abstract int readInt();
|
||||||
public abstract int readVarInt();
|
public abstract int readVarInt();
|
||||||
public abstract int readVarInt(int[] countReadBytes);
|
public abstract int readVarInt(AtomicInteger countReadBytes);
|
||||||
public abstract long readLong();
|
public abstract long readLong();
|
||||||
public abstract float readFloat();
|
public abstract float readFloat();
|
||||||
public abstract double readDouble();
|
public abstract double readDouble();
|
||||||
@@ -35,22 +36,22 @@ public abstract class NetInputStream extends InputStream {
|
|||||||
public abstract void skipBytes(int count);
|
public abstract void skipBytes(int count);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read() throws IOException {
|
public int read() {
|
||||||
return readByte();
|
return readByte();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read(byte[] b) throws IOException {
|
public int read(@NotNull byte[] b) {
|
||||||
return readBytes(b);
|
return readBytes(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read(byte[] b, int off, int len) throws IOException {
|
public int read(@NotNull byte[] b, int off, int len) {
|
||||||
return readBytes(b, off, len);
|
return readBytes(b, off, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long skip(long n) throws IOException {
|
public long skip(long n) {
|
||||||
skipBytes((int) n);
|
skipBytes((int) n);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,13 +8,14 @@ import mc.core.network.NetInputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public abstract class NetInputStream_p340 extends NetInputStream {
|
public abstract class NetInputStream_p340 extends NetInputStream {
|
||||||
private NBTInputStream nbtInputStream;
|
private NBTInputStream nbtInputStream;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int readVarInt(int[] countReadBytes) {
|
public int readVarInt(AtomicInteger countReadBytes) {
|
||||||
int numRead = 0;
|
int numRead = 0;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
byte read;
|
byte read;
|
||||||
@@ -30,7 +31,10 @@ public abstract class NetInputStream_p340 extends NetInputStream {
|
|||||||
numRead++;
|
numRead++;
|
||||||
} while ((read & 0b10000000) != 0);
|
} while ((read & 0b10000000) != 0);
|
||||||
|
|
||||||
if (countReadBytes != null && countReadBytes.length == 1) countReadBytes[0] = numRead;
|
if (countReadBytes != null) {
|
||||||
|
countReadBytes.set(numRead);
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 mc.core.network.proto_1_12_2.netty.wrappers.WrapperNetInputStream;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import static mc.core.network.proto_1_12_2.netty.NettyServer.ATTR_STATE;
|
import static mc.core.network.proto_1_12_2.netty.NettyServer.ATTR_STATE;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class PacketDecoder extends ReplayingDecoder<CSPacket> {
|
public class PacketDecoder extends ReplayingDecoder<CSPacket> {
|
||||||
private int[] countReadBytes = new int[]{0};
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
public void channelActive(ChannelHandlerContext ctx) {
|
||||||
ctx.channel().attr(ATTR_STATE).set(State.HANDSHAKE);
|
ctx.channel().attr(ATTR_STATE).set(State.HANDSHAKE);
|
||||||
ctx.fireChannelActive();
|
ctx.fireChannelActive();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
public void channelInactive(ChannelHandlerContext ctx) {
|
||||||
ctx.channel().attr(ATTR_STATE).set(null);
|
ctx.channel().attr(ATTR_STATE).set(null);
|
||||||
ctx.fireChannelInactive();
|
ctx.fireChannelInactive();
|
||||||
}
|
}
|
||||||
@@ -38,11 +38,12 @@ public class PacketDecoder extends ReplayingDecoder<CSPacket> {
|
|||||||
log.debug("Packet size: {}", packetSize);
|
log.debug("Packet size: {}", packetSize);
|
||||||
int leftDataPacket = packetSize;
|
int leftDataPacket = packetSize;
|
||||||
|
|
||||||
|
final AtomicInteger countReadBytes = new AtomicInteger(0);
|
||||||
int packetId = netStream.readVarInt(countReadBytes);
|
int packetId = netStream.readVarInt(countReadBytes);
|
||||||
String hexPacketId = Integer.toHexString(packetId).toUpperCase();
|
String hexPacketId = Integer.toHexString(packetId).toUpperCase();
|
||||||
if (hexPacketId.length() == 1) hexPacketId = "0" + hexPacketId;
|
if (hexPacketId.length() == 1) hexPacketId = "0" + hexPacketId;
|
||||||
log.debug("Packet id: 0x{}", hexPacketId);
|
log.debug("Packet id: 0x{}", hexPacketId);
|
||||||
leftDataPacket = leftDataPacket - countReadBytes[0];
|
leftDataPacket = leftDataPacket - countReadBytes.get();
|
||||||
|
|
||||||
Class<? extends CSPacket> packetClass = state.getClientSidePacket(packetId);
|
Class<? extends CSPacket> packetClass = state.getClientSidePacket(packetId);
|
||||||
if (packetClass == null) {
|
if (packetClass == null) {
|
||||||
|
|||||||
@@ -10,27 +10,14 @@ import io.netty.handler.codec.MessageToByteEncoder;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import mc.core.network.NetOutputStream;
|
import mc.core.network.NetOutputStream;
|
||||||
import mc.core.network.SCPacket;
|
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.State;
|
||||||
import mc.core.network.proto_1_12_2.netty.wrappers.WrapperNetOutputStream;
|
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 mc.core.network.proto_1_12_2.netty.NettyServer.ATTR_STATE;
|
||||||
import static org.slf4j.helpers.MessageFormatter.format;
|
import static org.slf4j.helpers.MessageFormatter.format;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class PacketEncoder extends MessageToByteEncoder<SCPacket> {
|
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
|
@Override
|
||||||
protected void encode(ChannelHandlerContext ctx, SCPacket packet, ByteBuf out) {
|
protected void encode(ChannelHandlerContext ctx, SCPacket packet, ByteBuf out) {
|
||||||
@@ -44,14 +31,9 @@ public class PacketEncoder extends MessageToByteEncoder<SCPacket> {
|
|||||||
log.debug("Send {}:{}", state, packet);
|
log.debug("Send {}:{}", state, packet);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
NetOutputStream netStream = new ByteArrayOutputNetStream();
|
NetOutputStream netStream = new WrapperNetOutputStream(out);
|
||||||
packet.writeSelf(netStream);
|
|
||||||
byte[] bytes = ((ByteArrayOutputNetStream) netStream).toByteArray();
|
|
||||||
netStream = new WrapperNetOutputStream(out);
|
|
||||||
|
|
||||||
netStream.writeVarInt(bytes.length + sizeVarInt(id));
|
|
||||||
netStream.writeVarInt(id);
|
netStream.writeVarInt(id);
|
||||||
netStream.writeBytes(bytes);
|
packet.writeSelf(netStream);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
log.error(format("Error encoding packet {}:{}", state, packet).getMessage(), 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