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