PocketMine-MP 5.18.1 git-9381fc4172e5dce4cada1cb356050c8a2ab57b94
PluginLoadabilityChecker.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
31use function array_intersect;
32use function count;
33use function extension_loaded;
34use function implode;
35use function in_array;
36use function phpversion;
37use function str_starts_with;
38use function stripos;
39use function strlen;
40use function substr;
41use function version_compare;
42
44
45 public function __construct(
46 private string $apiVersion
47 ){}
48
49 public function check(PluginDescription $description) : Translatable|null{
50 $name = $description->getName();
51 if(stripos($name, "pocketmine") !== false || stripos($name, "minecraft") !== false || stripos($name, "mojang") !== false){
52 return KnownTranslationFactory::pocketmine_plugin_restrictedName();
53 }
54
55 foreach($description->getCompatibleApis() as $api){
56 if(!VersionString::isValidBaseVersion($api)){
57 return KnownTranslationFactory::pocketmine_plugin_invalidAPI($api);
58 }
59 }
60
61 if(!ApiVersion::isCompatible($this->apiVersion, $description->getCompatibleApis())){
62 return KnownTranslationFactory::pocketmine_plugin_incompatibleAPI(implode(", ", $description->getCompatibleApis()));
63 }
64
65 $ambiguousVersions = ApiVersion::checkAmbiguousVersions($description->getCompatibleApis());
66 if(count($ambiguousVersions) > 0){
67 return KnownTranslationFactory::pocketmine_plugin_ambiguousMinAPI(implode(", ", $ambiguousVersions));
68 }
69
70 if(count($description->getCompatibleOperatingSystems()) > 0 && !in_array(Utils::getOS(), $description->getCompatibleOperatingSystems(), true)) {
71 return KnownTranslationFactory::pocketmine_plugin_incompatibleOS(implode(", ", $description->getCompatibleOperatingSystems()));
72 }
73
74 if(count($pluginMcpeProtocols = $description->getCompatibleMcpeProtocols()) > 0){
75 $serverMcpeProtocols = [ProtocolInfo::CURRENT_PROTOCOL];
76 if(count(array_intersect($pluginMcpeProtocols, $serverMcpeProtocols)) === 0){
77 return KnownTranslationFactory::pocketmine_plugin_incompatibleProtocol(implode(", ", $pluginMcpeProtocols));
78 }
79 }
80
81 foreach(Utils::stringifyKeys($description->getRequiredExtensions()) as $extensionName => $versionConstrs){
82 if(!extension_loaded($extensionName)){
83 return KnownTranslationFactory::pocketmine_plugin_extensionNotLoaded($extensionName);
84 }
85 $gotVersion = phpversion($extensionName);
86 if($gotVersion === false){
87 //extensions may set NULL as the extension version, in which case phpversion() may return false
88 $gotVersion = "**UNKNOWN**";
89 }
90
91 foreach($versionConstrs as $k => $constr){ // versionConstrs_loop
92 if($constr === "*"){
93 continue;
94 }
95 if($constr === ""){
96 return KnownTranslationFactory::pocketmine_plugin_emptyExtensionVersionConstraint(extensionName: $extensionName, constraintIndex: "$k");
97 }
98 foreach(["<=", "le", "<>", "!=", "ne", "<", "lt", "==", "=", "eq", ">=", "ge", ">", "gt"] as $comparator){
99 // warning: the > character should be quoted in YAML
100 if(str_starts_with($constr, $comparator)){
101 $version = substr($constr, strlen($comparator));
102 if(!version_compare($gotVersion, $version, $comparator)){
103 return KnownTranslationFactory::pocketmine_plugin_incompatibleExtensionVersion(extensionName: $extensionName, extensionVersion: $gotVersion, pluginRequirement: $constr);
104 }
105 continue 2; // versionConstrs_loop
106 }
107 }
108 return KnownTranslationFactory::pocketmine_plugin_invalidExtensionVersionConstraint(extensionName: $extensionName, versionConstraint: $constr);
109 }
110 }
111
112 return null;
113 }
114}
static isCompatible(string $myVersionStr, array $wantVersionsStr)
Definition: ApiVersion.php:41
static checkAmbiguousVersions(array $versions)
Definition: ApiVersion.php:72
static stringifyKeys(array $array)
Definition: Utils.php:605
static getOS(bool $recalculate=false)
Definition: Utils.php:275