PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
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 ];
153
154 $host = $sender->getServer()->getConfigGroup()->getPropertyString(YmlServerProperties::TIMINGS_HOST, "timings.pmmp.io");
155
156 $sender->getServer()->getAsyncPool()->submitTask(new BulkCurlTask(
157 [new BulkCurlTaskOperation(
158 "https://$host?upload=true",
159 10,
160 [],
161 [
162 CURLOPT_HTTPHEADER => [
163 "User-Agent: $agent",
164 "Content-Type: application/x-www-form-urlencoded"
165 ],
166 CURLOPT_POST => true,
167 CURLOPT_POSTFIELDS => http_build_query($data),
168 CURLOPT_AUTOREFERER => false,
169 CURLOPT_FOLLOWLOCATION => false
170 ]
171 )],
172 function(array $results) use ($sender, $host) : void{
174 if($sender instanceof Player && !$sender->isOnline()){ // TODO replace with a more generic API method for checking availability of CommandSender
175 return;
176 }
177 $result = $results[0];
178 if($result instanceof InternetException){
179 $sender->getServer()->getLogger()->logException($result);
180 return;
181 }
182 $response = json_decode($result->getBody(), true);
183 if(is_array($response) && isset($response["id"]) && (is_int($response["id"]) || is_string($response["id"]))){
184 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_timingsRead(
185 "https://" . $host . "/?id=" . $response["id"]));
186 }else{
187 $sender->getServer()->getLogger()->debug("Invalid response from timings server (" . $result->getCode() . "): " . $result->getBody());
188 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_pasteError());
189 }
190 }
191 ));
192 }
193}
execute(CommandSender $sender, string $commandLabel, array $args)
static trapAndRemoveFalse(\Closure $closure, int $levels=E_WARNING|E_NOTICE)