LevelProvider: added calculateChunkCount()

this will be used to provide progress information to the user during world conversion.
This commit is contained in:
Dylan K. Taylor 2019-03-03 17:32:57 +00:00
parent 1bb9b3d3ab
commit 3509b26751
4 changed files with 41 additions and 2 deletions

View File

@ -111,4 +111,10 @@ interface LevelProvider{
*/ */
public function getAllChunks() : \Generator; public function getAllChunks() : \Generator;
/**
* Returns the number of chunks in the provider. Used for world conversion time estimations.
*
* @return int
*/
public function calculateChunkCount() : int;
} }

View File

@ -373,4 +373,14 @@ class LevelDB extends BaseLevelProvider{
} }
} }
} }
public function calculateChunkCount() : int{
$count = 0;
foreach($this->db->getIterator() as $key => $_){
if(strlen($key) === 9 and substr($key, -1) === self::TAG_VERSION){
$count++;
}
}
return $count;
}
} }

View File

@ -208,8 +208,8 @@ abstract class RegionLevelProvider extends BaseLevelProvider{
$this->getRegion($regionX, $regionZ)->writeChunk($chunkX & 0x1f, $chunkZ & 0x1f, $this->serializeChunk($chunk)); $this->getRegion($regionX, $regionZ)->writeChunk($chunkX & 0x1f, $chunkZ & 0x1f, $this->serializeChunk($chunk));
} }
public function getAllChunks() : \Generator{ private function createRegionIterator() : \RegexIterator{
$iterator = new \RegexIterator( return new \RegexIterator(
new \FilesystemIterator( new \FilesystemIterator(
$this->path . '/region/', $this->path . '/region/',
\FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS
@ -217,6 +217,10 @@ abstract class RegionLevelProvider extends BaseLevelProvider{
'/\/r\.(-?\d+)\.(-?\d+)\.' . static::getRegionFileExtension() . '$/', '/\/r\.(-?\d+)\.(-?\d+)\.' . static::getRegionFileExtension() . '$/',
\RegexIterator::GET_MATCH \RegexIterator::GET_MATCH
); );
}
public function getAllChunks() : \Generator{
$iterator = $this->createRegionIterator();
foreach($iterator as $region){ foreach($iterator as $region){
$rX = ((int) $region[1]) << 5; $rX = ((int) $region[1]) << 5;
@ -232,4 +236,13 @@ abstract class RegionLevelProvider extends BaseLevelProvider{
} }
} }
} }
public function calculateChunkCount() : int{
$count = 0;
foreach($this->createRegionIterator() as $region){
$this->loadRegion((int) $region[1], (int) $region[2]);
$count += $this->getRegion((int) $region[1], (int) $region[2])->calculateChunkCount();
}
return $count;
}
} }

View File

@ -289,4 +289,14 @@ class RegionLoader{
public function getFilePath() : string{ public function getFilePath() : string{
return $this->filePath; return $this->filePath;
} }
public function calculateChunkCount() : int{
$count = 0;
for($i = 0; $i < 1024; ++$i){
if($this->isChunkGenerated($i)){
$count++;
}
}
return $count;
}
} }