mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 09:56:06 +00:00
Removed pocketmine subdirectory, map PSR-4 style
This commit is contained in:
79
src/resourcepacks/ResourcePack.php
Normal file
79
src/resourcepacks/ResourcePack.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?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;
|
||||
|
||||
|
||||
interface ResourcePack{
|
||||
|
||||
/**
|
||||
* Returns the path to the resource pack. This might be a file or a directory, depending on the type of pack.
|
||||
* @return string
|
||||
*/
|
||||
public function getPath() : string;
|
||||
|
||||
/**
|
||||
* Returns the human-readable name of the resource pack
|
||||
* @return string
|
||||
*/
|
||||
public function getPackName() : string;
|
||||
|
||||
/**
|
||||
* Returns the pack's UUID as a human-readable string
|
||||
* @return string
|
||||
*/
|
||||
public function getPackId() : string;
|
||||
|
||||
/**
|
||||
* Returns the size of the pack on disk in bytes.
|
||||
* @return int
|
||||
*/
|
||||
public function getPackSize() : int;
|
||||
|
||||
/**
|
||||
* Returns a version number for the pack in the format major.minor.patch
|
||||
* @return string
|
||||
*/
|
||||
public function getPackVersion() : string;
|
||||
|
||||
/**
|
||||
* Returns the raw SHA256 sum of the compressed resource pack zip. This is used by clients to validate pack downloads.
|
||||
* @return string byte-array length 32 bytes
|
||||
*/
|
||||
public function getSha256() : string;
|
||||
|
||||
/**
|
||||
* Returns a chunk of the resource pack zip as a byte-array for sending to clients.
|
||||
*
|
||||
* Note that resource packs must **always** be in zip archive format for sending.
|
||||
* A folder resource loader may need to perform on-the-fly compression for this purpose.
|
||||
*
|
||||
* @param int $start Offset to start reading the chunk from
|
||||
* @param int $length Maximum length of data to return.
|
||||
*
|
||||
* @return string byte-array
|
||||
* @throws \InvalidArgumentException if the chunk does not exist
|
||||
*/
|
||||
public function getPackChunk(int $start, int $length) : string;
|
||||
}
|
28
src/resourcepacks/ResourcePackException.php
Normal file
28
src/resourcepacks/ResourcePackException.php
Normal 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\resourcepacks;
|
||||
|
||||
class ResourcePackException extends \RuntimeException{
|
||||
|
||||
}
|
48
src/resourcepacks/ResourcePackInfoEntry.php
Normal file
48
src/resourcepacks/ResourcePackInfoEntry.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?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;
|
||||
|
||||
class ResourcePackInfoEntry{
|
||||
protected $packId; //UUID
|
||||
protected $version;
|
||||
protected $packSize;
|
||||
|
||||
public function __construct(string $packId, string $version, int $packSize = 0){
|
||||
$this->packId = $packId;
|
||||
$this->version = $version;
|
||||
$this->packSize = $packSize;
|
||||
}
|
||||
|
||||
public function getPackId() : string{
|
||||
return $this->packId;
|
||||
}
|
||||
|
||||
public function getVersion() : string{
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
public function getPackSize() : int{
|
||||
return $this->packSize;
|
||||
}
|
||||
}
|
165
src/resourcepacks/ResourcePackManager.php
Normal file
165
src/resourcepacks/ResourcePackManager.php
Normal file
@ -0,0 +1,165 @@
|
||||
<?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;
|
||||
|
||||
use pocketmine\utils\Config;
|
||||
use function array_keys;
|
||||
use function copy;
|
||||
use function count;
|
||||
use function file_exists;
|
||||
use function gettype;
|
||||
use function is_array;
|
||||
use function is_dir;
|
||||
use function mkdir;
|
||||
use function strtolower;
|
||||
use const DIRECTORY_SEPARATOR;
|
||||
|
||||
class ResourcePackManager{
|
||||
|
||||
/** @var string */
|
||||
private $path;
|
||||
|
||||
/** @var bool */
|
||||
private $serverForceResources = false;
|
||||
|
||||
/** @var ResourcePack[] */
|
||||
private $resourcePacks = [];
|
||||
|
||||
/** @var ResourcePack[] */
|
||||
private $uuidList = [];
|
||||
|
||||
/**
|
||||
* @param string $path Path to resource-packs directory.
|
||||
* @param \Logger $logger
|
||||
*/
|
||||
public function __construct(string $path, \Logger $logger){
|
||||
$this->path = $path;
|
||||
|
||||
if(!file_exists($this->path)){
|
||||
$logger->debug("Resource packs path $path does not exist, creating directory");
|
||||
mkdir($this->path);
|
||||
}elseif(!is_dir($this->path)){
|
||||
throw new \InvalidArgumentException("Resource packs path $path exists and is not a directory");
|
||||
}
|
||||
|
||||
if(!file_exists($this->path . "resource_packs.yml")){
|
||||
copy(\pocketmine\RESOURCE_PATH . "resource_packs.yml", $this->path . "resource_packs.yml");
|
||||
}
|
||||
|
||||
$resourcePacksConfig = new Config($this->path . "resource_packs.yml", Config::YAML, []);
|
||||
|
||||
$this->serverForceResources = (bool) $resourcePacksConfig->get("force_resources", false);
|
||||
|
||||
$logger->info("Loading resource packs...");
|
||||
|
||||
$resourceStack = $resourcePacksConfig->get("resource_stack", []);
|
||||
if(!is_array($resourceStack)){
|
||||
throw new \InvalidArgumentException("\"resource_stack\" key should contain a list of pack names");
|
||||
}
|
||||
|
||||
foreach($resourceStack as $pos => $pack){
|
||||
try{
|
||||
$pack = (string) $pack;
|
||||
}catch(\ErrorException $e){
|
||||
$logger->critical("Found invalid entry in resource pack list at offset $pos of type " . gettype($pack));
|
||||
continue;
|
||||
}
|
||||
try{
|
||||
/** @var string $pack */
|
||||
$packPath = $this->path . DIRECTORY_SEPARATOR . $pack;
|
||||
if(!file_exists($packPath)){
|
||||
throw new ResourcePackException("File or directory not found");
|
||||
}
|
||||
if(is_dir($packPath)){
|
||||
throw new ResourcePackException("Directory resource packs are unsupported");
|
||||
}
|
||||
|
||||
$newPack = null;
|
||||
//Detect the type of resource pack.
|
||||
$info = new \SplFileInfo($packPath);
|
||||
switch($info->getExtension()){
|
||||
case "zip":
|
||||
case "mcpack":
|
||||
$newPack = new ZippedResourcePack($packPath);
|
||||
break;
|
||||
}
|
||||
|
||||
if($newPack instanceof ResourcePack){
|
||||
$this->resourcePacks[] = $newPack;
|
||||
$this->uuidList[strtolower($newPack->getPackId())] = $newPack;
|
||||
}else{
|
||||
throw new ResourcePackException("Format not recognized");
|
||||
}
|
||||
}catch(ResourcePackException $e){
|
||||
$logger->critical("Could not load resource pack \"$pack\": " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
$logger->debug("Successfully loaded " . count($this->resourcePacks) . " resource packs");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the directory which resource packs are loaded from.
|
||||
* @return string
|
||||
*/
|
||||
public function getPath() : string{
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether players must accept resource packs in order to join.
|
||||
* @return bool
|
||||
*/
|
||||
public function resourcePacksRequired() : bool{
|
||||
return $this->serverForceResources;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of resource packs in use, sorted in order of priority.
|
||||
* @return ResourcePack[]
|
||||
*/
|
||||
public function getResourceStack() : array{
|
||||
return $this->resourcePacks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the resource pack matching the specified UUID string, or null if the ID was not recognized.
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return ResourcePack|null
|
||||
*/
|
||||
public function getPackById(string $id) : ?ResourcePack{
|
||||
return $this->uuidList[strtolower($id)] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of pack IDs for packs currently in use.
|
||||
* @return string[]
|
||||
*/
|
||||
public function getPackIdList() : array{
|
||||
return array_keys($this->uuidList);
|
||||
}
|
||||
}
|
156
src/resourcepacks/ZippedResourcePack.php
Normal file
156
src/resourcepacks/ZippedResourcePack.php
Normal file
@ -0,0 +1,156 @@
|
||||
<?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;
|
||||
|
||||
|
||||
use Ahc\Json\Comment as CommentedJsonDecoder;
|
||||
use function count;
|
||||
use function fclose;
|
||||
use function feof;
|
||||
use function file_exists;
|
||||
use function filesize;
|
||||
use function fopen;
|
||||
use function fread;
|
||||
use function fseek;
|
||||
use function hash_file;
|
||||
use function implode;
|
||||
|
||||
class ZippedResourcePack implements ResourcePack{
|
||||
|
||||
/**
|
||||
* Performs basic validation checks on a resource pack's manifest.json.
|
||||
* TODO: add more manifest validation
|
||||
*
|
||||
* @param \stdClass $manifest
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
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 */
|
||||
protected $manifest;
|
||||
|
||||
/** @var string */
|
||||
protected $sha256 = null;
|
||||
|
||||
/** @var resource */
|
||||
protected $fileResource;
|
||||
|
||||
/**
|
||||
* @param string $zipPath Path to the resource pack zip
|
||||
* @throws ResourcePackException
|
||||
*/
|
||||
public function __construct(string $zipPath){
|
||||
$this->path = $zipPath;
|
||||
|
||||
if(!file_exists($zipPath)){
|
||||
throw new ResourcePackException("File not found");
|
||||
}
|
||||
|
||||
$archive = new \ZipArchive();
|
||||
if(($openResult = $archive->open($zipPath)) !== true){
|
||||
throw new ResourcePackException("Encountered ZipArchive error code $openResult while trying to open $zipPath");
|
||||
}
|
||||
|
||||
if(($manifestData = $archive->getFromName("manifest.json")) === false){
|
||||
if($archive->locateName("pack_manifest.json") !== false){
|
||||
throw new ResourcePackException("Unsupported old pack format");
|
||||
}else{
|
||||
throw new ResourcePackException("manifest.json not found in the archive root");
|
||||
}
|
||||
}
|
||||
|
||||
$archive->close();
|
||||
|
||||
//maybe comments in the json, use stripped decoder (thanks mojang)
|
||||
try{
|
||||
$manifest = (new CommentedJsonDecoder())->decode($manifestData);
|
||||
}catch(\RuntimeException $e){
|
||||
throw new ResourcePackException("Failed to parse manifest.json: " . $e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
if(!self::verifyManifest($manifest)){
|
||||
throw new ResourcePackException("manifest.json is missing required fields");
|
||||
}
|
||||
|
||||
$this->manifest = $manifest;
|
||||
|
||||
$this->fileResource = fopen($zipPath, "rb");
|
||||
}
|
||||
|
||||
public function __destruct(){
|
||||
fclose($this->fileResource);
|
||||
}
|
||||
|
||||
public function getPath() : string{
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
public function getPackName() : string{
|
||||
return $this->manifest->header->name;
|
||||
}
|
||||
|
||||
public function getPackVersion() : string{
|
||||
return implode(".", $this->manifest->header->version);
|
||||
}
|
||||
|
||||
public function getPackId() : string{
|
||||
return $this->manifest->header->uuid;
|
||||
}
|
||||
|
||||
public function getPackSize() : int{
|
||||
return filesize($this->path);
|
||||
}
|
||||
|
||||
public function getSha256(bool $cached = true) : string{
|
||||
if($this->sha256 === null or !$cached){
|
||||
$this->sha256 = hash_file("sha256", $this->path, true);
|
||||
}
|
||||
return $this->sha256;
|
||||
}
|
||||
|
||||
public function getPackChunk(int $start, int $length) : string{
|
||||
fseek($this->fileResource, $start);
|
||||
if(feof($this->fileResource)){
|
||||
throw new \InvalidArgumentException("Requested a resource pack chunk with invalid start offset");
|
||||
}
|
||||
return fread($this->fileResource, $length);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user