Move generator classpath hack out of BaseLevelProvider

This commit is contained in:
Dylan K. Taylor 2018-10-04 18:55:20 +01:00
parent 49e47edcf5
commit 0cc4bc48cc
4 changed files with 33 additions and 34 deletions

View File

@ -43,36 +43,6 @@ abstract class BaseLevelProvider implements LevelProvider{
abstract protected function loadLevelData() : LevelData;
/**
* Hack to fix worlds broken previously by older versions of PocketMine-MP which incorrectly saved classpaths of
* generators into level.dat on imported (not generated) worlds.
*
* This should only have affected leveldb worlds as far as I know, because PC format worlds include the
* generatorName tag by default. However, MCPE leveldb ones didn't, and so they would get filled in with something
* broken.
*
* This bug took a long time to get found because previously the generator manager would just return the default
* generator silently on failure to identify the correct generator, which caused lots of unexpected bugs.
*
* Only classnames which were written into the level.dat from "fixing" the level data are included here. These are
* hardcoded to avoid problems fixing broken worlds in the future if these classes get moved, renamed or removed.
*
* @param string $className Classname saved in level.dat
*
* @return null|string Name of the correct generator to replace the broken value
*/
public static function hackyFixForGeneratorClasspathInLevelDat(string $className) : ?string{
//THESE ARE DELIBERATELY HARDCODED, DO NOT CHANGE!
switch($className){
case 'pocketmine\level\generator\normal\Normal':
return "normal";
case 'pocketmine\level\generator\Flat':
return "flat";
}
return null;
}
public function getPath() : string{
return $this->path;
}

View File

@ -54,8 +54,39 @@ abstract class BaseNbtLevelData implements LevelData{
*/
abstract protected function load() : ?CompoundTag;
abstract protected function fix() : void;
/**
* Hack to fix worlds broken previously by older versions of PocketMine-MP which incorrectly saved classpaths of
* generators into level.dat on imported (not generated) worlds.
*
* This should only have affected leveldb worlds as far as I know, because PC format worlds include the
* generatorName tag by default. However, MCPE leveldb ones didn't, and so they would get filled in with something
* broken.
*
* This bug took a long time to get found because previously the generator manager would just return the default
* generator silently on failure to identify the correct generator, which caused lots of unexpected bugs.
*
* Only classnames which were written into the level.dat from "fixing" the level data are included here. These are
* hardcoded to avoid problems fixing broken worlds in the future if these classes get moved, renamed or removed.
*
* @param string $className Classname saved in level.dat
*
* @return null|string Name of the correct generator to replace the broken value
*/
protected static function hackyFixForGeneratorClasspathInLevelDat(string $className) : ?string{
//THESE ARE DELIBERATELY HARDCODED, DO NOT CHANGE!
switch($className){
case 'pocketmine\level\generator\normal\Normal':
return "normal";
case 'pocketmine\level\generator\Flat':
return "flat";
}
return null;
}
public function getCompoundTag() : CompoundTag{
return $this->compoundTag;
}

View File

@ -23,7 +23,6 @@ declare(strict_types=1);
namespace pocketmine\level\format\io\data;
use pocketmine\level\format\io\BaseLevelProvider;
use pocketmine\level\generator\Flat;
use pocketmine\level\generator\GeneratorManager;
use pocketmine\level\Level;
@ -133,7 +132,7 @@ class BedrockLevelData extends BaseNbtLevelData{
}else{
$this->compoundTag->setString("generatorName", "default");
}
}elseif(($generatorName = BaseLevelProvider::hackyFixForGeneratorClasspathInLevelDat($this->compoundTag->getString("generatorName"))) !== null){
}elseif(($generatorName = self::hackyFixForGeneratorClasspathInLevelDat($this->compoundTag->getString("generatorName"))) !== null){
$this->compoundTag->setString("generatorName", $generatorName);
}

View File

@ -23,7 +23,6 @@ declare(strict_types=1);
namespace pocketmine\level\format\io\data;
use pocketmine\level\format\io\BaseLevelProvider;
use pocketmine\level\generator\GeneratorManager;
use pocketmine\level\Level;
use pocketmine\nbt\BigEndianNBTStream;
@ -77,7 +76,7 @@ class JavaLevelData extends BaseNbtLevelData{
protected function fix() : void{
if(!$this->compoundTag->hasTag("generatorName", StringTag::class)){
$this->compoundTag->setString("generatorName", "default", true);
}elseif(($generatorName = BaseLevelProvider::hackyFixForGeneratorClasspathInLevelDat($this->compoundTag->getString("generatorName"))) !== null){
}elseif(($generatorName = self::hackyFixForGeneratorClasspathInLevelDat($this->compoundTag->getString("generatorName"))) !== null){
$this->compoundTag->setString("generatorName", $generatorName);
}