3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-28 17:10:13 +01:00

Update legacy palette handling as well

Dieser Commit ist enthalten in:
Nassim Jahnke 2022-07-24 19:03:24 +02:00
Ursprung c2ee558235
Commit 56cc711c01
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
5 geänderte Dateien mit 41 neuen und 81 gelöschten Zeilen

Datei anzeigen

@ -36,7 +36,7 @@ public class LongArrayType extends Type<long[]> {
int length = Type.VAR_INT.readPrimitive(buffer); int length = Type.VAR_INT.readPrimitive(buffer);
long[] array = new long[length]; long[] array = new long[length];
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
array[i] = Type.LONG.readPrimitive(buffer); array[i] = buffer.readLong();
} }
return array; return array;
} }
@ -45,7 +45,7 @@ public class LongArrayType extends Type<long[]> {
public void write(ByteBuf buffer, long[] object) throws Exception { public void write(ByteBuf buffer, long[] object) throws Exception {
Type.VAR_INT.writePrimitive(buffer, object.length); Type.VAR_INT.writePrimitive(buffer, object.length);
for (long l : object) { for (long l : object) {
Type.LONG.writePrimitive(buffer, l); buffer.writeLong(l);
} }
} }
} }

Datei anzeigen

@ -41,8 +41,10 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
int bitsPerBlock = buffer.readUnsignedByte(); int bitsPerBlock = buffer.readUnsignedByte();
int originalBitsPerBlock = bitsPerBlock; int originalBitsPerBlock = bitsPerBlock;
if (bitsPerBlock == 0 || bitsPerBlock > 8) { if (bitsPerBlock > 8) {
bitsPerBlock = GLOBAL_PALETTE; bitsPerBlock = GLOBAL_PALETTE;
} else if (bitsPerBlock < 4) {
bitsPerBlock = 4;
} }
// Read palette // Read palette
@ -58,19 +60,14 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
} }
// Read blocks // Read blocks
long[] blockData = new long[Type.VAR_INT.readPrimitive(buffer)]; long[] blockData = Type.LONG_ARRAY_PRIMITIVE.read(buffer);
if (blockData.length > 0) { if (blockData.length > 0) {
int expectedLength = (int) Math.ceil(ChunkSection.SIZE * bitsPerBlock / 64.0); int expectedLength = (int) Math.ceil(ChunkSection.SIZE * bitsPerBlock / 64.0);
if (blockData.length != expectedLength) { if (blockData.length == expectedLength) {
throw new IllegalStateException("Block data length (" + blockData.length + ") does not match expected length (" + expectedLength + ")! bitsPerBlock=" + bitsPerBlock + ", originalBitsPerBlock=" + originalBitsPerBlock);
}
for (int i = 0; i < blockData.length; i++) {
blockData[i] = buffer.readLong();
}
CompactArrayUtil.iterateCompactArray(bitsPerBlock, ChunkSection.SIZE, blockData, CompactArrayUtil.iterateCompactArray(bitsPerBlock, ChunkSection.SIZE, blockData,
bitsPerBlock == GLOBAL_PALETTE ? chunkSection::setFlatBlock : chunkSection::setPaletteIndex); bitsPerBlock == GLOBAL_PALETTE ? chunkSection::setFlatBlock : chunkSection::setPaletteIndex);
} }
}
return chunkSection; return chunkSection;
} }
@ -88,7 +85,7 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
buffer.writeByte(bitsPerBlock); buffer.writeByte(bitsPerBlock);
// Write pallet (or not) // Write palette
if (bitsPerBlock != GLOBAL_PALETTE) { if (bitsPerBlock != GLOBAL_PALETTE) {
Type.VAR_INT.writePrimitive(buffer, chunkSection.getPaletteSize()); Type.VAR_INT.writePrimitive(buffer, chunkSection.getPaletteSize());
for (int i = 0; i < chunkSection.getPaletteSize(); i++) { for (int i = 0; i < chunkSection.getPaletteSize(); i++) {
@ -98,9 +95,6 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
long[] data = CompactArrayUtil.createCompactArray(bitsPerBlock, ChunkSection.SIZE, long[] data = CompactArrayUtil.createCompactArray(bitsPerBlock, ChunkSection.SIZE,
bitsPerBlock == GLOBAL_PALETTE ? chunkSection::getFlatBlock : chunkSection::getPaletteIndex); bitsPerBlock == GLOBAL_PALETTE ? chunkSection::getFlatBlock : chunkSection::getPaletteIndex);
Type.VAR_INT.writePrimitive(buffer, data.length); Type.LONG_ARRAY_PRIMITIVE.write(buffer, data);
for (long l : data) {
buffer.writeLong(l);
}
} }
} }

