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