This commit is contained in:
Shoghi Cervantes 2014-03-06 23:38:46 +01:00
parent 993620341a
commit 801e924783
2 changed files with 52 additions and 9 deletions

View File

@ -601,6 +601,14 @@ class Utils{
return Utils::writeByte($b === true ? 1 : 0); return Utils::writeByte($b === true ? 1 : 0);
} }
/**
* Reads an unsigned/signed byte
*
* @param $c
* @param bool $signed
*
* @return int
*/
public static function readByte($c, $signed = true){ public static function readByte($c, $signed = true){
$b = ord($c{0}); $b = ord($c{0});
if($signed === true and ($b & 0x80) === 0x80){ //calculate Two's complement if($signed === true and ($b & 0x80) === 0x80){ //calculate Two's complement
@ -610,6 +618,13 @@ class Utils{
return $b; return $b;
} }
/**
* Writes an unsigned/signed byte
*
* @param $c
*
* @return bool|string
*/
public static function writeByte($c){ public static function writeByte($c){
if($c > 0xff){ if($c > 0xff){
return false; return false;
@ -621,6 +636,14 @@ class Utils{
return chr($c); return chr($c);
} }
/**
* Reads a 16-bit signed/unsigned big-endian number
*
* @param $str
* @param bool $signed
*
* @return int
*/
public static function readShort($str, $signed = true){ public static function readShort($str, $signed = true){
list(, $unpacked) = @unpack("n", $str); list(, $unpacked) = @unpack("n", $str);
if($unpacked > 0x7fff and $signed === true){ if($unpacked > 0x7fff and $signed === true){
@ -630,6 +653,13 @@ class Utils{
return $unpacked; return $unpacked;
} }
/**
* Writes a 16-bit signed/unsigned big-endian number
*
* @param $value
*
* @return string
*/
public static function writeShort($value){ public static function writeShort($value){
if($value < 0){ if($value < 0){
$value += 0x10000; $value += 0x10000;
@ -638,6 +668,14 @@ class Utils{
return pack("n", $value); return pack("n", $value);
} }
/**
* Reads a 16-bit signed/unsigned little-endian number
*
* @param $str
* @param bool $signed
*
* @return int
*/
public static function readLShort($str, $signed = true){ public static function readLShort($str, $signed = true){
list(, $unpacked) = @unpack("v", $str); list(, $unpacked) = @unpack("v", $str);
if($unpacked > 0x7fff and $signed === true){ if($unpacked > 0x7fff and $signed === true){
@ -647,6 +685,13 @@ class Utils{
return $unpacked; return $unpacked;
} }
/**
* Writes a 16-bit signed/unsigned little-endian number
*
* @param $value
*
* @return string
*/
public static function writeLShort($value){ public static function writeLShort($value){
if($value < 0){ if($value < 0){
$value += 0x10000; $value += 0x10000;
@ -657,7 +702,7 @@ class Utils{
public static function readInt($str){ public static function readInt($str){
list(, $unpacked) = @unpack("N", $str); list(, $unpacked) = @unpack("N", $str);
if($unpacked >= 2147483648){ if($unpacked > 2147483647){
$unpacked -= 4294967296; $unpacked -= 4294967296;
} }
@ -665,10 +710,6 @@ class Utils{
} }
public static function writeInt($value){ public static function writeInt($value){
if($value < 0){
$value += 0x100000000;
}
return pack("N", $value); return pack("N", $value);
} }
@ -682,10 +723,6 @@ class Utils{
} }
public static function writeLInt($value){ public static function writeLInt($value){
if($value < 0){
$value += 0x100000000;
}
return pack("V", $value); return pack("V", $value);
} }

View File

@ -23,6 +23,12 @@ namespace PocketMine\Utils;
use PocketMine; use PocketMine;
/**
* Class VersionString
* Manages PocketMine-MP Version strings, and compares them
*
* @package PocketMine\Utils
*/
class VersionString{ class VersionString{
public static $stageOrder = array( public static $stageOrder = array(
"alpha" => 0, "alpha" => 0,