ResourcePackManager: extracted loadPackFromPath() private method from constructor body

This commit is contained in:
Dylan K. Taylor 2022-12-15 21:59:42 +00:00
parent c5d716dc9d
commit 50b70708fb
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -93,41 +93,21 @@ class ResourcePackManager{
} }
$pack = (string) $pack; $pack = (string) $pack;
try{ try{
$packPath = Path::join($this->path, $pack); $newPack = $this->loadPackFromPath(Path::join($this->path, $pack));
if(!file_exists($packPath)){
throw new ResourcePackException("File or directory not found");
}
if(is_dir($packPath)){
throw new ResourcePackException("Directory resource packs are unsupported");
}
$newPack = null; $this->resourcePacks[] = $newPack;
//Detect the type of resource pack. $index = strtolower($newPack->getPackId());
$info = new \SplFileInfo($packPath); $this->uuidList[$index] = $newPack;
switch($info->getExtension()){
case "zip":
case "mcpack":
$newPack = new ZippedResourcePack($packPath);
break;
}
if($newPack instanceof ResourcePack){ $keyPath = Path::join($this->path, $pack . ".key");
$this->resourcePacks[] = $newPack; if(file_exists($keyPath)){
$index = strtolower($newPack->getPackId()); try{
$this->uuidList[$index] = $newPack; $this->encryptionKeys[$index] = ErrorToExceptionHandler::trapAndRemoveFalse(
fn() => file_get_contents($keyPath)
$keyPath = Path::join($this->path, $pack . ".key"); );
if(file_exists($keyPath)){ }catch(\ErrorException $e){
try{ throw new ResourcePackException("Could not read encryption key file: " . $e->getMessage(), 0, $e);
$this->encryptionKeys[$index] = ErrorToExceptionHandler::trapAndRemoveFalse(
fn() => file_get_contents($keyPath)
);
}catch(\ErrorException $e){
throw new ResourcePackException("Could not read encryption key file: " . $e->getMessage(), 0, $e);
}
} }
}else{
throw new ResourcePackException("Format not recognized");
} }
}catch(ResourcePackException $e){ }catch(ResourcePackException $e){
$logger->critical("Could not load resource pack \"$pack\": " . $e->getMessage()); $logger->critical("Could not load resource pack \"$pack\": " . $e->getMessage());
@ -137,6 +117,25 @@ class ResourcePackManager{
$logger->debug("Successfully loaded " . count($this->resourcePacks) . " resource packs"); $logger->debug("Successfully loaded " . count($this->resourcePacks) . " resource packs");
} }
private function loadPackFromPath(string $packPath) : ResourcePack{
if(!file_exists($packPath)){
throw new ResourcePackException("File or directory not found");
}
if(is_dir($packPath)){
throw new ResourcePackException("Directory resource packs are unsupported");
}
//Detect the type of resource pack.
$info = new \SplFileInfo($packPath);
switch($info->getExtension()){
case "zip":
case "mcpack":
return new ZippedResourcePack($packPath);
}
throw new ResourcePackException("Format not recognized");
}
/** /**
* Returns the directory which resource packs are loaded from. * Returns the directory which resource packs are loaded from.
*/ */