mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-15 07:55:31 +00:00
ChunkSerializer: Extract serializeSubChunk() from main serialize() routine
this will be used in the future for cache-based chunk sends.
This commit is contained in:
parent
5fbc7681b0
commit
e5327a0f3e
@ -71,7 +71,7 @@ class ChunkRequestTask extends AsyncTask{
|
|||||||
$chunk = FastChunkSerializer::deserialize($this->chunk);
|
$chunk = FastChunkSerializer::deserialize($this->chunk);
|
||||||
$subCount = ChunkSerializer::getSubChunkCount($chunk);
|
$subCount = ChunkSerializer::getSubChunkCount($chunk);
|
||||||
$encoderContext = new PacketSerializerContext(GlobalItemTypeDictionary::getInstance()->getDictionary());
|
$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()));
|
$this->setResult($this->compressor->compress(PacketBatch::fromPackets($encoderContext, LevelChunkPacket::withoutCache($this->chunkX, $this->chunkZ, $subCount, $payload))->getBuffer()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,6 +30,7 @@ use pocketmine\network\mcpe\protocol\serializer\PacketSerializerContext;
|
|||||||
use pocketmine\utils\Binary;
|
use pocketmine\utils\Binary;
|
||||||
use pocketmine\utils\BinaryStream;
|
use pocketmine\utils\BinaryStream;
|
||||||
use pocketmine\world\format\Chunk;
|
use pocketmine\world\format\Chunk;
|
||||||
|
use pocketmine\world\format\SubChunk;
|
||||||
use function count;
|
use function count;
|
||||||
|
|
||||||
final class ChunkSerializer{
|
final class ChunkSerializer{
|
||||||
@ -53,28 +54,11 @@ final class ChunkSerializer{
|
|||||||
return 0;
|
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);
|
$stream = PacketSerializer::encoder($encoderContext);
|
||||||
$subChunkCount = self::getSubChunkCount($chunk);
|
$subChunkCount = self::getSubChunkCount($chunk);
|
||||||
for($y = 0; $y < $subChunkCount; ++$y){
|
for($y = 0; $y < $subChunkCount; ++$y){
|
||||||
$layers = $chunk->getSubChunk($y)->getBlockLayers();
|
self::serializeSubChunk($chunk->getSubChunk($y), $blockMapper, $stream);
|
||||||
$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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$stream->put($chunk->getBiomeIdArray());
|
$stream->put($chunk->getBiomeIdArray());
|
||||||
$stream->putByte(0); //border block array count
|
$stream->putByte(0); //border block array count
|
||||||
@ -88,6 +72,27 @@ final class ChunkSerializer{
|
|||||||
return $stream->getBuffer();
|
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{
|
public static function serializeTiles(Chunk $chunk) : string{
|
||||||
$stream = new BinaryStream();
|
$stream = new BinaryStream();
|
||||||
foreach($chunk->getTiles() as $tile){
|
foreach($chunk->getTiles() as $tile){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user