mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-15 16:05:28 +00:00
resource packs: use JsonMapper for manifest parsing
This commit is contained in:
parent
1ef6e5e17b
commit
a920baa295
@ -24,8 +24,8 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\resourcepacks;
|
namespace pocketmine\resourcepacks;
|
||||||
|
|
||||||
use Ahc\Json\Comment as CommentedJsonDecoder;
|
use Ahc\Json\Comment as CommentedJsonDecoder;
|
||||||
|
use pocketmine\resourcepacks\json\Manifest;
|
||||||
use function assert;
|
use function assert;
|
||||||
use function count;
|
|
||||||
use function fclose;
|
use function fclose;
|
||||||
use function feof;
|
use function feof;
|
||||||
use function file_exists;
|
use function file_exists;
|
||||||
@ -41,28 +41,10 @@ use function strlen;
|
|||||||
|
|
||||||
class ZippedResourcePack implements ResourcePack{
|
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 */
|
/** @var string */
|
||||||
protected $path;
|
protected $path;
|
||||||
|
|
||||||
/** @var \stdClass */
|
/** @var Manifest */
|
||||||
protected $manifest;
|
protected $manifest;
|
||||||
|
|
||||||
/** @var string|null */
|
/** @var string|null */
|
||||||
@ -121,7 +103,15 @@ class ZippedResourcePack implements ResourcePack{
|
|||||||
if(!($manifest instanceof \stdClass)){
|
if(!($manifest instanceof \stdClass)){
|
||||||
throw new ResourcePackException("manifest.json should contain a JSON object, not " . gettype($manifest));
|
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");
|
throw new ResourcePackException("manifest.json is missing required fields");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
46
src/resourcepacks/json/Manifest.php
Normal file
46
src/resourcepacks/json/Manifest.php
Normal 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;
|
||||||
|
}
|
54
src/resourcepacks/json/ManifestHeader.php
Normal file
54
src/resourcepacks/json/ManifestHeader.php
Normal 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;
|
||||||
|
}
|
51
src/resourcepacks/json/ManifestModuleEntry.php
Normal file
51
src/resourcepacks/json/ManifestModuleEntry.php
Normal 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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user