mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-27 13:49:55 +00:00
This is better for performance because these then don't need to be reevaluated every time they are called. When encountering an unqualified function or constant reference, PHP will first try to locate a symbol in the current namespace by that name, and then fall back to the global namespace. This short-circuits the check, which has substantial performance effects in some cases - in particular, ord(), chr() and strlen() show ~1500x faster calls when they are fully qualified. However, this doesn't mean that PM is getting a massive amount faster. In real world terms, this translates to about 10-15% performance improvement. But before anyone gets excited, you should know that the CodeOptimizer in the PreProcessor repo has been applying fully-qualified symbol optimizations to Jenkins builds for years, which is one of the reasons why Jenkins builds have better performance than home-built or source installations. We're choosing to do this for the sake of future SafePHP integration and also to be able to get rid of the buggy CodeOptimizer, so that phar and source are more consistent.
147 lines
3.4 KiB
PHP
147 lines
3.4 KiB
PHP
<?php
|
|
|
|
/*
|
|
*
|
|
* ____ _ _ __ __ _ __ __ ____
|
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* @author PocketMine Team
|
|
* @link http://www.pocketmine.net/
|
|
*
|
|
*
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace pocketmine\utils;
|
|
|
|
|
|
use function count;
|
|
use function preg_match;
|
|
|
|
/**
|
|
* Manages PocketMine-MP version strings, and compares them
|
|
*/
|
|
class VersionString{
|
|
/** @var string */
|
|
private $baseVersion;
|
|
/** @var string */
|
|
private $suffix;
|
|
|
|
/** @var int */
|
|
private $major;
|
|
/** @var int */
|
|
private $minor;
|
|
/** @var int */
|
|
private $patch;
|
|
|
|
/** @var int */
|
|
private $build;
|
|
/** @var bool */
|
|
private $development = false;
|
|
|
|
/**
|
|
* @param string $baseVersion
|
|
* @param bool $isDevBuild
|
|
* @param int $buildNumber
|
|
*/
|
|
public function __construct(string $baseVersion, bool $isDevBuild = false, int $buildNumber = 0){
|
|
$this->baseVersion = $baseVersion;
|
|
$this->development = $isDevBuild;
|
|
$this->build = $buildNumber;
|
|
|
|
preg_match('/(\d+)\.(\d+)\.(\d+)(?:-(.*))?$/', $this->baseVersion, $matches);
|
|
if(count($matches) < 4){
|
|
throw new \InvalidArgumentException("Invalid base version \"$baseVersion\", should contain at least 3 version digits");
|
|
}
|
|
|
|
$this->major = (int) $matches[1];
|
|
$this->minor = (int) $matches[2];
|
|
$this->patch = (int) $matches[3];
|
|
$this->suffix = $matches[4] ?? "";
|
|
}
|
|
|
|
public function getNumber() : int{
|
|
return (($this->major << 9) + ($this->minor << 5) + $this->patch);
|
|
}
|
|
|
|
public function getBaseVersion() : string{
|
|
return $this->baseVersion;
|
|
}
|
|
|
|
public function getFullVersion(bool $build = false) : string{
|
|
$retval = $this->baseVersion;
|
|
if($this->development){
|
|
$retval .= "+dev";
|
|
if($build and $this->build > 0){
|
|
$retval .= "." . $this->build;
|
|
}
|
|
}
|
|
|
|
return $retval;
|
|
}
|
|
|
|
public function getMajor() : int{
|
|
return $this->major;
|
|
}
|
|
|
|
public function getMinor() : int{
|
|
return $this->minor;
|
|
}
|
|
|
|
public function getPatch() : int{
|
|
return $this->patch;
|
|
}
|
|
|
|
public function getSuffix() : string{
|
|
return $this->suffix;
|
|
}
|
|
|
|
public function getBuild() : int{
|
|
return $this->build;
|
|
}
|
|
|
|
public function isDev() : bool{
|
|
return $this->development;
|
|
}
|
|
|
|
public function __toString() : string{
|
|
return $this->getFullVersion();
|
|
}
|
|
|
|
/**
|
|
* @param VersionString $target
|
|
* @param bool $diff
|
|
*
|
|
* @return int
|
|
*/
|
|
public function compare(VersionString $target, bool $diff = false) : int{
|
|
$number = $this->getNumber();
|
|
$tNumber = $target->getNumber();
|
|
if($diff){
|
|
return $tNumber - $number;
|
|
}
|
|
if($number > $tNumber){
|
|
return -1; //Target is older
|
|
}elseif($number < $tNumber){
|
|
return 1; //Target is newer
|
|
}elseif($target->isDev() and !$this->isDev()){
|
|
return -1; //Dev builds of the same version are always considered older than a release
|
|
}elseif($target->getBuild() > $this->getBuild()){
|
|
return 1;
|
|
}elseif($target->getBuild() < $this->getBuild()){
|
|
return -1;
|
|
}else{
|
|
return 0; //Same version
|
|
}
|
|
}
|
|
}
|