mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 17:59:48 +00:00
Allow specifying a key for encrypted resource packs (#5297)
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user