Start using webmozart/pathutil for joining paths (#4287)

This commit is contained in:
Dylan T
2021-06-29 19:40:43 +01:00
committed by GitHub
parent aee7b03c1d
commit 32d7b1e6af
31 changed files with 252 additions and 172 deletions

View File

@ -27,6 +27,7 @@ use pocketmine\utils\Filesystem;
use pocketmine\utils\Utils;
use pocketmine\world\generator\GeneratorManager;
use pocketmine\world\WorldCreationOptions;
use Webmozart\PathUtil\Path;
use function basename;
use function crc32;
use function file_exists;
@ -71,7 +72,7 @@ class FormatConverter{
}
$nextSuffix = "";
do{
$this->backupPath = $backupPath . DIRECTORY_SEPARATOR . basename($this->oldProvider->getPath()) . $nextSuffix;
$this->backupPath = Path::join($backupPath, basename($this->oldProvider->getPath()) . $nextSuffix);
$nextSuffix = "_" . crc32(random_bytes(4));
}while(file_exists($this->backupPath));
}

View File

@ -47,6 +47,7 @@ use pocketmine\world\format\io\WritableWorldProvider;
use pocketmine\world\format\PalettedBlockArray;
use pocketmine\world\format\SubChunk;
use pocketmine\world\WorldCreationOptions;
use Webmozart\PathUtil\Path;
use function array_map;
use function array_values;
use function chr;
@ -62,7 +63,6 @@ use function strlen;
use function substr;
use function trim;
use function unpack;
use const DIRECTORY_SEPARATOR;
use const LEVELDB_ZLIB_RAW_COMPRESSION;
class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
@ -110,7 +110,7 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
* @throws \LevelDBException
*/
private static function createDB(string $path) : \LevelDB{
return new \LevelDB($path . "/db", [
return new \LevelDB(Path::join($path, "db"), [
"compression" => LEVELDB_ZLIB_RAW_COMPRESSION,
"block_size" => 64 * 1024 //64KB, big enough for most chunks
]);
@ -129,7 +129,7 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
}
protected function loadLevelData() : WorldData{
return new BedrockWorldData($this->getPath() . DIRECTORY_SEPARATOR . "level.dat");
return new BedrockWorldData(Path::join($this->getPath(), "level.dat"));
}
public function getWorldMinY() : int{
@ -141,14 +141,15 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
}
public static function isValid(string $path) : bool{
return file_exists($path . "/level.dat") and is_dir($path . "/db/");
return file_exists(Path::join($path, "level.dat")) and is_dir(Path::join($path, "db"));
}
public static function generate(string $path, string $name, WorldCreationOptions $options) : void{
self::checkForLevelDBExtension();
if(!file_exists($path . "/db")){
mkdir($path . "/db", 0777, true);
$dbPath = Path::join($path, "db");
if(!file_exists($dbPath)){
mkdir($dbPath, 0777, true);
}
BedrockWorldData::generate($path, $name, $options);

View File

@ -32,6 +32,7 @@ use pocketmine\world\format\io\BaseWorldProvider;
use pocketmine\world\format\io\data\JavaWorldData;
use pocketmine\world\format\io\exception\CorruptedChunkException;
use pocketmine\world\format\io\WorldData;
use Webmozart\PathUtil\Path;
use function assert;
use function file_exists;
use function is_dir;
@ -43,7 +44,6 @@ use function strlen;
use function strrpos;
use function substr;
use function time;
use const DIRECTORY_SEPARATOR;
use const SCANDIR_SORT_NONE;
abstract class RegionWorldProvider extends BaseWorldProvider{
@ -59,8 +59,8 @@ abstract class RegionWorldProvider extends BaseWorldProvider{
abstract protected static function getPcWorldFormatVersion() : int;
public static function isValid(string $path) : bool{
if(file_exists($path . "/level.dat") and is_dir($path . "/region/")){
foreach(scandir($path . "/region/", SCANDIR_SORT_NONE) as $file){
if(file_exists(Path::join($path, "level.dat")) and is_dir($regionPath = Path::join($path, "region"))){
foreach(scandir($regionPath, SCANDIR_SORT_NONE) as $file){
$extPos = strrpos($file, ".");
if($extPos !== false && substr($file, $extPos + 1) === static::getRegionFileExtension()){
//we don't care if other region types exist, we only care if this format is possible
@ -76,7 +76,7 @@ abstract class RegionWorldProvider extends BaseWorldProvider{
protected $regions = [];
protected function loadLevelData() : WorldData{
return new JavaWorldData($this->getPath() . DIRECTORY_SEPARATOR . "level.dat");
return new JavaWorldData(Path::join($this->getPath(), "level.dat"));
}
public function doGarbageCollection() : void{
@ -106,7 +106,7 @@ abstract class RegionWorldProvider extends BaseWorldProvider{
* Returns the path to a specific region file based on its X/Z coordinates
*/
protected function pathToRegion(int $regionX, int $regionZ) : string{
return $this->path . "/region/r.$regionX.$regionZ." . static::getRegionFileExtension();
return Path::join($this->path, "region", "r.$regionX.$regionZ." . static::getRegionFileExtension());
}
protected function loadRegion(int $regionX, int $regionZ) : RegionLoader{
@ -205,7 +205,7 @@ abstract class RegionWorldProvider extends BaseWorldProvider{
private function createRegionIterator() : \RegexIterator{
return new \RegexIterator(
new \FilesystemIterator(
$this->path . '/region/',
Path::join($this->path, 'region'),
\FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS
),
'/\/r\.(-?\d+)\.(-?\d+)\.' . static::getRegionFileExtension() . '$/',

View File

@ -27,6 +27,7 @@ use pocketmine\world\format\Chunk;
use pocketmine\world\format\io\data\JavaWorldData;
use pocketmine\world\format\io\WritableWorldProvider;
use pocketmine\world\WorldCreationOptions;
use Webmozart\PathUtil\Path;
use function file_exists;
use function mkdir;
@ -42,8 +43,9 @@ abstract class WritableRegionWorldProvider extends RegionWorldProvider implement
mkdir($path, 0777, true);
}
if(!file_exists($path . "/region")){
mkdir($path . "/region", 0777);
$regionPath = Path::join($path, "region");
if(!file_exists($regionPath)){
mkdir($regionPath, 0777);
}
JavaWorldData::generate($path, $name, $options, static::getPcWorldFormatVersion());