PocketMine-MP 5.37.1 git-da6732df2656426fbd1b7898ed06c8286969d2f1
Loading...
Searching...
No Matches
TimingsCommand.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\command\defaults;
25
39use Symfony\Component\Filesystem\Path;
40use function count;
41use function http_build_query;
42use function implode;
43use function is_array;
44use function is_int;
45use function is_string;
46use function json_decode;
47use function strtolower;
48use const CURLOPT_AUTOREFERER;
49use const CURLOPT_FOLLOWLOCATION;
50use const CURLOPT_HTTPHEADER;
51use const CURLOPT_POST;
52use const CURLOPT_POSTFIELDS;
53
55
56 public function __construct(string $namespace, string $name){
57 parent::__construct(
58 $namespace,
59 $name,
60 KnownTranslationFactory::pocketmine_command_timings_description(),
61 KnownTranslationFactory::pocketmine_command_timings_usage()
62 );
63 $this->setPermission(DefaultPermissionNames::COMMAND_TIMINGS);
64 }
65
66 public function execute(CommandSender $sender, string $commandLabel, array $args){
67 if(count($args) !== 1){
69 }
70
71 $mode = strtolower($args[0]);
72
73 if($mode === "on"){
74 if(TimingsHandler::isEnabled()){
75 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_timings_alreadyEnabled());
76 return true;
77 }
78 TimingsHandler::setEnabled();
79 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_enable());
80
81 return true;
82 }elseif($mode === "off"){
83 TimingsHandler::setEnabled(false);
84 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_disable());
85 return true;
86 }
87
88 if(!TimingsHandler::isEnabled()){
89 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_timings_timingsDisabled());
90
91 return true;
92 }
93
94 $paste = $mode === "paste";
95
96 if($mode === "reset"){
97 TimingsHandler::reload();
98 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_reset());
99 }elseif($mode === "merged" || $mode === "report" || $paste){
100 if($paste){
101 $timingsPromise = TimingsHandler::requestPrintTimings();
102 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_collect());
103 $timingsPromise->onCompletion(
104 fn(array $lines) => $this->uploadReport($lines, $sender),
105 fn() => throw new AssumptionFailedError("This promise is not expected to be rejected")
106 );
107 }else{
108 TimingsHandler::createReportFile(Path::join($sender->getServer()->getDataPath(), "timings"))->onCompletion(
109 function(string $timingsFile) use ($sender) : void{
110 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_timingsWrite($timingsFile));
111 },
112 fn() => $sender->getServer()->getLogger()->error("Failed to create timings report file")
113 );
114 }
115 }else{
117 }
118
119 return true;
120 }
121
126 private function uploadReport(array $lines, CommandSender $sender) : void{
127 $data = [
128 "browser" => $agent = $sender->getServer()->getName() . " " . $sender->getServer()->getPocketMineVersion(),
129 "data" => implode("\n", $lines),
130 "private" => "true"
131 ];
132
133 $host = $sender->getServer()->getConfigGroup()->getPropertyString(YmlServerProperties::TIMINGS_HOST, "timings.pmmp.io");
134
135 $sender->getServer()->getAsyncPool()->submitTask(new BulkCurlTask(
137 "https://$host?upload=true",
138 10,
139 [],
140 [
141 CURLOPT_HTTPHEADER => [
142 "User-Agent: $agent",
143 "Content-Type: application/x-www-form-urlencoded"
144 ],
145 CURLOPT_POST => true,
146 CURLOPT_POSTFIELDS => http_build_query($data),
147 CURLOPT_AUTOREFERER => false,
148 CURLOPT_FOLLOWLOCATION => false
149 ]
150 )],
151 function(array $results) use ($sender, $host) : void{
153 if($sender instanceof Player && !$sender->isOnline()){ // TODO replace with a more generic API method for checking availability of CommandSender
154 return;
155 }
156 $result = $results[0];
157 if($result instanceof InternetException){
158 $sender->getServer()->getLogger()->logException($result);
159 return;
160 }
161 $response = json_decode($result->getBody(), true);
162 if(is_array($response) && isset($response["id"]) && (is_int($response["id"]) || is_string($response["id"]))){
163 $url = "https://" . $host . "/?id=" . $response["id"];
164 if(isset($response["access_token"]) && is_string($response["access_token"])){
165 $url .= "&access_token=" . $response["access_token"];
166 }else{
167 $sender->getServer()->getLogger()->warning("Your chosen timings host does not support private reports. Anyone will be able to see your report if they guess the ID.");
168 }
169 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_timingsRead($url));
170 }else{
171 $sender->getServer()->getLogger()->debug("Invalid response from timings server (" . $result->getCode() . "): " . $result->getBody());
172 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_pasteError());
173 }
174 }
175 ));
176 }
177}
execute(CommandSender $sender, string $commandLabel, array $args)