PocketMine-MP 5.33.2 git-919492bdcad8510eb6606439eb77e1c604f1d1ea
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
40use Symfony\Component\Filesystem\Path;
41use function count;
42use function fclose;
43use function file_exists;
44use function fopen;
45use function fwrite;
46use function http_build_query;
47use function implode;
48use function is_array;
49use function is_int;
50use function is_string;
51use function json_decode;
52use function mkdir;
53use function strtolower;
54use const CURLOPT_AUTOREFERER;
55use const CURLOPT_FOLLOWLOCATION;
56use const CURLOPT_HTTPHEADER;
57use const CURLOPT_POST;
58use const CURLOPT_POSTFIELDS;
59use const PHP_EOL;
60
62
63 public function __construct(){
64 parent::__construct(
65 "timings",
66 KnownTranslationFactory::pocketmine_command_timings_description(),
67 KnownTranslationFactory::pocketmine_command_timings_usage()
68 );
69 $this->setPermission(DefaultPermissionNames::COMMAND_TIMINGS);
70 }
71
72 public function execute(CommandSender $sender, string $commandLabel, array $args){
73 if(count($args) !== 1){
75 }
76
77 $mode = strtolower($args[0]);
78
79 if($mode === "on"){
80 if(TimingsHandler::isEnabled()){
81 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_timings_alreadyEnabled());
82 return true;
83 }
84 TimingsHandler::setEnabled();
85 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_enable());
86
87 return true;
88 }elseif($mode === "off"){
89 TimingsHandler::setEnabled(false);
90 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_disable());
91 return true;
92 }
93
94 if(!TimingsHandler::isEnabled()){
95 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_timings_timingsDisabled());
96
97 return true;
98 }
99
100 $paste = $mode === "paste";
101
102 if($mode === "reset"){
103 TimingsHandler::reload();
104 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_reset());
105 }elseif($mode === "merged" || $mode === "report" || $paste){
106 $timingsPromise = TimingsHandler::requestPrintTimings();
107 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_collect());
108 $timingsPromise->onCompletion(
109 fn(array $lines) => $paste ? $this->uploadReport($lines, $sender) : $this->createReportFile($lines, $sender),
110 fn() => throw new AssumptionFailedError("This promise is not expected to be rejected")
111 );
112 }else{
114 }
115
116 return true;
117 }
118
123 private function createReportFile(array $lines, CommandSender $sender) : void{
124 $index = 0;
125 $timingFolder = Path::join($sender->getServer()->getDataPath(), "timings");
126
127 if(!file_exists($timingFolder)){
128 mkdir($timingFolder, 0777);
129 }
130 $timings = Path::join($timingFolder, "timings.txt");
131 while(file_exists($timings)){
132 $timings = Path::join($timingFolder, "timings" . (++$index) . ".txt");
133 }
134
135 $fileTimings = ErrorToExceptionHandler::trapAndRemoveFalse(fn() => fopen($timings, "a+b"));
136 foreach($lines as $line){
137 fwrite($fileTimings, $line . PHP_EOL);
138 }
139 fclose($fileTimings);
140
141 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_timingsWrite($timings));
142 }
143
148 private function uploadReport(array $lines, CommandSender $sender) : void{
149 $data = [
150 "browser" => $agent = $sender->getServer()->getName() . " " . $sender->getServer()->getPocketMineVersion(),
151 "data" => implode("\n", $lines),
152 "private" => "true"
153 ];
154
155 $host = $sender->getServer()->getConfigGroup()->getPropertyString(YmlServerProperties::TIMINGS_HOST, "timings.pmmp.io");
156
157 $sender->getServer()->getAsyncPool()->submitTask(new BulkCurlTask(
158 [new BulkCurlTaskOperation(
159 "https://$host?upload=true",
160 10,
161 [],
162 [
163 CURLOPT_HTTPHEADER => [
164 "User-Agent: $agent",
165 "Content-Type: application/x-www-form-urlencoded"
166 ],
167 CURLOPT_POST => true,
168 CURLOPT_POSTFIELDS => http_build_query($data),
169 CURLOPT_AUTOREFERER => false,
170 CURLOPT_FOLLOWLOCATION => false
171 ]
172 )],
173 function(array $results) use ($sender, $host) : void{
175 if($sender instanceof Player && !$sender->isOnline()){ // TODO replace with a more generic API method for checking availability of CommandSender
176 return;
177 }
178 $result = $results[0];
179 if($result instanceof InternetException){
180 $sender->getServer()->getLogger()->logException($result);
181 return;
182 }
183 $response = json_decode($result->getBody(), true);
184 if(is_array($response) && isset($response["id"]) && (is_int($response["id"]) || is_string($response["id"]))){
185 $url = "https://" . $host . "/?id=" . $response["id"];
186 if(isset($response["access_token"]) && is_string($response["access_token"])){
187 $url .= "&access_token=" . $response["access_token"];
188 }else{
189 $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.");
190 }
191 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_timingsRead($url));
192 }else{
193 $sender->getServer()->getLogger()->debug("Invalid response from timings server (" . $result->getCode() . "): " . $result->getBody());
194 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_pasteError());
195 }
196 }
197 ));
198 }
199}
execute(CommandSender $sender, string $commandLabel, array $args)
static trapAndRemoveFalse(\Closure $closure, int $levels=E_WARNING|E_NOTICE)