Datei anzeigen

@ -41,8 +41,10 @@ public class ChunkSectionType1_16 extends Type<ChunkSection> {
int bitsPerBlock = buffer.readUnsignedByte(); int bitsPerBlock = buffer.readUnsignedByte();
int originalBitsPerBlock = bitsPerBlock; int originalBitsPerBlock = bitsPerBlock;
if (bitsPerBlock == 0 || bitsPerBlock > 8) { if (bitsPerBlock > 8) {
bitsPerBlock = GLOBAL_PALETTE; bitsPerBlock = GLOBAL_PALETTE;
} else if (bitsPerBlock < 4) {
bitsPerBlock = 4;
} }
// Read palette // Read palette
@ -58,20 +60,15 @@ public class ChunkSectionType1_16 extends Type<ChunkSection> {
} }
// Read blocks // Read blocks
long[] blockData = new long[Type.VAR_INT.readPrimitive(buffer)]; long[] blockData = Type.LONG_ARRAY_PRIMITIVE.read(buffer);
if (blockData.length > 0) { if (blockData.length > 0) {
char valuesPerLong = (char) (64 / bitsPerBlock); char valuesPerLong = (char) (64 / bitsPerBlock);
int expectedLength = (ChunkSection.SIZE + valuesPerLong - 1) / valuesPerLong; int expectedLength = (ChunkSection.SIZE + valuesPerLong - 1) / valuesPerLong;
if (blockData.length != expectedLength) { if (blockData.length == expectedLength) {
throw new IllegalStateException("Block data length (" + blockData.length + ") does not match expected length (" + expectedLength + ")! bitsPerBlock=" + bitsPerBlock + ", originalBitsPerBlock=" + originalBitsPerBlock);
}
for (int i = 0; i < blockData.length; i++) {
blockData[i] = buffer.readLong();
}
CompactArrayUtil.iterateCompactArrayWithPadding(bitsPerBlock, ChunkSection.SIZE, blockData, CompactArrayUtil.iterateCompactArrayWithPadding(bitsPerBlock, ChunkSection.SIZE, blockData,
bitsPerBlock == GLOBAL_PALETTE ? chunkSection::setFlatBlock : chunkSection::setPaletteIndex); bitsPerBlock == GLOBAL_PALETTE ? chunkSection::setFlatBlock : chunkSection::setPaletteIndex);
} }
}
return chunkSection; return chunkSection;
} }
@ -89,7 +86,7 @@ public class ChunkSectionType1_16 extends Type<ChunkSection> {
buffer.writeByte(bitsPerBlock); buffer.writeByte(bitsPerBlock);
// Write pallet (or not) // Write palette
if (bitsPerBlock != GLOBAL_PALETTE) { if (bitsPerBlock != GLOBAL_PALETTE) {
Type.VAR_INT.writePrimitive(buffer, chunkSection.getPaletteSize()); Type.VAR_INT.writePrimitive(buffer, chunkSection.getPaletteSize());
for (int i = 0; i < chunkSection.getPaletteSize(); i++) { for (int i = 0; i < chunkSection.getPaletteSize(); i++) {
@ -99,9 +96,6 @@ public class ChunkSectionType1_16 extends Type<ChunkSection> {
long[] data = CompactArrayUtil.createCompactArrayWithPadding(bitsPerBlock, ChunkSection.SIZE, long[] data = CompactArrayUtil.createCompactArrayWithPadding(bitsPerBlock, ChunkSection.SIZE,
bitsPerBlock == GLOBAL_PALETTE ? chunkSection::getFlatBlock : chunkSection::getPaletteIndex); bitsPerBlock == GLOBAL_PALETTE ? chunkSection::getFlatBlock : chunkSection::getPaletteIndex);
Type.VAR_INT.writePrimitive(buffer, data.length); Type.LONG_ARRAY_PRIMITIVE.write(buffer, data);
for (long l : data) {
buffer.writeLong(l);
}
} }
} }

Datei anzeigen

@ -40,10 +40,6 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
// Reaad bits per block // Reaad bits per block
int bitsPerBlock = buffer.readUnsignedByte(); int bitsPerBlock = buffer.readUnsignedByte();
int originalBitsPerBlock = bitsPerBlock; int originalBitsPerBlock = bitsPerBlock;
if (bitsPerBlock == 0) {
bitsPerBlock = GLOBAL_PALETTE;
}
if (bitsPerBlock < 4) { if (bitsPerBlock < 4) {
bitsPerBlock = 4; bitsPerBlock = 4;
} }
@ -63,20 +59,15 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
} }
// Read blocks // Read blocks
long[] blockData = new long[Type.VAR_INT.readPrimitive(buffer)]; long[] blockData = Type.LONG_ARRAY_PRIMITIVE.read(buffer);
if (blockData.length > 0) { if (blockData.length > 0) {
int expectedLength = (int) Math.ceil(ChunkSection.SIZE * bitsPerBlock / 64.0); int expectedLength = (int) Math.ceil(ChunkSection.SIZE * bitsPerBlock / 64.0);
if (blockData.length != expectedLength) { if (blockData.length == expectedLength) {
throw new IllegalStateException("Block data length (" + blockData.length + ") does not match expected length (" + expectedLength + ")! bitsPerBlock=" + bitsPerBlock + ", originalBitsPerBlock=" + originalBitsPerBlock);
}
for (int i = 0; i < blockData.length; i++) {
blockData[i] = buffer.readLong();
}
CompactArrayUtil.iterateCompactArray(bitsPerBlock, ChunkSection.SIZE, blockData, CompactArrayUtil.iterateCompactArray(bitsPerBlock, ChunkSection.SIZE, blockData,
bitsPerBlock == GLOBAL_PALETTE ? chunkSection::setFlatBlock bitsPerBlock == GLOBAL_PALETTE ? chunkSection::setFlatBlock
: chunkSection::setPaletteIndex); : chunkSection::setPaletteIndex);
} }
}
return chunkSection; return chunkSection;
} }
@ -92,10 +83,9 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
bitsPerBlock = GLOBAL_PALETTE; bitsPerBlock = GLOBAL_PALETTE;
} }
long maxEntryValue = (1L << bitsPerBlock) - 1;
buffer.writeByte(bitsPerBlock); buffer.writeByte(bitsPerBlock);
// Write pallet (or not) // Write palette
if (bitsPerBlock != GLOBAL_PALETTE) { if (bitsPerBlock != GLOBAL_PALETTE) {
Type.VAR_INT.writePrimitive(buffer, chunkSection.getPaletteSize()); Type.VAR_INT.writePrimitive(buffer, chunkSection.getPaletteSize());
for (int i = 0; i < chunkSection.getPaletteSize(); i++) { for (int i = 0; i < chunkSection.getPaletteSize(); i++) {
@ -107,9 +97,6 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
long[] data = CompactArrayUtil.createCompactArray(bitsPerBlock, ChunkSection.SIZE, long[] data = CompactArrayUtil.createCompactArray(bitsPerBlock, ChunkSection.SIZE,
bitsPerBlock == GLOBAL_PALETTE ? chunkSection::getFlatBlock : chunkSection::getPaletteIndex); bitsPerBlock == GLOBAL_PALETTE ? chunkSection::getFlatBlock : chunkSection::getPaletteIndex);
Type.VAR_INT.writePrimitive(buffer, data.length); Type.LONG_ARRAY_PRIMITIVE.write(buffer, data);
for (long l : data) {
buffer.writeLong(l);
}
} }
} }

