PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
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
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 = PacketSerializer::encoder();
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 $stream->putByte(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->put($tiles);
108 }else{
109 $stream->put(self::serializeTiles($chunk));
110 }
111 return $stream->getBuffer();
112 }
113
114 public static function serializeSubChunk(SubChunk $subChunk, BlockTranslator $blockTranslator, PacketSerializer $stream, bool $persistentBlockStates) : void{
115 $layers = $subChunk->getBlockLayers();
116 $stream->putByte(8); //version
117
118 $stream->putByte(count($layers));
119
120 $blockStateDictionary = $blockTranslator->getBlockStateDictionary();
121
122 foreach($layers as $blocks){
123 $bitsPerBlock = $blocks->getBitsPerBlock();
124 $words = $blocks->getWordArray();
125 $stream->putByte(($bitsPerBlock << 1) | ($persistentBlockStates ? 0 : 1));
126 $stream->put($words);
127 $palette = $blocks->getPalette();
128
129 if($bitsPerBlock !== 0){
130 //these LSHIFT by 1 uvarints are optimizations: the client expects zigzag varints here
131 //but since we know they are always unsigned, we can avoid the extra fcall overhead of
132 //zigzag and just shift directly.
133 $stream->putUnsignedVarInt(count($palette) << 1); //yes, this is intentionally zigzag
134 }
135 if($persistentBlockStates){
136 $nbtSerializer = new NetworkNbtSerializer();
137 foreach($palette as $p){
138 //TODO: introduce a binary cache for this
139 $state = $blockStateDictionary->generateDataFromStateId($blockTranslator->internalIdToNetworkId($p));
140 if($state === null){
141 $state = $blockTranslator->getFallbackStateData();
142 }
143
144 $stream->put($nbtSerializer->write(new TreeRoot($state->toNbt())));
145 }
146 }else{
147 foreach($palette as $p){
148 $stream->put(Binary::writeUnsignedVarInt($blockTranslator->internalIdToNetworkId($p) << 1));
149 }
150 }
151 }
152 }
153
154 private static function serializeBiomePalette(PalettedBlockArray $biomePalette, LegacyBiomeIdToStringIdMap $biomeIdMap, PacketSerializer $stream) : void{
155 $biomePaletteBitsPerBlock = $biomePalette->getBitsPerBlock();
156 $stream->putByte(($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
157 $stream->put($biomePalette->getWordArray());
158
159 //these LSHIFT by 1 uvarints are optimizations: the client expects zigzag varints here
160 //but since we know they are always unsigned, we can avoid the extra fcall overhead of
161 //zigzag and just shift directly.
162 $biomePaletteArray = $biomePalette->getPalette();
163 if($biomePaletteBitsPerBlock !== 0){
164 $stream->putUnsignedVarInt(count($biomePaletteArray) << 1);
165 }
166
167 foreach($biomePaletteArray as $p){
168 if($biomeIdMap->legacyToString($p) === null){
169 //make sure we aren't sending bogus biomes - the 1.18.0 client crashes if we do this
170 $p = BiomeIds::OCEAN;
171 }
172 $stream->put(Binary::writeUnsignedVarInt($p << 1));
173 }
174 }
175
176 public static function serializeTiles(Chunk $chunk) : string{
177 $stream = new BinaryStream();
178 foreach($chunk->getTiles() as $tile){
179 if($tile instanceof Spawnable){
180 $stream->put($tile->getSerializedSpawnCompound()->getEncodedNbt());
181 }
182 }
183
184 return $stream->getBuffer();
185 }
186}
static serializeFullChunk(Chunk $chunk, int $dimensionId, BlockTranslator $blockTranslator, ?string $tiles=null)
static getSubChunkCount(Chunk $chunk, int $dimensionId)