Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement client-side support of downloading RP from CDN #6064

Open
wants to merge 7 commits into
base: minor-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions resources/resource_packs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ resource_stack:
# - vanilla.zip
#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

cdn:
#Resource packs listed here will allow the client to download the resource pack from an external web server rather than PocketMine.
#This option is much faster to download multiple packs than the PocketMine and can be used even if your server has low bandwidth.
#However, the resource packs still need to be loaded into PocketMine.
#Example
#natural.zip: https://example.com/natural
#vanilla.zip: https://example.com/vanilla
2 changes: 1 addition & 1 deletion src/network/mcpe/handler/ResourcePacksPacketHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function setUp() : void{
);
}, $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->getPackURLs()));
$this->session->getLogger()->debug("Waiting for client to accept resource packs");
}

Expand Down
23 changes: 23 additions & 0 deletions src/resourcepacks/ResourcePackManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class ResourcePackManager{
*/
private array $encryptionKeys = [];

/** @phpstan-var array<string, string> */
private array $packURLs = [];

/**
* @param string $path Path to resource-packs directory.
*/
Expand Down Expand Up @@ -87,6 +90,11 @@ public function __construct(string $path, \Logger $logger){
throw new \InvalidArgumentException("\"resource_stack\" key should contain a list of pack names");
}

$remotePacks = $resourcePacksConfig->get("cdn", []);
if(!is_array($remotePacks)){
throw new \InvalidArgumentException("\"cdn\" should be an array of pack URLs");
}

foreach($resourceStack as $pos => $pack){
if(!is_string($pack) && !is_int($pack) && !is_float($pack)){
$logger->critical("Found invalid entry in resource pack list at offset $pos of type " . gettype($pack));
Expand All @@ -113,6 +121,13 @@ public function __construct(string $path, \Logger $logger){
}
$this->encryptionKeys[$index] = $key;
}
if(isset($remotePacks[$pack])){
$url = $remotePacks[$pack];
if(!is_string($url)){
alvin0319 marked this conversation as resolved.
Show resolved Hide resolved
throw new ResourcePackException("Invalid URL for pack $pack");
}
$this->packURLs[$newPack->getPackId() . "_" . $newPack->getPackVersion()] = $url;
}
}catch(ResourcePackException $e){
$logger->critical("Could not load resource pack \"$pack\": " . $e->getMessage());
}
Expand Down Expand Up @@ -169,6 +184,14 @@ public function getResourceStack() : array{
return $this->resourcePacks;
}

/**
* Returns an array of pack URLs.
* @phpstan-return array<string, string>
*/
public function getPackURLs() : array{
return $this->packURLs;
}

/**
* Sets the resource packs to use. Packs earliest in the list will appear at the top of the stack (maximum
* priority), and later ones will appear below (lower priority), in the same manner as the Bedrock resource packs
Expand Down