PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
ChunkSerializer.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\network\mcpe\serializer;
25
26use pmmp\encoding\Byte;
27use pmmp\encoding\ByteBufferWriter;
28use pmmp\encoding\VarInt;
37use pocketmine\world\format\PalettedBlockArray;
39use function count;
40
41final class ChunkSerializer{
42 private function __construct(){
43 //NOOP
44 }
45
54 public static function getDimensionChunkBounds(int $dimensionId) : array{
55 return match($dimensionId){
56 DimensionIds::OVERWORLD => [-4, 19],
57 DimensionIds::NETHER => [0, 7],
58 DimensionIds::THE_END => [0, 15],
59 default => throw new \InvalidArgumentException("Unknown dimension ID $dimensionId"),
60 };
61 }
62
69 public static function getSubChunkCount(Chunk $chunk, int $dimensionId) : int{
70 //if the protocol world bounds ever exceed the PM supported bounds again in the future, we might need to
71 //polyfill some stuff here
72 [$minSubChunkIndex, $maxSubChunkIndex] = self::getDimensionChunkBounds($dimensionId);
73 for($y = $maxSubChunkIndex, $count = $maxSubChunkIndex - $minSubChunkIndex + 1; $y >= $minSubChunkIndex; --$y, --$count){
74 if($chunk->getSubChunk($y)->isEmptyFast()){
75 continue;
76 }
77 return $count;
78 }
79
80 return 0;
81 }
82
86 public static function serializeFullChunk(Chunk $chunk, int $dimensionId, BlockTranslator $blockTranslator, ?string $tiles = null) : string{
87 $stream = new ByteBufferWriter();
88
89 $subChunkCount = self::getSubChunkCount($chunk, $dimensionId);
90 $writtenCount = 0;
91
92 [$minSubChunkIndex, $maxSubChunkIndex] = self::getDimensionChunkBounds($dimensionId);
93 for($y = $minSubChunkIndex; $writtenCount < $subChunkCount; ++$y, ++$writtenCount){
94 self::serializeSubChunk($chunk->getSubChunk($y), $blockTranslator, $stream, false);
95 }
96
97 $biomeIdMap = LegacyBiomeIdToStringIdMap::getInstance();
98 //all biomes must always be written :(
99 for($y = $minSubChunkIndex; $y <= $maxSubChunkIndex; ++$y){
100 self::serializeBiomePalette($chunk->getSubChunk($y)->getBiomeArray(), $biomeIdMap, $stream);
101 }
102
103 Byte::writeUnsigned($stream, 0); //border block array count
104 //Border block entry format: 1 byte (4 bits X, 4 bits Z). These are however useless since they crash the regular client.
105
106 if($tiles !== null){
107 $stream->writeByteArray($tiles);
108 }else{
109 $stream->writeByteArray(self::serializeTiles($chunk));
110 }
111 return $stream->getData();
112 }
113
114 public static function serializeSubChunk(SubChunk $subChunk, BlockTranslator $blockTranslator, ByteBufferWriter $stream, bool $persistentBlockStates) : void{
115 $layers = $subChunk->getBlockLayers();
116 Byte::writeUnsigned($stream, 8); //version
117
118 Byte::writeUnsigned($stream, count($layers));
119
120 $blockStateDictionary = $blockTranslator->getBlockStateDictionary();
121
122 foreach($layers as $blocks){
123 $bitsPerBlock = $blocks->getBitsPerBlock();
124 $words = $blocks->getWordArray();
125 Byte::writeUnsigned($stream, ($bitsPerBlock << 1) | ($persistentBlockStates ? 0 : 1));
126 $stream->writeByteArray($words);
127 $palette = $blocks->getPalette();
128
129 if($bitsPerBlock !== 0){
130 VarInt::writeSignedInt($stream, count($palette)); //yes, this is intentionally zigzag
131 }
132 if($persistentBlockStates){
133 $nbtSerializer = new NetworkNbtSerializer();
134 foreach($palette as $p){
135 //TODO: introduce a binary cache for this
136 $state = $blockStateDictionary->generateDataFromStateId($blockTranslator->internalIdToNetworkId($p));
137 if($state === null){
138 $state = $blockTranslator->getFallbackStateData();
139 }
140
141 $stream->writeByteArray($nbtSerializer->write(new TreeRoot($state->toNbt())));
142 }
143 }else{
144 //we would use writeSignedIntArray() here, but the gains of writing in batch are negated by the cost of
145 //allocating a temporary array for the mapped palette IDs, especially for small palettes
146 foreach($palette as $p){
147 VarInt::writeSignedInt($stream, $blockTranslator->internalIdToNetworkId($p));
148 }
149 }
150 }
151 }
152
153 private static function serializeBiomePalette(PalettedBlockArray $biomePalette, LegacyBiomeIdToStringIdMap $biomeIdMap, ByteBufferWriter $stream) : void{
154 $biomePaletteBitsPerBlock = $biomePalette->getBitsPerBlock();
155 Byte::writeUnsigned($stream, ($biomePaletteBitsPerBlock << 1) | 1); //the last bit is non-persistence (like for blocks), though it has no effect on biomes since they always use integer IDs
156 $stream->writeByteArray($biomePalette->getWordArray());
157
158 $biomePaletteArray = $biomePalette->getPalette();
159 if($biomePaletteBitsPerBlock !== 0){
160 VarInt::writeSignedInt($stream, count($biomePaletteArray));
161 }
162
163 foreach($biomePaletteArray as $p){
164 //we would use writeSignedIntArray() here, but the gains of writing in batch are negated by the cost of
165 //allocating a temporary array for the mapped palette IDs, especially for small palettes
166 VarInt::writeSignedInt($stream, $biomeIdMap->legacyToString($p) !== null ? $p : BiomeIds::OCEAN);
167 }
168 }
169
170 public static function serializeTiles(Chunk $chunk) : string{
171 $stream = new ByteBufferWriter();
172 foreach($chunk->getTiles() as $tile){
173 if($tile instanceof Spawnable){
174 $stream->writeByteArray($tile->getSerializedSpawnCompound()->getEncodedNbt());
175 }
176 }
177
178 return $stream->getData();
179 }
180}
static serializeFullChunk(Chunk $chunk, int $dimensionId, BlockTranslator $blockTranslator, ?string $tiles=null)
static getSubChunkCount(Chunk $chunk, int $dimensionId)