PocketMine-MP 5.39.3 git-400eb2dddf91a9c112aa09f3b498ffc8c85e98ed
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 function assert;
31use function fclose;
32use function feof;
33use function file_exists;
34use function filesize;
35use function fopen;
36use function fread;
37use function fseek;
38use function gettype;
39use function hash_file;
40use function implode;
41use function preg_match;
42use function strlen;
43
45 protected string $path;
46 protected Manifest $manifest;
47 protected ?string $sha256 = null;
48
50 protected $fileResource;
51
56 public function __construct(string $zipPath){
57 $this->path = $zipPath;
58
59 if(!file_exists($zipPath)){
60 throw new ResourcePackException("File not found");
61 }
62 $size = filesize($zipPath);
63 if($size === false){
64 throw new ResourcePackException("Unable to determine size of file");
65 }
66 if($size === 0){
67 throw new ResourcePackException("Empty file, probably corrupted");
68 }
69
70 $archive = new \ZipArchive();
71 if(($openResult = $archive->open($zipPath)) !== true){
72 throw new ResourcePackException("Encountered ZipArchive error code $openResult while trying to open $zipPath");
73 }
74
75 if(($manifestData = $archive->getFromName("manifest.json")) === false){
76 $manifestPath = null;
77 $manifestIdx = null;
78 for($i = 0; $i < $archive->numFiles; ++$i){
79 $name = Utils::assumeNotFalse($archive->getNameIndex($i), "This index should be valid");
80 if(
81 ($manifestPath === null || strlen($name) < strlen($manifestPath)) &&
82 preg_match('#.*/manifest.json$#', $name) === 1
83 ){
84 $manifestPath = $name;
85 $manifestIdx = $i;
86 }
87 }
88 if($manifestIdx !== null){
89 $manifestData = $archive->getFromIndex($manifestIdx);
90 assert($manifestData !== false);
91 }elseif($archive->locateName("pack_manifest.json") !== false){
92 throw new ResourcePackException("Unsupported old pack format");
93 }else{
94 throw new ResourcePackException("manifest.json not found in the archive root");
95 }
96 }
97
98 $archive->close();
99
100 //maybe comments in the json, use stripped decoder (thanks mojang)
101 try{
102 $manifest = (new CommentedJsonDecoder())->decode($manifestData);
103 }catch(\RuntimeException $e){
104 throw new ResourcePackException("Failed to parse manifest.json: " . $e->getMessage(), 0, $e);
105 }
106 if(!($manifest instanceof \stdClass)){
107 throw new ResourcePackException("manifest.json should contain a JSON object, not " . gettype($manifest));
108 }
109
110 $mapper = new \JsonMapper();
111 $mapper->bExceptionOnMissingData = true;
112 $mapper->bStrictObjectTypeChecking = true;
113
114 try{
116 $manifest = $mapper->map($manifest, new Manifest());
117 }catch(\JsonMapper_Exception $e){
118 throw new ResourcePackException("Invalid manifest.json contents: " . $e->getMessage(), 0, $e);
119 }
120 if(!Uuid::isValid($manifest->header->uuid)){
121 throw new ResourcePackException("Resource pack has an invalid UUID");
122 }
123
124 $this->manifest = $manifest;
125
126 $this->fileResource = fopen($zipPath, "rb");
127 }
128
129 public function __destruct(){
130 fclose($this->fileResource);
131 }
132
133 public function getPath() : string{
134 return $this->path;
135 }
136
137 public function getPackName() : string{
138 return $this->manifest->header->name;
139 }
140
141 public function getPackVersion() : string{
142 return implode(".", $this->manifest->header->version);
143 }
144
145 public function getPackId() : string{
146 return $this->manifest->header->uuid;
147 }
148
149 public function getPackSize() : int{
150 return filesize($this->path);
151 }
152
153 public function getSha256(bool $cached = true) : string{
154 if($this->sha256 === null || !$cached){
155 $this->sha256 = hash_file("sha256", $this->path, true);
156 }
157 return $this->sha256;
158 }
159
160 public function getPackChunk(int $start, int $length) : string{
161 if($length < 1){
162 throw new \InvalidArgumentException("Pack length must be positive");
163 }
164 fseek($this->fileResource, $start);
165 if(feof($this->fileResource)){
166 throw new \InvalidArgumentException("Requested a resource pack chunk with invalid start offset");
167 }
168 return Utils::assumeNotFalse(fread($this->fileResource, $length), "Already checked that we're not at EOF");
169 }
170}