PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
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
28use Symfony\Component\Filesystem\Path;
29use function array_keys;
30use function copy;
31use function count;
32use function file_exists;
33use function gettype;
34use function is_array;
35use function is_dir;
36use function is_float;
37use function is_int;
38use function is_string;
39use function mkdir;
40use function rtrim;
41use function strlen;
42use function strtolower;
43use const DIRECTORY_SEPARATOR;
44
46 private string $path;
47 private bool $serverForceResources = false;
48
50 private array $resourcePacks = [];
51
53 private array $uuidList = [];
54
59 private array $encryptionKeys = [];
60
64 public function __construct(string $path, \Logger $logger){
65 $this->path = $path;
66
67 if(!file_exists($this->path)){
68 $logger->debug("Resource packs path $path does not exist, creating directory");
69 mkdir($this->path);
70 }elseif(!is_dir($this->path)){
71 throw new \InvalidArgumentException("Resource packs path $path exists and is not a directory");
72 }
73
74 $resourcePacksYml = Path::join($this->path, "resource_packs.yml");
75 if(!file_exists($resourcePacksYml)){
76 copy(Path::join(\pocketmine\RESOURCE_PATH, "resource_packs.yml"), $resourcePacksYml);
77 }
78
79 $resourcePacksConfig = new Config($resourcePacksYml, Config::YAML, []);
80
81 $this->serverForceResources = (bool) $resourcePacksConfig->get("force_resources", false);
82
83 $logger->info("Loading resource packs...");
84
85 $resourceStack = $resourcePacksConfig->get("resource_stack", []);
86 if(!is_array($resourceStack)){
87 throw new \InvalidArgumentException("\"resource_stack\" key should contain a list of pack names");
88 }
89
90 foreach($resourceStack as $pos => $pack){
91 if(!is_string($pack) && !is_int($pack) && !is_float($pack)){
92 $logger->critical("Found invalid entry in resource pack list at offset $pos of type " . gettype($pack));
93 continue;
94 }
95 $pack = (string) $pack;
96 try{
97 $newPack = $this->loadPackFromPath(Path::join($this->path, $pack));
98
99 $this->resourcePacks[] = $newPack;
100 $index = strtolower($newPack->getPackId());
101 $this->uuidList[$index] = $newPack;
102
103 $keyPath = Path::join($this->path, $pack . ".key");
104 if(file_exists($keyPath)){
105 try{
106 $key = Filesystem::fileGetContents($keyPath);
107 }catch(\RuntimeException $e){
108 throw new ResourcePackException("Could not read encryption key file: " . $e->getMessage(), 0, $e);
109 }
110 $key = rtrim($key, "\r\n");
111 if(strlen($key) !== 32){
112 throw new ResourcePackException("Invalid encryption key length, must be exactly 32 bytes");
113 }
114 $this->encryptionKeys[$index] = $key;
115 }
116 }catch(ResourcePackException $e){
117 $logger->critical("Could not load resource pack \"$pack\": " . $e->getMessage());
118 }
119 }
120
121 $logger->debug("Successfully loaded " . count($this->resourcePacks) . " resource packs");
122 }
123
124 private function loadPackFromPath(string $packPath) : ResourcePack{
125 if(!file_exists($packPath)){
126 throw new ResourcePackException("File or directory not found");
127 }
128 if(is_dir($packPath)){
129 throw new ResourcePackException("Directory resource packs are unsupported");
130 }
131
132 //Detect the type of resource pack.
133 $info = new \SplFileInfo($packPath);
134 switch($info->getExtension()){
135 case "zip":
136 case "mcpack":
137 return new ZippedResourcePack($packPath);
138 }
139
140 throw new ResourcePackException("Format not recognized");
141 }
142
146 public function getPath() : string{
147 return $this->path . DIRECTORY_SEPARATOR;
148 }
149
153 public function resourcePacksRequired() : bool{
154 return $this->serverForceResources;
155 }
156
160 public function setResourcePacksRequired(bool $value) : void{
161 $this->serverForceResources = $value;
162 }
163
168 public function getResourceStack() : array{
169 return $this->resourcePacks;
170 }
171
180 public function setResourceStack(array $resourceStack) : void{
181 $uuidList = [];
182 $resourcePacks = [];
183 foreach($resourceStack as $pack){
184 $uuid = strtolower($pack->getPackId());
185 if(isset($uuidList[$uuid])){
186 throw new \InvalidArgumentException("Cannot load two resource pack with the same UUID ($uuid)");
187 }
188 $uuidList[$uuid] = $pack;
189 $resourcePacks[] = $pack;
190 }
191 $this->resourcePacks = $resourcePacks;
192 $this->uuidList = $uuidList;
193 }
194
198 public function getPackById(string $id) : ?ResourcePack{
199 return $this->uuidList[strtolower($id)] ?? null;
200 }
201
206 public function getPackIdList() : array{
207 return array_keys($this->uuidList);
208 }
209
213 public function getPackEncryptionKey(string $id) : ?string{
214 return $this->encryptionKeys[strtolower($id)] ?? null;
215 }
216
221 public function setPackEncryptionKey(string $id, ?string $key) : void{
222 $id = strtolower($id);
223 if($key === null){
224 //allow deprovisioning keys for resource packs that have been removed
225 unset($this->encryptionKeys[$id]);
226 }elseif(isset($this->uuidList[$id])){
227 if(strlen($key) !== 32){
228 throw new \InvalidArgumentException("Encryption key must be exactly 32 bytes long");
229 }
230 $this->encryptionKeys[$id] = $key;
231 }else{
232 throw new \InvalidArgumentException("Unknown pack ID $id");
233 }
234 }
235}
static fileGetContents(string $fileName, bool $useIncludePath=false, $context=null, int $offset=0, ?int $length=null)
Definition: Filesystem.php:305
debug($message)
info($message)
critical($message)