ChunkSerializer: Extract serializeSubChunk() from main serialize() routine

this will be used in the future for cache-based chunk sends.
This commit is contained in:
Dylan K. Taylor 2021-07-17 18:29:14 +01:00
parent 5fbc7681b0
commit e5327a0f3e
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 25 additions and 20 deletions

View File

@ -71,7 +71,7 @@ class ChunkRequestTask extends AsyncTask{
$chunk = FastChunkSerializer::deserialize($this->chunk);
$subCount = ChunkSerializer::getSubChunkCount($chunk);
$encoderContext = new PacketSerializerContext(GlobalItemTypeDictionary::getInstance()->getDictionary());
$payload = ChunkSerializer::serialize($chunk, RuntimeBlockMapping::getInstance(), $encoderContext, $this->tiles);
$payload = ChunkSerializer::serializeFullChunk($chunk, RuntimeBlockMapping::getInstance(), $encoderContext, $this->tiles);
$this->setResult($this->compressor->compress(PacketBatch::fromPackets($encoderContext, LevelChunkPacket::withoutCache($this->chunkX, $this->chunkZ, $subCount, $payload))->getBuffer()));
}

View File

@ -30,6 +30,7 @@ use pocketmine\network\mcpe\protocol\serializer\PacketSerializerContext;
use pocketmine\utils\Binary;
use pocketmine\utils\BinaryStream;
use pocketmine\world\format\Chunk;
use pocketmine\world\format\SubChunk;
use function count;
final class ChunkSerializer{
@ -53,28 +54,11 @@ final class ChunkSerializer{
return 0;
}
public static function serialize(Chunk $chunk, RuntimeBlockMapping $blockMapper, PacketSerializerContext $encoderContext, ?string $tiles = null) : string{
public static function serializeFullChunk(Chunk $chunk, RuntimeBlockMapping $blockMapper, PacketSerializerContext $encoderContext, ?string $tiles = null) : string{
$stream = PacketSerializer::encoder($encoderContext);
$subChunkCount = self::getSubChunkCount($chunk);
for($y = 0; $y < $subChunkCount; ++$y){
$layers = $chunk->getSubChunk($y)->getBlockLayers();
$stream->putByte(8); //version
$stream->putByte(count($layers));
foreach($layers as $blocks){
$stream->putByte(($blocks->getBitsPerBlock() << 1) | 1); //last 1-bit means "network format", but seems pointless
$stream->put($blocks->getWordArray());
$palette = $blocks->getPalette();
//these LSHIFT by 1 uvarints are optimizations: the client expects zigzag varints here
//but since we know they are always unsigned, we can avoid the extra fcall overhead of
//zigzag and just shift directly.
$stream->putUnsignedVarInt(count($palette) << 1); //yes, this is intentionally zigzag
foreach($palette as $p){
$stream->put(Binary::writeUnsignedVarInt($blockMapper->toRuntimeId($p) << 1));
}
}
self::serializeSubChunk($chunk->getSubChunk($y), $blockMapper, $stream);
}
$stream->put($chunk->getBiomeIdArray());
$stream->putByte(0); //border block array count
@ -88,6 +72,27 @@ final class ChunkSerializer{
return $stream->getBuffer();
}
public static function serializeSubChunk(SubChunk $subChunk, RuntimeBlockMapping $blockMapper, PacketSerializer $stream) : void{
$layers = $subChunk->getBlockLayers();
$stream->putByte(8); //version
$stream->putByte(count($layers));
foreach($layers as $blocks){
$stream->putByte(($blocks->getBitsPerBlock() << 1) | 1); //last 1-bit means "network format", but seems pointless
$stream->put($blocks->getWordArray());
$palette = $blocks->getPalette();
//these LSHIFT by 1 uvarints are optimizations: the client expects zigzag varints here
//but since we know they are always unsigned, we can avoid the extra fcall overhead of
//zigzag and just shift directly.
$stream->putUnsignedVarInt(count($palette) << 1); //yes, this is intentionally zigzag
foreach($palette as $p){
$stream->put(Binary::writeUnsignedVarInt($blockMapper->toRuntimeId($p) << 1));
}
}
}
public static function serializeTiles(Chunk $chunk) : string{
$stream = new BinaryStream();
foreach($chunk->getTiles() as $tile){