2021-09-16 13:03:31 +02:00
|
|
|
/*
|
|
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
2023-01-12 12:45:53 +01:00
|
|
|
* Copyright (C) 2016-2023 ViaVersion and contributors
|
2021-09-16 13:03:31 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
package com.viaversion.viaversion.api.type.types.version;
|
|
|
|
|
|
|
|
import com.viaversion.viaversion.api.minecraft.chunks.DataPalette;
|
|
|
|
import com.viaversion.viaversion.api.minecraft.chunks.DataPaletteImpl;
|
|
|
|
import com.viaversion.viaversion.api.minecraft.chunks.PaletteType;
|
|
|
|
import com.viaversion.viaversion.api.type.Type;
|
|
|
|
import com.viaversion.viaversion.util.CompactArrayUtil;
|
2021-12-01 17:50:23 +01:00
|
|
|
import com.viaversion.viaversion.util.MathUtil;
|
2021-09-16 13:03:31 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
2021-09-16 18:12:33 +02:00
|
|
|
public final class PaletteType1_18 extends Type<DataPalette> {
|
|
|
|
private final int globalPaletteBits;
|
|
|
|
private final PaletteType type;
|
2021-09-16 13:03:31 +02:00
|
|
|
|
2021-09-16 18:12:33 +02:00
|
|
|
public PaletteType1_18(final PaletteType type, final int globalPaletteBits) {
|
|
|
|
super(DataPalette.class);
|
|
|
|
this.globalPaletteBits = globalPaletteBits;
|
|
|
|
this.type = type;
|
2021-09-16 13:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-09-16 18:12:33 +02:00
|
|
|
public DataPalette read(final ByteBuf buffer) throws Exception {
|
2023-10-11 15:14:41 +02:00
|
|
|
int bitsPerValue = buffer.readByte();
|
2021-09-16 13:03:31 +02:00
|
|
|
final DataPaletteImpl palette;
|
|
|
|
if (bitsPerValue == 0) {
|
2022-07-24 19:03:24 +02:00
|
|
|
// Single value storage
|
2021-10-08 12:52:53 +02:00
|
|
|
palette = new DataPaletteImpl(type.size(), 1);
|
2021-09-16 13:03:31 +02:00
|
|
|
palette.addId(Type.VAR_INT.readPrimitive(buffer));
|
2022-07-24 19:03:24 +02:00
|
|
|
Type.LONG_ARRAY_PRIMITIVE.read(buffer); // Just eat it if not empty - thanks, Hypixel
|
2021-09-16 13:03:31 +02:00
|
|
|
return palette;
|
|
|
|
}
|
|
|
|
|
2022-07-23 13:56:02 +02:00
|
|
|
if (bitsPerValue < 0 || bitsPerValue > type.highestBitsPerValue()) {
|
|
|
|
bitsPerValue = globalPaletteBits;
|
|
|
|
} else if (type == PaletteType.BLOCKS && bitsPerValue < 4) {
|
|
|
|
bitsPerValue = 4; // Linear block palette values are always 4 bits
|
|
|
|
}
|
|
|
|
|
2022-07-24 19:03:24 +02:00
|
|
|
// Read palette
|
2021-09-16 18:12:33 +02:00
|
|
|
if (bitsPerValue != globalPaletteBits) {
|
2021-09-16 13:03:31 +02:00
|
|
|
final int paletteLength = Type.VAR_INT.readPrimitive(buffer);
|
2021-10-08 12:52:53 +02:00
|
|
|
palette = new DataPaletteImpl(type.size(), paletteLength);
|
2021-09-16 13:03:31 +02:00
|
|
|
for (int i = 0; i < paletteLength; i++) {
|
|
|
|
palette.addId(Type.VAR_INT.readPrimitive(buffer));
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-08 12:52:53 +02:00
|
|
|
palette = new DataPaletteImpl(type.size());
|
2021-09-16 13:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read values
|
2022-07-24 19:03:24 +02:00
|
|
|
final long[] values = Type.LONG_ARRAY_PRIMITIVE.read(buffer);
|
|
|
|
if (values.length > 0) {
|
2022-07-24 18:42:25 +02:00
|
|
|
final int valuesPerLong = (char) (64 / bitsPerValue);
|
|
|
|
final int expectedLength = (type.size() + valuesPerLong - 1) / valuesPerLong;
|
2022-07-24 19:03:24 +02:00
|
|
|
if (values.length == expectedLength) { // Thanks, Hypixel
|
2022-07-24 18:42:25 +02:00
|
|
|
CompactArrayUtil.iterateCompactArrayWithPadding(bitsPerValue, type.size(), values,
|
|
|
|
bitsPerValue == globalPaletteBits ? palette::setIdAt : palette::setPaletteIndexAt);
|
|
|
|
}
|
2021-09-16 13:03:31 +02:00
|
|
|
}
|
|
|
|
return palette;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-09-16 18:12:33 +02:00
|
|
|
public void write(final ByteBuf buffer, final DataPalette palette) throws Exception {
|
2022-07-24 19:03:24 +02:00
|
|
|
final int size = palette.size();
|
|
|
|
if (size == 1) {
|
2021-12-01 17:50:23 +01:00
|
|
|
// Single value palette
|
|
|
|
buffer.writeByte(0); // 0 bit storage
|
2021-09-16 13:03:31 +02:00
|
|
|
Type.VAR_INT.writePrimitive(buffer, palette.idByIndex(0));
|
|
|
|
Type.VAR_INT.writePrimitive(buffer, 0); // Empty values length
|
|
|
|
return;
|
2022-07-24 19:03:24 +02:00
|
|
|
}
|
2021-12-01 17:50:23 +01:00
|
|
|
|
|
|
|
// 1, 2, and 3 bit linear block palettes can't be read by the client
|
|
|
|
final int min = type == PaletteType.BLOCKS ? 4 : 1;
|
2022-07-24 19:03:24 +02:00
|
|
|
int bitsPerValue = Math.max(min, MathUtil.ceilLog2(size));
|
2021-12-08 21:31:53 +01:00
|
|
|
if (bitsPerValue > type.highestBitsPerValue()) {
|
|
|
|
bitsPerValue = globalPaletteBits;
|
|
|
|
}
|
|
|
|
|
2021-12-01 17:50:23 +01:00
|
|
|
buffer.writeByte(bitsPerValue);
|
2021-09-16 13:03:31 +02:00
|
|
|
|
2021-09-16 18:12:33 +02:00
|
|
|
if (bitsPerValue != globalPaletteBits) {
|
2022-07-24 19:03:24 +02:00
|
|
|
// Write palette
|
|
|
|
Type.VAR_INT.writePrimitive(buffer, size);
|
|
|
|
for (int i = 0; i < size; i++) {
|
2021-09-16 13:03:31 +02:00
|
|
|
Type.VAR_INT.writePrimitive(buffer, palette.idByIndex(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 19:03:24 +02:00
|
|
|
Type.LONG_ARRAY_PRIMITIVE.write(buffer, CompactArrayUtil.createCompactArrayWithPadding(bitsPerValue, type.size(), bitsPerValue == globalPaletteBits ? palette::idAt : palette::paletteIndexAt));
|
2021-09-16 13:03:31 +02:00
|
|
|
}
|
|
|
|
}
|