Archived
0

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:
2019-01-27 03:44:01 +03:00
6 changed files with 60 additions and 32 deletions

View File

@@ -54,6 +54,8 @@ subprojects {
}
dependencies {
compile (group: 'org.jetbrains', name: 'annotations', version: '16.0.3')
/* Logger */
compile (group: 'org.slf4j', name: 'slf4j-api', version: slf4j_version)
compile (group: 'org.slf4j', name: 'jcl-over-slf4j', version: slf4j_version)

View File

@@ -3,10 +3,11 @@ package mc.core.network;
import com.flowpowered.nbt.Tag;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
public abstract class NetInputStream extends InputStream {
@Getter
@@ -24,7 +25,7 @@ public abstract class NetInputStream extends InputStream {
public abstract short readShort();
public abstract int readInt();
public abstract int readVarInt();
public abstract int readVarInt(int[] countReadBytes);
public abstract int readVarInt(AtomicInteger countReadBytes);
public abstract long readLong();
public abstract float readFloat();
public abstract double readDouble();
@@ -35,22 +36,22 @@ public abstract class NetInputStream extends InputStream {
public abstract void skipBytes(int count);
@Override
public int read() throws IOException {
public int read() {
return readByte();
}
@Override
public int read(byte[] b) throws IOException {
public int read(@NotNull byte[] b) {
return readBytes(b);
}
@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);
}
@Override
public long skip(long n) throws IOException {
public long skip(long n) {
skipBytes((int) n);
return n;
}

View File

@@ -8,13 +8,14 @@ import mc.core.network.NetInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
@Slf4j
public abstract class NetInputStream_p340 extends NetInputStream {
private NBTInputStream nbtInputStream;
@Override
public int readVarInt(int[] countReadBytes) {
public int readVarInt(AtomicInteger countReadBytes) {
int numRead = 0;
int result = 0;
byte read;
@@ -30,7 +31,10 @@ public abstract class NetInputStream_p340 extends NetInputStream {
numRead++;
} while ((read & 0b10000000) != 0);
if (countReadBytes != null && countReadBytes.length == 1) countReadBytes[0] = numRead;
if (countReadBytes != null) {
countReadBytes.set(numRead);
}
return result;
}

View File

@@ -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) {

View File

@@ -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);
}

View File

@@ -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);
}
}