PocketMine-MP 5.39.3 git-9a46a8bd745880ddf8eebaf28cda326bb97d2efa
Loading...
Searching...
No Matches
ZippedResourcePack.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\resourcepacks;
25
26use Ahc\Json\Comment as CommentedJsonDecoder;
29use Ramsey\Uuid\Uuid;
30use Ramsey\Uuid\UuidInterface;
31use function assert;
32use function fclose;
33use function feof;
34use function file_exists;
35use function filesize;
36use function fopen;
37use function fread;
38use function fseek;
39use function gettype;
40use function hash_file;
41use function implode;
42use function preg_match;
43use function strlen;
44
46 protected string $path;
47 protected Manifest $manifest;
48 protected ?string $sha256 = null;
49
51 protected $fileResource;
52
53 private UuidInterface $uuid;
54
59 public function __construct(string $zipPath){
60 $this->path = $zipPath;
61
62 if(!file_exists($zipPath)){
63 throw new ResourcePackException("File not found");
64 }
65 $size = filesize($zipPath);
66 if($size === false){
67 throw new ResourcePackException("Unable to determine size of file");
68 }
69 if($size === 0){
70 throw new ResourcePackException("Empty file, probably corrupted");
71 }
72
73 $archive = new \ZipArchive();
74 if(($openResult = $archive->open($zipPath)) !== true){
75 throw new ResourcePackException("Encountered ZipArchive error code $openResult while trying to open $zipPath");
76 }
77
78 if(($manifestData = $archive->getFromName("manifest.json")) === false){
79 $manifestPath = null;
80 $manifestIdx = null;
81 for($i = 0; $i < $archive->numFiles; ++$i){
82 $name = Utils::assumeNotFalse($archive->getNameIndex($i), "This index should be valid");
83 if(
84 ($manifestPath === null || strlen($name) < strlen($manifestPath)) &&
85 preg_match('#.*/manifest.json$#', $name) === 1
86 ){
87 $manifestPath = $name;
88 $manifestIdx = $i;
89 }
90 }
91 if($manifestIdx !== null){
92 $manifestData = $archive->getFromIndex($manifestIdx);
93 assert($manifestData !== false);
94 }elseif($archive->locateName("pack_manifest.json") !== false){
95 throw new ResourcePackException("Unsupported old pack format");
96 }else{
97 throw new ResourcePackException("manifest.json not found in the archive root");
98 }
99 }
100
101 $archive->close();
102
103 //maybe comments in the json, use stripped decoder (thanks mojang)
104 try{
105 $manifest = (new CommentedJsonDecoder())->decode($manifestData);
106 }catch(\RuntimeException $e){
107 throw new ResourcePackException("Failed to parse manifest.json: " . $e->getMessage(), 0, $e);
108 }
109 if(!($manifest instanceof \stdClass)){
110 throw new ResourcePackException("manifest.json should contain a JSON object, not " . gettype($manifest));
111 }
112
113 $mapper = new \JsonMapper();
114 $mapper->bExceptionOnMissingData = true;
115 $mapper->bStrictObjectTypeChecking = true;
116
117 try{
119 $manifest = $mapper->map($manifest, new Manifest());
120 }catch(\JsonMapper_Exception $e){
121 throw new ResourcePackException("Invalid manifest.json contents: " . $e->getMessage(), 0, $e);
122 }
123 if(!Uuid::isValid($manifest->header->uuid)){
124 throw new ResourcePackException("Resource pack has an invalid UUID");
125 }
126 $this->uuid = Uuid::fromString($manifest->header->uuid);
127
128 $this->manifest = $manifest;
129
130 $this->fileResource = fopen($zipPath, "rb");
131 }
132
133 public function __destruct(){
134 fclose($this->fileResource);
135 }
136
137 public function getPath() : string{
138 return $this->path;
139 }
140
141 public function getPackName() : string{
142 return $this->manifest->header->name;
143 }
144
145 public function getPackVersion() : string{
146 return implode(".", $this->manifest->header->version);
147 }
148
149 public function getPackId() : UuidInterface{
150 return $this->uuid;
151 }
152
153 public function getPackSize() : int{
154 return filesize($this->path);
155 }
156
157 public function getSha256(bool $cached = true) : string{
158 if($this->sha256 === null || !$cached){
159 $this->sha256 = hash_file("sha256", $this->path, true);
160 }
161 return $this->sha256;
162 }
163
164 public function getPackChunk(int $start, int $length) : string{
165 if($length < 1){
166 throw new \InvalidArgumentException("Pack length must be positive");
167 }
168 fseek($this->fileResource, $start);
169 if(feof($this->fileResource)){
170 throw new \InvalidArgumentException("Requested a resource pack chunk with invalid start offset");
171 }
172 return Utils::assumeNotFalse(fread($this->fileResource, $length), "Already checked that we're not at EOF");
173 }
174}