Datei anzeigen

@ -45,18 +45,12 @@ public final class PaletteType1_18 extends Type<DataPalette> {
final int originalBitsPerValue = buffer.readByte(); final int originalBitsPerValue = buffer.readByte();
int bitsPerValue = originalBitsPerValue; int bitsPerValue = originalBitsPerValue;
// Read palette
final DataPaletteImpl palette; final DataPaletteImpl palette;
if (bitsPerValue == 0) { if (bitsPerValue == 0) {
//TODO Create proper singleton palette Object // Single value storage
palette = new DataPaletteImpl(type.size(), 1); palette = new DataPaletteImpl(type.size(), 1);
palette.addId(Type.VAR_INT.readPrimitive(buffer)); palette.addId(Type.VAR_INT.readPrimitive(buffer));
Type.LONG_ARRAY_PRIMITIVE.read(buffer); // Just eat it if not empty - thanks, Hypixel
// Just eat it if not 0 - thanks, Hypixel
final int valuesLength = Type.VAR_INT.readPrimitive(buffer);
for (int i = 0; i < valuesLength; i++) {
buffer.readLong();
}
return palette; return palette;
} }
@ -66,6 +60,7 @@ public final class PaletteType1_18 extends Type<DataPalette> {
bitsPerValue = 4; // Linear block palette values are always 4 bits bitsPerValue = 4; // Linear block palette values are always 4 bits
} }
// Read palette
if (bitsPerValue != globalPaletteBits) { if (bitsPerValue != globalPaletteBits) {
final int paletteLength = Type.VAR_INT.readPrimitive(buffer); final int paletteLength = Type.VAR_INT.readPrimitive(buffer);
palette = new DataPaletteImpl(type.size(), paletteLength); palette = new DataPaletteImpl(type.size(), paletteLength);
@ -77,16 +72,11 @@ public final class PaletteType1_18 extends Type<DataPalette> {
} }
// Read values // Read values
final int valuesLength = Type.VAR_INT.readPrimitive(buffer); final long[] values = Type.LONG_ARRAY_PRIMITIVE.read(buffer);
if (valuesLength > 0) { if (values.length > 0) {
final long[] values = new long[valuesLength];
for (int i = 0; i < valuesLength; i++) {
values[i] = buffer.readLong();
}
final int valuesPerLong = (char) (64 / bitsPerValue); final int valuesPerLong = (char) (64 / bitsPerValue);
final int expectedLength = (type.size() + valuesPerLong - 1) / valuesPerLong; final int expectedLength = (type.size() + valuesPerLong - 1) / valuesPerLong;
if (valuesLength == expectedLength) { // Thanks, Hypixel if (values.length == expectedLength) { // Thanks, Hypixel
CompactArrayUtil.iterateCompactArrayWithPadding(bitsPerValue, type.size(), values, CompactArrayUtil.iterateCompactArrayWithPadding(bitsPerValue, type.size(), values,
bitsPerValue == globalPaletteBits ? palette::setIdAt : palette::setPaletteIndexAt); bitsPerValue == globalPaletteBits ? palette::setIdAt : palette::setPaletteIndexAt);
} }
@ -96,19 +86,18 @@ public final class PaletteType1_18 extends Type<DataPalette> {
@Override @Override
public void write(final ByteBuf buffer, final DataPalette palette) throws Exception { public void write(final ByteBuf buffer, final DataPalette palette) throws Exception {
if (palette.size() == 1) { final int size = palette.size();
if (size == 1) {
// Single value palette // Single value palette
buffer.writeByte(0); // 0 bit storage buffer.writeByte(0); // 0 bit storage
Type.VAR_INT.writePrimitive(buffer, palette.idByIndex(0)); Type.VAR_INT.writePrimitive(buffer, palette.idByIndex(0));
Type.VAR_INT.writePrimitive(buffer, 0); // Empty values length Type.VAR_INT.writePrimitive(buffer, 0); // Empty values length
return; return;
}/* else if (palette.size() == 0) { }
Via.getPlatform().getLogger().warning("Palette size is 0!");
}*/
// 1, 2, and 3 bit linear block palettes can't be read by the client // 1, 2, and 3 bit linear block palettes can't be read by the client
final int min = type == PaletteType.BLOCKS ? 4 : 1; final int min = type == PaletteType.BLOCKS ? 4 : 1;
int bitsPerValue = Math.max(min, MathUtil.ceilLog2(palette.size())); int bitsPerValue = Math.max(min, MathUtil.ceilLog2(size));
if (bitsPerValue > type.highestBitsPerValue()) { if (bitsPerValue > type.highestBitsPerValue()) {
bitsPerValue = globalPaletteBits; bitsPerValue = globalPaletteBits;
} }
@ -116,17 +105,13 @@ public final class PaletteType1_18 extends Type<DataPalette> {
buffer.writeByte(bitsPerValue); buffer.writeByte(bitsPerValue);
if (bitsPerValue != globalPaletteBits) { if (bitsPerValue != globalPaletteBits) {
// Write pallete // Write palette
Type.VAR_INT.writePrimitive(buffer, palette.size()); Type.VAR_INT.writePrimitive(buffer, size);
for (int i = 0; i < palette.size(); i++) { for (int i = 0; i < size; i++) {
Type.VAR_INT.writePrimitive(buffer, palette.idByIndex(i)); Type.VAR_INT.writePrimitive(buffer, palette.idByIndex(i));
} }
} }
final long[] data = CompactArrayUtil.createCompactArrayWithPadding(bitsPerValue, type.size(), bitsPerValue == globalPaletteBits ? palette::idAt : palette::paletteIndexAt); Type.LONG_ARRAY_PRIMITIVE.write(buffer, CompactArrayUtil.createCompactArrayWithPadding(bitsPerValue, type.size(), bitsPerValue == globalPaletteBits ? palette::idAt : palette::paletteIndexAt));
Type.VAR_INT.writePrimitive(buffer, data.length);
for (final long l : data) {
buffer.writeLong(l);
}
} }
} }