PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
UpdateChecker.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\updater;
25
31use function date;
32use function strtolower;
33use function ucfirst;
34
36
37 protected Server $server;
38 protected string $endpoint;
39 protected ?UpdateInfo $updateInfo = null;
40 private \Logger $logger;
41
42 public function __construct(Server $server, string $endpoint){
43 $this->server = $server;
44 $this->logger = new \PrefixedLogger($server->getLogger(), "Update Checker");
45 $this->endpoint = "http://$endpoint/api/";
46
47 if($server->getConfigGroup()->getPropertyBool(YmlServerProperties::AUTO_UPDATER_ENABLED, true)){
48 $this->doCheck();
49 }
50 }
51
52 public function checkUpdateError(string $error) : void{
53 $this->logger->debug("Async update check failed due to \"$error\"");
54 }
55
59 public function checkUpdateCallback(UpdateInfo $updateInfo) : void{
60 $this->checkUpdate($updateInfo);
61 if($this->hasUpdate()){
62 (new UpdateNotifyEvent($this))->call();
63 if($this->server->getConfigGroup()->getPropertyBool(YmlServerProperties::AUTO_UPDATER_ON_UPDATE_WARN_CONSOLE, true)){
64 $this->showConsoleUpdate();
65 }
66 }else{
67 if(!VersionInfo::IS_DEVELOPMENT_BUILD && $this->getChannel() !== "stable"){
68 $this->showChannelSuggestionStable();
69 }elseif(VersionInfo::IS_DEVELOPMENT_BUILD && $this->getChannel() === "stable"){
70 $this->showChannelSuggestionBeta();
71 }
72 }
73 }
74
78 public function hasUpdate() : bool{
79 return $this->updateInfo !== null;
80 }
81
85 public function showConsoleUpdate() : void{
86 if($this->updateInfo === null){
87 return;
88 }
89 $newVersion = new VersionString($this->updateInfo->base_version, $this->updateInfo->is_dev, $this->updateInfo->build);
90 $messages = [
91 "Your version of " . $this->server->getName() . " is out of date. Version " . $newVersion->getFullVersion(true) . " was released on " . date("D M j h:i:s Y", $this->updateInfo->date)
92 ];
93
94 $messages[] = "Details: " . $this->updateInfo->details_url;
95 $messages[] = "Download: " . $this->updateInfo->download_url;
96
97 $this->printConsoleMessage($messages, \LogLevel::WARNING);
98 }
99
100 protected function showChannelSuggestionStable() : void{
101 $this->printConsoleMessage([
102 "You're running a Stable build, but you're receiving update notifications for " . ucfirst($this->getChannel()) . " builds.",
103 "To get notified about new Stable builds only, change 'preferred-channel' in your pocketmine.yml to 'stable'."
104 ]);
105 }
106
107 protected function showChannelSuggestionBeta() : void{
108 $this->printConsoleMessage([
109 "You're running a Beta build, but you're receiving update notifications for Stable builds.",
110 "To get notified about new Beta or Development builds, change 'preferred-channel' in your pocketmine.yml to 'beta' or 'development'."
111 ]);
112 }
113
117 protected function printConsoleMessage(array $lines, string $logLevel = \LogLevel::INFO) : void{
118 foreach($lines as $line){
119 $this->logger->log($logLevel, $line);
120 }
121 }
122
126 public function getUpdateInfo() : ?UpdateInfo{
127 return $this->updateInfo;
128 }
129
133 public function doCheck() : void{
134 $this->server->getAsyncPool()->submitTask(new UpdateCheckTask($this, $this->endpoint, $this->getChannel()));
135 }
136
140 protected function checkUpdate(UpdateInfo $updateInfo) : void{
141 $currentVersion = VersionInfo::VERSION();
142 try{
143 $newVersion = new VersionString($updateInfo->base_version, $updateInfo->is_dev, $updateInfo->build);
144 }catch(\InvalidArgumentException $e){
145 //Invalid version returned from API, assume there's no update
146 $this->logger->debug("Assuming no update because \"" . $e->getMessage() . "\"");
147 return;
148 }
149
150 if($currentVersion->getBuild() > 0 && $currentVersion->compare($newVersion) > 0){
151 $this->updateInfo = $updateInfo;
152 }else{
153 $this->logger->debug("API reported version is an older version or the same version (" . $newVersion->getFullVersion() . "), not showing notification");
154 }
155 }
156
160 public function getChannel() : string{
161 return strtolower($this->server->getConfigGroup()->getPropertyString(YmlServerProperties::AUTO_UPDATER_PREFERRED_CHANNEL, "stable"));
162 }
163
167 public function getEndpoint() : string{
168 return $this->endpoint;
169 }
170}
checkUpdate(UpdateInfo $updateInfo)
printConsoleMessage(array $lines, string $logLevel=\LogLevel::INFO)
checkUpdateCallback(UpdateInfo $updateInfo)