61 public static function isValid(
string $path) : bool{
62 if(file_exists(Path::join($path,
"level.dat")) && is_dir($regionPath = Path::join($path,
"region"))){
63 foreach(scandir($regionPath, SCANDIR_SORT_NONE) as $file){
64 $extPos = strrpos($file,
".");
65 if($extPos !==
false && substr($file, $extPos + 1) === static::getRegionFileExtension()){
79 protected array $regions = [];
82 return new
JavaWorldData(Path::join($this->getPath(),
"level.dat"));
86 $limit = time() - 300;
87 foreach($this->regions as $index => $region){
88 if($region->lastUsed <= $limit){
90 unset($this->regions[$index]);
101 public static function getRegionIndex(
int $chunkX,
int $chunkZ, &$regionX, &$regionZ) : void{
102 $regionX = $chunkX >> 5;
103 $regionZ = $chunkZ >> 5;
106 protected function getRegion(
int $regionX,
int $regionZ) : ?
RegionLoader{
107 return $this->regions[morton2d_encode($regionX, $regionZ)] ?? null;
114 return Path::join($this->path,
"region",
"r.$regionX.$regionZ." . static::getRegionFileExtension());
117 protected function loadRegion(
int $regionX,
int $regionZ) :
RegionLoader{
118 if(!isset($this->regions[$index = morton2d_encode($regionX, $regionZ)])){
119 $path = $this->pathToRegion($regionX, $regionZ);
122 $this->regions[$index] = RegionLoader::loadExisting($path);
123 }
catch(CorruptedRegionException $e){
124 $this->logger->error(
"Corrupted region file detected: " . $e->getMessage());
126 $backupPath = $path .
".bak." . time();
127 rename($path, $backupPath);
128 $this->logger->error(
"Corrupted region file has been backed up to " . $backupPath);
130 $this->regions[$index] = RegionLoader::createNew($path);
133 return $this->regions[$index];
136 protected function unloadRegion(
int $regionX,
int $regionZ) : void{
137 if(isset($this->regions[$hash = morton2d_encode($regionX, $regionZ)])){
138 $this->regions[$hash]->close();
139 unset($this->regions[$hash]);
144 foreach($this->regions as $index => $region){
146 unset($this->regions[$index]);
160 if($list->count() === 0){
163 if($list->getTagType() !== NBT::TAG_Compound){
164 throw new CorruptedChunkException(
"Expected TAG_List<TAG_Compound> for '$context'");
167 foreach($list as $tag){
168 if(!($tag instanceof CompoundTag)){
170 throw new CorruptedChunkException(
"Expected TAG_List<TAG_Compound> for '$context'");
177 protected static function readFixedSizeByteArray(
CompoundTag $chunk,
string $tagName,
int $length) : string{
178 $tag = $chunk->getTag($tagName);
179 if(!($tag instanceof ByteArrayTag)){
186 if(strlen($data) !== $length){
187 throw new CorruptedChunkException(
"Expected '$tagName' payload to have exactly $length bytes, but have " . strlen($data));
196 $regionX = $regionZ = null;
197 self::getRegionIndex($chunkX, $chunkZ, $regionX, $regionZ);
198 assert(is_int($regionX) && is_int($regionZ));
200 if(!file_exists($this->pathToRegion($regionX, $regionZ))){
204 $chunkData = $this->loadRegion($regionX, $regionZ)->readChunk($chunkX & 0x1f, $chunkZ & 0x1f);
205 if($chunkData !==
null){
206 return $this->deserializeChunk($chunkData,
new \
PrefixedLogger($this->logger,
"Loading chunk x=$chunkX z=$chunkZ"));
212 private function createRegionIterator() : \RegexIterator{
213 return new \RegexIterator(
214 new \FilesystemIterator(
215 Path::join($this->path,
'region'),
216 \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS
218 '/\/r\.(-?\d+)\.(-?\d+)\.' . static::getRegionFileExtension() .
'$/',
219 \RegexIterator::GET_MATCH
224 $iterator = $this->createRegionIterator();
226 foreach($iterator as $region){
227 $regionX = ((int) $region[1]);
228 $regionZ = ((int) $region[2]);
232 for($chunkX = $rX; $chunkX < $rX + 32; ++$chunkX){
233 for($chunkZ = $rZ; $chunkZ < $rZ + 32; ++$chunkZ){
235 $chunk = $this->loadChunk($chunkX, $chunkZ);
237 yield [$chunkX, $chunkZ] => $chunk;
243 if($logger !==
null){
244 $logger->error(
"Skipped corrupted chunk $chunkX $chunkZ (" . $e->getMessage() .
")");
250 $this->unloadRegion($regionX, $regionZ);
256 foreach($this->createRegionIterator() as $region){
257 $regionX = ((int) $region[1]);
258 $regionZ = ((int) $region[2]);
259 $count += $this->loadRegion($regionX, $regionZ)->calculateChunkCount();
260 $this->unloadRegion($regionX, $regionZ);