diff --git a/src/world/format/io/region/RegionLoader.php b/src/world/format/io/region/RegionLoader.php index 1aa06812f..746e057f3 100644 --- a/src/world/format/io/region/RegionLoader.php +++ b/src/world/format/io/region/RegionLoader.php @@ -80,15 +80,13 @@ class RegionLoader{ /** @var int */ public $lastUsed = 0; - public function __construct(string $filePath){ - $this->filePath = $filePath; - $this->garbageTable = new RegionGarbageMap([]); - } - /** * @throws CorruptedRegionException */ - public function open() : void{ + public function __construct(string $filePath){ + $this->filePath = $filePath; + $this->garbageTable = new RegionGarbageMap([]); + clearstatcache(false, $this->filePath); $exists = file_exists($this->filePath); if(!$exists){ diff --git a/src/world/format/io/region/RegionWorldProvider.php b/src/world/format/io/region/RegionWorldProvider.php index 68ed8f1f6..2a16e8ae1 100644 --- a/src/world/format/io/region/RegionWorldProvider.php +++ b/src/world/format/io/region/RegionWorldProvider.php @@ -115,24 +115,18 @@ abstract class RegionWorldProvider extends BaseWorldProvider{ if(!isset($this->regions[$index = morton2d_encode($regionX, $regionZ)])){ $path = $this->pathToRegion($regionX, $regionZ); - $region = new RegionLoader($path); try{ - $region->open(); + $this->regions[$index] = new RegionLoader($path); }catch(CorruptedRegionException $e){ $logger = \GlobalLogger::get(); $logger->error("Corrupted region file detected: " . $e->getMessage()); - $region->close(); //Do not write anything to the file - $backupPath = $path . ".bak." . time(); rename($path, $backupPath); $logger->error("Corrupted region file has been backed up to " . $backupPath); - $region = new RegionLoader($path); - $region->open(); //this will create a new empty region to replace the corrupted one + $this->regions[$index] = new RegionLoader($path); //this will create a new empty region to replace the corrupted one } - - $this->regions[$index] = $region; } return $this->regions[$index]; } diff --git a/tests/phpunit/world/format/io/region/RegionLoaderTest.php b/tests/phpunit/world/format/io/region/RegionLoaderTest.php index f80df791e..2b35f2f19 100644 --- a/tests/phpunit/world/format/io/region/RegionLoaderTest.php +++ b/tests/phpunit/world/format/io/region/RegionLoaderTest.php @@ -44,7 +44,6 @@ class RegionLoaderTest extends TestCase{ unlink($this->regionPath); } $this->region = new RegionLoader($this->regionPath); - $this->region->open(); } public function tearDown() : void{ @@ -65,7 +64,6 @@ class RegionLoaderTest extends TestCase{ $this->region->close(); $r = new RegionLoader($this->regionPath); - $r->open(); self::assertSame($data, $r->readChunk(0, 0)); } @@ -122,12 +120,10 @@ class RegionLoaderTest extends TestCase{ public function testRegionHeaderCachedFilesizeRegression() : void{ $this->region->close(); $region = new RegionLoader($this->regionPath); //now we have a region, so the header will be verified, triggering two filesize() calls - $region->open(); $data = str_repeat("hello", 2000); $region->writeChunk(0, 0, $data); //add some data to the end of the file, to make the cached filesize invalid $region->close(); $region = new RegionLoader($this->regionPath); - $region->open(); self::assertSame($data, $region->readChunk(0, 0)); } } diff --git a/tools/compact-regions.php b/tools/compact-regions.php index 90d3f8ee8..77089ce0d 100644 --- a/tools/compact-regions.php +++ b/tools/compact-regions.php @@ -107,9 +107,8 @@ function main(array $argv) : int{ $doneCount = 0; $totalCount = count($files); foreach($files as $file => $size){ - $oldRegion = new RegionLoader($file); try{ - $oldRegion->open(); + $oldRegion = new RegionLoader($file); }catch(CorruptedRegionException $e){ $logger->error("Damaged region in file $file (" . $e->getMessage() . "), skipping"); $corruptedFiles[] = $file; @@ -119,7 +118,6 @@ function main(array $argv) : int{ $newFile = $file . ".compacted"; $newRegion = new RegionLoader($newFile); - $newRegion->open(); $emptyRegion = true; $corruption = false;