PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
BaseWorldProvider.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
32use pocketmine\world\format\PalettedBlockArray;
34use function count;
35use function file_exists;
36use function implode;
37
38abstract class BaseWorldProvider implements WorldProvider{
39 protected WorldData $worldData;
40
41 protected BlockStateDeserializer $blockStateDeserializer;
42 protected BlockDataUpgrader $blockDataUpgrader;
43 protected BlockStateSerializer $blockStateSerializer;
44
45 public function __construct(
46 protected string $path,
47 protected \Logger $logger
48 ){
49 if(!file_exists($path)){
50 throw new WorldException("World does not exist");
51 }
52
53 //TODO: this should not rely on singletons
54 $this->blockStateDeserializer = GlobalBlockStateHandlers::getDeserializer();
55 $this->blockDataUpgrader = GlobalBlockStateHandlers::getUpgrader();
56 $this->blockStateSerializer = GlobalBlockStateHandlers::getSerializer();
57
58 $this->worldData = $this->loadLevelData();
59 }
60
65 abstract protected function loadLevelData() : WorldData;
66
67 private function translatePalette(PalettedBlockArray $blockArray, \Logger $logger) : PalettedBlockArray{
68 $palette = $blockArray->getPalette();
69
70 $newPalette = [];
71 $blockDecodeErrors = [];
72 foreach($palette as $k => $legacyIdMeta){
73 //TODO: remember data for unknown states so we can implement them later
74 $id = $legacyIdMeta >> 4;
75 $meta = $legacyIdMeta & 0xf;
76 try{
77 $newStateData = $this->blockDataUpgrader->upgradeIntIdMeta($id, $meta);
79 $blockDecodeErrors[] = "Palette offset $k / Failed to upgrade legacy ID/meta $id:$meta: " . $e->getMessage();
80 $newStateData = GlobalBlockStateHandlers::getUnknownBlockStateData();
81 }
82
83 try{
84 $newPalette[$k] = $this->blockStateDeserializer->deserialize($newStateData);
86 //this should never happen anyway - if the upgrader returned an invalid state, we have bigger problems
87 $blockDecodeErrors[] = "Palette offset $k / Failed to deserialize upgraded state $id:$meta: " . $e->getMessage();
88 $newPalette[$k] = $this->blockStateDeserializer->deserialize(GlobalBlockStateHandlers::getUnknownBlockStateData());
89 }
90 }
91
92 if(count($blockDecodeErrors) > 0){
93 $logger->error("Errors decoding/upgrading blocks:\n - " . implode("\n - ", $blockDecodeErrors));
94 }
95
96 //TODO: this is sub-optimal since it reallocates the offset table multiple times
97 return PalettedBlockArray::fromData(
98 $blockArray->getBitsPerBlock(),
99 $blockArray->getWordArray(),
100 $newPalette
101 );
102 }
103
104 protected function palettizeLegacySubChunkXZY(string $idArray, string $metaArray, \Logger $logger) : PalettedBlockArray{
105 return $this->translatePalette(SubChunkConverter::convertSubChunkXZY($idArray, $metaArray), $logger);
106 }
107
108 protected function palettizeLegacySubChunkYZX(string $idArray, string $metaArray, \Logger $logger) : PalettedBlockArray{
109 return $this->translatePalette(SubChunkConverter::convertSubChunkYZX($idArray, $metaArray), $logger);
110 }
111
112 protected function palettizeLegacySubChunkFromColumn(string $idArray, string $metaArray, int $yOffset, \Logger $logger) : PalettedBlockArray{
113 return $this->translatePalette(SubChunkConverter::convertSubChunkFromLegacyColumn($idArray, $metaArray, $yOffset), $logger);
114 }
115
116 public function getPath() : string{
117 return $this->path;
118 }
119
120 public function getWorldData() : WorldData{
121 return $this->worldData;
122 }
123}
error($message)