ResourcePackManager: cleaned up pack loading error handling

This commit is contained in:
Dylan K. Taylor 2018-05-31 17:58:47 +01:00
parent 1b4723d816
commit 7864a315f6
3 changed files with 57 additions and 31 deletions

View File

@ -0,0 +1,28 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\resourcepacks;
class ResourcePackException extends \RuntimeException{
}

View File

@ -69,33 +69,31 @@ class ResourcePackManager{
foreach($resourcePacksConfig->get("resource_stack", []) as $pos => $pack){ foreach($resourcePacksConfig->get("resource_stack", []) as $pos => $pack){
try{ try{
$packPath = $this->path . DIRECTORY_SEPARATOR . $pack; $packPath = $this->path . DIRECTORY_SEPARATOR . $pack;
if(file_exists($packPath)){ if(!file_exists($packPath)){
$newPack = null; throw new ResourcePackException("File or directory not found");
//Detect the type of resource pack.
if(is_dir($packPath)){
$logger->warning("Skipped resource entry $pack due to directory resource packs currently unsupported");
}else{
$info = new \SplFileInfo($packPath);
switch($info->getExtension()){
case "zip":
case "mcpack":
$newPack = new ZippedResourcePack($packPath);
break;
default:
$logger->warning("Skipped resource entry $pack due to format not recognized");
break;
}
}
if($newPack instanceof ResourcePack){
$this->resourcePacks[] = $newPack;
$this->uuidList[strtolower($newPack->getPackId())] = $newPack;
}
}else{
$logger->warning("Skipped resource entry $pack due to file or directory not found");
} }
}catch(\Throwable $e){ if(is_dir($packPath)){
$logger->logException($e); throw new ResourcePackException("Directory resource packs are unsupported");
}
$newPack = null;
//Detect the type of resource pack.
$info = new \SplFileInfo($packPath);
switch($info->getExtension()){
case "zip":
case "mcpack":
$newPack = new ZippedResourcePack($packPath);
break;
}
if($newPack instanceof ResourcePack){
$this->resourcePacks[] = $newPack;
$this->uuidList[strtolower($newPack->getPackId())] = $newPack;
}else{
throw new ResourcePackException("Format not recognized");
}
}catch(ResourcePackException $e){
$logger->critical("Could not load resource pack \"$pack\": " . $e->getMessage());
} }
} }

View File

@ -67,19 +67,19 @@ class ZippedResourcePack implements ResourcePack{
$this->path = $zipPath; $this->path = $zipPath;
if(!file_exists($zipPath)){ if(!file_exists($zipPath)){
throw new \InvalidArgumentException("Could not open resource pack $zipPath: file not found"); throw new ResourcePackException("File not found");
} }
$archive = new \ZipArchive(); $archive = new \ZipArchive();
if(($openResult = $archive->open($zipPath)) !== true){ if(($openResult = $archive->open($zipPath)) !== true){
throw new \InvalidStateException("Encountered ZipArchive error code $openResult while trying to open $zipPath"); throw new ResourcePackException("Encountered ZipArchive error code $openResult while trying to open $zipPath");
} }
if(($manifestData = $archive->getFromName("manifest.json")) === false){ if(($manifestData = $archive->getFromName("manifest.json")) === false){
if($archive->locateName("pack_manifest.json") !== false){ if($archive->locateName("pack_manifest.json") !== false){
throw new \InvalidStateException("Could not load resource pack from $zipPath: unsupported old pack format"); throw new ResourcePackException("Unsupported old pack format");
}else{ }else{
throw new \InvalidStateException("Could not load resource pack from $zipPath: manifest.json not found in the archive root"); throw new ResourcePackException("manifest.json not found in the archive root");
} }
} }
@ -87,7 +87,7 @@ class ZippedResourcePack implements ResourcePack{
$manifest = json_decode($manifestData); $manifest = json_decode($manifestData);
if($manifest === null or !self::verifyManifest($manifest)){ if($manifest === null or !self::verifyManifest($manifest)){
throw new \InvalidStateException("Could not load resource pack from $zipPath: manifest.json is invalid or incomplete"); throw new ResourcePackException("manifest.json is invalid or incomplete");
} }
$this->manifest = $manifest; $this->manifest = $manifest;