FastChunkSerializer no longer serializes light by default

the core doesn't use this anywhere.
serializeWithoutLight() has been renamed to serializeTerrain() to more accurately describe what it does.
This commit is contained in:
Dylan K. Taylor 2021-10-01 22:57:22 +01:00
parent e6f6a036ef
commit 8de30e8162
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
4 changed files with 16 additions and 44 deletions

View File

@ -58,7 +58,7 @@ class ChunkRequestTask extends AsyncTask{
public function __construct(int $chunkX, int $chunkZ, Chunk $chunk, CompressBatchPromise $promise, Compressor $compressor, ?\Closure $onError = null){ public function __construct(int $chunkX, int $chunkZ, Chunk $chunk, CompressBatchPromise $promise, Compressor $compressor, ?\Closure $onError = null){
$this->compressor = $compressor; $this->compressor = $compressor;
$this->chunk = FastChunkSerializer::serializeWithoutLight($chunk); $this->chunk = FastChunkSerializer::serializeTerrain($chunk);
$this->chunkX = $chunkX; $this->chunkX = $chunkX;
$this->chunkZ = $chunkZ; $this->chunkZ = $chunkZ;
$this->tiles = ChunkSerializer::serializeTiles($chunk); $this->tiles = ChunkSerializer::serializeTiles($chunk);
@ -68,7 +68,7 @@ class ChunkRequestTask extends AsyncTask{
} }
public function onRun() : void{ public function onRun() : void{
$chunk = FastChunkSerializer::deserialize($this->chunk); $chunk = FastChunkSerializer::deserializeTerrain($this->chunk);
$subCount = ChunkSerializer::getSubChunkCount($chunk); $subCount = ChunkSerializer::getSubChunkCount($chunk);
$encoderContext = new PacketSerializerContext(GlobalItemTypeDictionary::getInstance()->getDictionary()); $encoderContext = new PacketSerializerContext(GlobalItemTypeDictionary::getInstance()->getDictionary());
$payload = ChunkSerializer::serializeFullChunk($chunk, RuntimeBlockMapping::getInstance(), $encoderContext, $this->tiles); $payload = ChunkSerializer::serializeFullChunk($chunk, RuntimeBlockMapping::getInstance(), $encoderContext, $this->tiles);

View File

@ -26,8 +26,6 @@ namespace pocketmine\world\format\io;
use pocketmine\utils\BinaryStream; use pocketmine\utils\BinaryStream;
use pocketmine\world\format\BiomeArray; use pocketmine\world\format\BiomeArray;
use pocketmine\world\format\Chunk; use pocketmine\world\format\Chunk;
use pocketmine\world\format\HeightArray;
use pocketmine\world\format\LightArray;
use pocketmine\world\format\PalettedBlockArray; use pocketmine\world\format\PalettedBlockArray;
use pocketmine\world\format\SubChunk; use pocketmine\world\format\SubChunk;
use function array_values; use function array_values;
@ -42,26 +40,18 @@ use function unpack;
*/ */
final class FastChunkSerializer{ final class FastChunkSerializer{
private const FLAG_POPULATED = 1 << 1; private const FLAG_POPULATED = 1 << 1;
private const FLAG_HAS_LIGHT = 1 << 2;
private function __construct(){ private function __construct(){
//NOOP //NOOP
} }
public static function serializeWithoutLight(Chunk $chunk) : string{
return self::serialize($chunk, false);
}
/** /**
* Fast-serializes the chunk for passing between threads * Fast-serializes the chunk for passing between threads
* TODO: tiles and entities * TODO: tiles and entities
*/ */
public static function serialize(Chunk $chunk, bool $includeLight = true) : string{ public static function serializeTerrain(Chunk $chunk) : string{
$includeLight = $includeLight && $chunk->isLightPopulated() === true;
$stream = new BinaryStream(); $stream = new BinaryStream();
$stream->putByte( $stream->putByte(
($includeLight ? self::FLAG_HAS_LIGHT : 0) |
($chunk->isPopulated() ? self::FLAG_POPULATED : 0) ($chunk->isPopulated() ? self::FLAG_POPULATED : 0)
); );
@ -85,18 +75,10 @@ final class FastChunkSerializer{
$stream->putInt(strlen($serialPalette)); $stream->putInt(strlen($serialPalette));
$stream->put($serialPalette); $stream->put($serialPalette);
} }
if($includeLight){
$stream->put($subChunk->getBlockSkyLightArray()->getData());
$stream->put($subChunk->getBlockLightArray()->getData());
}
} }
//biomes //biomes
$stream->put($chunk->getBiomeIdArray()); $stream->put($chunk->getBiomeIdArray());
if($includeLight){
$stream->put(pack("S*", ...$chunk->getHeightMapArray()));
}
return $stream->getBuffer(); return $stream->getBuffer();
} }
@ -104,15 +86,13 @@ final class FastChunkSerializer{
/** /**
* Deserializes a fast-serialized chunk * Deserializes a fast-serialized chunk
*/ */
public static function deserialize(string $data) : Chunk{ public static function deserializeTerrain(string $data) : Chunk{
$stream = new BinaryStream($data); $stream = new BinaryStream($data);
$flags = $stream->getByte(); $flags = $stream->getByte();
$lightPopulated = (bool) ($flags & self::FLAG_HAS_LIGHT);
$terrainPopulated = (bool) ($flags & self::FLAG_POPULATED); $terrainPopulated = (bool) ($flags & self::FLAG_POPULATED);
$subChunks = []; $subChunks = [];
$heightMap = null;
$count = $stream->getByte(); $count = $stream->getByte();
for($subCount = 0; $subCount < $count; ++$subCount){ for($subCount = 0; $subCount < $count; ++$subCount){
@ -130,21 +110,13 @@ final class FastChunkSerializer{
$layers[] = PalettedBlockArray::fromData($bitsPerBlock, $words, $palette); $layers[] = PalettedBlockArray::fromData($bitsPerBlock, $words, $palette);
} }
$subChunks[$y] = new SubChunk( $subChunks[$y] = new SubChunk($airBlockId, $layers);
$airBlockId, $layers, $lightPopulated ? new LightArray($stream->get(2048)) : null, $lightPopulated ? new LightArray($stream->get(2048)) : null
);
} }
$biomeIds = new BiomeArray($stream->get(256)); $biomeIds = new BiomeArray($stream->get(256));
if($lightPopulated){
/** @var int[] $unpackedHeightMap */
$unpackedHeightMap = unpack("S*", $stream->get(512)); //unpack() will never fail here
$heightMap = new HeightArray(array_values($unpackedHeightMap));
}
$chunk = new Chunk($subChunks, $biomeIds, $heightMap); $chunk = new Chunk($subChunks, $biomeIds);
$chunk->setPopulated($terrainPopulated); $chunk->setPopulated($terrainPopulated);
$chunk->setLightPopulated($lightPopulated);
$chunk->clearTerrainDirtyFlags(); $chunk->clearTerrainDirtyFlags();
return $chunk; return $chunk;

View File

@ -68,10 +68,10 @@ class PopulationTask extends AsyncTask{
$this->worldId = $world->getId(); $this->worldId = $world->getId();
$this->chunkX = $chunkX; $this->chunkX = $chunkX;
$this->chunkZ = $chunkZ; $this->chunkZ = $chunkZ;
$this->chunk = $chunk !== null ? FastChunkSerializer::serializeWithoutLight($chunk) : null; $this->chunk = $chunk !== null ? FastChunkSerializer::serializeTerrain($chunk) : null;
foreach($world->getAdjacentChunks($chunkX, $chunkZ) as $i => $c){ foreach($world->getAdjacentChunks($chunkX, $chunkZ) as $i => $c){
$this->{"chunk$i"} = $c !== null ? FastChunkSerializer::serializeWithoutLight($c) : null; $this->{"chunk$i"} = $c !== null ? FastChunkSerializer::serializeTerrain($c) : null;
} }
$this->storeLocal(self::TLS_KEY_WORLD, $world); $this->storeLocal(self::TLS_KEY_WORLD, $world);
@ -88,7 +88,7 @@ class PopulationTask extends AsyncTask{
/** @var Chunk[] $chunks */ /** @var Chunk[] $chunks */
$chunks = []; $chunks = [];
$chunk = $this->chunk !== null ? FastChunkSerializer::deserialize($this->chunk) : null; $chunk = $this->chunk !== null ? FastChunkSerializer::deserializeTerrain($this->chunk) : null;
for($i = 0; $i < 9; ++$i){ for($i = 0; $i < 9; ++$i){
if($i === 4){ if($i === 4){
@ -98,7 +98,7 @@ class PopulationTask extends AsyncTask{
if($ck === null){ if($ck === null){
$chunks[$i] = null; $chunks[$i] = null;
}else{ }else{
$chunks[$i] = FastChunkSerializer::deserialize($ck); $chunks[$i] = FastChunkSerializer::deserializeTerrain($ck);
} }
} }
@ -135,10 +135,10 @@ class PopulationTask extends AsyncTask{
$chunk = $manager->getChunk($this->chunkX, $this->chunkZ); $chunk = $manager->getChunk($this->chunkX, $this->chunkZ);
$chunk->setPopulated(); $chunk->setPopulated();
$this->chunk = FastChunkSerializer::serializeWithoutLight($chunk); $this->chunk = FastChunkSerializer::serializeTerrain($chunk);
foreach($chunks as $i => $c){ foreach($chunks as $i => $c){
$this->{"chunk$i"} = $c->isTerrainDirty() ? FastChunkSerializer::serializeWithoutLight($c) : null; $this->{"chunk$i"} = $c->isTerrainDirty() ? FastChunkSerializer::serializeTerrain($c) : null;
} }
} }
@ -146,7 +146,7 @@ class PopulationTask extends AsyncTask{
/** @var World $world */ /** @var World $world */
$world = $this->fetchLocal(self::TLS_KEY_WORLD); $world = $this->fetchLocal(self::TLS_KEY_WORLD);
if($world->isLoaded()){ if($world->isLoaded()){
$chunk = $this->chunk !== null ? FastChunkSerializer::deserialize($this->chunk) : null; $chunk = $this->chunk !== null ? FastChunkSerializer::deserializeTerrain($this->chunk) : null;
for($i = 0; $i < 9; ++$i){ for($i = 0; $i < 9; ++$i){
if($i === 4){ if($i === 4){
@ -157,7 +157,7 @@ class PopulationTask extends AsyncTask{
$xx = -1 + $i % 3; $xx = -1 + $i % 3;
$zz = -1 + intdiv($i, 3); $zz = -1 + intdiv($i, 3);
$c = FastChunkSerializer::deserialize($c); $c = FastChunkSerializer::deserializeTerrain($c);
$world->generateChunkCallback($this->chunkX + $xx, $this->chunkZ + $zz, $c); $world->generateChunkCallback($this->chunkX + $xx, $this->chunkZ + $zz, $c);
} }
} }

View File

@ -51,12 +51,12 @@ class LightPopulationTask extends AsyncTask{
* @phpstan-param \Closure(array<int, LightArray> $blockLight, array<int, LightArray> $skyLight, array<int, int> $heightMap) : void $onCompletion * @phpstan-param \Closure(array<int, LightArray> $blockLight, array<int, LightArray> $skyLight, array<int, int> $heightMap) : void $onCompletion
*/ */
public function __construct(Chunk $chunk, \Closure $onCompletion){ public function __construct(Chunk $chunk, \Closure $onCompletion){
$this->chunk = FastChunkSerializer::serializeWithoutLight($chunk); $this->chunk = FastChunkSerializer::serializeTerrain($chunk);
$this->storeLocal(self::TLS_KEY_COMPLETION_CALLBACK, $onCompletion); $this->storeLocal(self::TLS_KEY_COMPLETION_CALLBACK, $onCompletion);
} }
public function onRun() : void{ public function onRun() : void{
$chunk = FastChunkSerializer::deserialize($this->chunk); $chunk = FastChunkSerializer::deserializeTerrain($this->chunk);
$manager = new SimpleChunkManager(World::Y_MIN, World::Y_MAX); $manager = new SimpleChunkManager(World::Y_MIN, World::Y_MAX);
$manager->setChunk(0, 0, $chunk); $manager->setChunk(0, 0, $chunk);