resource packs: use JsonMapper for manifest parsing

This commit is contained in:
Dylan K. Taylor 2020-06-21 18:47:27 +01:00
parent 1ef6e5e17b
commit a920baa295
4 changed files with 162 additions and 21 deletions

View File

@ -24,8 +24,8 @@ declare(strict_types=1);
namespace pocketmine\resourcepacks;
use Ahc\Json\Comment as CommentedJsonDecoder;
use pocketmine\resourcepacks\json\Manifest;
use function assert;
use function count;
use function fclose;
use function feof;
use function file_exists;
@ -41,28 +41,10 @@ use function strlen;
class ZippedResourcePack implements ResourcePack{
/**
* Performs basic validation checks on a resource pack's manifest.json.
* TODO: add more manifest validation
*/
public static function verifyManifest(\stdClass $manifest) : bool{
if(!isset($manifest->format_version) or !isset($manifest->header) or !isset($manifest->modules)){
return false;
}
//Right now we don't care about anything else, only the stuff we're sending to clients.
return
isset($manifest->header->description) and
isset($manifest->header->name) and
isset($manifest->header->uuid) and
isset($manifest->header->version) and
count($manifest->header->version) === 3;
}
/** @var string */
protected $path;
/** @var \stdClass */
/** @var Manifest */
protected $manifest;
/** @var string|null */
@ -121,7 +103,15 @@ class ZippedResourcePack implements ResourcePack{
if(!($manifest instanceof \stdClass)){
throw new ResourcePackException("manifest.json should contain a JSON object, not " . gettype($manifest));
}
if(!self::verifyManifest($manifest)){
$mapper = new \JsonMapper();
$mapper->bExceptionOnUndefinedProperty = true;
$mapper->bExceptionOnMissingData = true;
try{
/** @var Manifest $manifest */
$manifest = $mapper->map($manifest, new Manifest());
}catch(\JsonMapper_Exception $e){
throw new ResourcePackException("manifest.json is missing required fields");
}

View File

@ -0,0 +1,46 @@
<?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\resourcepacks\json;
/**
* Model for JsonMapper to represent resource pack manifest.json contents.
*/
final class Manifest{
/**
* @var int
* @required
*/
public $format_version;
/**
* @var ManifestHeader
* @required
*/
public $header;
/**
* @var ManifestModuleEntry[]
*/
public $modules;
}

View File

@ -0,0 +1,54 @@
<?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\resourcepacks\json;
final class ManifestHeader{
/** @var string */
public $description;
/**
* @var string
* @required
*/
public $name;
/**
* @var string
* @required
*/
public $uuid;
/**
* @var int[]
* @phpstan-var array{int, int, int}
* @required
*/
public $version;
/**
* @var int[]
* @phpstan-var array{int, int, int}
*/
public $min_engine_version;
}

View File

@ -0,0 +1,51 @@
<?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\resourcepacks\json;
final class ManifestModuleEntry{
/**
* @var string
*/
public $description;
/**
* @var string
* @required
*/
public $type;
/**
* @var string
* @required
*/
public $uuid;
/**
* @var int[]
* @phpstan-var array{int, int, int}
* @required
*/
public $version;
}