58 public static function isValid(
string $path) : bool{
59 if(file_exists(Path::join($path,
"level.dat")) && is_dir($regionPath = Path::join($path,
"region"))){
60 $files = scandir($regionPath, SCANDIR_SORT_NONE);
65 foreach($files as $file){
66 $extPos = strrpos($file,
".");
67 if($extPos !==
false && substr($file, $extPos + 1) === static::getRegionFileExtension()){
81 protected array $regions = [];
84 return new
JavaWorldData(Path::join($this->getPath(),
"level.dat"));
88 $limit = time() - 300;
89 foreach($this->regions as $index => $region){
90 if($region->lastUsed <= $limit){
92 unset($this->regions[$index]);
105 public static function getRegionIndex(
int $chunkX,
int $chunkZ, &$regionX, &$regionZ) : void{
106 $regionX = $chunkX >> 5;
107 $regionZ = $chunkZ >> 5;
110 protected function getRegion(
int $regionX,
int $regionZ) : ?
RegionLoader{
111 return $this->regions[morton2d_encode($regionX, $regionZ)] ?? null;
118 return Path::join($this->path,
"region",
"r.$regionX.$regionZ." . static::getRegionFileExtension());
121 protected function loadRegion(
int $regionX,
int $regionZ) :
RegionLoader{
122 if(!isset($this->regions[$index = morton2d_encode($regionX, $regionZ)])){
123 $path = $this->pathToRegion($regionX, $regionZ);
126 $this->regions[$index] = RegionLoader::loadExisting($path);
127 }
catch(CorruptedRegionException $e){
128 $this->logger->error(
"Corrupted region file detected: " . $e->getMessage());
130 $backupPath = $path .
".bak." . time();
131 rename($path, $backupPath);
132 $this->logger->error(
"Corrupted region file has been backed up to " . $backupPath);
134 $this->regions[$index] = RegionLoader::createNew($path);
137 return $this->regions[$index];
140 protected function unloadRegion(
int $regionX,
int $regionZ) : void{
141 if(isset($this->regions[$hash = morton2d_encode($regionX, $regionZ)])){
142 $this->regions[$hash]->close();
143 unset($this->regions[$hash]);
148 foreach($this->regions as $index => $region){
150 unset($this->regions[$index]);
167 if($compoundList ===
null){
170 return $compoundList->getValue();
173 protected static function readFixedSizeByteArray(
CompoundTag $chunk,
string $tagName,
int $length) : string{
174 $tag = $chunk->getTag($tagName);
175 if(!($tag instanceof ByteArrayTag)){
182 if(strlen($data) !== $length){
183 throw new CorruptedChunkException(
"Expected '$tagName' payload to have exactly $length bytes, but have " . strlen($data));
192 $regionX = $regionZ = null;
193 self::getRegionIndex($chunkX, $chunkZ, $regionX, $regionZ);
195 if(!file_exists($this->pathToRegion($regionX, $regionZ))){
199 $chunkData = $this->loadRegion($regionX, $regionZ)->readChunk($chunkX & 0x1f, $chunkZ & 0x1f);
200 if($chunkData !==
null){
201 return $this->deserializeChunk($chunkData,
new \
PrefixedLogger($this->logger,
"Loading chunk x=$chunkX z=$chunkZ"));
210 private function createRegionIterator() : \RegexIterator{
211 return new \RegexIterator(
212 new \FilesystemIterator(
213 Path::join($this->path,
'region'),
214 \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS
216 '/\/r\.(-?\d+)\.(-?\d+)\.' . static::getRegionFileExtension() .
'$/',
217 \RegexIterator::GET_MATCH
222 $iterator = $this->createRegionIterator();
224 foreach($iterator as $region){
225 $regionX = ((int) $region[1]);
226 $regionZ = ((int) $region[2]);
230 for($chunkX = $rX; $chunkX < $rX + 32; ++$chunkX){
231 for($chunkZ = $rZ; $chunkZ < $rZ + 32; ++$chunkZ){
233 $chunk = $this->loadChunk($chunkX, $chunkZ);
235 yield [$chunkX, $chunkZ] => $chunk;
241 if($logger !==
null){
242 $logger->
error(
"Skipped corrupted chunk $chunkX $chunkZ (" . $e->getMessage() .
")");
248 $this->unloadRegion($regionX, $regionZ);
254 foreach($this->createRegionIterator() as $region){
255 $regionX = ((int) $region[1]);
256 $regionZ = ((int) $region[2]);
257 $count += $this->loadRegion($regionX, $regionZ)->calculateChunkCount();
258 $this->unloadRegion($regionX, $regionZ);