60 $this->path = $zipPath;
62 if(!file_exists($zipPath)){
65 $size = filesize($zipPath);
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");
78 if(($manifestData = $archive->getFromName(
"manifest.json")) ===
false){
81 for($i = 0; $i < $archive->numFiles; ++$i){
82 $name = Utils::assumeNotFalse($archive->getNameIndex($i),
"This index should be valid");
84 ($manifestPath ===
null || strlen($name) < strlen($manifestPath)) &&
85 preg_match(
'#.*/manifest.json$#', $name) === 1
87 $manifestPath = $name;
91 if($manifestIdx !==
null){
92 $manifestData = $archive->getFromIndex($manifestIdx);
93 assert($manifestData !==
false);
94 }elseif($archive->locateName(
"pack_manifest.json") !==
false){
105 $manifest = (
new CommentedJsonDecoder())->decode($manifestData);
106 }
catch(\RuntimeException $e){
109 if(!($manifest instanceof \stdClass)){
110 throw new ResourcePackException(
"manifest.json should contain a JSON object, not " . gettype($manifest));
113 $mapper = new \JsonMapper();
114 $mapper->bExceptionOnMissingData =
true;
115 $mapper->bStrictObjectTypeChecking =
true;
119 $manifest = $mapper->map($manifest,
new Manifest());
120 }
catch(\JsonMapper_Exception $e){
123 if(!Uuid::isValid($manifest->header->uuid)){
126 $this->uuid = Uuid::fromString($manifest->header->uuid);
128 $this->manifest = $manifest;
130 $this->fileResource = fopen($zipPath,
"rb");
166 throw new \InvalidArgumentException(
"Pack length must be positive");
168 fseek($this->fileResource, $start);
169 if(feof($this->fileResource)){
170 throw new \InvalidArgumentException(
"Requested a resource pack chunk with invalid start offset");
172 return Utils::assumeNotFalse(fread($this->fileResource, $length),
"Already checked that we're not at EOF");