mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-15 02:09:42 +00:00
Made Binary::readVarInt/VarLong methods less useless
This commit is contained in:
parent
caced595d2
commit
db3cd1829c
@ -285,7 +285,7 @@ class NBT{
|
||||
|
||||
public function getInt(bool $network = false){
|
||||
if($network === true){
|
||||
return Binary::readVarInt($this);
|
||||
return Binary::readVarInt($this->buffer, $this->offset);
|
||||
}
|
||||
return $this->endianness === self::BIG_ENDIAN ? Binary::readInt($this->get(4)) : Binary::readLInt($this->get(4));
|
||||
}
|
||||
@ -323,7 +323,7 @@ class NBT{
|
||||
}
|
||||
|
||||
public function getString(bool $network = false){
|
||||
$len = $network ? Binary::readUnsignedVarInt($this) : $this->getShort();
|
||||
$len = $network ? Binary::readUnsignedVarInt($this->buffer, $this->offset) : $this->getShort();
|
||||
return $this->get($len);
|
||||
}
|
||||
|
||||
|
@ -341,13 +341,14 @@ class Binary{
|
||||
/**
|
||||
* Reads a 32-bit zigzag-encoded variable-length integer from the supplied stream.
|
||||
*
|
||||
* @param \pocketmine\nbt\NBT|BinaryStream $stream
|
||||
* @param string $buffer
|
||||
* @param int &$offset
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function readVarInt($stream){
|
||||
public static function readVarInt(string $buffer, int &$offset){
|
||||
$shift = PHP_INT_SIZE === 8 ? 63 : 31;
|
||||
$raw = self::readUnsignedVarInt($stream);
|
||||
$raw = self::readUnsignedVarInt($buffer, $offset);
|
||||
$temp = ((($raw << $shift) >> $shift) ^ $raw) >> 1;
|
||||
return $temp ^ ($raw & (1 << $shift));
|
||||
}
|
||||
@ -355,18 +356,21 @@ class Binary{
|
||||
/**
|
||||
* Reads a 32-bit variable-length unsigned integer from the supplied stream.
|
||||
*
|
||||
* @param \pocketmine\nbt\NBT|BinaryStream $stream
|
||||
* @param string $buffer
|
||||
* @param int &$offset
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function readUnsignedVarInt($stream){
|
||||
public static function readUnsignedVarInt(string $buffer, int &$offset){
|
||||
$value = 0;
|
||||
for($i = 0; $i <= 35; $i += 7){
|
||||
$b = $stream->getByte();
|
||||
$b = ord($buffer{$offset++});
|
||||
$value |= (($b & 0x7f) << $i);
|
||||
|
||||
if(($b & 0x80) === 0){
|
||||
return $value;
|
||||
}elseif(!isset($buffer{$offset})){
|
||||
throw new \UnexpectedValueException("Expected more bytes, none left to read");
|
||||
}
|
||||
}
|
||||
|
||||
@ -414,27 +418,29 @@ class Binary{
|
||||
|
||||
/**
|
||||
* Reads a 64-bit zigzag-encoded variable-length integer from the supplied stream.
|
||||
* @param \pocketmine\nbt\NBT|BinaryStream $stream
|
||||
* @param string $buffer
|
||||
* @param int &$offset
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
public static function readVarLong($stream){
|
||||
public static function readVarLong(string $buffer, int &$offset){
|
||||
if(PHP_INT_SIZE === 8){
|
||||
return self::readVarLong_64($stream);
|
||||
return self::readVarLong_64($buffer, $offset);
|
||||
}else{
|
||||
return self::readVarLong_32($stream);
|
||||
return self::readVarLong_32($buffer, $offset);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy BC Math zigzag VarLong reader. Will work on 32-bit or 64-bit, but will be slower than the regular 64-bit method.
|
||||
* @param \pocketmine\nbt\NBT|BinaryStream $stream
|
||||
* @param string $buffer
|
||||
* @param int &$offset
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function readVarLong_32($stream){
|
||||
public static function readVarLong_32(string $buffer, int &$offset){
|
||||
/** @var string $raw */
|
||||
$raw = self::readUnsignedVarLong_32($stream);
|
||||
$raw = self::readUnsignedVarLong_32($buffer, $offset);
|
||||
$result = bcdiv($raw, "2");
|
||||
if(bcmod($raw, "2") === "1"){
|
||||
$result = bcsub(bcmul($result, "-1"), "1");
|
||||
@ -445,44 +451,49 @@ class Binary{
|
||||
|
||||
/**
|
||||
* 64-bit zizgag VarLong reader.
|
||||
* @param \pocketmine\nbt\NBT|BinaryStream $stream
|
||||
* @param string $buffer
|
||||
* @param int &$offset
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function readVarLong_64($stream){
|
||||
$raw = self::readUnsignedVarLong_64($stream);
|
||||
public static function readVarLong_64(string $buffer, int &$offset){
|
||||
$raw = self::readUnsignedVarLong_64($buffer, $offset);
|
||||
$temp = ((($raw << 63) >> 63) ^ $raw) >> 1;
|
||||
return $temp ^ ($raw & (1 << 63));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an unsigned VarLong from the supplied stream.
|
||||
* @param \pocketmine\nbt\NBT|BinaryStream $stream
|
||||
* @param string $buffer
|
||||
* @param int &$offset
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
public static function readUnsignedVarLong($stream){
|
||||
public static function readUnsignedVarLong(string $buffer, int &$offset){
|
||||
if(PHP_INT_SIZE === 8){
|
||||
return self::readUnsignedVarLong_64($stream);
|
||||
return self::readUnsignedVarLong_64($buffer, $offset);
|
||||
}else{
|
||||
return self::readUnsignedVarLong_32($stream);
|
||||
return self::readUnsignedVarLong_32($buffer, $offset);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy BC Math unsigned VarLong reader.
|
||||
* @param \pocketmine\nbt\NBT|BinaryStream $stream
|
||||
* @param string $buffer
|
||||
* @param int &$offset
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function readUnsignedVarLong_32($stream){
|
||||
public static function readUnsignedVarLong_32(string $buffer, int &$offset){
|
||||
$value = "0";
|
||||
for($i = 0; $i <= 63; $i += 7){
|
||||
$b = $stream->getByte();
|
||||
$b = ord($buffer{$offset++});
|
||||
$value = bcadd($value, bcmul($b & 0x7f, bcpow("2", "$i")));
|
||||
|
||||
if(($b & 0x80) === 0){
|
||||
return $value;
|
||||
}elseif(!isset($buffer{$offset})){
|
||||
throw new \UnexpectedValueException("Expected more bytes, none left to read");
|
||||
}
|
||||
}
|
||||
|
||||
@ -491,18 +502,21 @@ class Binary{
|
||||
|
||||
/**
|
||||
* 64-bit unsigned VarLong reader.
|
||||
* @param \pocketmine\nbt\NBT|BinaryStream $stream
|
||||
* @param string $buffer
|
||||
* @param int &$offset
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function readUnsignedVarLong_64($stream){
|
||||
public static function readUnsignedVarLong_64(string $buffer, int &$offset){
|
||||
$value = 0;
|
||||
for($i = 0; $i <= 63; $i += 7){
|
||||
$b = $stream->getByte();
|
||||
$b = ord($buffer{$offset++});
|
||||
$value |= (($b & 0x7f) << $i);
|
||||
|
||||
if(($b & 0x80) === 0){
|
||||
return $value;
|
||||
}elseif(!isset($buffer{$offset})){
|
||||
throw new \UnexpectedValueException("Expected more bytes, none left to read");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -246,7 +246,7 @@ class BinaryStream extends \stdClass{
|
||||
* @return int
|
||||
*/
|
||||
public function getUnsignedVarInt(){
|
||||
return Binary::readUnsignedVarInt($this);
|
||||
return Binary::readUnsignedVarInt($this->buffer, $this->offset);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -262,7 +262,7 @@ class BinaryStream extends \stdClass{
|
||||
* @return int
|
||||
*/
|
||||
public function getVarInt(){
|
||||
return Binary::readVarInt($this);
|
||||
return Binary::readVarInt($this->buffer, $this->offset);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -278,7 +278,7 @@ class BinaryStream extends \stdClass{
|
||||
* @return int|string int, or the string representation of an int64 on 32-bit platforms
|
||||
*/
|
||||
public function getUnsignedVarLong(){
|
||||
return Binary::readUnsignedVarLong($this);
|
||||
return Binary::readUnsignedVarLong($this->buffer, $this->offset);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -294,7 +294,7 @@ class BinaryStream extends \stdClass{
|
||||
* @return int|string int, or the string representation of an int64 on 32-bit platforms
|
||||
*/
|
||||
public function getVarLong(){
|
||||
return Binary::readVarLong($this);
|
||||
return Binary::readVarLong($this->buffer, $this->offset);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user