mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-14 22:01:59 +00:00
improve reusability
This commit is contained in:
parent
db9d769db6
commit
8a11ed70e3
@ -38,6 +38,7 @@ use function array_map;
|
|||||||
use function count;
|
use function count;
|
||||||
use function file_get_contents;
|
use function file_get_contents;
|
||||||
use function get_class;
|
use function get_class;
|
||||||
|
use function gettype;
|
||||||
use function implode;
|
use function implode;
|
||||||
use function is_int;
|
use function is_int;
|
||||||
use function is_object;
|
use function is_object;
|
||||||
@ -275,7 +276,6 @@ final class BlockStateUpgradeSchemaUtils{
|
|||||||
|
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
$jsonMapper = new \JsonMapper();
|
|
||||||
/** @var string[] $matches */
|
/** @var string[] $matches */
|
||||||
foreach($iterator as $matches){
|
foreach($iterator as $matches){
|
||||||
$filename = $matches[0];
|
$filename = $matches[0];
|
||||||
@ -283,16 +283,18 @@ final class BlockStateUpgradeSchemaUtils{
|
|||||||
|
|
||||||
$fullPath = Path::join($path, $filename);
|
$fullPath = Path::join($path, $filename);
|
||||||
|
|
||||||
//TODO: should we bother handling exceptions in here?
|
try{
|
||||||
$raw = ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents($fullPath));
|
$raw = ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents($fullPath));
|
||||||
|
}catch(\ErrorException $e){
|
||||||
$json = json_decode($raw, false, flags: JSON_THROW_ON_ERROR);
|
throw new \RuntimeException("Loading schema file $fullPath: " . $e->getMessage(), 0, $e);
|
||||||
if(!is_object($json)){
|
}
|
||||||
throw new \RuntimeException("Unexpected root type of schema file $fullPath");
|
|
||||||
|
try{
|
||||||
|
$schema = self::loadSchemaFromString($raw);
|
||||||
|
}catch(\RuntimeException $e){
|
||||||
|
throw new \RuntimeException("Loading schema file $fullPath: " . $e->getMessage(), 0, $e);
|
||||||
}
|
}
|
||||||
$model = $jsonMapper->map($json, new BlockStateUpgradeSchemaModel());
|
|
||||||
|
|
||||||
$schema = self::fromJsonModel($model);
|
|
||||||
if($schema->getVersionId() > $currentVersion){
|
if($schema->getVersionId() > $currentVersion){
|
||||||
//this might be a beta schema which shouldn't be applicable
|
//this might be a beta schema which shouldn't be applicable
|
||||||
//TODO: why do we load the whole schema just to throw it away if it's too new? ...
|
//TODO: why do we load the whole schema just to throw it away if it's too new? ...
|
||||||
@ -305,4 +307,24 @@ final class BlockStateUpgradeSchemaUtils{
|
|||||||
ksort($result, SORT_NUMERIC);
|
ksort($result, SORT_NUMERIC);
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function loadSchemaFromString(string $raw) : BlockStateUpgradeSchema{
|
||||||
|
try{
|
||||||
|
$json = json_decode($raw, false, flags: JSON_THROW_ON_ERROR);
|
||||||
|
}catch(\JsonException $e){
|
||||||
|
throw new \RuntimeException($e->getMessage(), 0, $e);
|
||||||
|
}
|
||||||
|
if(!is_object($json)){
|
||||||
|
throw new \RuntimeException("Unexpected root type of schema file " . gettype($json) . ", expected object");
|
||||||
|
}
|
||||||
|
|
||||||
|
$jsonMapper = new \JsonMapper();
|
||||||
|
try{
|
||||||
|
$model = $jsonMapper->map($json, new BlockStateUpgradeSchemaModel());
|
||||||
|
}catch(\JsonMapper_Exception $e){
|
||||||
|
throw new \RuntimeException($e->getMessage(), 0, $e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::fromJsonModel($model);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user