PocketMine-MP 5.35.1 git-d241807752ab518f2ffc7e3b71f6a72f9cdf0c30
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
87 return $stream->getData();
88 }
89
90 private static function deserializePalettedArray(ByteBufferReader $stream) : PalettedBlockArray{
91 $bitsPerBlock = Byte::readUnsigned($stream);
92 $words = $stream->readByteArray(PalettedBlockArray::getExpectedWordArraySize($bitsPerBlock));
93 $paletteSize = BE::readUnsignedInt($stream);
95 $unpackedPalette = unpack("L*", $stream->readByteArray($paletteSize)); //unpack() will never fail here
96 $palette = array_values($unpackedPalette);
97
98 return PalettedBlockArray::fromData($bitsPerBlock, $words, $palette);
99 }
100
104 public static function deserializeTerrain(string $data) : Chunk{
105 $stream = new ByteBufferReader($data);
106
107 $flags = Byte::readUnsigned($stream);
108 $terrainPopulated = (bool) ($flags & self::FLAG_POPULATED);
109
110 $subChunks = [];
111
112 $count = Byte::readUnsigned($stream);
113 for($subCount = 0; $subCount < $count; ++$subCount){
114 $y = Byte::readSigned($stream);
115 //TODO: why the heck are we using big-endian here?
116 $airBlockId = BE::readUnsignedInt($stream);
117
118 $layers = [];
119 for($i = 0, $layerCount = Byte::readUnsigned($stream); $i < $layerCount; ++$i){
120 $layers[] = self::deserializePalettedArray($stream);
121 }
122 $biomeArray = self::deserializePalettedArray($stream);
123 $subChunks[$y] = new SubChunk($airBlockId, $layers, $biomeArray);
124 }
125
126 return new Chunk($subChunks, $terrainPopulated);
127 }
128}