PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
VersionString.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 
22 namespace pocketmine\utils;
23 
24 
29  private $major;
30  private $build;
31  private $minor;
32  private $development = false;
33 
34  public function __construct($version = \pocketmine\VERSION){
35  if(is_int($version)){
36  $this->minor = $version & 0x1F;
37  $this->major = ($version >> 5) & 0x0F;
38  $this->generation = ($version >> 9) & 0x0F;
39  }else{
40  $version = preg_split("/([A-Za-z]*)[ _\\-]?([0-9]*)\\.([0-9]*)\\.{0,1}([0-9]*)(dev|)(-[\\0-9]{1,}|)/", $version, -1, PREG_SPLIT_DELIM_CAPTURE);
41  $this->generation = isset($version[2]) ? (int) $version[2] : 0; //0-15
42  $this->major = isset($version[3]) ? (int) $version[3] : 0; //0-15
43  $this->minor = isset($version[4]) ? (int) $version[4] : 0; //0-31
44  $this->development = $version[5] === "dev" ? true : false;
45  if($version[6] !== ""){
46  $this->build = intval(substr($version[6], 1));
47  }else{
48  $this->build = 0;
49  }
50  }
51  }
52 
53  public function getNumber(){
54  return (int) (($this->generation << 9) + ($this->major << 5) + $this->minor);
55  }
56 
60  public function getStage(){
61  return "final";
62  }
63 
64  public function getGeneration(){
65  return $this->generation;
66  }
67 
68  public function getMajor(){
69  return $this->major;
70  }
71 
72  public function getMinor(){
73  return $this->minor;
74  }
75 
76  public function getRelease(){
77  return $this->generation . "." . $this->major . ($this->minor > 0 ? "." . $this->minor : "");
78  }
79 
80  public function getBuild(){
81  return $this->build;
82  }
83 
84  public function isDev(){
85  return $this->development === true;
86  }
87 
88  public function get($build = false){
89  return $this->getRelease() . ($this->development === true ? "dev" : "") . (($this->build > 0 and $build === true) ? "-" . $this->build : "");
90  }
91 
92  public function __toString(){
93  return $this->get();
94  }
95 
96  public function compare($target, $diff = false){
97  if(($target instanceof VersionString) === false){
98  $target = new VersionString($target);
99  }
100  $number = $this->getNumber();
101  $tNumber = $target->getNumber();
102  if($diff === true){
103  return $tNumber - $number;
104  }
105  if($number > $tNumber){
106  return -1; //Target is older
107  }elseif($number < $tNumber){
108  return 1; //Target is newer
109  }elseif($target->getBuild() > $this->getBuild()){
110  return 1;
111  }elseif($target->getBuild() < $this->getBuild()){
112  return -1;
113  }else{
114  return 0; //Same version
115  }
116  }
117 }