Allow specifying a key for encrypted resource packs (#5297)

This commit is contained in:
BrandPVP
2022-09-28 19:27:33 +03:00
committed by GitHub
parent 1c7b1e9e5d
commit b21cd82e94
3 changed files with 39 additions and 2 deletions

View File

@ -23,12 +23,14 @@ declare(strict_types=1);
namespace pocketmine\resourcepacks;
use pocketmine\errorhandler\ErrorToExceptionHandler;
use pocketmine\utils\Config;
use Webmozart\PathUtil\Path;
use function array_keys;
use function copy;
use function count;
use function file_exists;
use function file_get_contents;
use function gettype;
use function is_array;
use function is_dir;
@ -49,6 +51,12 @@ class ResourcePackManager{
/** @var ResourcePack[] */
private array $uuidList = [];
/**
* @var string[]
* @phpstan-var array<string, string>
*/
private array $encryptionKeys = [];
/**
* @param string $path Path to resource-packs directory.
*/
@ -106,6 +114,17 @@ class ResourcePackManager{
if($newPack instanceof ResourcePack){
$this->resourcePacks[] = $newPack;
$this->uuidList[strtolower($newPack->getPackId())] = $newPack;
$keyPath = Path::join($this->path, $pack . ".key");
if(file_exists($keyPath)){
try{
$this->encryptionKeys[strtolower($newPack->getPackId())] = 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");
}
@ -153,4 +172,11 @@ class ResourcePackManager{
public function getPackIdList() : array{
return array_keys($this->uuidList);
}
/**
* Returns the key with which the pack was encrypted, or null if the pack has no key.
*/
public function getPackEncryptionKey(string $id) : ?string{
return $this->encryptionKeys[strtolower($id)] ?? null;
}
}