From 4f421d561c5c381ee1d444012e67546d216eb559 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 4 Oct 2018 15:41:18 +0100 Subject: [PATCH] BaseLevelProvider: clean up not-exists error handling disaster It checks for the existence of (and creates) the world directory if it doesn't exist. But what sense does this make when the world obviously doesn't exist in this case and must be generated first? --- src/pocketmine/level/format/io/BaseLevelProvider.php | 12 ++++++++---- src/pocketmine/level/format/io/leveldb/LevelDB.php | 6 +++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/pocketmine/level/format/io/BaseLevelProvider.php b/src/pocketmine/level/format/io/BaseLevelProvider.php index 574c519a6..99bb35de2 100644 --- a/src/pocketmine/level/format/io/BaseLevelProvider.php +++ b/src/pocketmine/level/format/io/BaseLevelProvider.php @@ -37,18 +37,22 @@ abstract class BaseLevelProvider implements LevelProvider{ protected $levelData; public function __construct(string $path){ - $this->path = $path; - if(!file_exists($this->path)){ - mkdir($this->path, 0777, true); + if(!file_exists($path)){ + throw new LevelException("Level does not exist"); } + $this->path = $path; $this->loadLevelData(); $this->fixLevelData(); } protected function loadLevelData() : void{ + $levelDatPath = $this->getPath() . "level.dat"; + if(!file_exists($levelDatPath)){ + throw new LevelException("level.dat not found"); + } $nbt = new BigEndianNBTStream(); - $levelData = $nbt->readCompressed(file_get_contents($this->getPath() . "level.dat")); + $levelData = $nbt->readCompressed(file_get_contents($levelDatPath)); if(!($levelData instanceof CompoundTag) or !$levelData->hasTag("Data", CompoundTag::class)){ throw new LevelException("Invalid level.dat"); diff --git a/src/pocketmine/level/format/io/leveldb/LevelDB.php b/src/pocketmine/level/format/io/leveldb/LevelDB.php index 14199abbb..6272a292e 100644 --- a/src/pocketmine/level/format/io/leveldb/LevelDB.php +++ b/src/pocketmine/level/format/io/leveldb/LevelDB.php @@ -100,8 +100,12 @@ class LevelDB extends BaseLevelProvider{ } protected function loadLevelData() : void{ + $levelDatPath = $this->getPath() . "level.dat"; + if(!file_exists($levelDatPath)){ + throw new LevelException("level.dat not found"); + } $nbt = new LittleEndianNBTStream(); - $levelData = $nbt->read(substr(file_get_contents($this->getPath() . "level.dat"), 8)); + $levelData = $nbt->read(substr(file_get_contents($levelDatPath), 8)); if($levelData instanceof CompoundTag){ $this->levelData = $levelData; }else{