22declare(strict_types=1);
24namespace pocketmine\resourcepacks;
26use Ahc\Json\Comment as CommentedJsonDecoder;
32use
function file_exists;
38use
function hash_file;
40use
function preg_match;
44 protected string $path;
46 protected ?
string $sha256 =
null;
49 protected $fileResource;
56 $this->path = $zipPath;
58 if(!file_exists($zipPath)){
61 $size = filesize($zipPath);
69 $archive = new \ZipArchive();
70 if(($openResult = $archive->open($zipPath)) !==
true){
71 throw new ResourcePackException(
"Encountered ZipArchive error code $openResult while trying to open $zipPath");
74 if(($manifestData = $archive->getFromName(
"manifest.json")) ===
false){
77 for($i = 0; $i < $archive->numFiles; ++$i){
80 ($manifestPath ===
null || strlen($name) < strlen($manifestPath)) &&
81 preg_match(
'#.*/manifest.json$#', $name) === 1
83 $manifestPath = $name;
87 if($manifestIdx !==
null){
88 $manifestData = $archive->getFromIndex($manifestIdx);
89 assert($manifestData !==
false);
90 }elseif($archive->locateName(
"pack_manifest.json") !==
false){
101 $manifest = (
new CommentedJsonDecoder())->decode($manifestData);
102 }
catch(\RuntimeException $e){
103 throw new ResourcePackException(
"Failed to parse manifest.json: " . $e->getMessage(), $e->getCode(), $e);
105 if(!($manifest instanceof \stdClass)){
106 throw new ResourcePackException(
"manifest.json should contain a JSON object, not " . gettype($manifest));
109 $mapper = new \JsonMapper();
110 $mapper->bExceptionOnMissingData =
true;
111 $mapper->bStrictObjectTypeChecking =
true;
115 $manifest = $mapper->map($manifest,
new Manifest());
120 $this->manifest = $manifest;
122 $this->fileResource = fopen($zipPath,
"rb");
125 public function __destruct(){
126 fclose($this->fileResource);
129 public function getPath() : string{
134 return $this->manifest->header->name;
138 return implode(
".", $this->manifest->header->version);
142 return $this->manifest->header->uuid;
146 return filesize($this->path);
149 public function getSha256(
bool $cached =
true) : string{
150 if($this->sha256 === null || !$cached){
151 $this->sha256 = hash_file(
"sha256", $this->path,
true);
153 return $this->sha256;
157 fseek($this->fileResource, $start);
158 if(feof($this->fileResource)){
159 throw new \InvalidArgumentException(
"Requested a resource pack chunk with invalid start offset");
161 return Utils::assumeNotFalse(fread($this->fileResource, $length),
"Already checked that we're not at EOF");
__construct(string $zipPath)
getPackChunk(int $start, int $length)
static assumeNotFalse(mixed $value, \Closure|string $context="This should never be false")