Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
add system url resources pack in symply
Browse files Browse the repository at this point in the history
  • Loading branch information
SenseiTarzan committed Dec 3, 2023
1 parent f9faed2 commit 0113fa9
Show file tree
Hide file tree
Showing 9 changed files with 334 additions and 22 deletions.
7 changes: 7 additions & 0 deletions resources/resource_packs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@ resource_stack:
#Example
# - natural.zip
# - vanilla.zip
# - path: file.zip
# encryptionKey: "your encrypt Key" <- is optionnal option
# - path: 'https://exemple.com/natural.zip'
# name: "name in manifest.json"
# uuid: "uuid in manifest.json"
# version: "version in manifest.json"
# encryptionKey: "your encrypt Key" <- is optionnal option
#If you want to force clients to use vanilla resources, you must place a vanilla resource pack in your resources folder and add it to the stack here.
#To specify a resource encryption key, put the key in the <resource>.key file alongside the resource pack. Example: vanilla.zip.key
17 changes: 12 additions & 5 deletions src/network/mcpe/handler/ResourcePacksPacketHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use pocketmine\network\mcpe\protocol\types\resourcepacks\ResourcePackType;
use pocketmine\resourcepacks\ResourcePack;
use pocketmine\resourcepacks\ResourcePackManager;
use symply\resourcepacks\URLResourcePack;
use function array_map;
use function ceil;
use function count;
Expand Down Expand Up @@ -67,20 +68,19 @@ public function __construct(
public function setUp() : void{
$resourcePackEntries = array_map(function(ResourcePack $pack) : ResourcePackInfoEntry{
//TODO: more stuff
$encryptionKey = $this->resourcePackManager->getPackEncryptionKey($pack->getPackId());

return new ResourcePackInfoEntry(
$pack->getPackId(),
$pack->getPackVersion(),
$pack->getPackSize(),
$encryptionKey ?? "",
$pack->getEncryptionKey(),
"",
$pack->getPackId(),
$pack->getContentId(),
false
);
}, $this->resourcePackManager->getResourceStack());
//TODO: support forcing server packs
$this->session->sendDataPacket(ResourcePacksInfoPacket::create($resourcePackEntries, [], $this->resourcePackManager->resourcePacksRequired(), false, false, []));
$this->session->sendDataPacket(ResourcePacksInfoPacket::create($resourcePackEntries, [], $this->resourcePackManager->resourcePacksRequired(), false, false, $this->resourcePackManager->getPackUrl()));
$this->session->getLogger()->debug("Waiting for client to accept resource packs");
}

Expand Down Expand Up @@ -109,6 +109,10 @@ public function handleResourcePackClientResponse(ResourcePackClientResponsePacke
$this->disconnectWithError("Unknown pack $uuid requested, available packs: " . implode(", ", $this->resourcePackManager->getPackIdList()));
return false;
}
if ($pack instanceof URLResourcePack){
$this->disconnectWithError("Invalid URL for pack chunk in $uuid. Unable to use the getPackChunk method.");
return false;
}

$this->session->sendDataPacket(ResourcePackDataInfoPacket::create(
$pack->getPackId(),
Expand Down Expand Up @@ -154,8 +158,11 @@ public function handleResourcePackChunkRequest(ResourcePackChunkRequestPacket $p
$this->disconnectWithError("Invalid request for chunk $packet->chunkIndex of unknown pack $packet->packId, available packs: " . implode(", ", $this->resourcePackManager->getPackIdList()));
return false;
}

$packId = $pack->getPackId(); //use this because case may be different
if ($pack instanceof URLResourcePack){
$this->disconnectWithError("Invalid URL for pack chunk in $packId. Unable to use the getPackChunk method.");
return false;
}

if(isset($this->downloadedChunks[$packId][$packet->chunkIndex])){
$this->disconnectWithError("Duplicate request for chunk $packet->chunkIndex of pack $packet->packId");
Expand Down
15 changes: 15 additions & 0 deletions src/resourcepacks/ResourcePack.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,19 @@ public function getSha256() : string;
* @throws \InvalidArgumentException if the chunk does not exist
*/
public function getPackChunk(int $start, int $length) : string;

/**
* Returns the EncryptionKey of ResourcesPack
*/
public function getEncryptionKey() : string;

/**
* Returns the ContentId of ResourcesPack
*/
public function getContentId() : string;

/**
* can set the EncryptionKey without pass by new system symplu
*/
public function setEncryptionKey(string $key) : void;
}
60 changes: 44 additions & 16 deletions src/resourcepacks/ResourcePackManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use pocketmine\utils\Config;
use pocketmine\utils\Filesystem;
use Symfony\Component\Filesystem\Path;
use symply\resourcepacks\URLResourcePack;
use symply\utils\Utils as UtilsSymply;
use function array_keys;
use function copy;
use function count;
Expand Down Expand Up @@ -56,7 +58,7 @@ class ResourcePackManager{
* @var string[]
* @phpstan-var array<string, string>
*/
private array $encryptionKeys = [];
private array $packUrl = [];

/**
* @param string $path Path to resource-packs directory.
Expand Down Expand Up @@ -88,17 +90,23 @@ public function __construct(string $path, \Logger $logger){
}

foreach($resourceStack as $pos => $pack){
if(!is_string($pack) && !is_int($pack) && !is_float($pack)){
if(!(is_string($pack) || is_array($pack)) && !is_int($pack) && !is_float($pack)){
$logger->critical("Found invalid entry in resource pack list at offset $pos of type " . gettype($pack));
continue;
}
$pack = (string) $pack;
try{
$newPack = $this->loadPackFromPath(Path::join($this->path, $pack));

if (is_string($pack)) {
$newPack = $this->loadPackFromPath(Path::join($this->path, (string) $pack));

Check failure on line 99 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.3)

Casting to string something that's already string.

Check failure on line 99 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)

Casting to string something that's already string.

Check failure on line 99 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)

Casting to string something that's already string.

Check failure on line 99 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)

Casting to string something that's already string.

Check failure on line 99 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)

Casting to string something that's already string.

Check failure on line 99 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.3)

Casting to string something that's already string.
}else {
$newPack = $this->loadPackFromArray((array) $pack);
}
$this->resourcePacks[] = $newPack;
$index = strtolower($newPack->getPackId());
$this->uuidList[$index] = $newPack;
if ($newPack instanceof URLResourcePack){
$this->addPackUrl($newPack);
return;
}

$keyPath = Path::join($this->path, $pack . ".key");

Check failure on line 111 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.3)

Binary operation "." between array|float|int|string and '.key' results in an error.

Check failure on line 111 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)

Binary operation "." between array|float|int|string and '.key' results in an error.

Check failure on line 111 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)

Binary operation "." between array|float|int|string and '.key' results in an error.

Check failure on line 111 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)

Binary operation "." between array|float|int|string and '.key' results in an error.

Check failure on line 111 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)

Binary operation "." between array|float|int|string and '.key' results in an error.

Check failure on line 111 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.3)

Binary operation "." between array|float|int|string and '.key' results in an error.
if(file_exists($keyPath)){
Expand All @@ -111,7 +119,7 @@ public function __construct(string $path, \Logger $logger){
if(strlen($key) !== 32){
throw new ResourcePackException("Invalid encryption key length, must be exactly 32 bytes");
}
$this->encryptionKeys[$index] = $key;
$this->setPackEncryptionKey($index, $key);
}
}catch(ResourcePackException $e){
$logger->critical("Could not load resource pack \"$pack\": " . $e->getMessage());

Check failure on line 125 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.3)

Part $pack (array|float|int|string) of encapsed string cannot be cast to string.

Check failure on line 125 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)

Part $pack (array|float|int|string) of encapsed string cannot be cast to string.

Check failure on line 125 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)

Part $pack (array|float|int|string) of encapsed string cannot be cast to string.

Check failure on line 125 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)

Part $pack (array|float|int|string) of encapsed string cannot be cast to string.

Check failure on line 125 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)

Part $pack (array|float|int|string) of encapsed string cannot be cast to string.

Check failure on line 125 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.3)

Part $pack (array|float|int|string) of encapsed string cannot be cast to string.
Expand Down Expand Up @@ -140,6 +148,21 @@ private function loadPackFromPath(string $packPath) : ResourcePack{
throw new ResourcePackException("Format not recognized");
}

private function loadPackFromArray(array $info) : ResourcePack{

Check failure on line 151 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.3)

Method pocketmine\resourcepacks\ResourcePackManager::loadPackFromArray() has parameter $info with no value type specified in iterable type array.

Check failure on line 151 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)

Method pocketmine\resourcepacks\ResourcePackManager::loadPackFromArray() has parameter $info with no value type specified in iterable type array.

Check failure on line 151 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)

Method pocketmine\resourcepacks\ResourcePackManager::loadPackFromArray() has parameter $info with no value type specified in iterable type array.

Check failure on line 151 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)

Method pocketmine\resourcepacks\ResourcePackManager::loadPackFromArray() has parameter $info with no value type specified in iterable type array.

Check failure on line 151 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)

Method pocketmine\resourcepacks\ResourcePackManager::loadPackFromArray() has parameter $info with no value type specified in iterable type array.

Check failure on line 151 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.3)

Method pocketmine\resourcepacks\ResourcePackManager::loadPackFromArray() has parameter $info with no value type specified in iterable type array.
$packPath = $info['path'];
if (UtilsSymply::isUrl($packPath)){
return new URLResourcePack($info['name'], $info['uuid'], $info['version'], $packPath, UtilsSymply::getSizeOfResourcesPack($packPath), $info['encryptionKey'] ?? "");
}
$packPath = Path::join($this->path, $packPath);
if(!file_exists($packPath)){
throw new ResourcePackException("File or directory not found");
}
if(is_dir($packPath)){
throw new ResourcePackException("Directory resource packs are unsupported");
}
return new ZippedResourcePack($packPath, $info['encryptionKey'] ?? "");
}

/**
* Returns the directory which resource packs are loaded from.
*/
Expand Down Expand Up @@ -207,29 +230,34 @@ 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;
}

/**
* Sets the encryption key to use for decrypting the specified resource pack. The pack will **NOT** be decrypted by
* PocketMine-MP; the key is simply passed to the client to allow it to decrypt the pack after downloading it.
*/
public function setPackEncryptionKey(string $id, ?string $key) : void{
$id = strtolower($id);
if (!isset($this->uuidList[$id])){
throw new ResourcePackException("Resource pack with ID $id not found.");
}
if($key === null){
//allow deprovisioning keys for resource packs that have been removed
unset($this->encryptionKeys[$id]);
$this->uuidList[$id]->setEncryptionKey("");
}elseif(isset($this->uuidList[$id])){
if(strlen($key) !== 32){
throw new \InvalidArgumentException("Encryption key must be exactly 32 bytes long");
}
$this->encryptionKeys[$id] = $key;
$this->uuidList[$id]->setEncryptionKey($key);
}else{
throw new \InvalidArgumentException("Unknown pack ID $id");
}
}

public function getPackUrl() : array

Check failure on line 254 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.3)

Method pocketmine\resourcepacks\ResourcePackManager::getPackUrl() return type has no value type specified in iterable type array.

Check failure on line 254 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)

Method pocketmine\resourcepacks\ResourcePackManager::getPackUrl() return type has no value type specified in iterable type array.

Check failure on line 254 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)

