PocketMine-MP 5.30.2 git-98f04176111e5ecab5e8814ffc69d992bfb64939
Loading...
Searching...
No Matches
LevelDB.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\leveldb;
25
53use pocketmine\world\format\PalettedBlockArray;
56use Symfony\Component\Filesystem\Path;
57use function array_map;
58use function array_values;
59use function chr;
60use function count;
61use function defined;
62use function extension_loaded;
63use function file_exists;
64use function implode;
65use function is_dir;
66use function mkdir;
67use function ord;
68use function str_repeat;
69use function strlen;
70use function substr;
71use function trim;
72use function unpack;
73use const LEVELDB_ZLIB_RAW_COMPRESSION;
74
76
77 protected const FINALISATION_NEEDS_INSTATICKING = 0;
78 protected const FINALISATION_NEEDS_POPULATION = 1;
79 protected const FINALISATION_DONE = 2;
80
81 protected const ENTRY_FLAT_WORLD_LAYERS = "game_flatworldlayers";
82
83 protected const CURRENT_LEVEL_CHUNK_VERSION = WorldDataVersions::CHUNK;
84 protected const CURRENT_LEVEL_SUBCHUNK_VERSION = WorldDataVersions::SUBCHUNK;
85
86 private const CAVES_CLIFFS_EXPERIMENTAL_SUBCHUNK_KEY_OFFSET = 4;
87
88 protected \LevelDB $db;
89
90 private static function checkForLevelDBExtension() : void{
91 if(!extension_loaded('leveldb')){
92 throw new UnsupportedWorldFormatException("The leveldb PHP extension is required to use this world format");
93 }
94
95 if(!defined('LEVELDB_ZLIB_RAW_COMPRESSION')){
96 throw new UnsupportedWorldFormatException("Given version of php-leveldb doesn't support zlib raw compression");
97 }
98 }
99
103 private static function createDB(string $path) : \LevelDB{
104 return new \LevelDB(Path::join($path, "db"), [
105 "compression" => LEVELDB_ZLIB_RAW_COMPRESSION,
106 "block_size" => 64 * 1024 //64KB, big enough for most chunks
107 ]);
108 }
109
110 public function __construct(string $path, \Logger $logger){
111 self::checkForLevelDBExtension();
112 parent::__construct($path, $logger);
113
114 try{
115 $this->db = self::createDB($path);
116 }catch(\LevelDBException $e){
117 //we can't tell the difference between errors caused by bad permissions and actual corruption :(
118 throw new CorruptedWorldException(trim($e->getMessage()), 0, $e);
119 }
120 }
121
122 protected function loadLevelData() : WorldData{
123 return new BedrockWorldData(Path::join($this->getPath(), "level.dat"));
124 }
125
126 public function getWorldMinY() : int{
127 return -64;
128 }
129
130 public function getWorldMaxY() : int{
131 return 320;
132 }
133
134 public static function isValid(string $path) : bool{
135 return file_exists(Path::join($path, "level.dat")) && is_dir(Path::join($path, "db"));
136 }
137
138 public static function generate(string $path, string $name, WorldCreationOptions $options) : void{
139 self::checkForLevelDBExtension();
140
141 $dbPath = Path::join($path, "db");
142 if(!file_exists($dbPath)){
143 mkdir($dbPath, 0777, true);
144 }
145
146 BedrockWorldData::generate($path, $name, $options);
147 }
148
152 protected function deserializeBlockPalette(BinaryStream $stream, \Logger $logger) : PalettedBlockArray{
153 $bitsPerBlock = $stream->getByte() >> 1;
154
155 try{
156 $words = $stream->get(PalettedBlockArray::getExpectedWordArraySize($bitsPerBlock));
157 }catch(\InvalidArgumentException $e){
158 throw new CorruptedChunkException("Failed to deserialize paletted storage: " . $e->getMessage(), 0, $e);
159 }
160 $nbt = new LittleEndianNbtSerializer();
161 $palette = [];
162
163 if($bitsPerBlock === 0){
164 $paletteSize = 1;
165 /*
166 * Due to code copy-paste in a public plugin, some PM4 worlds have 0 bpb palettes with a length prefix.
167 * This is invalid and does not happen in vanilla.
168 * These palettes were accepted by PM4 despite being invalid, but PM5 considered them corrupt, causing loss
169 * of data. Since many users were affected by this, a workaround is therefore necessary to allow PM5 to read
170 * these worlds without data loss.
171 *
172 * References:
173 * - https://github.com/Refaltor77/CustomItemAPI/issues/68
174 * - https://github.com/pmmp/PocketMine-MP/issues/5911
175 */
176 $offset = $stream->getOffset();
177 $byte1 = $stream->getByte();
178 $stream->setOffset($offset); //reset offset
179
180 if($byte1 !== NBT::TAG_Compound){ //normally the first byte would be the NBT of the blockstate
181 $susLength = $stream->getLInt();
182 if($susLength !== 1){ //make sure the data isn't complete garbage
183 throw new CorruptedChunkException("CustomItemAPI borked 0 bpb palette should always have a length of 1");
184 }
185 $logger->error("Unexpected palette size for 0 bpb palette");
186 }
187 }else{
188 $paletteSize = $stream->getLInt();
189 }
190
191 $blockDecodeErrors = [];
192
193 for($i = 0; $i < $paletteSize; ++$i){
194 try{
195 $offset = $stream->getOffset();
196 $blockStateNbt = $nbt->read($stream->getBuffer(), $offset)->mustGetCompoundTag();
197 $stream->setOffset($offset);
198 }catch(NbtDataException $e){
199 //NBT borked, unrecoverable
200 throw new CorruptedChunkException("Invalid blockstate NBT at offset $i in paletted storage: " . $e->getMessage(), 0, $e);
201 }
202
203 //TODO: remember data for unknown states so we can implement them later
204 try{
205 $blockStateData = $this->blockDataUpgrader->upgradeBlockStateNbt($blockStateNbt);
206 }catch(BlockStateDeserializeException $e){
207 //while not ideal, this is not a fatal error
208 $errorMessage = "Upgrade error: " . $e->getMessage() . ", NBT: " . $blockStateNbt->toString();
209 $blockDecodeErrors[$errorMessage][] = $i;
210 $palette[] = $this->blockStateDeserializer->deserialize(GlobalBlockStateHandlers::getUnknownBlockStateData());
211 continue;
212 }
213 try{
214 $palette[] = $this->blockStateDeserializer->deserialize($blockStateData);
215 }catch(UnsupportedBlockStateException $e){
216 $blockDecodeErrors[$e->getMessage()][] = $i;
217 $palette[] = $this->blockStateDeserializer->deserialize(GlobalBlockStateHandlers::getUnknownBlockStateData());
218 }catch(BlockStateDeserializeException $e){
219 $errorMessage = "Deserialize error: " . $e->getMessage() . ", NBT: " . $blockStateNbt->toString();
220 $blockDecodeErrors[$errorMessage][] = $i;
221 $palette[] = $this->blockStateDeserializer->deserialize(GlobalBlockStateHandlers::getUnknownBlockStateData());
222 }
223 }
224
225 if(count($blockDecodeErrors) > 0){
226 $finalErrors = [];
227 foreach(Utils::promoteKeys($blockDecodeErrors) as $errorMessage => $paletteOffsets){
228 $finalErrors[] = "$errorMessage (palette offsets: " . implode(", ", $paletteOffsets) . ")";
229 }
230 $logger->error("Errors decoding blocks:\n - " . implode("\n - ", $finalErrors));
231 }
232
233 //TODO: exceptions
234 return PalettedBlockArray::fromData($bitsPerBlock, $words, $palette);
235 }
236
237 private function serializeBlockPalette(BinaryStream $stream, PalettedBlockArray $blocks) : void{
238 $stream->putByte($blocks->getBitsPerBlock() << 1);
239 $stream->put($blocks->getWordArray());
240
241 $palette = $blocks->getPalette();
242 if($blocks->getBitsPerBlock() !== 0){
243 $stream->putLInt(count($palette));
244 }
245 $tags = [];
246 foreach($palette as $p){
247 $tags[] = new TreeRoot($this->blockStateSerializer->serialize($p)->toNbt());
248 }
249
250 $stream->put((new LittleEndianNbtSerializer())->writeMultiple($tags));
251 }
252
256 private static function getExpected3dBiomesCount(int $chunkVersion) : int{
257 return match(true){
258 $chunkVersion >= ChunkVersion::v1_18_30 => 24,
259 $chunkVersion >= ChunkVersion::v1_18_0_25_beta => 25,
260 $chunkVersion >= ChunkVersion::v1_18_0_24_beta => 32,
261 $chunkVersion >= ChunkVersion::v1_18_0_22_beta => 65,
262 $chunkVersion >= ChunkVersion::v1_17_40_20_beta_experimental_caves_cliffs => 32,
263 default => throw new CorruptedChunkException("Chunk version $chunkVersion should not have 3D biomes")
264 };
265 }
266
270 private static function deserializeBiomePalette(BinaryStream $stream, int $bitsPerBlock) : PalettedBlockArray{
271 try{
272 $words = $stream->get(PalettedBlockArray::getExpectedWordArraySize($bitsPerBlock));
273 }catch(\InvalidArgumentException $e){
274 throw new CorruptedChunkException("Failed to deserialize paletted biomes: " . $e->getMessage(), 0, $e);
275 }
276 $palette = [];
277 $paletteSize = $bitsPerBlock === 0 ? 1 : $stream->getLInt();
278
279 for($i = 0; $i < $paletteSize; ++$i){
280 $palette[] = $stream->getLInt();
281 }
282
283 //TODO: exceptions
284 return PalettedBlockArray::fromData($bitsPerBlock, $words, $palette);
285 }
286
287 private static function serializeBiomePalette(BinaryStream $stream, PalettedBlockArray $biomes) : void{
288 $stream->putByte($biomes->getBitsPerBlock() << 1);
289 $stream->put($biomes->getWordArray());
290
291 $palette = $biomes->getPalette();
292 if($biomes->getBitsPerBlock() !== 0){
293 $stream->putLInt(count($palette));
294 }
295 foreach($palette as $p){
296 $stream->putLInt($p);
297 }
298 }
299
305 private static function deserialize3dBiomes(BinaryStream $stream, int $chunkVersion, \Logger $logger) : array{
306 $previous = null;
307 $result = [];
308 $nextIndex = Chunk::MIN_SUBCHUNK_INDEX;
309
310 $expectedCount = self::getExpected3dBiomesCount($chunkVersion);
311 for($i = 0; $i < $expectedCount; ++$i){
312 try{
313 $bitsPerBlock = $stream->getByte() >> 1;
314 if($bitsPerBlock === 127){
315 if($previous === null){
316 throw new CorruptedChunkException("Serialized biome palette $i has no previous palette to copy from");
317 }
318 $decoded = clone $previous;
319 }else{
320 $decoded = self::deserializeBiomePalette($stream, $bitsPerBlock);
321 }
322 $previous = $decoded;
323 if($nextIndex <= Chunk::MAX_SUBCHUNK_INDEX){ //older versions wrote additional superfluous biome palettes
324 $result[$nextIndex++] = $decoded;
325 }elseif($stream->feof()){
326 //not enough padding biome arrays for the given version - this is non-critical since we discard the excess anyway, but this should be logged
327 $logger->error("Wrong number of 3D biome palettes for this chunk version: expected $expectedCount, but got " . ($i + 1) . " - this is not a problem, but may indicate a corrupted chunk");
328 break;
329 }
330 }catch(BinaryDataException $e){
331 throw new CorruptedChunkException("Failed to deserialize biome palette $i: " . $e->getMessage(), 0, $e);
332 }
333 }
334 if(!$stream->feof()){
335 //maybe bad output produced by a third-party conversion tool like Chunker
336 $logger->error("Unexpected trailing data after 3D biomes data");
337 }
338
339 return $result;
340 }
341
345 private static function serialize3dBiomes(BinaryStream $stream, array $subChunks) : void{
346 //TODO: the server-side min/max may not coincide with the world storage min/max - we may need additional logic to handle this
347 for($y = Chunk::MIN_SUBCHUNK_INDEX; $y <= Chunk::MAX_SUBCHUNK_INDEX; $y++){
348 //TODO: is it worth trying to use the previous palette if it's the same as the current one? vanilla supports
349 //this, but it's not clear if it's worth the effort to implement.
350 self::serializeBiomePalette($stream, $subChunks[$y]->getBiomeArray());
351 }
352 }
353
359 protected static function deserializeExtraDataKey(int $chunkVersion, int $key, ?int &$x, ?int &$y, ?int &$z) : void{
360 if($chunkVersion >= ChunkVersion::v1_0_0){
361 $x = ($key >> 12) & 0xf;
362 $z = ($key >> 8) & 0xf;
363 $y = $key & 0xff;
364 }else{ //pre-1.0, 7 bits were used because the build height limit was lower
365 $x = ($key >> 11) & 0xf;
366 $z = ($key >> 7) & 0xf;
367 $y = $key & 0x7f;
368 }
369 }
370
374 protected function deserializeLegacyExtraData(string $index, int $chunkVersion, \Logger $logger) : array{
375 if(($extraRawData = $this->db->get($index . ChunkDataKey::LEGACY_BLOCK_EXTRA_DATA)) === false || $extraRawData === ""){
376 return [];
377 }
378
380 $extraDataLayers = [];
381 $binaryStream = new BinaryStream($extraRawData);
382 $count = $binaryStream->getLInt();
383
384 for($i = 0; $i < $count; ++$i){
385 $key = $binaryStream->getLInt();
386 $value = $binaryStream->getLShort();
387
388 self::deserializeExtraDataKey($chunkVersion, $key, $x, $fullY, $z);
389
390 $ySub = ($fullY >> SubChunk::COORD_BIT_SIZE);
391 $y = $key & SubChunk::COORD_MASK;
392
393 $blockId = $value & 0xff;
394 $blockData = ($value >> 8) & 0xf;
395 try{
396 $blockStateData = $this->blockDataUpgrader->upgradeIntIdMeta($blockId, $blockData);
398 //TODO: we could preserve this in case it's supported in the future, but this was historically only
399 //used for grass anyway, so we probably don't need to care
400 $logger->error("Failed to upgrade legacy extra block: " . $e->getMessage() . " ($blockId:$blockData)");
401 continue;
402 }
403 //assume this won't throw
404 $blockStateId = $this->blockStateDeserializer->deserialize($blockStateData);
405
406 if(!isset($extraDataLayers[$ySub])){
407 $extraDataLayers[$ySub] = new PalettedBlockArray(Block::EMPTY_STATE_ID);
408 }
409 $extraDataLayers[$ySub]->set($x, $y, $z, $blockStateId);
410 }
411
412 return $extraDataLayers;
413 }
414
415 private function readVersion(int $chunkX, int $chunkZ) : ?int{
416 $index = self::chunkIndex($chunkX, $chunkZ);
417 $chunkVersionRaw = $this->db->get($index . ChunkDataKey::NEW_VERSION);
418 if($chunkVersionRaw === false){
419 $chunkVersionRaw = $this->db->get($index . ChunkDataKey::OLD_VERSION);
420 if($chunkVersionRaw === false){
421 return null;
422 }
423 }
424
425 return ord($chunkVersionRaw);
426 }
427
435 private function deserializeLegacyTerrainData(string $index, int $chunkVersion, \Logger $logger) : array{
436 $convertedLegacyExtraData = $this->deserializeLegacyExtraData($index, $chunkVersion, $logger);
437
438 $legacyTerrain = $this->db->get($index . ChunkDataKey::LEGACY_TERRAIN);
439 if($legacyTerrain === false){
440 throw new CorruptedChunkException("Missing expected LEGACY_TERRAIN tag for format version $chunkVersion");
441 }
442 $binaryStream = new BinaryStream($legacyTerrain);
443 try{
444 $fullIds = $binaryStream->get(32768);
445 $fullData = $binaryStream->get(16384);
446 $binaryStream->get(32768); //legacy light info, discard it
447 }catch(BinaryDataException $e){
448 throw new CorruptedChunkException($e->getMessage(), 0, $e);
449 }
450
451 try{
452 $binaryStream->get(256); //heightmap, discard it
454 $unpackedBiomeArray = unpack("N*", $binaryStream->get(1024)); //unpack() will never fail here
455 $biomes3d = ChunkUtils::extrapolate3DBiomes(ChunkUtils::convertBiomeColors(array_values($unpackedBiomeArray))); //never throws
456 }catch(BinaryDataException $e){
457 throw new CorruptedChunkException($e->getMessage(), 0, $e);
458 }
459 if(!$binaryStream->feof()){
460 $logger->error("Unexpected trailing data in legacy terrain data");
461 }
462
463 $subChunks = [];
464 for($yy = 0; $yy < 8; ++$yy){
465 $storages = [$this->palettizeLegacySubChunkFromColumn($fullIds, $fullData, $yy, new \PrefixedLogger($logger, "Subchunk y=$yy"))];
466 if(isset($convertedLegacyExtraData[$yy])){
467 $storages[] = $convertedLegacyExtraData[$yy];
468 }
469 $subChunks[$yy] = new SubChunk(Block::EMPTY_STATE_ID, $storages, clone $biomes3d);
470 }
471
472 //make sure extrapolated biomes get filled in correctly
473 for($yy = Chunk::MIN_SUBCHUNK_INDEX; $yy <= Chunk::MAX_SUBCHUNK_INDEX; ++$yy){
474 if(!isset($subChunks[$yy])){
475 $subChunks[$yy] = new SubChunk(Block::EMPTY_STATE_ID, [], clone $biomes3d);
476 }
477 }
478
479 return $subChunks;
480 }
481
485 private function deserializeNonPalettedSubChunkData(BinaryStream $binaryStream, int $chunkVersion, ?PalettedBlockArray $convertedLegacyExtraData, PalettedBlockArray $biomePalette, \Logger $logger) : SubChunk{
486 try{
487 $blocks = $binaryStream->get(4096);
488 $blockData = $binaryStream->get(2048);
489 }catch(BinaryDataException $e){
490 throw new CorruptedChunkException($e->getMessage(), 0, $e);
491 }
492
493 if($chunkVersion < ChunkVersion::v1_1_0){
494 try{
495 $binaryStream->get(4096); //legacy light info, discard it
496 if(!$binaryStream->feof()){
497 $logger->error("Unexpected trailing data in legacy subchunk data");
498 }
499 }catch(BinaryDataException $e){
500 $logger->error("Failed to read legacy subchunk light info: " . $e->getMessage());
501 }
502 }
503
504 $storages = [$this->palettizeLegacySubChunkXZY($blocks, $blockData, $logger)];
505 if($convertedLegacyExtraData !== null){
506 $storages[] = $convertedLegacyExtraData;
507 }
508
509 return new SubChunk(Block::EMPTY_STATE_ID, $storages, $biomePalette);
510 }
511
518 private function deserializeSubChunkData(BinaryStream $binaryStream, int $chunkVersion, int $subChunkVersion, ?PalettedBlockArray $convertedLegacyExtraData, PalettedBlockArray $biomePalette, \Logger $logger) : SubChunk{
519 switch($subChunkVersion){
520 case SubChunkVersion::CLASSIC:
521 case SubChunkVersion::CLASSIC_BUG_2: //these are all identical to version 0, but vanilla respects these so we should also
522 case SubChunkVersion::CLASSIC_BUG_3:
523 case SubChunkVersion::CLASSIC_BUG_4:
524 case SubChunkVersion::CLASSIC_BUG_5:
525 case SubChunkVersion::CLASSIC_BUG_6:
526 case SubChunkVersion::CLASSIC_BUG_7:
527 return $this->deserializeNonPalettedSubChunkData($binaryStream, $chunkVersion, $convertedLegacyExtraData, $biomePalette, $logger);
528 case SubChunkVersion::PALETTED_SINGLE:
529 $storages = [$this->deserializeBlockPalette($binaryStream, $logger)];
530 if($convertedLegacyExtraData !== null){
531 $storages[] = $convertedLegacyExtraData;
532 }
533 return new SubChunk(Block::EMPTY_STATE_ID, $storages, $biomePalette);
534 case SubChunkVersion::PALETTED_MULTI:
535 case SubChunkVersion::PALETTED_MULTI_WITH_OFFSET:
536 //legacy extradata layers intentionally ignored because they aren't supposed to exist in v8
537
538 $storageCount = $binaryStream->getByte();
539 if($subChunkVersion >= SubChunkVersion::PALETTED_MULTI_WITH_OFFSET){
540 //height ignored; this seems pointless since this is already in the key anyway
541 $binaryStream->getByte();
542 }
543
544 $storages = [];
545 for($k = 0; $k < $storageCount; ++$k){
546 $storages[] = $this->deserializeBlockPalette($binaryStream, $logger);
547 }
548 return new SubChunk(Block::EMPTY_STATE_ID, $storages, $biomePalette);
549 default:
550 //this should never happen - an unsupported chunk appearing in a supported world is a sign of corruption
551 throw new CorruptedChunkException("don't know how to decode LevelDB subchunk format version $subChunkVersion");
552 }
553 }
554
555 private static function hasOffsetCavesAndCliffsSubChunks(int $chunkVersion) : bool{
556 return $chunkVersion >= ChunkVersion::v1_16_220_50_unused && $chunkVersion <= ChunkVersion::v1_16_230_50_unused;
557 }
558
572 private function deserializeAllSubChunkData(string $index, int $chunkVersion, bool &$hasBeenUpgraded, array $convertedLegacyExtraData, array $biomeArrays, \Logger $logger) : array{
573 $subChunks = [];
574
575 $subChunkKeyOffset = self::hasOffsetCavesAndCliffsSubChunks($chunkVersion) ? self::CAVES_CLIFFS_EXPERIMENTAL_SUBCHUNK_KEY_OFFSET : 0;
576 for($y = Chunk::MIN_SUBCHUNK_INDEX; $y <= Chunk::MAX_SUBCHUNK_INDEX; ++$y){
577 if(($data = $this->db->get($index . ChunkDataKey::SUBCHUNK . chr($y + $subChunkKeyOffset))) === false){
578 $subChunks[$y] = new SubChunk(Block::EMPTY_STATE_ID, [], $biomeArrays[$y]);
579 continue;
580 }
581
582 $binaryStream = new BinaryStream($data);
583 if($binaryStream->feof()){
584 throw new CorruptedChunkException("Unexpected empty data for subchunk $y");
585 }
586 $subChunkVersion = $binaryStream->getByte();
587 if($subChunkVersion < self::CURRENT_LEVEL_SUBCHUNK_VERSION){
588 $hasBeenUpgraded = true;
589 }
590
591 $subChunks[$y] = $this->deserializeSubChunkData(
592 $binaryStream,
593 $chunkVersion,
594 $subChunkVersion,
595 $convertedLegacyExtraData[$y] ?? null,
596 $biomeArrays[$y],
597 new \PrefixedLogger($logger, "Subchunk y=$y v$subChunkVersion")
598 );
599 }
600
601 return $subChunks;
602 }
603
610 private function deserializeBiomeData(string $index, int $chunkVersion, \Logger $logger) : array{
611 $biomeArrays = [];
612 if(($maps2d = $this->db->get($index . ChunkDataKey::HEIGHTMAP_AND_2D_BIOMES)) !== false){
613 $binaryStream = new BinaryStream($maps2d);
614
615 try{
616 $binaryStream->get(512); //heightmap, discard it
617 $biomes3d = ChunkUtils::extrapolate3DBiomes($binaryStream->get(256)); //never throws
618 if(!$binaryStream->feof()){
619 $logger->error("Unexpected trailing data after 2D biome data");
620 }
621 }catch(BinaryDataException $e){
622 throw new CorruptedChunkException($e->getMessage(), 0, $e);
623 }
624 for($i = Chunk::MIN_SUBCHUNK_INDEX; $i <= Chunk::MAX_SUBCHUNK_INDEX; ++$i){
625 $biomeArrays[$i] = clone $biomes3d;
626 }
627 }elseif(($maps3d = $this->db->get($index . ChunkDataKey::HEIGHTMAP_AND_3D_BIOMES)) !== false){
628 $binaryStream = new BinaryStream($maps3d);
629
630 try{
631 $binaryStream->get(512);
632 $biomeArrays = self::deserialize3dBiomes($binaryStream, $chunkVersion, $logger);
633 }catch(BinaryDataException $e){
634 throw new CorruptedChunkException($e->getMessage(), 0, $e);
635 }
636 }else{
637 $logger->error("Missing biome data, using default ocean biome");
638 for($i = Chunk::MIN_SUBCHUNK_INDEX; $i <= Chunk::MAX_SUBCHUNK_INDEX; ++$i){
639 $biomeArrays[$i] = new PalettedBlockArray(BiomeIds::OCEAN); //polyfill
640 }
641 }
642
643 return $biomeArrays;
644 }
645
649 public function loadChunk(int $chunkX, int $chunkZ) : ?LoadedChunkData{
650 $index = LevelDB::chunkIndex($chunkX, $chunkZ);
651
652 $chunkVersion = $this->readVersion($chunkX, $chunkZ);
653 if($chunkVersion === null){
654 //TODO: this might be a slightly-corrupted chunk with a missing version field
655 return null;
656 }
657
658 //TODO: read PM_DATA_VERSION - we'll need it to fix up old chunks
659
660 $logger = new \PrefixedLogger($this->logger, "Loading chunk x=$chunkX z=$chunkZ v$chunkVersion");
661
662 $hasBeenUpgraded = $chunkVersion < self::CURRENT_LEVEL_CHUNK_VERSION;
663
664 switch($chunkVersion){
665 case ChunkVersion::v1_21_40:
666 //TODO: BiomeStates became shorts instead of bytes
667 case ChunkVersion::v1_18_30:
668 case ChunkVersion::v1_18_0_25_beta:
669 case ChunkVersion::v1_18_0_24_unused:
670 case ChunkVersion::v1_18_0_24_beta:
671 case ChunkVersion::v1_18_0_22_unused:
672 case ChunkVersion::v1_18_0_22_beta:
673 case ChunkVersion::v1_18_0_20_unused:
674 case ChunkVersion::v1_18_0_20_beta:
675 case ChunkVersion::v1_17_40_unused:
676 case ChunkVersion::v1_17_40_20_beta_experimental_caves_cliffs:
677 case ChunkVersion::v1_17_30_25_unused:
678 case ChunkVersion::v1_17_30_25_beta_experimental_caves_cliffs:
679 case ChunkVersion::v1_17_30_23_unused:
680 case ChunkVersion::v1_17_30_23_beta_experimental_caves_cliffs:
681 case ChunkVersion::v1_16_230_50_unused:
682 case ChunkVersion::v1_16_230_50_beta_experimental_caves_cliffs:
683 case ChunkVersion::v1_16_220_50_unused:
684 case ChunkVersion::v1_16_220_50_beta_experimental_caves_cliffs:
685 case ChunkVersion::v1_16_210:
686 case ChunkVersion::v1_16_100_57_beta:
687 case ChunkVersion::v1_16_100_52_beta:
688 case ChunkVersion::v1_16_0:
689 case ChunkVersion::v1_16_0_51_beta:
690 //TODO: check walls
691 case ChunkVersion::v1_12_0_unused2:
692 case ChunkVersion::v1_12_0_unused1:
693 case ChunkVersion::v1_12_0_4_beta:
694 case ChunkVersion::v1_11_1:
695 case ChunkVersion::v1_11_0_4_beta:
696 case ChunkVersion::v1_11_0_3_beta:
697 case ChunkVersion::v1_11_0_1_beta:
698 case ChunkVersion::v1_9_0:
699 case ChunkVersion::v1_8_0:
700 case ChunkVersion::v1_2_13:
701 case ChunkVersion::v1_2_0:
702 case ChunkVersion::v1_2_0_2_beta:
703 case ChunkVersion::v1_1_0_converted_from_console:
704 case ChunkVersion::v1_1_0:
705 //TODO: check beds
706 case ChunkVersion::v1_0_0:
707 $convertedLegacyExtraData = $this->deserializeLegacyExtraData($index, $chunkVersion, $logger);
708 $biomeArrays = $this->deserializeBiomeData($index, $chunkVersion, $logger);
709 $subChunks = $this->deserializeAllSubChunkData($index, $chunkVersion, $hasBeenUpgraded, $convertedLegacyExtraData, $biomeArrays, $logger);
710 break;
711 case ChunkVersion::v0_9_5:
712 case ChunkVersion::v0_9_2:
713 case ChunkVersion::v0_9_0:
714 $subChunks = $this->deserializeLegacyTerrainData($index, $chunkVersion, $logger);
715 break;
716 default:
717 throw new CorruptedChunkException("don't know how to decode chunk format version $chunkVersion");
718 }
719
720 $nbt = new LittleEndianNbtSerializer();
721
722 $entities = [];
723 if(($entityData = $this->db->get($index . ChunkDataKey::ENTITIES)) !== false && $entityData !== ""){
724 try{
725 $entities = array_map(fn(TreeRoot $root) => $root->mustGetCompoundTag(), $nbt->readMultiple($entityData));
726 }catch(NbtDataException $e){
727 throw new CorruptedChunkException($e->getMessage(), 0, $e);
728 }
729 }
730
731 $tiles = [];
732 if(($tileData = $this->db->get($index . ChunkDataKey::BLOCK_ENTITIES)) !== false && $tileData !== ""){
733 try{
734 $tiles = array_map(fn(TreeRoot $root) => $root->mustGetCompoundTag(), $nbt->readMultiple($tileData));
735 }catch(NbtDataException $e){
736 throw new CorruptedChunkException($e->getMessage(), 0, $e);
737 }
738 }
739
740 $finalisationChr = $this->db->get($index . ChunkDataKey::FINALIZATION);
741 if($finalisationChr !== false){
742 $finalisation = ord($finalisationChr);
743 $terrainPopulated = $finalisation === self::FINALISATION_DONE;
744 }else{ //older versions didn't have this tag
745 $terrainPopulated = true;
746 }
747
748 //TODO: tile ticks, biome states (?)
749
750 return new LoadedChunkData(
751 data: new ChunkData($subChunks, $terrainPopulated, $entities, $tiles),
752 upgraded: $hasBeenUpgraded,
753 fixerFlags: LoadedChunkData::FIXER_FLAG_ALL //TODO: fill this by version rather than just setting all flags
754 );
755 }
756
757 public function saveChunk(int $chunkX, int $chunkZ, ChunkData $chunkData, int $dirtyFlags) : void{
758 $index = LevelDB::chunkIndex($chunkX, $chunkZ);
759
760 $write = new \LevelDBWriteBatch();
761
762 $write->put($index . ChunkDataKey::NEW_VERSION, chr(self::CURRENT_LEVEL_CHUNK_VERSION));
763 $write->put($index . ChunkDataKey::PM_DATA_VERSION, Binary::writeLLong(VersionInfo::WORLD_DATA_VERSION));
764
765 $subChunks = $chunkData->getSubChunks();
766
767 if(($dirtyFlags & Chunk::DIRTY_FLAG_BLOCKS) !== 0){
768
769 foreach($subChunks as $y => $subChunk){
770 $key = $index . ChunkDataKey::SUBCHUNK . chr($y);
771 if($subChunk->isEmptyAuthoritative()){
772 $write->delete($key);
773 }else{
774 $subStream = new BinaryStream();
775 $subStream->putByte(self::CURRENT_LEVEL_SUBCHUNK_VERSION);
776
777 $layers = $subChunk->getBlockLayers();
778 $subStream->putByte(count($layers));
779 foreach($layers as $blocks){
780 $this->serializeBlockPalette($subStream, $blocks);
781 }
782
783 $write->put($key, $subStream->getBuffer());
784 }
785 }
786 }
787
788 if(($dirtyFlags & Chunk::DIRTY_FLAG_BIOMES) !== 0){
789 $write->delete($index . ChunkDataKey::HEIGHTMAP_AND_2D_BIOMES);
790 $stream = new BinaryStream();
791 $stream->put(str_repeat("\x00", 512)); //fake heightmap
792 self::serialize3dBiomes($stream, $subChunks);
793 $write->put($index . ChunkDataKey::HEIGHTMAP_AND_3D_BIOMES, $stream->getBuffer());
794 }
795
796 //TODO: use this properly
797 $write->put($index . ChunkDataKey::FINALIZATION, chr($chunkData->isPopulated() ? self::FINALISATION_DONE : self::FINALISATION_NEEDS_POPULATION));
798
799 $this->writeTags($chunkData->getTileNBT(), $index . ChunkDataKey::BLOCK_ENTITIES, $write);
800 $this->writeTags($chunkData->getEntityNBT(), $index . ChunkDataKey::ENTITIES, $write);
801
802 $write->delete($index . ChunkDataKey::HEIGHTMAP_AND_2D_BIOME_COLORS);
803 $write->delete($index . ChunkDataKey::LEGACY_TERRAIN);
804
805 $this->db->write($write);
806 }
807
811 private function writeTags(array $targets, string $index, \LevelDBWriteBatch $write) : void{
812 if(count($targets) > 0){
813 $nbt = new LittleEndianNbtSerializer();
814 $write->put($index, $nbt->writeMultiple(array_map(fn(CompoundTag $tag) => new TreeRoot($tag), $targets)));
815 }else{
816 $write->delete($index);
817 }
818 }
819
820 public function getDatabase() : \LevelDB{
821 return $this->db;
822 }
823
824 public static function chunkIndex(int $chunkX, int $chunkZ) : string{
825 return Binary::writeLInt($chunkX) . Binary::writeLInt($chunkZ);
826 }
827
828 public function doGarbageCollection() : void{
829
830 }
831
832 public function close() : void{
833 unset($this->db);
834 }
835
836 public function getAllChunks(bool $skipCorrupted = false, ?\Logger $logger = null) : \Generator{
837 foreach($this->db->getIterator() as $key => $_){
838 if(strlen($key) === 9 && ($key[8] === ChunkDataKey::NEW_VERSION || $key[8] === ChunkDataKey::OLD_VERSION)){
839 $chunkX = Binary::readLInt(substr($key, 0, 4));
840 $chunkZ = Binary::readLInt(substr($key, 4, 4));
841 try{
842 if(($chunk = $this->loadChunk($chunkX, $chunkZ)) !== null){
843 yield [$chunkX, $chunkZ] => $chunk;
844 }
845 }catch(CorruptedChunkException $e){
846 if(!$skipCorrupted){
847 throw $e;
848 }
849 if($logger !== null){
850 $logger->error("Skipped corrupted chunk $chunkX $chunkZ (" . $e->getMessage() . ")");
851 }
852 }
853 }
854 }
855 }
856
857 public function calculateChunkCount() : int{
858 $count = 0;
859 foreach($this->db->getIterator() as $key => $_){
860 if(strlen($key) === 9 && ($key[8] === ChunkDataKey::NEW_VERSION || $key[8] === ChunkDataKey::OLD_VERSION)){
861 $count++;
862 }
863 }
864 return $count;
865 }
866}
getAllChunks(bool $skipCorrupted=false, ?\Logger $logger=null)
Definition LevelDB.php:836
loadChunk(int $chunkX, int $chunkZ)
Definition LevelDB.php:649
deserializeBlockPalette(BinaryStream $stream, \Logger $logger)
Definition LevelDB.php:152
static deserializeExtraDataKey(int $chunkVersion, int $key, ?int &$x, ?int &$y, ?int &$z)
Definition LevelDB.php:359
deserializeLegacyExtraData(string $index, int $chunkVersion, \Logger $logger)
Definition LevelDB.php:374
saveChunk(int $chunkX, int $chunkZ, ChunkData $chunkData, int $dirtyFlags)
Definition LevelDB.php:757
error($message)