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