mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-11 08:19:45 +00:00
Cleanup to world provider exception handling
This commit is contained in:
parent
a51c06116a
commit
cf73c7f5c1
@ -30,6 +30,7 @@ use pocketmine\event\world\WorldUnloadEvent;
|
|||||||
use pocketmine\Server;
|
use pocketmine\Server;
|
||||||
use pocketmine\timings\Timings;
|
use pocketmine\timings\Timings;
|
||||||
use pocketmine\utils\Utils;
|
use pocketmine\utils\Utils;
|
||||||
|
use pocketmine\world\format\io\exception\CorruptedWorldException;
|
||||||
use pocketmine\world\format\io\exception\UnsupportedWorldFormatException;
|
use pocketmine\world\format\io\exception\UnsupportedWorldFormatException;
|
||||||
use pocketmine\world\format\io\FormatConverter;
|
use pocketmine\world\format\io\FormatConverter;
|
||||||
use pocketmine\world\format\io\WorldProvider;
|
use pocketmine\world\format\io\WorldProvider;
|
||||||
@ -195,7 +196,7 @@ class WorldManager{
|
|||||||
*/
|
*/
|
||||||
public function loadWorld(string $name, bool $autoUpgrade = false) : bool{
|
public function loadWorld(string $name, bool $autoUpgrade = false) : bool{
|
||||||
if(trim($name) === ""){
|
if(trim($name) === ""){
|
||||||
throw new WorldException("Invalid empty world name");
|
throw new \InvalidArgumentException("Invalid empty world name");
|
||||||
}
|
}
|
||||||
if($this->isWorldLoaded($name)){
|
if($this->isWorldLoaded($name)){
|
||||||
return true;
|
return true;
|
||||||
@ -221,7 +222,15 @@ class WorldManager{
|
|||||||
* @var WorldProvider $provider
|
* @var WorldProvider $provider
|
||||||
* @see WorldProvider::__construct()
|
* @see WorldProvider::__construct()
|
||||||
*/
|
*/
|
||||||
$provider = new $providerClass($path);
|
try{
|
||||||
|
$provider = new $providerClass($path);
|
||||||
|
}catch(CorruptedWorldException $e){
|
||||||
|
$this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.level.loadError", [$name, "Corruption detected: " . $e->getMessage()]));
|
||||||
|
return false;
|
||||||
|
}catch(UnsupportedWorldFormatException $e){
|
||||||
|
$this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.level.loadError", [$name, "Unsupported format: " . $e->getMessage()]));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
try{
|
try{
|
||||||
GeneratorManager::getGenerator($provider->getWorldData()->getGenerator(), true);
|
GeneratorManager::getGenerator($provider->getWorldData()->getGenerator(), true);
|
||||||
}catch(\InvalidArgumentException $e){
|
}catch(\InvalidArgumentException $e){
|
||||||
@ -230,7 +239,7 @@ class WorldManager{
|
|||||||
}
|
}
|
||||||
if(!($provider instanceof WritableWorldProvider)){
|
if(!($provider instanceof WritableWorldProvider)){
|
||||||
if(!$autoUpgrade){
|
if(!$autoUpgrade){
|
||||||
throw new WorldException("World \"$name\" is in an unsupported format and needs to be upgraded");
|
throw new UnsupportedWorldFormatException("World \"$name\" is in an unsupported format and needs to be upgraded");
|
||||||
}
|
}
|
||||||
$this->server->getLogger()->notice("Upgrading world \"$name\" to new format. This may take a while.");
|
$this->server->getLogger()->notice("Upgrading world \"$name\" to new format. This may take a while.");
|
||||||
|
|
||||||
@ -240,12 +249,7 @@ class WorldManager{
|
|||||||
$this->server->getLogger()->notice("Upgraded world \"$name\" to new format successfully. Backed up pre-conversion world at " . $converter->getBackupPath());
|
$this->server->getLogger()->notice("Upgraded world \"$name\" to new format successfully. Backed up pre-conversion world at " . $converter->getBackupPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
try{
|
$world = new World($this->server, $name, $provider);
|
||||||
$world = new World($this->server, $name, $provider);
|
|
||||||
}catch(UnsupportedWorldFormatException $e){
|
|
||||||
$this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.level.loadError", [$name, $e->getMessage()]));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->worlds[$world->getId()] = $world;
|
$this->worlds[$world->getId()] = $world;
|
||||||
$world->setAutoSave($this->autoSave);
|
$world->setAutoSave($this->autoSave);
|
||||||
|
@ -25,7 +25,9 @@ namespace pocketmine\world\format\io;
|
|||||||
|
|
||||||
use pocketmine\world\format\Chunk;
|
use pocketmine\world\format\Chunk;
|
||||||
use pocketmine\world\format\io\exception\CorruptedChunkException;
|
use pocketmine\world\format\io\exception\CorruptedChunkException;
|
||||||
|
use pocketmine\world\format\io\exception\CorruptedWorldException;
|
||||||
use pocketmine\world\format\io\exception\UnsupportedChunkFormatException;
|
use pocketmine\world\format\io\exception\UnsupportedChunkFormatException;
|
||||||
|
use pocketmine\world\format\io\exception\UnsupportedWorldFormatException;
|
||||||
use pocketmine\world\WorldException;
|
use pocketmine\world\WorldException;
|
||||||
use function file_exists;
|
use function file_exists;
|
||||||
|
|
||||||
@ -44,6 +46,11 @@ abstract class BaseWorldProvider implements WorldProvider{
|
|||||||
$this->worldData = $this->loadLevelData();
|
$this->worldData = $this->loadLevelData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return WorldData
|
||||||
|
* @throws CorruptedWorldException
|
||||||
|
* @throws UnsupportedWorldFormatException
|
||||||
|
*/
|
||||||
abstract protected function loadLevelData() : WorldData;
|
abstract protected function loadLevelData() : WorldData;
|
||||||
|
|
||||||
public function getPath() : string{
|
public function getPath() : string{
|
||||||
|
@ -25,12 +25,16 @@ namespace pocketmine\world\format\io;
|
|||||||
|
|
||||||
use pocketmine\world\format\Chunk;
|
use pocketmine\world\format\Chunk;
|
||||||
use pocketmine\world\format\io\exception\CorruptedChunkException;
|
use pocketmine\world\format\io\exception\CorruptedChunkException;
|
||||||
|
use pocketmine\world\format\io\exception\CorruptedWorldException;
|
||||||
use pocketmine\world\format\io\exception\UnsupportedChunkFormatException;
|
use pocketmine\world\format\io\exception\UnsupportedChunkFormatException;
|
||||||
|
use pocketmine\world\format\io\exception\UnsupportedWorldFormatException;
|
||||||
|
|
||||||
interface WorldProvider{
|
interface WorldProvider{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $path
|
* @param string $path
|
||||||
|
* @throws CorruptedWorldException
|
||||||
|
* @throws UnsupportedWorldFormatException
|
||||||
*/
|
*/
|
||||||
public function __construct(string $path);
|
public function __construct(string $path);
|
||||||
|
|
||||||
|
@ -25,8 +25,9 @@ namespace pocketmine\world\format\io\data;
|
|||||||
|
|
||||||
use pocketmine\math\Vector3;
|
use pocketmine\math\Vector3;
|
||||||
use pocketmine\nbt\tag\CompoundTag;
|
use pocketmine\nbt\tag\CompoundTag;
|
||||||
|
use pocketmine\world\format\io\exception\CorruptedWorldException;
|
||||||
|
use pocketmine\world\format\io\exception\UnsupportedWorldFormatException;
|
||||||
use pocketmine\world\format\io\WorldData;
|
use pocketmine\world\format\io\WorldData;
|
||||||
use pocketmine\world\WorldException;
|
|
||||||
use function file_exists;
|
use function file_exists;
|
||||||
|
|
||||||
abstract class BaseNbtWorldData implements WorldData{
|
abstract class BaseNbtWorldData implements WorldData{
|
||||||
@ -37,26 +38,38 @@ abstract class BaseNbtWorldData implements WorldData{
|
|||||||
/** @var CompoundTag */
|
/** @var CompoundTag */
|
||||||
protected $compoundTag;
|
protected $compoundTag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $dataPath
|
||||||
|
*
|
||||||
|
* @throws CorruptedWorldException
|
||||||
|
* @throws UnsupportedWorldFormatException
|
||||||
|
*/
|
||||||
public function __construct(string $dataPath){
|
public function __construct(string $dataPath){
|
||||||
$this->dataPath = $dataPath;
|
$this->dataPath = $dataPath;
|
||||||
|
|
||||||
if(!file_exists($this->dataPath)){
|
if(!file_exists($this->dataPath)){
|
||||||
throw new WorldException("World data not found at $dataPath");
|
throw new CorruptedWorldException("World data not found at $dataPath");
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->compoundTag = $this->load();
|
try{
|
||||||
if($this->compoundTag === null){
|
$this->compoundTag = $this->load();
|
||||||
throw new WorldException("Invalid world data");
|
}catch(CorruptedWorldException $e){
|
||||||
|
throw new CorruptedWorldException("Corrupted world data: " . $e->getMessage(), 0, $e);
|
||||||
}
|
}
|
||||||
$this->fix();
|
$this->fix();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return CompoundTag
|
* @return CompoundTag
|
||||||
|
* @throws CorruptedWorldException
|
||||||
|
* @throws UnsupportedWorldFormatException
|
||||||
*/
|
*/
|
||||||
abstract protected function load() : ?CompoundTag;
|
abstract protected function load() : CompoundTag;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws CorruptedWorldException
|
||||||
|
* @throws UnsupportedWorldFormatException
|
||||||
|
*/
|
||||||
abstract protected function fix() : void;
|
abstract protected function fix() : void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -24,6 +24,7 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\world\format\io\data;
|
namespace pocketmine\world\format\io\data;
|
||||||
|
|
||||||
use pocketmine\nbt\LittleEndianNbtSerializer;
|
use pocketmine\nbt\LittleEndianNbtSerializer;
|
||||||
|
use pocketmine\nbt\NbtDataException;
|
||||||
use pocketmine\nbt\tag\CompoundTag;
|
use pocketmine\nbt\tag\CompoundTag;
|
||||||
use pocketmine\nbt\tag\IntTag;
|
use pocketmine\nbt\tag\IntTag;
|
||||||
use pocketmine\nbt\tag\StringTag;
|
use pocketmine\nbt\tag\StringTag;
|
||||||
@ -31,6 +32,7 @@ use pocketmine\nbt\TreeRoot;
|
|||||||
use pocketmine\network\mcpe\protocol\ProtocolInfo;
|
use pocketmine\network\mcpe\protocol\ProtocolInfo;
|
||||||
use pocketmine\utils\Binary;
|
use pocketmine\utils\Binary;
|
||||||
use pocketmine\utils\Utils;
|
use pocketmine\utils\Utils;
|
||||||
|
use pocketmine\world\format\io\exception\CorruptedWorldException;
|
||||||
use pocketmine\world\format\io\exception\UnsupportedWorldFormatException;
|
use pocketmine\world\format\io\exception\UnsupportedWorldFormatException;
|
||||||
use pocketmine\world\generator\Flat;
|
use pocketmine\world\generator\Flat;
|
||||||
use pocketmine\world\generator\Generator;
|
use pocketmine\world\generator\Generator;
|
||||||
@ -102,13 +104,17 @@ class BedrockWorldData extends BaseNbtWorldData{
|
|||||||
file_put_contents($path . "level.dat", Binary::writeLInt(self::CURRENT_STORAGE_VERSION) . Binary::writeLInt(strlen($buffer)) . $buffer);
|
file_put_contents($path . "level.dat", Binary::writeLInt(self::CURRENT_STORAGE_VERSION) . Binary::writeLInt(strlen($buffer)) . $buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function load() : ?CompoundTag{
|
protected function load() : CompoundTag{
|
||||||
$nbt = new LittleEndianNbtSerializer();
|
$nbt = new LittleEndianNbtSerializer();
|
||||||
$worldData = $nbt->read(substr(file_get_contents($this->dataPath), 8))->getTag();
|
try{
|
||||||
|
$worldData = $nbt->read(substr(file_get_contents($this->dataPath), 8))->getTag();
|
||||||
|
}catch(NbtDataException $e){
|
||||||
|
throw new CorruptedWorldException($e->getMessage(), 0, $e);
|
||||||
|
}
|
||||||
|
|
||||||
$version = $worldData->getInt("StorageVersion", INT32_MAX, true);
|
$version = $worldData->getInt("StorageVersion", INT32_MAX, true);
|
||||||
if($version > self::CURRENT_STORAGE_VERSION){
|
if($version > self::CURRENT_STORAGE_VERSION){
|
||||||
throw new UnsupportedWorldFormatException("Specified LevelDB world format version ($version) is not supported by " . \pocketmine\NAME);
|
throw new UnsupportedWorldFormatException("LevelDB world format version $version is currently unsupported");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $worldData;
|
return $worldData;
|
||||||
@ -130,7 +136,7 @@ class BedrockWorldData extends BaseNbtWorldData{
|
|||||||
case self::GENERATOR_LIMITED:
|
case self::GENERATOR_LIMITED:
|
||||||
throw new UnsupportedWorldFormatException("Limited worlds are not currently supported");
|
throw new UnsupportedWorldFormatException("Limited worlds are not currently supported");
|
||||||
default:
|
default:
|
||||||
throw new UnsupportedWorldFormatException("Unknown LevelDB world format type, this world cannot be loaded");
|
throw new UnsupportedWorldFormatException("Unknown LevelDB generator type");
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
$this->compoundTag->setString("generatorName", "default");
|
$this->compoundTag->setString("generatorName", "default");
|
||||||
|
@ -24,11 +24,13 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\world\format\io\data;
|
namespace pocketmine\world\format\io\data;
|
||||||
|
|
||||||
use pocketmine\nbt\BigEndianNbtSerializer;
|
use pocketmine\nbt\BigEndianNbtSerializer;
|
||||||
|
use pocketmine\nbt\NbtDataException;
|
||||||
use pocketmine\nbt\tag\CompoundTag;
|
use pocketmine\nbt\tag\CompoundTag;
|
||||||
use pocketmine\nbt\tag\FloatTag;
|
use pocketmine\nbt\tag\FloatTag;
|
||||||
use pocketmine\nbt\tag\StringTag;
|
use pocketmine\nbt\tag\StringTag;
|
||||||
use pocketmine\nbt\TreeRoot;
|
use pocketmine\nbt\TreeRoot;
|
||||||
use pocketmine\utils\Utils;
|
use pocketmine\utils\Utils;
|
||||||
|
use pocketmine\world\format\io\exception\CorruptedWorldException;
|
||||||
use pocketmine\world\generator\Generator;
|
use pocketmine\world\generator\Generator;
|
||||||
use pocketmine\world\generator\GeneratorManager;
|
use pocketmine\world\generator\GeneratorManager;
|
||||||
use pocketmine\world\World;
|
use pocketmine\world\World;
|
||||||
@ -67,13 +69,18 @@ class JavaWorldData extends BaseNbtWorldData{
|
|||||||
file_put_contents($path . "level.dat", $buffer);
|
file_put_contents($path . "level.dat", $buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function load() : ?CompoundTag{
|
protected function load() : CompoundTag{
|
||||||
$nbt = new BigEndianNbtSerializer();
|
$nbt = new BigEndianNbtSerializer();
|
||||||
$worldData = $nbt->readCompressed(file_get_contents($this->dataPath))->getTag();
|
try{
|
||||||
if($worldData->hasTag("Data", CompoundTag::class)){
|
$worldData = $nbt->readCompressed(file_get_contents($this->dataPath))->getTag();
|
||||||
return $worldData->getCompoundTag("Data");
|
}catch(NbtDataException $e){
|
||||||
|
throw new CorruptedWorldException($e->getMessage(), 0, $e);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
if(!$worldData->hasTag("Data", CompoundTag::class)){
|
||||||
|
throw new CorruptedWorldException("Missing 'Data' key or wrong type");
|
||||||
|
}
|
||||||
|
return $worldData->getCompoundTag("Data");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function fix() : void{
|
protected function fix() : void{
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pocketmine\world\format\io\exception;
|
||||||
|
|
||||||
|
use pocketmine\world\WorldException;
|
||||||
|
|
||||||
|
class CorruptedWorldException extends WorldException{
|
||||||
|
|
||||||
|
}
|
@ -23,6 +23,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\world\format\io\exception;
|
namespace pocketmine\world\format\io\exception;
|
||||||
|
|
||||||
class UnsupportedWorldFormatException extends \RuntimeException{
|
use pocketmine\world\WorldException;
|
||||||
|
|
||||||
|
class UnsupportedWorldFormatException extends WorldException{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ use pocketmine\world\format\io\BaseWorldProvider;
|
|||||||
use pocketmine\world\format\io\ChunkUtils;
|
use pocketmine\world\format\io\ChunkUtils;
|
||||||
use pocketmine\world\format\io\data\BedrockWorldData;
|
use pocketmine\world\format\io\data\BedrockWorldData;
|
||||||
use pocketmine\world\format\io\exception\CorruptedChunkException;
|
use pocketmine\world\format\io\exception\CorruptedChunkException;
|
||||||
|
use pocketmine\world\format\io\exception\CorruptedWorldException;
|
||||||
use pocketmine\world\format\io\exception\UnsupportedChunkFormatException;
|
use pocketmine\world\format\io\exception\UnsupportedChunkFormatException;
|
||||||
use pocketmine\world\format\io\exception\UnsupportedWorldFormatException;
|
use pocketmine\world\format\io\exception\UnsupportedWorldFormatException;
|
||||||
use pocketmine\world\format\io\SubChunkConverter;
|
use pocketmine\world\format\io\SubChunkConverter;
|
||||||
@ -61,6 +62,7 @@ use function ord;
|
|||||||
use function str_repeat;
|
use function str_repeat;
|
||||||
use function strlen;
|
use function strlen;
|
||||||
use function substr;
|
use function substr;
|
||||||
|
use function trim;
|
||||||
use function unpack;
|
use function unpack;
|
||||||
use const LEVELDB_ZLIB_RAW_COMPRESSION;
|
use const LEVELDB_ZLIB_RAW_COMPRESSION;
|
||||||
|
|
||||||
@ -105,6 +107,12 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
*
|
||||||
|
* @return \LevelDB
|
||||||
|
* @throws \LevelDBException
|
||||||
|
*/
|
||||||
private static function createDB(string $path) : \LevelDB{
|
private static function createDB(string $path) : \LevelDB{
|
||||||
return new \LevelDB($path . "/db", [
|
return new \LevelDB($path . "/db", [
|
||||||
"compression" => LEVELDB_ZLIB_RAW_COMPRESSION
|
"compression" => LEVELDB_ZLIB_RAW_COMPRESSION
|
||||||
@ -115,7 +123,12 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
|
|||||||
self::checkForLevelDBExtension();
|
self::checkForLevelDBExtension();
|
||||||
parent::__construct($path);
|
parent::__construct($path);
|
||||||
|
|
||||||
$this->db = self::createDB($path);
|
try{
|
||||||
|
$this->db = self::createDB($path);
|
||||||
|
}catch(\LevelDBException $e){
|
||||||
|
//we can't tell the difference between errors caused by bad permissions and actual corruption :(
|
||||||
|
throw new CorruptedWorldException(trim($e->getMessage()), 0, $e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function loadLevelData() : WorldData{
|
protected function loadLevelData() : WorldData{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user