Merged server and API version

This commit is contained in:
Dylan K. Taylor
2018-06-17 12:54:18 +01:00
parent 77f3ca4d47
commit 1f9bed275a
8 changed files with 81 additions and 69 deletions

View File

@ -28,47 +28,62 @@ namespace pocketmine\utils;
* Manages PocketMine-MP version strings, and compares them
*/
class VersionString{
/** @var int */
private $generation;
/** @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;
/**
* VersionString constructor.
*
* @param int|string $version
* @param string $baseVersion
* @param bool $isDevBuild
* @param int $buildNumber
*/
public function __construct($version = \pocketmine\VERSION){
if(is_int($version)){
$this->minor = $version & 0x1F;
$this->major = ($version >> 5) & 0x0F;
$this->generation = ($version >> 9) & 0x0F;
}else{
$version = preg_split("/([A-Za-z]*)[ _\\-]?([0-9]*)\\.([0-9]*)\\.{0,1}([0-9]*)(dev|)(-[\\0-9]{1,}|)/", $version, -1, PREG_SPLIT_DELIM_CAPTURE);
$this->generation = (int) ($version[2] ?? 0); //0-15
$this->major = (int) ($version[3] ?? 0); //0-15
$this->minor = (int) ($version[4] ?? 0); //0-31
$this->development = $version[5] === "dev";
if($version[6] !== ""){
$this->build = (int) substr($version[6], 1);
}else{
$this->build = 0;
}
public function __construct(string $baseVersion, bool $isDevBuild = false, int $buildNumber = 0){
$this->baseVersion = $baseVersion;
$this->development = $isDevBuild;
$this->build = $buildNumber;
preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)(?:-(.*))?$/', $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->generation << 9) + ($this->major << 5) + $this->minor);
return (($this->major << 9) + ($this->minor << 5) + $this->patch);
}
public function getGeneration() : int{
return $this->generation;
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{
@ -79,8 +94,12 @@ class VersionString{
return $this->minor;
}
public function getRelease() : string{
return $this->generation . "." . $this->major . "." . $this->minor;
public function getPatch() : int{
return $this->patch;
}
public function getSuffix() : string{
return $this->suffix;
}
public function getBuild() : int{
@ -91,24 +110,17 @@ class VersionString{
return $this->development;
}
public function get(bool $build = false) : string{
return $this->getRelease() . ($this->development ? "dev" : "") . (($this->build > 0 and $build) ? "-" . $this->build : "");
}
public function __toString() : string{
return $this->get();
return $this->getFullVersion();
}
/**
* @param VersionString|int|string $target
* @param bool $diff
* @param VersionString $target
* @param bool $diff
*
* @return int
*/
public function compare($target, bool $diff = false) : int{
if(!($target instanceof VersionString)){
$target = new VersionString($target);
}
public function compare(VersionString $target, bool $diff = false) : int{
$number = $this->getNumber();
$tNumber = $target->getNumber();
if($diff){