PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
Loading...
Searching...
No Matches
RegionWorldProvider.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\region;
25
35use Symfony\Component\Filesystem\Path;
36use function file_exists;
37use function is_dir;
38use function morton2d_encode;
39use function rename;
40use function scandir;
41use function strlen;
42use function strrpos;
43use function substr;
44use function time;
45use const SCANDIR_SORT_NONE;
46
48
52 abstract protected static function getRegionFileExtension() : string;
53
57 abstract protected static function getPcWorldFormatVersion() : int;
58
59 public static function isValid(string $path) : bool{
60 if(file_exists(Path::join($path, "level.dat")) && is_dir($regionPath = Path::join($path, "region"))){
61 $files = scandir($regionPath, SCANDIR_SORT_NONE);
62 if($files === false){
63 //we can't tell the type if we don't have read perms
64 return false;
65 }
66 foreach($files as $file){
67 $extPos = strrpos($file, ".");
68 if($extPos !== false && substr($file, $extPos + 1) === static::getRegionFileExtension()){
69 //we don't care if other region types exist, we only care if this format is possible
70 return true;
71 }
72 }
73 }
74
75 return false;
76 }
77
82 protected array $regions = [];
83
84 protected function loadLevelData() : WorldData{
85 return new JavaWorldData(Path::join($this->getPath(), "level.dat"));
86 }
87
88 public function doGarbageCollection() : void{
89 $limit = time() - 300;
90 foreach($this->regions as $index => $region){
91 if($region->lastUsed <= $limit){
92 $region->close();
93 unset($this->regions[$index]);
94 }
95 }
96 }
97
106 public static function getRegionIndex(int $chunkX, int $chunkZ, &$regionX, &$regionZ) : void{
107 $regionX = $chunkX >> 5;
108 $regionZ = $chunkZ >> 5;
109 }
110
111 protected function getRegion(int $regionX, int $regionZ) : ?RegionLoader{
112 return $this->regions[morton2d_encode($regionX, $regionZ)] ?? null;
113 }
114
118 protected function pathToRegion(int $regionX, int $regionZ) : string{
119 return Path::join($this->path, "region", "r.$regionX.$regionZ." . static::getRegionFileExtension());
120 }
121
122 protected function loadRegion(int $regionX, int $regionZ) : RegionLoader{
123 if(!isset($this->regions[$index = morton2d_encode($regionX, $regionZ)])){
124 $path = $this->pathToRegion($regionX, $regionZ);
125
126 try{
127 $this->regions[$index] = RegionLoader::loadExisting($path);
128 }catch(CorruptedRegionException $e){
129 $this->logger->error("Corrupted region file detected: " . $e->getMessage());
130
131 $backupPath = $path . ".bak." . time();
132 rename($path, $backupPath);
133 $this->logger->error("Corrupted region file has been backed up to " . $backupPath);
134
135 $this->regions[$index] = RegionLoader::createNew($path);
136 }
137 }
138 return $this->regions[$index];
139 }
140
141 protected function unloadRegion(int $regionX, int $regionZ) : void{
142 if(isset($this->regions[$hash = morton2d_encode($regionX, $regionZ)])){
143 $this->regions[$hash]->close();
144 unset($this->regions[$hash]);
145 }
146 }
147
148 public function close() : void{
149 foreach($this->regions as $index => $region){
150 $region->close();
151 unset($this->regions[$index]);
152 }
153 }
154
158 abstract protected function deserializeChunk(string $data, \Logger $logger) : ?LoadedChunkData;
159
166 protected static function getCompoundList(string $context, ListTag $list) : array{
167 if($list->count() === 0){ //empty lists might have wrong types, we don't care
168 return [];
169 }
170 if($list->getTagType() !== NBT::TAG_Compound){
171 throw new CorruptedChunkException("Expected TAG_List<TAG_Compound> for '$context'");
172 }
173 $result = [];
174 foreach($list as $tag){
175 if(!($tag instanceof CompoundTag)){
176 //this should never happen, but it's still possible due to lack of native type safety
177 throw new CorruptedChunkException("Expected TAG_List<TAG_Compound> for '$context'");
178 }
179 $result[] = $tag;
180 }
181 return $result;
182 }
183
184 protected static function readFixedSizeByteArray(CompoundTag $chunk, string $tagName, int $length) : string{
185 $tag = $chunk->getTag($tagName);
186 if(!($tag instanceof ByteArrayTag)){
187 if($tag === null){
188 throw new CorruptedChunkException("'$tagName' key is missing from chunk NBT");
189 }
190 throw new CorruptedChunkException("Expected TAG_ByteArray for '$tagName'");
191 }
192 $data = $tag->getValue();
193 if(strlen($data) !== $length){
194 throw new CorruptedChunkException("Expected '$tagName' payload to have exactly $length bytes, but have " . strlen($data));
195 }
196 return $data;
197 }
198
202 public function loadChunk(int $chunkX, int $chunkZ) : ?LoadedChunkData{
203 $regionX = $regionZ = null;
204 self::getRegionIndex($chunkX, $chunkZ, $regionX, $regionZ);
205
206 if(!file_exists($this->pathToRegion($regionX, $regionZ))){
207 return null;
208 }
209
210 $chunkData = $this->loadRegion($regionX, $regionZ)->readChunk($chunkX & 0x1f, $chunkZ & 0x1f);
211 if($chunkData !== null){
212 return $this->deserializeChunk($chunkData, new \PrefixedLogger($this->logger, "Loading chunk x=$chunkX z=$chunkZ"));
213 }
214
215 return null;
216 }
217
221 private function createRegionIterator() : \RegexIterator{
222 return new \RegexIterator(
223 new \FilesystemIterator(
224 Path::join($this->path, 'region'),
225 \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS
226 ),
227 '/\/r\.(-?\d+)\.(-?\d+)\.' . static::getRegionFileExtension() . '$/',
228 \RegexIterator::GET_MATCH
229 );
230 }
231
232 public function getAllChunks(bool $skipCorrupted = false, ?\Logger $logger = null) : \Generator{
233 $iterator = $this->createRegionIterator();
234
235 foreach($iterator as $region){
236 $regionX = ((int) $region[1]);
237 $regionZ = ((int) $region[2]);
238 $rX = $regionX << 5;
239 $rZ = $regionZ << 5;
240
241 for($chunkX = $rX; $chunkX < $rX + 32; ++$chunkX){
242 for($chunkZ = $rZ; $chunkZ < $rZ + 32; ++$chunkZ){
243 try{
244 $chunk = $this->loadChunk($chunkX, $chunkZ);
245 if($chunk !== null){
246 yield [$chunkX, $chunkZ] => $chunk;
247 }
248 }catch(CorruptedChunkException $e){
249 if(!$skipCorrupted){
250 throw $e;
251 }
252 if($logger !== null){
253 $logger->error("Skipped corrupted chunk $chunkX $chunkZ (" . $e->getMessage() . ")");
254 }
255 }
256 }
257 }
258
259 $this->unloadRegion($regionX, $regionZ);
260 }
261 }
262
263 public function calculateChunkCount() : int{
264 $count = 0;
265 foreach($this->createRegionIterator() as $region){
266 $regionX = ((int) $region[1]);
267 $regionZ = ((int) $region[2]);
268 $count += $this->loadRegion($regionX, $regionZ)->calculateChunkCount();
269 $this->unloadRegion($regionX, $regionZ);
270 }
271 return $count;
272 }
273}
getAllChunks(bool $skipCorrupted=false, ?\Logger $logger=null)
deserializeChunk(string $data, \Logger $logger)
static getCompoundList(string $context, ListTag $list)
static getRegionIndex(int $chunkX, int $chunkZ, &$regionX, &$regionZ)