PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
FastChunkSerializer.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\world\format\io;
25
26use pmmp\encoding\BE;
27use pmmp\encoding\Byte;
28use pmmp\encoding\ByteBufferReader;
29use pmmp\encoding\ByteBufferWriter;
31use pocketmine\world\format\PalettedBlockArray;
33use function array_values;
34use function count;
35use function pack;
36use function strlen;
37use function unpack;
38
44 private const FLAG_POPULATED = 1 << 1;
45
46 private function __construct(){
47 //NOOP
48 }
49
50 private static function serializePalettedArray(ByteBufferWriter $stream, PalettedBlockArray $array) : void{
51 $wordArray = $array->getWordArray();
52 $palette = $array->getPalette();
53
54 Byte::writeUnsigned($stream, $array->getBitsPerBlock());
55 $stream->writeByteArray($wordArray);
56 $serialPalette = pack("L*", ...$palette);
57 BE::writeUnsignedInt($stream, strlen($serialPalette));
58 $stream->writeByteArray($serialPalette);
59 }
60
65 public static function serializeTerrain(Chunk $chunk) : string{
66 $stream = new ByteBufferWriter();
67 Byte::writeUnsigned($stream, ($chunk->isPopulated() ? self::FLAG_POPULATED : 0));
68
69 //subchunks
70 $subChunks = $chunk->getSubChunks();
71 $count = count($subChunks);
72 Byte::writeUnsigned($stream, $count);
73
74 foreach($subChunks as $y => $subChunk){
75 Byte::writeSigned($stream, $y);
76 BE::writeUnsignedInt($stream, $subChunk->getEmptyBlockId());
77
78 $layers = $subChunk->getBlockLayers();
79 Byte::writeUnsigned($stream, count($layers));
80 foreach($layers as $blocks){
81 self::serializePalettedArray($stream, $blocks);
82 }
83 self::serializePalettedArray($stream, $subChunk->getBiomeArray());
84 }
85
86 return $stream->getData();
87 }
88
89 private static function deserializePalettedArray(ByteBufferReader $stream) : PalettedBlockArray{
90 $bitsPerBlock = Byte::readUnsigned($stream);
91 $words = $stream->readByteArray(PalettedBlockArray::getExpectedWordArraySize($bitsPerBlock));
92 $paletteSize = BE::readUnsignedInt($stream);
94 $unpackedPalette = unpack("L*", $stream->readByteArray($paletteSize)); //unpack() will never fail here
95 $palette = array_values($unpackedPalette);
96
97 return PalettedBlockArray::fromData($bitsPerBlock, $words, $palette);
98 }
99
103 public static function deserializeTerrain(string $data) : Chunk{
104 $stream = new ByteBufferReader($data);
105
106 $flags = Byte::readUnsigned($stream);
107 $terrainPopulated = (bool) ($flags & self::FLAG_POPULATED);
108
109 $subChunks = [];
110
111 $count = Byte::readUnsigned($stream);
112 for($subCount = 0; $subCount < $count; ++$subCount){
113 $y = Byte::readSigned($stream);
114 //TODO: why the heck are we using big-endian here?
115 $airBlockId = BE::readUnsignedInt($stream);
116
117 $layerCount = Byte::readUnsigned($stream);
118 if($layerCount > 2){
119 throw new \UnexpectedValueException("Expected at most 2 layers, but got $layerCount");
120 }
121 $layer0 = $layerCount >= 1 ? self::deserializePalettedArray($stream) : null;
122 $layer1 = $layerCount === 2 ? self::deserializePalettedArray($stream) : null;
123
124 $biomeArray = self::deserializePalettedArray($stream);
125 $subChunks[$y] = new SubChunk($airBlockId, $layer0, $layer1, $biomeArray);
126 }
127
128 return new Chunk($subChunks, $terrainPopulated);
129 }
130}