mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-29 04:28:21 +01:00
Remove magic value for pre-filled var ints
This commit is contained in:
parent
d41a4dcf54
commit
4ab35fbbc0
@ -143,7 +143,7 @@ public final class PacketUtils {
|
||||
* @param packet the packet to write into {@code buf}
|
||||
*/
|
||||
public static void writePacket(@NotNull ByteBuf buf, @NotNull ServerPacket packet) {
|
||||
Utils.writeVarIntBuf(buf, packet.getId());
|
||||
Utils.writeVarInt(buf, packet.getId());
|
||||
writePacketPayload(buf, packet);
|
||||
}
|
||||
|
||||
@ -173,14 +173,13 @@ public final class PacketUtils {
|
||||
public static void frameBuffer(@NotNull ByteBuf packetBuffer, @NotNull ByteBuf frameTarget) {
|
||||
final int packetSize = packetBuffer.readableBytes();
|
||||
final int headerSize = Utils.getVarIntSize(packetSize);
|
||||
|
||||
if (headerSize > 3) {
|
||||
throw new IllegalStateException("Unable to fit " + headerSize + " into 3");
|
||||
}
|
||||
|
||||
frameTarget.ensureWritable(packetSize + headerSize);
|
||||
|
||||
Utils.writeVarIntBuf(frameTarget, packetSize);
|
||||
Utils.writeVarInt(frameTarget, 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) {
|
||||
final int packetLength = packetBuffer.readableBytes();
|
||||
final boolean compression = packetLength > MinecraftServer.getCompressionThreshold();
|
||||
Utils.writeVarIntBuf(compressionTarget, compression ? packetLength : 0);
|
||||
Utils.writeVarInt(compressionTarget, compression ? packetLength : 0);
|
||||
if (compression) {
|
||||
compress(compressor, packetBuffer, compressionTarget);
|
||||
} else {
|
||||
@ -217,10 +216,11 @@ public final class PacketUtils {
|
||||
final int compressionThreshold = MinecraftServer.getCompressionThreshold();
|
||||
|
||||
// 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) {
|
||||
// Index of the uncompressed payload length
|
||||
final int dataLengthIndex = Utils.writeEmptyVarIntHeader(buffer);
|
||||
final int dataLengthIndex = Utils.writeEmpty3BytesVarInt(buffer);
|
||||
|
||||
// Write packet
|
||||
final int contentIndex = buffer.writerIndex();
|
||||
@ -228,7 +228,7 @@ public final class PacketUtils {
|
||||
final int packetSize = buffer.writerIndex() - contentIndex;
|
||||
|
||||
final int uncompressedLength = packetSize >= compressionThreshold ? packetSize : 0;
|
||||
Utils.overrideVarIntHeader(buffer, dataLengthIndex, uncompressedLength);
|
||||
Utils.write3BytesVarInt(buffer, dataLengthIndex, uncompressedLength);
|
||||
if (uncompressedLength > 0) {
|
||||
// Packet large enough, compress
|
||||
ByteBuf uncompressedCopy = buffer.copy(contentIndex, packetSize);
|
||||
@ -241,8 +241,8 @@ public final class PacketUtils {
|
||||
writePacket(buffer, serverPacket);
|
||||
}
|
||||
// Total length
|
||||
final int totalPacketLength = buffer.writerIndex() - packetLengthIndex - Utils.VARINT_HEADER_SIZE;
|
||||
Utils.overrideVarIntHeader(buffer, packetLengthIndex, totalPacketLength);
|
||||
final int totalPacketLength = buffer.writerIndex() - startIndex;
|
||||
Utils.write3BytesVarInt(buffer, packetLengthIndex, totalPacketLength);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -10,11 +10,7 @@ import java.util.UUID;
|
||||
|
||||
public final class Utils {
|
||||
|
||||
// Do NOT modify
|
||||
public static final int VARINT_HEADER_SIZE = 3;
|
||||
|
||||
private Utils() {
|
||||
|
||||
}
|
||||
|
||||
public static int getVarIntSize(int input) {
|
||||
@ -25,7 +21,7 @@ public final class Utils {
|
||||
? 4 : 5;
|
||||
}
|
||||
|
||||
public static void writeVarIntBuf(ByteBuf buf, int value) {
|
||||
public static void writeVarInt(@NotNull ByteBuf buf, int value) {
|
||||
// Took from velocity
|
||||
if ((value & (0xFFFFFFFF << 7)) == 0) {
|
||||
buf.writeByte(value);
|
||||
@ -36,19 +32,14 @@ public final class Utils {
|
||||
int w = (value & 0x7F | 0x80) << 16 | ((value >>> 7) & 0x7F | 0x80) << 8 | (value >>> 14);
|
||||
buf.writeMedium(w);
|
||||
} else {
|
||||
// Naive loop
|
||||
while (true) {
|
||||
if ((value & 0xFFFFFF80) == 0) {
|
||||
buf.writeByte(value);
|
||||
return;
|
||||
}
|
||||
buf.writeByte(value & 0x7F | 0x80);
|
||||
value >>>= 7;
|
||||
}
|
||||
int w = (value & 0x7F | 0x80) << 24 | ((value >>> 7) & 0x7F | 0x80) << 16
|
||||
| ((value >>> 14) & 0x7F | 0x80) << 8 | ((value >>> 21) & 0x7F | 0x80);
|
||||
buf.writeInt(w);
|
||||
buf.writeByte(value >>> 28);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
buffer.writerIndex(startIndex);
|
||||
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);
|
||||
}
|
||||
|
||||
public static int writeEmptyVarIntHeader(@NotNull ByteBuf buffer) {
|
||||
public static int writeEmpty3BytesVarInt(@NotNull ByteBuf buffer) {
|
||||
final int index = buffer.writerIndex();
|
||||
buffer.writeMedium(0);
|
||||
return index;
|
||||
}
|
||||
|
||||
public static int readVarInt(ByteBuf buffer) {
|
||||
int numRead = 0;
|
||||
int result = 0;
|
||||
byte read;
|
||||
do {
|
||||
read = buffer.readByte();
|
||||
int value = (read & 0b01111111);
|
||||
result |= (value << (7 * numRead));
|
||||
|
||||
numRead++;
|
||||
if (numRead > 5) {
|
||||
throw new RuntimeException("VarInt is too big");
|
||||
public static int readVarInt(ByteBuf buf) {
|
||||
int i = 0;
|
||||
final int maxRead = Math.min(5, buf.readableBytes());
|
||||
for (int j = 0; j < maxRead; j++) {
|
||||
final int k = buf.readByte();
|
||||
i |= (k & 0x7F) << j * 7;
|
||||
if ((k & 0x80) != 128) {
|
||||
return i;
|
||||
}
|
||||
} while ((read & 0b10000000) != 0);
|
||||
|
||||
return result;
|
||||
}
|
||||
throw new RuntimeException("VarInt is too big");
|
||||
}
|
||||
|
||||
public static long readVarLong(ByteBuf buffer) {
|
||||
public static long readVarLong(@NotNull ByteBuf buffer) {
|
||||
int numRead = 0;
|
||||
long result = 0;
|
||||
byte read;
|
||||
@ -148,14 +134,14 @@ public final class Utils {
|
||||
if (bitsPerEntry < 9) {
|
||||
// Palette has to exist
|
||||
final Short2ShortLinkedOpenHashMap paletteBlockMap = section.getPaletteBlockMap();
|
||||
writeVarIntBuf(buffer, paletteBlockMap.size());
|
||||
writeVarInt(buffer, paletteBlockMap.size());
|
||||
for (short paletteValue : paletteBlockMap.values()) {
|
||||
writeVarIntBuf(buffer, paletteValue);
|
||||
writeVarInt(buffer, paletteValue);
|
||||
}
|
||||
}
|
||||
|
||||
final long[] blocks = section.getBlocks();
|
||||
writeVarIntBuf(buffer, blocks.length);
|
||||
writeVarInt(buffer, blocks.length);
|
||||
for (long datum : blocks) {
|
||||
buffer.writeLong(datum);
|
||||
}
|
||||
|
@ -152,7 +152,7 @@ public class BinaryWriter extends OutputStream {
|
||||
* @param i the int to write
|
||||
*/
|
||||
public void writeVarInt(int i) {
|
||||
Utils.writeVarIntBuf(buffer, i);
|
||||
Utils.writeVarInt(buffer, i);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user