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);
66 foreach($files as $file){
67 $extPos = strrpos($file,
".");
68 if($extPos !==
false && substr($file, $extPos + 1) === static::getRegionFileExtension()){
82 protected array $regions = [];
85 return new
JavaWorldData(Path::join($this->getPath(),
"level.dat"));
89 $limit = time() - 300;
90 foreach($this->regions as $index => $region){
91 if($region->lastUsed <= $limit){
93 unset($this->regions[$index]);
106 public static function getRegionIndex(
int $chunkX,
int $chunkZ, &$regionX, &$regionZ) : void{
107 $regionX = $chunkX >> 5;
108 $regionZ = $chunkZ >> 5;
111 protected function getRegion(
int $regionX,
int $regionZ) : ?
RegionLoader{
112 return $this->regions[morton2d_encode($regionX, $regionZ)] ?? null;
119 return Path::join($this->path,
"region",
"r.$regionX.$regionZ." . static::getRegionFileExtension());
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);
127 $this->regions[$index] = RegionLoader::loadExisting($path);
128 }
catch(CorruptedRegionException $e){
129 $this->logger->error(
"Corrupted region file detected: " . $e->getMessage());
131 $backupPath = $path .
".bak." . time();
132 rename($path, $backupPath);
133 $this->logger->error(
"Corrupted region file has been backed up to " . $backupPath);
135 $this->regions[$index] = RegionLoader::createNew($path);
138 return $this->regions[$index];
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]);
149 foreach($this->regions as $index => $region){
151 unset($this->regions[$index]);
167 if($list->count() === 0){
170 if($list->getTagType() !== NBT::TAG_Compound){
171 throw new CorruptedChunkException(
"Expected TAG_List<TAG_Compound> for '$context'");
174 foreach($list as $tag){
175 if(!($tag instanceof CompoundTag)){
177 throw new CorruptedChunkException(
"Expected TAG_List<TAG_Compound> for '$context'");
184 protected static function readFixedSizeByteArray(
CompoundTag $chunk,
string $tagName,
int $length) : string{
185 $tag = $chunk->getTag($tagName);
186 if(!($tag instanceof ByteArrayTag)){
193 if(strlen($data) !== $length){
194 throw new CorruptedChunkException(
"Expected '$tagName' payload to have exactly $length bytes, but have " . strlen($data));
203 $regionX = $regionZ = null;
204 self::getRegionIndex($chunkX, $chunkZ, $regionX, $regionZ);
206 if(!file_exists($this->pathToRegion($regionX, $regionZ))){
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"));
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
227 '/\/r\.(-?\d+)\.(-?\d+)\.' . static::getRegionFileExtension() .
'$/',
228 \RegexIterator::GET_MATCH
233 $iterator = $this->createRegionIterator();
235 foreach($iterator as $region){
236 $regionX = ((int) $region[1]);
237 $regionZ = ((int) $region[2]);
241 for($chunkX = $rX; $chunkX < $rX + 32; ++$chunkX){
242 for($chunkZ = $rZ; $chunkZ < $rZ + 32; ++$chunkZ){
244 $chunk = $this->loadChunk($chunkX, $chunkZ);
246 yield [$chunkX, $chunkZ] => $chunk;
252 if($logger !==
null){
253 $logger->error(
"Skipped corrupted chunk $chunkX $chunkZ (" . $e->getMessage() .
")");
259 $this->unloadRegion($regionX, $regionZ);
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);