PocketMine-MP 5.27.1 git-9af3cde03fabbe4129c79e46dc87ffa0fff446e6
Loading...
Searching...
No Matches
ResourcePackManager.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\resourcepacks;
25
29use Ramsey\Uuid\Uuid;
30use Symfony\Component\Filesystem\Path;
31use function array_keys;
32use function copy;
33use function count;
34use function file_exists;
35use function gettype;
36use function is_array;
37use function is_dir;
38use function is_float;
39use function is_int;
40use function is_string;
41use function mkdir;
42use function rtrim;
43use function strlen;
44use function strtolower;
45use const DIRECTORY_SEPARATOR;
46
48 private string $path;
49 private bool $serverForceResources = false;
50
55 private array $resourcePacks = [];
56
61 private array $uuidList = [];
62
67 private array $encryptionKeys = [];
68
72 public function __construct(string $path, \Logger $logger){
73 $this->path = $path;
74
75 if(!file_exists($this->path)){
76 $logger->debug("Resource packs path $path does not exist, creating directory");
77 mkdir($this->path);
78 }elseif(!is_dir($this->path)){
79 throw new \InvalidArgumentException("Resource packs path $path exists and is not a directory");
80 }
81
82 $resourcePacksYml = Path::join($this->path, "resource_packs.yml");
83 if(!file_exists($resourcePacksYml)){
84 copy(Path::join(\pocketmine\RESOURCE_PATH, "resource_packs.yml"), $resourcePacksYml);
85 }
86
87 $resourcePacksConfig = new Config($resourcePacksYml, Config::YAML, []);
88
89 $this->serverForceResources = (bool) $resourcePacksConfig->get("force_resources", false);
90
91 $logger->info("Loading resource packs...");
92
93 $resourceStack = $resourcePacksConfig->get("resource_stack", []);
94 if(!is_array($resourceStack)){
95 throw new \InvalidArgumentException("\"resource_stack\" key should contain a list of pack names");
96 }
97
98 foreach(Utils::promoteKeys($resourceStack) as $pos => $pack){
99 if(!is_string($pack) && !is_int($pack) && !is_float($pack)){
100 $logger->critical("Found invalid entry in resource pack list at offset $pos of type " . gettype($pack));
101 continue;
102 }
103 $pack = (string) $pack;
104 try{
105 $newPack = $this->loadPackFromPath(Path::join($this->path, $pack));
106
107 $index = strtolower($newPack->getPackId());
108 if(!Uuid::isValid($index)){
109 //TODO: we should use Uuid in ResourcePack interface directly but that would break BC
110 //for now we need to validate this here to make sure it doesn't cause crashes later on
111 throw new ResourcePackException("Invalid UUID ($index)");
112 }
113 $this->uuidList[$index] = $newPack;
114 $this->resourcePacks[] = $newPack;
115
116 $keyPath = Path::join($this->path, $pack . ".key");
117 if(file_exists($keyPath)){
118 try{
119 $key = Filesystem::fileGetContents($keyPath);
120 }catch(\RuntimeException $e){
121 throw new ResourcePackException("Could not read encryption key file: " . $e->getMessage(), 0, $e);
122 }
123 $key = rtrim($key, "\r\n");
124 if(strlen($key) !== 32){
125 throw new ResourcePackException("Invalid encryption key length, must be exactly 32 bytes");
126 }
127 $this->encryptionKeys[$index] = $key;
128 }
129 }catch(ResourcePackException $e){
130 $logger->critical("Could not load resource pack \"$pack\": " . $e->getMessage());
131 }
132 }
133
134 $logger->debug("Successfully loaded " . count($this->resourcePacks) . " resource packs");
135 }
136
137 private function loadPackFromPath(string $packPath) : ResourcePack{
138 if(!file_exists($packPath)){
139 throw new ResourcePackException("File or directory not found");
140 }
141 if(is_dir($packPath)){
142 throw new ResourcePackException("Directory resource packs are unsupported");
143 }
144
145 //Detect the type of resource pack.
146 $info = new \SplFileInfo($packPath);
147 switch($info->getExtension()){
148 case "zip":
149 case "mcpack":
150 return new ZippedResourcePack($packPath);
151 }
152
153 throw new ResourcePackException("Format not recognized");
154 }
155
159 public function getPath() : string{
160 return $this->path . DIRECTORY_SEPARATOR;
161 }
162
166 public function resourcePacksRequired() : bool{
167 return $this->serverForceResources;
168 }
169
173 public function setResourcePacksRequired(bool $value) : void{
174 $this->serverForceResources = $value;
175 }
176
182 public function getResourceStack() : array{
183 return $this->resourcePacks;
184 }
185
194 public function setResourceStack(array $resourceStack) : void{
195 $uuidList = [];
196 $resourcePacks = [];
197 foreach($resourceStack as $pack){
198 $uuid = strtolower($pack->getPackId());
199 if(!Uuid::isValid($uuid)){
200 //TODO: we should use Uuid in ResourcePack interface directly but that would break BC
201 //for now we need to validate this here to make sure it doesn't cause crashes later on
202 throw new \InvalidArgumentException("Invalid resource pack UUID ($uuid)");
203 }
204 if(isset($uuidList[$uuid])){
205 throw new \InvalidArgumentException("Cannot load two resource pack with the same UUID ($uuid)");
206 }
207 $uuidList[$uuid] = $pack;
208 $resourcePacks[] = $pack;
209 }
210 $this->resourcePacks = $resourcePacks;
211 $this->uuidList = $uuidList;
212 }
213
217 public function getPackById(string $id) : ?ResourcePack{
218 return $this->uuidList[strtolower($id)] ?? null;
219 }
220
225 public function getPackIdList() : array{
226 return array_keys($this->uuidList);
227 }
228
232 public function getPackEncryptionKey(string $id) : ?string{
233 return $this->encryptionKeys[strtolower($id)] ?? null;
234 }
235
240 public function setPackEncryptionKey(string $id, ?string $key) : void{
241 $id = strtolower($id);
242 if($key === null){
243 //allow deprovisioning keys for resource packs that have been removed
244 unset($this->encryptionKeys[$id]);
245 }elseif(isset($this->uuidList[$id])){
246 if(strlen($key) !== 32){
247 throw new \InvalidArgumentException("Encryption key must be exactly 32 bytes long");
248 }
249 $this->encryptionKeys[$id] = $key;
250 }else{
251 throw new \InvalidArgumentException("Unknown pack ID $id");
252 }
253 }
254}
debug($message)
info($message)
critical($message)