Crash the generator when encountering invalid presets

this is a partial fix for #2717, but still not ideal because it'll spam whenever a chunk is attempted to be generated. However, fixing this properly requires potentially breaking API changes.
This commit is contained in:
Dylan K. Taylor 2019-01-30 12:29:04 +00:00
parent eebd90ec42
commit 331ae5498f
6 changed files with 73 additions and 5 deletions

View File

@ -382,6 +382,7 @@ class Level implements ChunkManager, Metadatable{
$this->server->getLogger()->info($this->server->getLanguage()->translateString("pocketmine.level.preparing", [$this->displayName]));
$this->generator = GeneratorManager::getGenerator($this->provider->getGenerator());
//TODO: validate generator options
$this->folderName = $name;

View File

@ -33,10 +33,11 @@ use pocketmine\level\generator\populator\Ore;
use pocketmine\level\generator\populator\Populator;
use pocketmine\math\Vector3;
use pocketmine\utils\Random;
use function array_map;
use function count;
use function explode;
use function preg_match;
use function preg_match_all;
use function str_replace;
class Flat extends Generator{
/** @var Chunk */
@ -62,6 +63,11 @@ class Flat extends Generator{
return "flat";
}
/**
* @param array $options
*
* @throws InvalidGeneratorOptionsException
*/
public function __construct(array $options = []){
$this->options = $options;
if(isset($this->options["preset"]) and $this->options["preset"] != ""){
@ -89,13 +95,28 @@ class Flat extends Generator{
}
}
/**
* @param string $layers
*
* @return int[][]
* @throws InvalidGeneratorOptionsException
*/
public static function parseLayers(string $layers) : array{
$result = [];
preg_match_all('#^(([0-9]*x|)([0-9]{1,3})(|:[0-9]{0,2}))$#m', str_replace(",", "\n", $layers), $matches);
$split = array_map('\trim', explode(',', $layers));
$y = 0;
foreach($matches[3] as $i => $b){
$b = ItemFactory::fromString($b . $matches[4][$i]);
$cnt = $matches[2][$i] === "" ? 1 : (int) $matches[2][$i];
foreach($split as $line){
preg_match('#^(?:(\d+)x)?(.+)$#', $line, $matches);
if(count($matches) !== 3){
throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\"");
}
$cnt = $matches[1] !== "" ? (int) $matches[1] : 1;
try{
$b = ItemFactory::fromString($matches[2])->getBlock();
}catch(\InvalidArgumentException $e){
throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\": " . $e->getMessage(), 0, $e);
}
for($cY = $y, $y += $cnt; $cY < $y; ++$cY){
$result[$cY] = [$b->getId(), $b->getDamage()];
}
@ -113,6 +134,7 @@ class Flat extends Generator{
$this->floorLevel = count($this->structure);
//TODO: more error checking
preg_match_all('#(([0-9a-z_]{1,})\(?([0-9a-z_ =:]{0,})\)?),?#', $options, $matches);
foreach($matches[2] as $i => $option){
$params = true;

View File

@ -58,6 +58,11 @@ abstract class Generator{
/** @var Random */
protected $random;
/**
* @param array $settings
*
* @throws InvalidGeneratorOptionsException
*/
abstract public function __construct(array $settings = []);

View File

@ -0,0 +1,28 @@
<?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\level\generator;
class InvalidGeneratorOptionsException extends \UnexpectedValueException{
}

View File

@ -27,6 +27,7 @@ use pocketmine\block\Block;
use pocketmine\level\biome\Biome;
use pocketmine\level\ChunkManager;
use pocketmine\level\generator\Generator;
use pocketmine\level\generator\InvalidGeneratorOptionsException;
use pocketmine\level\generator\noise\Simplex;
use pocketmine\level\generator\populator\Populator;
use pocketmine\math\Vector3;
@ -51,6 +52,11 @@ class Nether extends Generator{
/** @var Simplex */
private $noiseBase;
/**
* @param array $options
*
* @throws InvalidGeneratorOptionsException
*/
public function __construct(array $options = []){
}

View File

@ -29,6 +29,7 @@ use pocketmine\level\biome\Biome;
use pocketmine\level\ChunkManager;
use pocketmine\level\generator\biome\BiomeSelector;
use pocketmine\level\generator\Generator;
use pocketmine\level\generator\InvalidGeneratorOptionsException;
use pocketmine\level\generator\noise\Simplex;
use pocketmine\level\generator\object\OreType;
use pocketmine\level\generator\populator\GroundCover;
@ -57,6 +58,11 @@ class Normal extends Generator{
private static $GAUSSIAN_KERNEL = null;
private static $SMOOTH_SIZE = 2;
/**
* @param array $options
*
* @throws InvalidGeneratorOptionsException
*/
public function __construct(array $options = []){
if(self::$GAUSSIAN_KERNEL === null){
self::generateKernel();