Remove VERSION and GIT_COMMIT constants

these are now lazily computed in VersionInfo as needed.
This commit is contained in:
Dylan K. Taylor 2020-07-25 19:17:33 +01:00
parent 2645b19617
commit 2c29634d03
10 changed files with 51 additions and 35 deletions

View File

@ -61,7 +61,7 @@ function main(array $argv) : void{
if(isset($argv[1])){
$currentVer = new VersionString($argv[1]);
}else{
$currentVer = new VersionString(VersionInfo::BASE_VERSION);
$currentVer = VersionInfo::getVersionObj();
}
$nextVer = new VersionString(sprintf(
"%u.%u.%u",

View File

@ -30,7 +30,6 @@ use pocketmine\plugin\PluginBase;
use pocketmine\plugin\PluginManager;
use pocketmine\utils\Filesystem;
use pocketmine\utils\Utils;
use pocketmine\utils\VersionString;
use function base64_encode;
use function date;
use function error_get_last;
@ -308,14 +307,14 @@ class CrashDump{
}
private function generalData() : void{
$version = new VersionString(VersionInfo::BASE_VERSION, VersionInfo::IS_DEVELOPMENT_BUILD, VersionInfo::BUILD_NUMBER);
$version = VersionInfo::getVersionObj();
$this->data["general"] = [];
$this->data["general"]["name"] = $this->server->getName();
$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"]["git"] = VersionInfo::getGitHash();
$this->data["general"]["uname"] = php_uname("a");
$this->data["general"]["php"] = phpversion();
$this->data["general"]["zend"] = zend_version();
@ -323,7 +322,7 @@ class CrashDump{
$this->data["general"]["os"] = Utils::getOS();
$this->data["general"]["composer_libraries"] = Versions::VERSIONS;
$this->addLine($this->server->getName() . " version: " . $version->getFullVersion(true) . " [Protocol " . ProtocolInfo::CURRENT_PROTOCOL . "]");
$this->addLine("Git commit: " . \pocketmine\GIT_COMMIT);
$this->addLine("Git commit: " . VersionInfo::getGitHash());
$this->addLine("uname -a: " . php_uname("a"));
$this->addLine("PHP Version: " . phpversion());
$this->addLine("Zend version: " . zend_version());

View File

@ -197,23 +197,6 @@ namespace pocketmine {
ErrorToExceptionHandler::set();
$version = new VersionString(VersionInfo::BASE_VERSION, VersionInfo::IS_DEVELOPMENT_BUILD, VersionInfo::BUILD_NUMBER);
define('pocketmine\VERSION', $version->getFullVersion(true));
$gitHash = str_repeat("00", 20);
if(\Phar::running(true) === ""){
$gitHash = Git::getRepositoryStatePretty(\pocketmine\PATH);
}else{
$phar = new \Phar(\Phar::running(false));
$meta = $phar->getMetadata();
if(isset($meta["git"])){
$gitHash = $meta["git"];
}
}
define('pocketmine\GIT_COMMIT', $gitHash);
$opts = getopt("", ["data:", "plugins:", "no-wizard", "enable-ansi", "disable-ansi"]);
$dataPath = isset($opts["data"]) ? $opts["data"] . DIRECTORY_SEPARATOR : realpath(getcwd()) . DIRECTORY_SEPARATOR;

View File

@ -288,7 +288,7 @@ class Server{
}
public function getPocketMineVersion() : string{
return \pocketmine\VERSION;
return VersionInfo::getVersionObj()->getFullVersion(true);
}
public function getVersion() : string{
@ -1455,7 +1455,7 @@ class Server{
$report = false;
}
if(strrpos(\pocketmine\GIT_COMMIT, "-dirty") !== false or \pocketmine\GIT_COMMIT === str_repeat("00", 20)){
if(strrpos(VersionInfo::getGitHash(), "-dirty") !== false or VersionInfo::getGitHash() === str_repeat("00", 20)){
$this->logger->debug("Not sending crashdump due to locally modified");
$report = false; //Don't send crashdumps for locally modified builds
}

View File

@ -23,6 +23,10 @@ declare(strict_types=1);
namespace pocketmine;
use pocketmine\utils\Git;
use pocketmine\utils\VersionString;
use function str_repeat;
final class VersionInfo{
public const NAME = "PocketMine-MP";
public const BASE_VERSION = "4.0.0";
@ -32,4 +36,37 @@ final class VersionInfo{
private function __construct(){
//NOOP
}
/** @var string|null */
private static $gitHash = null;
public static function getGitHash() : string{
if(self::$gitHash === null){
$gitHash = str_repeat("00", 20);
if(\Phar::running(true) === ""){
$gitHash = Git::getRepositoryStatePretty(\pocketmine\PATH);
}else{
$phar = new \Phar(\Phar::running(false));
$meta = $phar->getMetadata();
if(isset($meta["git"])){
$gitHash = $meta["git"];
}
}
self::$gitHash = $gitHash;
}
return self::$gitHash;
}
/** @var VersionString|null */
private static $fullVersion = null;
public static function getVersionObj() : VersionString{
if(self::$fullVersion === null){
self::$fullVersion = new VersionString(self::BASE_VERSION, self::IS_DEVELOPMENT_BUILD, self::BUILD_NUMBER);
}
return self::$fullVersion;
}
}

View File

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

View File

@ -160,7 +160,7 @@ class AutoUpdater{
if($this->updateInfo === null){
return;
}
$currentVersion = new VersionString(VersionInfo::BASE_VERSION, VersionInfo::IS_DEVELOPMENT_BUILD, VersionInfo::BUILD_NUMBER);
$currentVersion = VersionInfo::getVersionObj();
try{
$newVersion = new VersionString($this->updateInfo->base_version, $this->updateInfo->is_dev, $this->updateInfo->build);
}catch(\InvalidArgumentException $e){

View File

@ -224,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 " . VersionInfo::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 . "/" . VersionInfo::getVersionObj()->getFullVersion(true)], $extraHeaders),
CURLOPT_HEADER => true
]);
try{

View File

@ -25,6 +25,4 @@ define('pocketmine\_PHPSTAN_ANALYSIS', true);
//TODO: these need to be defined properly or removed
define('pocketmine\COMPOSER_AUTOLOADER_PATH', dirname(__DIR__, 2) . '/vendor/autoload.php');
define('pocketmine\GIT_COMMIT', str_repeat('00', 20));
define('pocketmine\PLUGIN_PATH', '');
define('pocketmine\VERSION', '9.9.9');

View File

@ -30,11 +30,6 @@ parameters:
count: 7
path: ../../../src/MemoryManager.php
-
message: "#^Cannot access offset 'git' on mixed\\.$#"
count: 2
path: ../../../src/PocketMine.php
-
message: "#^Cannot cast mixed to int\\.$#"
count: 6
@ -90,6 +85,11 @@ parameters:
count: 1
path: ../../../src/ServerConfigGroup.php
-
message: "#^Cannot access offset 'git' on mixed\\.$#"
count: 2
path: ../../../src/VersionInfo.php
-
message: "#^Cannot cast mixed to string\\.$#"
count: 1