Method pocketmine\resourcepacks\ResourcePackManager::getPackUrl() return type has no value type specified in iterable type array.

Check failure on line 254 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)

Method pocketmine\resourcepacks\ResourcePackManager::getPackUrl() return type has no value type specified in iterable type array.

Check failure on line 254 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)

Method pocketmine\resourcepacks\ResourcePackManager::getPackUrl() return type has no value type specified in iterable type array.

Check failure on line 254 in src/resourcepacks/ResourcePackManager.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.3)

Method pocketmine\resourcepacks\ResourcePackManager::getPackUrl() return type has no value type specified in iterable type array.
{
return $this->packUrl;
}

public function addPackUrl(URLResourcePack $packUrl) : void
{
$this->packUrl[$packUrl->getRealName()] = $packUrl->getUrl();
}
}
19 changes: 18 additions & 1 deletion src/resourcepacks/ZippedResourcePack.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ZippedResourcePack implements ResourcePack{
* @param string $zipPath Path to the resource pack zip
* @throws ResourcePackException
*/
public function __construct(string $zipPath){
public function __construct(string $zipPath, protected string $encryptionKey = ""){
$this->path = $zipPath;

if(!file_exists($zipPath)){
Expand Down Expand Up @@ -159,4 +159,21 @@ public function getPackChunk(int $start, int $length) : string{
}
return Utils::assumeNotFalse(fread($this->fileResource, $length), "Already checked that we're not at EOF");
}

public function getEncryptionKey() : string{
return $this->encryptionKey;
}

public function setEncryptionKey(string $key) : void{
$this->encryptionKey = $key;
}

private function hasEncryptionKey() : bool{
return $this->encryptionKey != "";
}

public function getContentId() : string
{
return $this->hasEncryptionKey() ? $this->getPackId() : "";
}
}
32 changes: 32 additions & 0 deletions symply/resourcepacks/InvalidPackChunkURLException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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 Symply Team
* @link http://www.symplymc.com/
*
*
*/

declare(strict_types=1);

namespace symply\resourcepacks;

class InvalidPackChunkURLException extends \Exception
{

}
34 changes: 34 additions & 0 deletions symply/resourcepacks/PackSizeUnavailable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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 Symply Team
* @link http://www.symplymc.com/
*
*
*/

declare(strict_types=1);

namespace symply\resourcepacks;

use Exception;

class PackSizeUnavailable extends Exception
{

}
Loading

0 comments on commit 0113fa9

Please sign in to comment.