PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
Loading...
Searching...
No Matches
PluginDescription.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\plugin;
25
30use function array_map;
31use function array_values;
32use function get_debug_type;
33use function is_array;
34use function is_string;
35use function preg_match;
36use function str_replace;
37use function stripos;
38use function yaml_parse;
39
41 private const KEY_NAME = "name";
42 private const KEY_VERSION = "version";
43 private const KEY_MAIN = "main";
44 private const KEY_SRC_NAMESPACE_PREFIX = "src-namespace-prefix";
45 private const KEY_API = "api";
46 private const KEY_MCPE_PROTOCOL = "mcpe-protocol";
47 private const KEY_OS = "os";
48 private const KEY_DEPEND = "depend";
49 private const KEY_SOFTDEPEND = "softdepend";
50 private const KEY_LOADBEFORE = "loadbefore";
51 private const KEY_EXTENSIONS = "extensions";
52 private const KEY_WEBSITE = "website";
53 private const KEY_DESCRIPTION = "description";
54 private const KEY_LOGGER_PREFIX = "prefix";
55 private const KEY_LOAD = "load";
56 private const KEY_AUTHOR = "author";
57 private const KEY_AUTHORS = "authors";
58 private const KEY_PERMISSIONS = "permissions";
59
60 private const KEY_COMMANDS = "commands";
61 private const KEY_COMMAND_PERMISSION = "permission";
62 private const KEY_COMMAND_DESCRIPTION = self::KEY_DESCRIPTION;
63 private const KEY_COMMAND_USAGE = "usage";
64 private const KEY_COMMAND_ALIASES = "aliases";
65 private const KEY_COMMAND_PERMISSION_MESSAGE = "permission-message";
66
71 private array $map;
72
73 private string $name;
74 private string $main;
75 private string $srcNamespacePrefix = "";
77 private array $api;
79 private array $compatibleMcpeProtocols = [];
81 private array $compatibleOperatingSystems = [];
86 private array $extensions = [];
91 private array $depend = [];
96 private array $softDepend = [];
101 private array $loadBefore = [];
102 private string $version;
107 private array $commands = [];
108 private string $description = "";
110 private array $authors = [];
111 private string $website = "";
112 private string $prefix = "";
113 private PluginEnableOrder $order;
114
119 private array $permissions = [];
120
124 public function __construct(array|string $yamlString){
125 if(is_string($yamlString)){
126 $map = yaml_parse($yamlString);
127 if($map === false){
128 throw new PluginDescriptionParseException("YAML parsing error in plugin manifest");
129 }
130 if(!is_array($map)){
131 throw new PluginDescriptionParseException("Invalid structure of plugin manifest, expected array but have " . get_debug_type($map));
132 }
133 }else{
134 $map = $yamlString;
135 }
136 $this->loadMap($map);
137 }
138
143 private function loadMap(array $plugin) : void{
144 $this->map = $plugin;
145
146 $this->name = $plugin[self::KEY_NAME];
147 if(preg_match('/^[A-Za-z0-9 _.-]+$/', $this->name) === 0){
148 throw new PluginDescriptionParseException("Invalid Plugin name");
149 }
150 $this->name = str_replace(" ", "_", $this->name);
151 $this->version = (string) $plugin[self::KEY_VERSION];
152 $this->main = $plugin[self::KEY_MAIN];
153 if(stripos($this->main, "pocketmine\\") === 0){
154 throw new PluginDescriptionParseException("Invalid Plugin main, cannot start within the PocketMine namespace");
155 }
156
157 $this->srcNamespacePrefix = $plugin[self::KEY_SRC_NAMESPACE_PREFIX] ?? "";
158
159 $this->api = array_map("\strval", (array) ($plugin[self::KEY_API] ?? []));
160 $this->compatibleMcpeProtocols = array_map("\intval", (array) ($plugin[self::KEY_MCPE_PROTOCOL] ?? []));
161 $this->compatibleOperatingSystems = array_map("\strval", (array) ($plugin[self::KEY_OS] ?? []));
162
163 if(isset($plugin[self::KEY_COMMANDS]) && is_array($plugin[self::KEY_COMMANDS])){
164 foreach(Utils::promoteKeys($plugin[self::KEY_COMMANDS]) as $commandName => $commandData){
165 if(!is_string($commandName)){
166 throw new PluginDescriptionParseException("Invalid Plugin commands, key must be the name of the command");
167 }
168 if(!is_array($commandData)){
169 throw new PluginDescriptionParseException("Command $commandName has invalid properties");
170 }
171 if(!isset($commandData[self::KEY_COMMAND_PERMISSION]) || !is_string($commandData[self::KEY_COMMAND_PERMISSION])){
172 throw new PluginDescriptionParseException("Command $commandName does not have a valid permission set");
173 }
174 $this->commands[$commandName] = new PluginDescriptionCommandEntry(
175 $commandData[self::KEY_COMMAND_DESCRIPTION] ?? null,
176 $commandData[self::KEY_COMMAND_USAGE] ?? null,
177 $commandData[self::KEY_COMMAND_ALIASES] ?? [],
178 $commandData[self::KEY_COMMAND_PERMISSION],
179 $commandData[self::KEY_COMMAND_PERMISSION_MESSAGE] ?? null
180 );
181 }
182 }
183
184 if(isset($plugin[self::KEY_DEPEND])){
185 $this->depend = array_values((array) $plugin[self::KEY_DEPEND]);
186 }
187 if(isset($plugin[self::KEY_EXTENSIONS])){
188 $extensions = (array) $plugin[self::KEY_EXTENSIONS];
189 $isLinear = $extensions === array_values($extensions);
190 foreach(Utils::promoteKeys($extensions) as $k => $v){
191 if($isLinear){
192 $k = $v;
193 $v = "*";
194 }
195 $this->extensions[(string) $k] = array_values(array_map('strval', is_array($v) ? $v : [$v]));
196 }
197 }
198
199 $this->softDepend = array_values((array) ($plugin[self::KEY_SOFTDEPEND] ?? $this->softDepend));
200
201 $this->loadBefore = array_values((array) ($plugin[self::KEY_LOADBEFORE] ?? $this->loadBefore));
202
203 $this->website = (string) ($plugin[self::KEY_WEBSITE] ?? $this->website);
204
205 $this->description = (string) ($plugin[self::KEY_DESCRIPTION] ?? $this->description);
206
207 $this->prefix = (string) ($plugin[self::KEY_LOGGER_PREFIX] ?? $this->prefix);
208
209 if(isset($plugin[self::KEY_LOAD])){
210 $order = PluginEnableOrder::fromString($plugin[self::KEY_LOAD]);
211 if($order === null){
212 throw new PluginDescriptionParseException("Invalid Plugin \"" . self::KEY_LOAD . "\"");
213 }
214 $this->order = $order;
215 }else{
216 $this->order = PluginEnableOrder::POSTWORLD;
217 }
218
219 $this->authors = [];
220 if(isset($plugin[self::KEY_AUTHOR])){
221 if(is_array($plugin[self::KEY_AUTHOR])){
222 $this->authors = array_values($plugin[self::KEY_AUTHOR]);
223 }else{
224 $this->authors[] = $plugin[self::KEY_AUTHOR];
225 }
226 }
227 if(isset($plugin[self::KEY_AUTHORS])){
228 foreach($plugin[self::KEY_AUTHORS] as $author){
229 $this->authors[] = $author;
230 }
231 }
232
233 if(isset($plugin[self::KEY_PERMISSIONS])){
234 try{
235 $this->permissions = PermissionParser::loadPermissions($plugin[self::KEY_PERMISSIONS]);
236 }catch(PermissionParserException $e){
237 throw new PluginDescriptionParseException("Invalid Plugin \"" . self::KEY_PERMISSIONS . "\": " . $e->getMessage(), 0, $e);
238 }
239 }
240 }
241
242 public function getFullName() : string{
243 return $this->name . " v" . $this->version;
244 }
245
249 public function getCompatibleApis() : array{
250 return $this->api;
251 }
252
256 public function getCompatibleMcpeProtocols() : array{
257 return $this->compatibleMcpeProtocols;
258 }
259
263 public function getCompatibleOperatingSystems() : array{
264 return $this->compatibleOperatingSystems;
265 }
266
270 public function getAuthors() : array{
271 return $this->authors;
272 }
273
274 public function getPrefix() : string{
275 return $this->prefix;
276 }
277
282 public function getCommands() : array{
283 return $this->commands;
284 }
285
290 public function getRequiredExtensions() : array{
291 return $this->extensions;
292 }
293
298 public function getDepend() : array{
299 return $this->depend;
300 }
301
302 public function getDescription() : string{
303 return $this->description;
304 }
305
310 public function getLoadBefore() : array{
311 return $this->loadBefore;
312 }
313
314 public function getMain() : string{
315 return $this->main;
316 }
317
318 public function getSrcNamespacePrefix() : string{ return $this->srcNamespacePrefix; }
319
320 public function getName() : string{
321 return $this->name;
322 }
323
324 public function getOrder() : PluginEnableOrder{
325 return $this->order;
326 }
327
332 public function getPermissions() : array{
333 return $this->permissions;
334 }
335
340 public function getSoftDepend() : array{
341 return $this->softDepend;
342 }
343
344 public function getVersion() : string{
345 return $this->version;
346 }
347
348 public function getWebsite() : string{
349 return $this->website;
350 }
351
356 public function getMap() : array{
357 return $this->map;
358 }
359}
__construct(array|string $yamlString)