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);
}
public function getByte(bool $signed = false){
return Binary::readByte($this->get(1), $signed);
public function getByte(){
return Binary::readByte($this->get(1));
}
public function getSignedByte(){
return Binary::readSignedByte($this->get(1));
}
public function putByte($v){

View File

@ -32,7 +32,7 @@ class ByteTag extends NamedTag{
}
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){

View File

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

View File

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

View File

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

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