From e4e4ef5f2a5b50ad9dc546b08a1f812da43ca0ef Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 2 Jun 2017 12:50:48 +0100 Subject: [PATCH] Fixed preprocessor issues with signed bytes and floats, close #967 --- src/pocketmine/nbt/NBT.php | 8 ++- src/pocketmine/nbt/tag/ByteTag.php | 2 +- .../network/mcpe/protocol/DataPacket.php | 6 +- src/pocketmine/utils/Binary.php | 69 +++++++++---------- src/pocketmine/utils/BinaryStream.php | 16 +++-- tests/preprocessor | 2 +- 6 files changed, 56 insertions(+), 47 deletions(-) diff --git a/src/pocketmine/nbt/NBT.php b/src/pocketmine/nbt/NBT.php index 1e0b5b6ec..ba84bbab6 100644 --- a/src/pocketmine/nbt/NBT.php +++ b/src/pocketmine/nbt/NBT.php @@ -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){ diff --git a/src/pocketmine/nbt/tag/ByteTag.php b/src/pocketmine/nbt/tag/ByteTag.php index 33f9cfbee..4f34b1fff 100644 --- a/src/pocketmine/nbt/tag/ByteTag.php +++ b/src/pocketmine/nbt/tag/ByteTag.php @@ -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){ diff --git a/src/pocketmine/network/mcpe/protocol/DataPacket.php b/src/pocketmine/network/mcpe/protocol/DataPacket.php index b97a53479..7dea45991 100644 --- a/src/pocketmine/network/mcpe/protocol/DataPacket.php +++ b/src/pocketmine/network/mcpe/protocol/DataPacket.php @@ -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); } /** diff --git a/src/pocketmine/utils/Binary.php b/src/pocketmine/utils/Binary.php index 922c6c582..7fd18495c 100644 --- a/src/pocketmine/utils/Binary.php +++ b/src/pocketmine/utils/Binary.php @@ -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){ diff --git a/src/pocketmine/utils/BinaryStream.php b/src/pocketmine/utils/BinaryStream.php index f6ccfe28e..6b8f9d9d5 100644 --- a/src/pocketmine/utils/BinaryStream.php +++ b/src/pocketmine/utils/BinaryStream.php @@ -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){ diff --git a/tests/preprocessor b/tests/preprocessor index 6099fff7a..a635b557d 160000 --- a/tests/preprocessor +++ b/tests/preprocessor @@ -1 +1 @@ -Subproject commit 6099fff7a32fc069607c6a7339da7d19144146e4 +Subproject commit a635b557db631fb0e25ca7666f768ff59c7cd390