Fixed preprocessor issues with signed bytes and floats, close #967

This commit is contained in:
Dylan K. Taylor 2017-06-02 12:50:48 +01:00
parent 3ac51e1095
commit e4e4ef5f2a
6 changed files with 56 additions and 47 deletions

View File

@ -267,8 +267,12 @@ class NBT{
$tag->write($this, $network); $tag->write($this, $network);
} }
public function getByte(bool $signed = false){ public function getByte(){
return Binary::readByte($this->get(1), $signed); return Binary::readByte($this->get(1));
}
public function getSignedByte(){
return Binary::readSignedByte($this->get(1));
} }
public function putByte($v){ public function putByte($v){

View File

@ -32,7 +32,7 @@ class ByteTag extends NamedTag{
} }
public function read(NBT $nbt, bool $network = false){ public function read(NBT $nbt, bool $network = false){
$this->value = $nbt->getByte(true); $this->value = $nbt->getSignedByte();
} }
public function write(NBT $nbt, bool $network = false){ public function write(NBT $nbt, bool $network = false){

View File

@ -337,9 +337,9 @@ abstract class DataPacket extends BinaryStream{
* @param float $z * @param float $z
*/ */
public function getVector3f(&$x, &$y, &$z){ public function getVector3f(&$x, &$y, &$z){
$x = $this->getLFloat(4); $x = $this->getRoundedLFloat(4);
$y = $this->getLFloat(4); $y = $this->getRoundedLFloat(4);
$z = $this->getLFloat(4); $z = $this->getRoundedLFloat(4);
} }
/** /**

View File

@ -82,56 +82,55 @@ class Binary{
/** /**
* Reads a byte boolean * Reads a byte boolean
* *
* @param $b * @param string $b
* *
* @return bool * @return bool
*/ */
public static function readBool($b){ public static function readBool(string $b) : bool{
return self::readByte($b, false) === 0 ? false : true; return $b !== "\x00";
} }
/** /**
* Writes a byte boolean * Writes a byte boolean
* *
* @param $b * @param bool $b
* *
* @return bool|string * @return string
*/ */
public static function writeBool($b){ public static function writeBool(bool $b) : string{
return self::writeByte($b === true ? 1 : 0); return $b ? "\x01" : "\x00";
} }
/** /**
* Reads an unsigned/signed byte * Reads an unsigned byte (0 - 255)
* *
* @param string $c * @param string $c
* @param bool $signed
* *
* @return int * @return int
*/ */
public static function readByte($c, $signed = true){ public static function readByte(string $c){
self::checkLength($c, 1); self::checkLength($c, 1);
$b = ord($c{0}); return ord($c{0});
}
if($signed){ /**
if(PHP_INT_SIZE === 8){ * Reads a signed byte (-128 - 127)
return $b << 56 >> 56; * @param string $c
}else{ *
return $b << 24 >> 24; * @return int
} */
}else{ public static function readSignedByte(string $c) : int{
return $b; return PHP_INT_SIZE === 8 ? (ord($c{0}) << 56 >> 56) : (ord($c{0}) << 24 >> 24);
}
} }
/** /**
* Writes an unsigned/signed byte * Writes an unsigned/signed byte
* *
* @param $c * @param int $c
* *
* @return string * @return string
*/ */
public static function writeByte($c){ public static function writeByte(int $c) : string{
return chr($c); return chr($c);
} }
@ -239,28 +238,26 @@ class Binary{
return pack("V", $value); return pack("V", $value);
} }
public static function readFloat($str, int $accuracy = -1){ public static function readFloat($str){
self::checkLength($str, 4); self::checkLength($str, 4);
$value = ENDIANNESS === self::BIG_ENDIAN ? unpack("f", $str)[1] : unpack("f", strrev($str))[1]; return (ENDIANNESS === self::BIG_ENDIAN ? unpack("f", $str)[1] : unpack("f", strrev($str))[1]);
if($accuracy > -1){ }
return round($value, $accuracy);
}else{ public static function readRoundedFloat(string $str, int $accuracy){
return $value; return round(self::readFloat($str), $accuracy);
}
} }
public static function writeFloat($value){ public static function writeFloat($value){
return ENDIANNESS === self::BIG_ENDIAN ? pack("f", $value) : strrev(pack("f", $value)); return ENDIANNESS === self::BIG_ENDIAN ? pack("f", $value) : strrev(pack("f", $value));
} }
public static function readLFloat($str, int $accuracy = -1){ public static function readLFloat($str){
self::checkLength($str, 4); self::checkLength($str, 4);
$value = ENDIANNESS === self::BIG_ENDIAN ? unpack("f", strrev($str))[1] : unpack("f", $str)[1]; return (ENDIANNESS === self::BIG_ENDIAN ? unpack("f", strrev($str))[1] : unpack("f", $str)[1]);
if($accuracy > -1){ }
return round($value, $accuracy);
}else{ public static function readRoundedLFloat(string $str, int $accuracy){
return $value; return round(self::readLFloat($str), $accuracy);
}
} }
public static function writeLFloat($value){ public static function writeLFloat($value){

View File

@ -126,8 +126,12 @@ class BinaryStream{
$this->buffer .= Binary::writeShort($v); $this->buffer .= Binary::writeShort($v);
} }
public function getFloat(int $accuracy = -1){ public function getFloat(){
return Binary::readFloat($this->get(4), $accuracy); return Binary::readFloat($this->get(4));
}
public function getRoundedFloat(int $accuracy){
return Binary::readRoundedFloat($this->get(4), $accuracy);
} }
public function putFloat($v){ public function putFloat($v){
@ -142,8 +146,12 @@ class BinaryStream{
$this->buffer .= Binary::writeLShort($v); $this->buffer .= Binary::writeLShort($v);
} }
public function getLFloat(int $accuracy = -1){ public function getLFloat(){
return Binary::readLFloat($this->get(4), $accuracy); return Binary::readLFloat($this->get(4));
}
public function getRoundedLFloat(int $accuracy){
return Binary::readRoundedLFloat($this->get(4), $accuracy);
} }
public function putLFloat($v){ public function putLFloat($v){

@ -1 +1 @@
Subproject commit 6099fff7a32fc069607c6a7339da7d19144146e4 Subproject commit a635b557db631fb0e25ca7666f768ff59c7cd390