mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-13 01:09:44 +00:00
Moved network chunk serializing code to network\mcpe namespace
This commit is contained in:
parent
08de657c8d
commit
2cb6fda286
@ -43,7 +43,7 @@ class ChunkRequestTask extends AsyncTask{
|
|||||||
public function __construct(int $chunkX, int $chunkZ, Chunk $chunk, CompressBatchPromise $promise, ?\Closure $onError = null){
|
public function __construct(int $chunkX, int $chunkZ, Chunk $chunk, CompressBatchPromise $promise, ?\Closure $onError = null){
|
||||||
$this->compressionLevel = NetworkCompression::$LEVEL;
|
$this->compressionLevel = NetworkCompression::$LEVEL;
|
||||||
|
|
||||||
$this->chunk = $chunk->networkSerialize();
|
$this->chunk = ChunkSerializer::serialize($chunk);
|
||||||
$this->chunkX = $chunkX;
|
$this->chunkX = $chunkX;
|
||||||
$this->chunkZ = $chunkZ;
|
$this->chunkZ = $chunkZ;
|
||||||
|
|
||||||
|
77
src/pocketmine/network/mcpe/ChunkSerializer.php
Normal file
77
src/pocketmine/network/mcpe/ChunkSerializer.php
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pocketmine\network\mcpe;
|
||||||
|
|
||||||
|
use pocketmine\block\tile\Spawnable;
|
||||||
|
use pocketmine\network\mcpe\protocol\types\RuntimeBlockMapping;
|
||||||
|
use pocketmine\world\format\Chunk;
|
||||||
|
use function count;
|
||||||
|
use function pack;
|
||||||
|
|
||||||
|
final class ChunkSerializer{
|
||||||
|
|
||||||
|
private function __construct(){
|
||||||
|
//NOOP
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Chunk $chunk
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function serialize(Chunk $chunk) : string{
|
||||||
|
$stream = new NetworkBinaryStream();
|
||||||
|
$subChunkCount = $chunk->getSubChunkSendCount();
|
||||||
|
$stream->putByte($subChunkCount);
|
||||||
|
|
||||||
|
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();
|
||||||
|
$stream->putVarInt(count($palette)); //yes, this is intentionally zigzag
|
||||||
|
foreach($palette as $p){
|
||||||
|
$stream->putVarInt(RuntimeBlockMapping::toStaticRuntimeId($p >> 4, $p & 0xf));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$stream->put(pack("v*", ...$chunk->getHeightMapArray()));
|
||||||
|
$stream->put($chunk->getBiomeIdArray());
|
||||||
|
$stream->putByte(0); //border block array count
|
||||||
|
//Border block entry format: 1 byte (4 bits X, 4 bits Z). These are however useless since they crash the regular client.
|
||||||
|
|
||||||
|
foreach($chunk->getTiles() as $tile){
|
||||||
|
if($tile instanceof Spawnable){
|
||||||
|
$stream->put($tile->getSerializedSpawnCompound());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $stream->getBuffer();
|
||||||
|
}
|
||||||
|
}
|
@ -28,14 +28,11 @@ namespace pocketmine\world\format;
|
|||||||
|
|
||||||
use pocketmine\block\BlockFactory;
|
use pocketmine\block\BlockFactory;
|
||||||
use pocketmine\block\BlockLegacyIds;
|
use pocketmine\block\BlockLegacyIds;
|
||||||
use pocketmine\block\tile\Spawnable;
|
|
||||||
use pocketmine\block\tile\Tile;
|
use pocketmine\block\tile\Tile;
|
||||||
use pocketmine\block\tile\TileFactory;
|
use pocketmine\block\tile\TileFactory;
|
||||||
use pocketmine\entity\Entity;
|
use pocketmine\entity\Entity;
|
||||||
use pocketmine\entity\EntityFactory;
|
use pocketmine\entity\EntityFactory;
|
||||||
use pocketmine\nbt\tag\CompoundTag;
|
use pocketmine\nbt\tag\CompoundTag;
|
||||||
use pocketmine\network\mcpe\NetworkBinaryStream;
|
|
||||||
use pocketmine\network\mcpe\protocol\types\RuntimeBlockMapping;
|
|
||||||
use pocketmine\Player;
|
use pocketmine\Player;
|
||||||
use pocketmine\world\World;
|
use pocketmine\world\World;
|
||||||
use function array_fill;
|
use function array_fill;
|
||||||
@ -45,7 +42,6 @@ use function assert;
|
|||||||
use function chr;
|
use function chr;
|
||||||
use function count;
|
use function count;
|
||||||
use function ord;
|
use function ord;
|
||||||
use function pack;
|
|
||||||
use function str_repeat;
|
use function str_repeat;
|
||||||
use function strlen;
|
use function strlen;
|
||||||
|
|
||||||
@ -723,46 +719,6 @@ class Chunk{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Serializes the chunk for sending to players
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function networkSerialize() : string{
|
|
||||||
$stream = new NetworkBinaryStream();
|
|
||||||
$subChunkCount = $this->getSubChunkSendCount();
|
|
||||||
$stream->putByte($subChunkCount);
|
|
||||||
|
|
||||||
for($y = 0; $y < $subChunkCount; ++$y){
|
|
||||||
$layers = $this->subChunks[$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();
|
|
||||||
$stream->putVarInt(count($palette)); //yes, this is intentionally zigzag
|
|
||||||
foreach($palette as $p){
|
|
||||||
$stream->putVarInt(RuntimeBlockMapping::toStaticRuntimeId($p >> 4, $p & 0xf));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$stream->put(pack("v*", ...$this->heightMap));
|
|
||||||
$stream->put($this->biomeIds);
|
|
||||||
$stream->putByte(0); //border block array count
|
|
||||||
//Border block entry format: 1 byte (4 bits X, 4 bits Z). These are however useless since they crash the regular client.
|
|
||||||
|
|
||||||
foreach($this->tiles as $tile){
|
|
||||||
if($tile instanceof Spawnable){
|
|
||||||
$stream->put($tile->getSerializedSpawnCompound());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $stream->getBuffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hashes the given chunk block coordinates into a single integer.
|
* Hashes the given chunk block coordinates into a single integer.
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user