From a920baa2957fa6cca3391ed875c3bb5489ac9473 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 21 Jun 2020 18:47:27 +0100 Subject: [PATCH] resource packs: use JsonMapper for manifest parsing --- src/resourcepacks/ZippedResourcePack.php | 32 ++++------- src/resourcepacks/json/Manifest.php | 46 ++++++++++++++++ src/resourcepacks/json/ManifestHeader.php | 54 +++++++++++++++++++ .../json/ManifestModuleEntry.php | 51 ++++++++++++++++++ 4 files changed, 162 insertions(+), 21 deletions(-) create mode 100644 src/resourcepacks/json/Manifest.php create mode 100644 src/resourcepacks/json/ManifestHeader.php create mode 100644 src/resourcepacks/json/ManifestModuleEntry.php diff --git a/src/resourcepacks/ZippedResourcePack.php b/src/resourcepacks/ZippedResourcePack.php index 89b9a97a7..c9acd2a27 100644 --- a/src/resourcepacks/ZippedResourcePack.php +++ b/src/resourcepacks/ZippedResourcePack.php @@ -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"); } diff --git a/src/resourcepacks/json/Manifest.php b/src/resourcepacks/json/Manifest.php new file mode 100644 index 000000000..dee929a80 --- /dev/null +++ b/src/resourcepacks/json/Manifest.php @@ -0,0 +1,46 @@ +