Convert VersionInfo into a final class

this allows it to be loaded by the autoloader without additional changes.
This commit is contained in:
Dylan K. Taylor 2020-07-25 19:00:48 +01:00
parent 5910905e95
commit 2645b19617
11 changed files with 45 additions and 45 deletions

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\build\make_release;
use pocketmine\utils\VersionString;
use pocketmine\VersionInfo;
use function dirname;
use function fgets;
use function file_get_contents;
@ -32,7 +33,6 @@ use function preg_replace;
use function sleep;
use function sprintf;
use function system;
use const pocketmine\BASE_VERSION;
use const STDIN;
require_once dirname(__DIR__) . '/vendor/autoload.php';
@ -41,13 +41,13 @@ require_once dirname(__DIR__) . '/vendor/autoload.php';
function replaceVersion(string $versionInfoPath, string $newVersion, bool $isDev) : void{
$versionInfo = file_get_contents($versionInfoPath);
$versionInfo = preg_replace(
$pattern = '/^const BASE_VERSION = "(\d+)\.(\d+)\.(\d+)(?:-(.*))?";$/m',
'const BASE_VERSION = "' . $newVersion . '";',
$pattern = '/^([\t ]*public )?const BASE_VERSION = "(\d+)\.(\d+)\.(\d+)(?:-(.*))?";$/m',
'$1const BASE_VERSION = "' . $newVersion . '";',
$versionInfo
);
$versionInfo = preg_replace(
'/^const IS_DEVELOPMENT_BUILD = (?:true|false);$/m',
'const IS_DEVELOPMENT_BUILD = ' . ($isDev ? 'true' : 'false') . ';',
'/^([\t ]*public )?const IS_DEVELOPMENT_BUILD = (?:true|false);$/m',
'$1const IS_DEVELOPMENT_BUILD = ' . ($isDev ? 'true' : 'false') . ';',
$versionInfo
);
file_put_contents($versionInfoPath, $versionInfo);
@ -61,7 +61,7 @@ function main(array $argv) : void{
if(isset($argv[1])){
$currentVer = new VersionString($argv[1]);
}else{
$currentVer = new VersionString(BASE_VERSION);
$currentVer = new VersionString(VersionInfo::BASE_VERSION);
}
$nextVer = new VersionString(sprintf(
"%u.%u.%u",

View File

@ -60,8 +60,7 @@
"pocketmine\\": "src/"
},
"files": [
"src/CoreConstants.php",
"src/VersionInfo.php"
"src/CoreConstants.php"
]
},
"autoload-dev": {

View File

@ -35,7 +35,7 @@ parameters:
- build/server-phar.php
- tests/phpunit
dynamicConstantNames:
- pocketmine\IS_DEVELOPMENT_BUILD
- pocketmine\VersionInfo::IS_DEVELOPMENT_BUILD
- pocketmine\DEBUG
stubFiles:
- tests/phpstan/stubs/JsonMapper.stub

View File

@ -308,12 +308,12 @@ class CrashDump{
}
private function generalData() : void{
$version = new VersionString(\pocketmine\BASE_VERSION, \pocketmine\IS_DEVELOPMENT_BUILD, \pocketmine\BUILD_NUMBER);
$version = new VersionString(VersionInfo::BASE_VERSION, VersionInfo::IS_DEVELOPMENT_BUILD, VersionInfo::BUILD_NUMBER);
$this->data["general"] = [];
$this->data["general"]["name"] = $this->server->getName();
$this->data["general"]["base_version"] = \pocketmine\BASE_VERSION;
$this->data["general"]["build"] = \pocketmine\BUILD_NUMBER;
$this->data["general"]["is_dev"] = \pocketmine\IS_DEVELOPMENT_BUILD;
$this->data["general"]["base_version"] = VersionInfo::BASE_VERSION;
$this->data["general"]["build"] = VersionInfo::BUILD_NUMBER;
$this->data["general"]["is_dev"] = VersionInfo::IS_DEVELOPMENT_BUILD;
$this->data["general"]["protocol"] = ProtocolInfo::CURRENT_PROTOCOL;
$this->data["general"]["git"] = \pocketmine\GIT_COMMIT;
$this->data["general"]["uname"] = php_uname("a");

View File

@ -197,7 +197,7 @@ namespace pocketmine {
ErrorToExceptionHandler::set();
$version = new VersionString(\pocketmine\BASE_VERSION, \pocketmine\IS_DEVELOPMENT_BUILD, \pocketmine\BUILD_NUMBER);
$version = new VersionString(VersionInfo::BASE_VERSION, VersionInfo::IS_DEVELOPMENT_BUILD, VersionInfo::BUILD_NUMBER);
define('pocketmine\VERSION', $version->getFullVersion(true));
$gitHash = str_repeat("00", 20);
@ -225,7 +225,7 @@ namespace pocketmine {
$lockFilePath = $dataPath . '/server.lock';
if(($pid = Filesystem::createLockFile($lockFilePath)) !== null){
critical_error("Another " . \pocketmine\NAME . " instance (PID $pid) is already using this folder (" . realpath($dataPath) . ").");
critical_error("Another " . VersionInfo::NAME . " instance (PID $pid) is already using this folder (" . realpath($dataPath) . ").");
critical_error("Please stop the other server first before running a new one.");
exit(1);
}

View File

@ -280,7 +280,7 @@ class Server{
private $playerList = [];
public function getName() : string{
return \pocketmine\NAME;
return VersionInfo::NAME;
}
public function isRunning() : bool{
@ -296,7 +296,7 @@ class Server{
}
public function getApiVersion() : string{
return \pocketmine\BASE_VERSION;
return VersionInfo::BASE_VERSION;
}
public function getFilePath() : string{
@ -385,7 +385,7 @@ class Server{
}
public function getMotd() : string{
return $this->configGroup->getConfigString("motd", \pocketmine\NAME . " Server");
return $this->configGroup->getConfigString("motd", VersionInfo::NAME . " Server");
}
/**
@ -778,7 +778,7 @@ class Server{
$this->logger->info("Loading server configuration");
if(!file_exists($this->dataPath . "pocketmine.yml")){
$content = file_get_contents(\pocketmine\RESOURCE_PATH . "pocketmine.yml");
if(\pocketmine\IS_DEVELOPMENT_BUILD){
if(VersionInfo::IS_DEVELOPMENT_BUILD){
$content = str_replace("preferred-channel: stable", "preferred-channel: beta", $content);
}
@file_put_contents($this->dataPath . "pocketmine.yml", $content);
@ -787,7 +787,7 @@ class Server{
$this->configGroup = new ServerConfigGroup(
new Config($this->dataPath . "pocketmine.yml", Config::YAML, []),
new Config($this->dataPath . "server.properties", Config::PROPERTIES, [
"motd" => \pocketmine\NAME . " Server",
"motd" => VersionInfo::NAME . " Server",
"server-port" => 19132,
"white-list" => false,
"max-players" => 20,
@ -829,9 +829,9 @@ class Server{
$this->logger->info($this->getLanguage()->translateString("language.selected", [$this->getLanguage()->getName(), $this->getLanguage()->getLang()]));
if(\pocketmine\IS_DEVELOPMENT_BUILD){
if(VersionInfo::IS_DEVELOPMENT_BUILD){
if(!((bool) $this->configGroup->getProperty("settings.enable-dev-builds", false))){
$this->logger->emergency($this->language->translateString("pocketmine.server.devBuild.error1", [\pocketmine\NAME]));
$this->logger->emergency($this->language->translateString("pocketmine.server.devBuild.error1", [VersionInfo::NAME]));
$this->logger->emergency($this->language->translateString("pocketmine.server.devBuild.error2"));
$this->logger->emergency($this->language->translateString("pocketmine.server.devBuild.error3"));
$this->logger->emergency($this->language->translateString("pocketmine.server.devBuild.error4", ["settings.enable-dev-builds"]));
@ -842,7 +842,7 @@ class Server{
}
$this->logger->warning(str_repeat("-", 40));
$this->logger->warning($this->language->translateString("pocketmine.server.devBuild.warning1", [\pocketmine\NAME]));
$this->logger->warning($this->language->translateString("pocketmine.server.devBuild.warning1", [VersionInfo::NAME]));
$this->logger->warning($this->language->translateString("pocketmine.server.devBuild.warning2"));
$this->logger->warning($this->language->translateString("pocketmine.server.devBuild.warning3"));
$this->logger->warning(str_repeat("-", 40));
@ -923,7 +923,7 @@ class Server{
$this->logger->info($this->getLanguage()->translateString("pocketmine.server.info", [
$this->getName(),
(\pocketmine\IS_DEVELOPMENT_BUILD ? TextFormat::YELLOW : "") . $this->getPocketMineVersion() . TextFormat::RESET
(VersionInfo::IS_DEVELOPMENT_BUILD ? TextFormat::YELLOW : "") . $this->getPocketMineVersion() . TextFormat::RESET
]));
$this->logger->info($this->getLanguage()->translateString("pocketmine.server.license", [$this->getName()]));

View File

@ -23,16 +23,13 @@ declare(strict_types=1);
namespace pocketmine;
use function defined;
final class VersionInfo{
public const NAME = "PocketMine-MP";
public const BASE_VERSION = "4.0.0";
public const IS_DEVELOPMENT_BUILD = true;
public const BUILD_NUMBER = 0;
// composer autoload doesn't use require_once and also pthreads can inherit things
// TODO: drop this file and use a final class with constants
if(defined('pocketmine\_VERSION_INFO_INCLUDED')){
return;
private function __construct(){
//NOOP
}
}
const _VERSION_INFO_INCLUDED = true;
const NAME = "PocketMine-MP";
const BASE_VERSION = "4.0.0";
const IS_DEVELOPMENT_BUILD = true;
const BUILD_NUMBER = 0;

View File

@ -33,6 +33,7 @@ use pocketmine\utils\Process;
use pocketmine\utils\Utils;
use pocketmine\utils\VersionString;
use pocketmine\uuid\UUID;
use pocketmine\VersionInfo;
use function array_map;
use function array_values;
use function count;
@ -71,7 +72,7 @@ class SendUsageTask extends AsyncTask{
case self::TYPE_OPEN:
$data["event"] = "open";
$version = new VersionString(\pocketmine\BASE_VERSION, \pocketmine\IS_DEVELOPMENT_BUILD, \pocketmine\BUILD_NUMBER);
$version = new VersionString(VersionInfo::BASE_VERSION, VersionInfo::IS_DEVELOPMENT_BUILD, VersionInfo::BUILD_NUMBER);
$data["server"] = [
"port" => $server->getPort(),

View File

@ -28,6 +28,7 @@ use pocketmine\player\Player;
use pocketmine\Server;
use pocketmine\utils\TextFormat;
use pocketmine\utils\VersionString;
use pocketmine\VersionInfo;
use function date;
use function sprintf;
use function str_repeat;
@ -75,9 +76,9 @@ class AutoUpdater{
$this->showConsoleUpdate();
}
}else{
if(!\pocketmine\IS_DEVELOPMENT_BUILD and $this->getChannel() !== "stable"){
if(!VersionInfo::IS_DEVELOPMENT_BUILD and $this->getChannel() !== "stable"){
$this->showChannelSuggestionStable();
}elseif(\pocketmine\IS_DEVELOPMENT_BUILD and $this->getChannel() === "stable"){
}elseif(VersionInfo::IS_DEVELOPMENT_BUILD and $this->getChannel() === "stable"){
$this->showChannelSuggestionBeta();
}
}
@ -159,7 +160,7 @@ class AutoUpdater{
if($this->updateInfo === null){
return;
}
$currentVersion = new VersionString(\pocketmine\BASE_VERSION, \pocketmine\IS_DEVELOPMENT_BUILD, \pocketmine\BUILD_NUMBER);
$currentVersion = new VersionString(VersionInfo::BASE_VERSION, VersionInfo::IS_DEVELOPMENT_BUILD, VersionInfo::BUILD_NUMBER);
try{
$newVersion = new VersionString($this->updateInfo->base_version, $this->updateInfo->is_dev, $this->updateInfo->build);
}catch(\InvalidArgumentException $e){

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\utils;
use pocketmine\VersionInfo;
use function array_merge;
use function curl_close;
use function curl_error;
@ -223,7 +224,7 @@ class Internet{
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT_MS => (int) ($timeout * 1000),
CURLOPT_TIMEOUT_MS => (int) ($timeout * 1000),
CURLOPT_HTTPHEADER => array_merge(["User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 " . \pocketmine\NAME . "/" . \pocketmine\VERSION], $extraHeaders),
CURLOPT_HTTPHEADER => array_merge(["User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 " . VersionInfo::NAME . "/" . \pocketmine\VERSION], $extraHeaders),
CURLOPT_HEADER => true
]);
try{

View File

@ -33,6 +33,7 @@ use pocketmine\player\GameMode;
use pocketmine\utils\Config;
use pocketmine\utils\Internet;
use pocketmine\utils\InternetException;
use pocketmine\VersionInfo;
use function fgets;
use function sleep;
use function strtolower;
@ -41,7 +42,7 @@ use const PHP_EOL;
use const STDIN;
class SetupWizard{
public const DEFAULT_NAME = \pocketmine\NAME . " Server";
public const DEFAULT_NAME = VersionInfo::NAME . " Server";
public const DEFAULT_PORT = 19132;
public const DEFAULT_PLAYERS = 20;
@ -55,7 +56,7 @@ class SetupWizard{
}
public function run() : bool{
$this->message(\pocketmine\NAME . " set-up wizard");
$this->message(VersionInfo::NAME . " set-up wizard");
try{
$langs = Language::getLanguageList();
@ -107,7 +108,7 @@ class SetupWizard{
}
private function showLicense() : bool{
$this->message($this->lang->translateString("welcome_to_pocketmine", [\pocketmine\NAME]));
$this->message($this->lang->translateString("welcome_to_pocketmine", [VersionInfo::NAME]));
echo <<<LICENSE
This program is free software: you can redistribute it and/or modify
@ -118,7 +119,7 @@ class SetupWizard{
LICENSE;
$this->writeLine();
if(strtolower($this->getInput($this->lang->get("accept_license"), "n", "y/N")) !== "y"){
$this->error($this->lang->translateString("you_have_to_accept_the_license", [\pocketmine\NAME]));
$this->error($this->lang->translateString("you_have_to_accept_the_license", [VersionInfo::NAME]));
sleep(5);
return false;
@ -220,7 +221,7 @@ LICENSE;
private function endWizard() : void{
$this->message($this->lang->get("you_have_finished"));
$this->message($this->lang->get("pocketmine_plugins"));
$this->message($this->lang->translateString("pocketmine_will_start", [\pocketmine\NAME]));
$this->message($this->lang->translateString("pocketmine_will_start", [VersionInfo::NAME]));
$this->writeLine();
$this->writeLine();