Remove magic value for pre-filled var ints

This commit is contained in:
TheMode 2021-05-11 03:10:18 +02:00
parent d41a4dcf54
commit 4ab35fbbc0
3 changed files with 31 additions and 45 deletions

View File

@ -143,7 +143,7 @@ public final class PacketUtils {
* @param packet the packet to write into {@code buf} * @param packet the packet to write into {@code buf}
*/ */
public static void writePacket(@NotNull ByteBuf buf, @NotNull ServerPacket packet) { public static void writePacket(@NotNull ByteBuf buf, @NotNull ServerPacket packet) {
Utils.writeVarIntBuf(buf, packet.getId()); Utils.writeVarInt(buf, packet.getId());
writePacketPayload(buf, packet); writePacketPayload(buf, packet);
} }
@ -173,14 +173,13 @@ public final class PacketUtils {
public static void frameBuffer(@NotNull ByteBuf packetBuffer, @NotNull ByteBuf frameTarget) { public static void frameBuffer(@NotNull ByteBuf packetBuffer, @NotNull ByteBuf frameTarget) {
final int packetSize = packetBuffer.readableBytes(); final int packetSize = packetBuffer.readableBytes();
final int headerSize = Utils.getVarIntSize(packetSize); final int headerSize = Utils.getVarIntSize(packetSize);
if (headerSize > 3) { if (headerSize > 3) {
throw new IllegalStateException("Unable to fit " + headerSize + " into 3"); throw new IllegalStateException("Unable to fit " + headerSize + " into 3");
} }
frameTarget.ensureWritable(packetSize + headerSize); frameTarget.ensureWritable(packetSize + headerSize);
Utils.writeVarIntBuf(frameTarget, packetSize); Utils.writeVarInt(frameTarget, packetSize);
frameTarget.writeBytes(packetBuffer, packetBuffer.readerIndex(), packetSize); frameTarget.writeBytes(packetBuffer, packetBuffer.readerIndex(), packetSize);
} }
@ -196,7 +195,7 @@ public final class PacketUtils {
public static void compressBuffer(@NotNull VelocityCompressor compressor, @NotNull ByteBuf packetBuffer, @NotNull ByteBuf compressionTarget) { public static void compressBuffer(@NotNull VelocityCompressor compressor, @NotNull ByteBuf packetBuffer, @NotNull ByteBuf compressionTarget) {
final int packetLength = packetBuffer.readableBytes(); final int packetLength = packetBuffer.readableBytes();
final boolean compression = packetLength > MinecraftServer.getCompressionThreshold(); final boolean compression = packetLength > MinecraftServer.getCompressionThreshold();
Utils.writeVarIntBuf(compressionTarget, compression ? packetLength : 0); Utils.writeVarInt(compressionTarget, compression ? packetLength : 0);
if (compression) { if (compression) {
compress(compressor, packetBuffer, compressionTarget); compress(compressor, packetBuffer, compressionTarget);
} else { } else {
@ -217,10 +216,11 @@ public final class PacketUtils {
final int compressionThreshold = MinecraftServer.getCompressionThreshold(); final int compressionThreshold = MinecraftServer.getCompressionThreshold();
// Index of the var-int containing the complete packet length // Index of the var-int containing the complete packet length
final int packetLengthIndex = Utils.writeEmptyVarIntHeader(buffer); final int packetLengthIndex = Utils.writeEmpty3BytesVarInt(buffer);
final int startIndex = buffer.writerIndex(); // Index where the content starts (after length)
if (compressionThreshold > 0) { if (compressionThreshold > 0) {
// Index of the uncompressed payload length // Index of the uncompressed payload length
final int dataLengthIndex = Utils.writeEmptyVarIntHeader(buffer); final int dataLengthIndex = Utils.writeEmpty3BytesVarInt(buffer);
// Write packet // Write packet
final int contentIndex = buffer.writerIndex(); final int contentIndex = buffer.writerIndex();
@ -228,7 +228,7 @@ public final class PacketUtils {
final int packetSize = buffer.writerIndex() - contentIndex; final int packetSize = buffer.writerIndex() - contentIndex;
final int uncompressedLength = packetSize >= compressionThreshold ? packetSize : 0; final int uncompressedLength = packetSize >= compressionThreshold ? packetSize : 0;
Utils.overrideVarIntHeader(buffer, dataLengthIndex, uncompressedLength); Utils.write3BytesVarInt(buffer, dataLengthIndex, uncompressedLength);
if (uncompressedLength > 0) { if (uncompressedLength > 0) {
// Packet large enough, compress // Packet large enough, compress
ByteBuf uncompressedCopy = buffer.copy(contentIndex, packetSize); ByteBuf uncompressedCopy = buffer.copy(contentIndex, packetSize);
@ -241,8 +241,8 @@ public final class PacketUtils {
writePacket(buffer, serverPacket); writePacket(buffer, serverPacket);
} }
// Total length // Total length
final int totalPacketLength = buffer.writerIndex() - packetLengthIndex - Utils.VARINT_HEADER_SIZE; final int totalPacketLength = buffer.writerIndex() - startIndex;
Utils.overrideVarIntHeader(buffer, packetLengthIndex, totalPacketLength); Utils.write3BytesVarInt(buffer, packetLengthIndex, totalPacketLength);
} }
/** /**

View File

@ -10,11 +10,7 @@ import java.util.UUID;
public final class Utils { public final class Utils {
// Do NOT modify
public static final int VARINT_HEADER_SIZE = 3;
private Utils() { private Utils() {
} }
public static int getVarIntSize(int input) { public static int getVarIntSize(int input) {
@ -25,7 +21,7 @@ public final class Utils {
? 4 : 5; ? 4 : 5;
} }
public static void writeVarIntBuf(ByteBuf buf, int value) { public static void writeVarInt(@NotNull ByteBuf buf, int value) {
// Took from velocity // Took from velocity
if ((value & (0xFFFFFFFF << 7)) == 0) { if ((value & (0xFFFFFFFF << 7)) == 0) {
buf.writeByte(value); buf.writeByte(value);
@ -36,19 +32,14 @@ public final class Utils {
int w = (value & 0x7F | 0x80) << 16 | ((value >>> 7) & 0x7F | 0x80) << 8 | (value >>> 14); int w = (value & 0x7F | 0x80) << 16 | ((value >>> 7) & 0x7F | 0x80) << 8 | (value >>> 14);
buf.writeMedium(w); buf.writeMedium(w);
} else { } else {
// Naive loop int w = (value & 0x7F | 0x80) << 24 | ((value >>> 7) & 0x7F | 0x80) << 16
while (true) { | ((value >>> 14) & 0x7F | 0x80) << 8 | ((value >>> 21) & 0x7F | 0x80);
if ((value & 0xFFFFFF80) == 0) { buf.writeInt(w);
buf.writeByte(value); buf.writeByte(value >>> 28);
return;
}
buf.writeByte(value & 0x7F | 0x80);
value >>>= 7;
}
} }
} }
public static void overrideVarIntHeader(@NotNull ByteBuf buffer, int startIndex, int value) { public static void write3BytesVarInt(@NotNull ByteBuf buffer, int startIndex, int value) {
final int indexCache = buffer.writerIndex(); final int indexCache = buffer.writerIndex();
buffer.writerIndex(startIndex); buffer.writerIndex(startIndex);
final int w = (value & 0x7F | 0x80) << 16 | ((value >>> 7) & 0x7F | 0x80) << 8 | (value >>> 14); final int w = (value & 0x7F | 0x80) << 16 | ((value >>> 7) & 0x7F | 0x80) << 8 | (value >>> 14);
@ -56,31 +47,26 @@ public final class Utils {
buffer.writerIndex(indexCache); buffer.writerIndex(indexCache);
} }
public static int writeEmptyVarIntHeader(@NotNull ByteBuf buffer) { public static int writeEmpty3BytesVarInt(@NotNull ByteBuf buffer) {
final int index = buffer.writerIndex(); final int index = buffer.writerIndex();
buffer.writeMedium(0); buffer.writeMedium(0);
return index; return index;
} }
public static int readVarInt(ByteBuf buffer) { public static int readVarInt(ByteBuf buf) {
int numRead = 0; int i = 0;
int result = 0; final int maxRead = Math.min(5, buf.readableBytes());
byte read; for (int j = 0; j < maxRead; j++) {
do { final int k = buf.readByte();
read = buffer.readByte(); i |= (k & 0x7F) << j * 7;
int value = (read & 0b01111111); if ((k & 0x80) != 128) {
result |= (value << (7 * numRead)); return i;
numRead++;
if (numRead > 5) {
throw new RuntimeException("VarInt is too big");
} }
} while ((read & 0b10000000) != 0); }
throw new RuntimeException("VarInt is too big");
return result;
} }
public static long readVarLong(ByteBuf buffer) { public static long readVarLong(@NotNull ByteBuf buffer) {
int numRead = 0; int numRead = 0;
long result = 0; long result = 0;
byte read; byte read;
@ -148,14 +134,14 @@ public final class Utils {
if (bitsPerEntry < 9) { if (bitsPerEntry < 9) {
// Palette has to exist // Palette has to exist
final Short2ShortLinkedOpenHashMap paletteBlockMap = section.getPaletteBlockMap(); final Short2ShortLinkedOpenHashMap paletteBlockMap = section.getPaletteBlockMap();
writeVarIntBuf(buffer, paletteBlockMap.size()); writeVarInt(buffer, paletteBlockMap.size());
for (short paletteValue : paletteBlockMap.values()) { for (short paletteValue : paletteBlockMap.values()) {
writeVarIntBuf(buffer, paletteValue); writeVarInt(buffer, paletteValue);
} }
} }
final long[] blocks = section.getBlocks(); final long[] blocks = section.getBlocks();
writeVarIntBuf(buffer, blocks.length); writeVarInt(buffer, blocks.length);
for (long datum : blocks) { for (long datum : blocks) {
buffer.writeLong(datum); buffer.writeLong(datum);
} }

View File

@ -152,7 +152,7 @@ public class BinaryWriter extends OutputStream {
* @param i the int to write * @param i the int to write
*/ */
public void writeVarInt(int i) { public void writeVarInt(int i) {
Utils.writeVarIntBuf(buffer, i); Utils.writeVarInt(buffer, i);
} }
/** /**