mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 00:07:30 +00:00
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:
parent
e6f6a036ef
commit
8de30e8162
@ -58,7 +58,7 @@ class ChunkRequestTask extends AsyncTask{
|
||||
public function __construct(int $chunkX, int $chunkZ, Chunk $chunk, CompressBatchPromise $promise, Compressor $compressor, ?\Closure $onError = null){
|
||||
$this->compressor = $compressor;
|
||||
|
||||
$this->chunk = FastChunkSerializer::serializeWithoutLight($chunk);
|
||||
$this->chunk = FastChunkSerializer::serializeTerrain($chunk);
|
||||
$this->chunkX = $chunkX;
|
||||
$this->chunkZ = $chunkZ;
|
||||
$this->tiles = ChunkSerializer::serializeTiles($chunk);
|
||||
@ -68,7 +68,7 @@ class ChunkRequestTask extends AsyncTask{
|
||||
}
|
||||
|
||||
public function onRun() : void{
|
||||
$chunk = FastChunkSerializer::deserialize($this->chunk);
|
||||
$chunk = FastChunkSerializer::deserializeTerrain($this->chunk);
|
||||
$subCount = ChunkSerializer::getSubChunkCount($chunk);
|
||||
$encoderContext = new PacketSerializerContext(GlobalItemTypeDictionary::getInstance()->getDictionary());
|
||||
$payload = ChunkSerializer::serializeFullChunk($chunk, RuntimeBlockMapping::getInstance(), $encoderContext, $this->tiles);
|
||||
|
@ -26,8 +26,6 @@ namespace pocketmine\world\format\io;
|
||||
use pocketmine\utils\BinaryStream;
|
||||
use pocketmine\world\format\BiomeArray;
|
||||
use pocketmine\world\format\Chunk;
|
||||
use pocketmine\world\format\HeightArray;
|
||||
use pocketmine\world\format\LightArray;
|
||||
use pocketmine\world\format\PalettedBlockArray;
|
||||
use pocketmine\world\format\SubChunk;
|
||||
use function array_values;
|
||||
@ -42,26 +40,18 @@ use function unpack;
|
||||
*/
|
||||
final class FastChunkSerializer{
|
||||
private const FLAG_POPULATED = 1 << 1;
|
||||
private const FLAG_HAS_LIGHT = 1 << 2;
|
||||
|
||||
private function __construct(){
|
||||
//NOOP
|
||||
}
|
||||
|
||||
public static function serializeWithoutLight(Chunk $chunk) : string{
|
||||
return self::serialize($chunk, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fast-serializes the chunk for passing between threads
|
||||
* TODO: tiles and entities
|
||||
*/
|
||||
public static function serialize(Chunk $chunk, bool $includeLight = true) : string{
|
||||
$includeLight = $includeLight && $chunk->isLightPopulated() === true;
|
||||
|
||||
public static function serializeTerrain(Chunk $chunk) : string{
|
||||
$stream = new BinaryStream();
|
||||
$stream->putByte(
|
||||
($includeLight ? self::FLAG_HAS_LIGHT : 0) |
|
||||
($chunk->isPopulated() ? self::FLAG_POPULATED : 0)
|
||||
);
|
||||
|
||||
@ -85,18 +75,10 @@ final class FastChunkSerializer{
|
||||
$stream->putInt(strlen($serialPalette));
|
||||
$stream->put($serialPalette);
|
||||
}
|
||||
|
||||
if($includeLight){
|
||||
$stream->put($subChunk->getBlockSkyLightArray()->getData());
|
||||
$stream->put($subChunk->getBlockLightArray()->getData());
|
||||
}
|
||||
}
|
||||
|
||||
//biomes
|
||||
$stream->put($chunk->getBiomeIdArray());
|
||||
if($includeLight){
|
||||
$stream->put(pack("S*", ...$chunk->getHeightMapArray()));
|
||||
}
|
||||
|
||||
return $stream->getBuffer();
|
||||
}
|
||||
@ -104,15 +86,13 @@ final class FastChunkSerializer{
|
||||
/**
|
||||
* Deserializes a fast-serialized chunk
|
||||
*/
|
||||
public static function deserialize(string $data) : Chunk{
|
||||
public static function deserializeTerrain(string $data) : Chunk{
|
||||
$stream = new BinaryStream($data);
|
||||
|
||||
$flags = $stream->getByte();
|
||||
$lightPopulated = (bool) ($flags & self::FLAG_HAS_LIGHT);
|
||||
$terrainPopulated = (bool) ($flags & self::FLAG_POPULATED);
|
||||
|
||||
$subChunks = [];
|
||||
$heightMap = null;
|
||||
|
||||
$count = $stream->getByte();
|
||||
for($subCount = 0; $subCount < $count; ++$subCount){
|
||||
@ -130,21 +110,13 @@ final class FastChunkSerializer{
|
||||
|
||||
$layers[] = PalettedBlockArray::fromData($bitsPerBlock, $words, $palette);
|
||||
}
|
||||
$subChunks[$y] = new SubChunk(
|
||||
$airBlockId, $layers, $lightPopulated ? new LightArray($stream->get(2048)) : null, $lightPopulated ? new LightArray($stream->get(2048)) : null
|
||||
);
|
||||
$subChunks[$y] = new SubChunk($airBlockId, $layers);
|
||||
}
|
||||
|
||||
$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->setLightPopulated($lightPopulated);
|
||||
$chunk->clearTerrainDirtyFlags();
|
||||
|
||||
return $chunk;
|
||||
|
@ -68,10 +68,10 @@ class PopulationTask extends AsyncTask{
|
||||
$this->worldId = $world->getId();
|
||||
$this->chunkX = $chunkX;
|
||||
$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){
|
||||
$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);
|
||||
@ -88,7 +88,7 @@ class PopulationTask extends AsyncTask{
|
||||
/** @var Chunk[] $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){
|
||||
if($i === 4){
|
||||
@ -98,7 +98,7 @@ class PopulationTask extends AsyncTask{
|
||||
if($ck === null){
|
||||
$chunks[$i] = null;
|
||||
}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->setPopulated();
|
||||
|
||||
$this->chunk = FastChunkSerializer::serializeWithoutLight($chunk);
|
||||
$this->chunk = FastChunkSerializer::serializeTerrain($chunk);
|
||||
|
||||
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 */
|
||||
$world = $this->fetchLocal(self::TLS_KEY_WORLD);
|
||||
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){
|
||||
if($i === 4){
|
||||
@ -157,7 +157,7 @@ class PopulationTask extends AsyncTask{
|
||||
$xx = -1 + $i % 3;
|
||||
$zz = -1 + intdiv($i, 3);
|
||||
|
||||
$c = FastChunkSerializer::deserialize($c);
|
||||
$c = FastChunkSerializer::deserializeTerrain($c);
|
||||
$world->generateChunkCallback($this->chunkX + $xx, $this->chunkZ + $zz, $c);
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
public function onRun() : void{
|
||||
$chunk = FastChunkSerializer::deserialize($this->chunk);
|
||||
$chunk = FastChunkSerializer::deserializeTerrain($this->chunk);
|
||||
|
||||
$manager = new SimpleChunkManager(World::Y_MIN, World::Y_MAX);
|
||||
$manager->setChunk(0, 0, $chunk);
|
||||
|
Loading…
x
Reference in New Issue
Block a user