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){
78 $name = Utils::assumeNotFalse($archive->getNameIndex($i),
"This index should be valid");
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());
116 }
catch(\JsonMapper_Exception $e){
120 $this->manifest = $manifest;
122 $this->fileResource = fopen($zipPath,
"rb");
158 throw new \InvalidArgumentException(
"Pack length must be positive");
160 fseek($this->fileResource, $start);
161 if(feof($this->fileResource)){
162 throw new \InvalidArgumentException(
"Requested a resource pack chunk with invalid start offset");
164 return Utils::assumeNotFalse(fread($this->fileResource, $length),
"Already checked that we're not at EOF");