mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-07 10:22:56 +00:00
Typehinted things in BinaryStream, sorted some methods and related bugfixes
This commit is contained in:
@ -29,10 +29,12 @@ use pocketmine\item\Item;
|
||||
|
||||
class BinaryStream{
|
||||
|
||||
/** @var int */
|
||||
public $offset;
|
||||
/** @var string */
|
||||
public $buffer;
|
||||
|
||||
public function __construct($buffer = "", $offset = 0){
|
||||
public function __construct(string $buffer = "", int $offset = 0){
|
||||
$this->buffer = $buffer;
|
||||
$this->offset = $offset;
|
||||
}
|
||||
@ -42,24 +44,24 @@ class BinaryStream{
|
||||
$this->offset = 0;
|
||||
}
|
||||
|
||||
public function setBuffer($buffer = null, $offset = 0){
|
||||
public function setBuffer(string $buffer = "", int $offset = 0){
|
||||
$this->buffer = $buffer;
|
||||
$this->offset = (int) $offset;
|
||||
$this->offset = $offset;
|
||||
}
|
||||
|
||||
public function getOffset(){
|
||||
public function getOffset() : int{
|
||||
return $this->offset;
|
||||
}
|
||||
|
||||
public function getBuffer(){
|
||||
public function getBuffer() : string{
|
||||
return $this->buffer;
|
||||
}
|
||||
|
||||
public function get($len){
|
||||
public function get(int $len) : string{
|
||||
if($len < 0){
|
||||
$this->offset = strlen($this->buffer) - 1;
|
||||
return "";
|
||||
}elseif($len === true){
|
||||
}elseif($len === 0){
|
||||
$str = substr($this->buffer, $this->offset);
|
||||
$this->offset = strlen($this->buffer);
|
||||
return $str;
|
||||
@ -68,115 +70,19 @@ class BinaryStream{
|
||||
return $len === 1 ? $this->buffer{$this->offset++} : substr($this->buffer, ($this->offset += $len) - $len, $len);
|
||||
}
|
||||
|
||||
public function put($str){
|
||||
public function put(string $str){
|
||||
$this->buffer .= $str;
|
||||
}
|
||||
|
||||
|
||||
public function getBool() : bool{
|
||||
return $this->get(1) !== "\x00";
|
||||
}
|
||||
|
||||
public function putBool($v){
|
||||
public function putBool(bool $v){
|
||||
$this->buffer .= ($v ? "\x01" : "\x00");
|
||||
}
|
||||
|
||||
public function getLong(){
|
||||
return Binary::readLong($this->get(8));
|
||||
}
|
||||
|
||||
public function putLong($v){
|
||||
$this->buffer .= Binary::writeLong($v);
|
||||
}
|
||||
|
||||
public function getInt(){
|
||||
return Binary::readInt($this->get(4));
|
||||
}
|
||||
|
||||
public function putInt($v){
|
||||
$this->buffer .= Binary::writeInt($v);
|
||||
}
|
||||
|
||||
public function getLLong(){
|
||||
return Binary::readLLong($this->get(8));
|
||||
}
|
||||
|
||||
public function putLLong($v){
|
||||
$this->buffer .= Binary::writeLLong($v);
|
||||
}
|
||||
|
||||
public function getLInt(){
|
||||
return Binary::readLInt($this->get(4));
|
||||
}
|
||||
|
||||
public function putLInt($v){
|
||||
$this->buffer .= Binary::writeLInt($v);
|
||||
}
|
||||
|
||||
public function getSignedShort(){
|
||||
return Binary::readSignedShort($this->get(2));
|
||||
}
|
||||
|
||||
public function putShort($v){
|
||||
$this->buffer .= Binary::writeShort($v);
|
||||
}
|
||||
|
||||
public function getShort(){
|
||||
return Binary::readShort($this->get(2));
|
||||
}
|
||||
|
||||
public function putSignedShort($v){
|
||||
$this->buffer .= Binary::writeShort($v);
|
||||
}
|
||||
|
||||
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){
|
||||
$this->buffer .= Binary::writeFloat($v);
|
||||
}
|
||||
|
||||
public function getLShort($signed = true){
|
||||
return $signed ? Binary::readSignedLShort($this->get(2)) : Binary::readLShort($this->get(2));
|
||||
}
|
||||
|
||||
public function putLShort($v){
|
||||
$this->buffer .= Binary::writeLShort($v);
|
||||
}
|
||||
|
||||
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){
|
||||
$this->buffer .= Binary::writeLFloat($v);
|
||||
}
|
||||
|
||||
|
||||
public function getTriad(){
|
||||
return Binary::readTriad($this->get(3));
|
||||
}
|
||||
|
||||
public function putTriad($v){
|
||||
$this->buffer .= Binary::writeTriad($v);
|
||||
}
|
||||
|
||||
|
||||
public function getLTriad(){
|
||||
return Binary::readLTriad($this->get(3));
|
||||
}
|
||||
|
||||
public function putLTriad($v){
|
||||
$this->buffer .= Binary::writeLTriad($v);
|
||||
}
|
||||
|
||||
public function getByte() : int{
|
||||
return ord($this->buffer{$this->offset++});
|
||||
@ -186,7 +92,131 @@ class BinaryStream{
|
||||
$this->buffer .= chr($v);
|
||||
}
|
||||
|
||||
public function getUUID(){
|
||||
|
||||
public function getShort() : int{
|
||||
return Binary::readShort($this->get(2));
|
||||
}
|
||||
|
||||
public function getSignedShort() : int{
|
||||
return Binary::readSignedShort($this->get(2));
|
||||
}
|
||||
|
||||
public function putShort(int $v){
|
||||
$this->buffer .= Binary::writeShort($v);
|
||||
}
|
||||
|
||||
public function getLShort() : int{
|
||||
return Binary::readLShort($this->get(2));
|
||||
}
|
||||
|
||||
public function getSignedLShort() : int{
|
||||
return Binary::readSignedLShort($this->get(2));
|
||||
}
|
||||
|
||||
public function putLShort(int $v){
|
||||
$this->buffer .= Binary::writeLShort($v);
|
||||
}
|
||||
|
||||
|
||||
public function getTriad() : int{
|
||||
return Binary::readTriad($this->get(3));
|
||||
}
|
||||
|
||||
public function putTriad(int $v){
|
||||
$this->buffer .= Binary::writeTriad($v);
|
||||
}
|
||||
|
||||
public function getLTriad() : int{
|
||||
return Binary::readLTriad($this->get(3));
|
||||
}
|
||||
|
||||
public function putLTriad(int $v){
|
||||
$this->buffer .= Binary::writeLTriad($v);
|
||||
}
|
||||
|
||||
|
||||
public function getInt() : int{
|
||||
return Binary::readInt($this->get(4));
|
||||
}
|
||||
|
||||
public function putInt(int $v){
|
||||
$this->buffer .= Binary::writeInt($v);
|
||||
}
|
||||
|
||||
public function getLInt() : int{
|
||||
return Binary::readLInt($this->get(4));
|
||||
}
|
||||
|
||||
public function putLInt(int $v){
|
||||
$this->buffer .= Binary::writeLInt($v);
|
||||
}
|
||||
|
||||
|
||||
public function getFloat() : float{
|
||||
return Binary::readFloat($this->get(4));
|
||||
}
|
||||
|
||||
public function getRoundedFloat(int $accuracy) : float{
|
||||
return Binary::readRoundedFloat($this->get(4), $accuracy);
|
||||
}
|
||||
|
||||
public function putFloat(float $v){
|
||||
$this->buffer .= Binary::writeFloat($v);
|
||||
}
|
||||
|
||||
public function getLFloat() : float{
|
||||
return Binary::readLFloat($this->get(4));
|
||||
}
|
||||
|
||||
public function getRoundedLFloat(int $accuracy) : float{
|
||||
return Binary::readRoundedLFloat($this->get(4), $accuracy);
|
||||
}
|
||||
|
||||
public function putLFloat(float $v){
|
||||
$this->buffer .= Binary::writeLFloat($v);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int|string
|
||||
*/
|
||||
public function getLong(){
|
||||
return Binary::readLong($this->get(8));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|string $v
|
||||
*/
|
||||
public function putLong($v){
|
||||
$this->buffer .= Binary::writeLong($v);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|string
|
||||
*/
|
||||
public function getLLong(){
|
||||
return Binary::readLLong($this->get(8));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|string $v
|
||||
*/
|
||||
public function putLLong($v){
|
||||
$this->buffer .= Binary::writeLLong($v);
|
||||
}
|
||||
|
||||
|
||||
public function getString() : string{
|
||||
return $this->get($this->getUnsignedVarInt());
|
||||
}
|
||||
|
||||
public function putString(string $v){
|
||||
$this->putUnsignedVarInt(strlen($v));
|
||||
$this->put($v);
|
||||
}
|
||||
|
||||
|
||||
public function getUUID() : UUID{
|
||||
//This is actually two little-endian longs: UUID Most followed by UUID Least
|
||||
$part1 = $this->getLInt();
|
||||
$part0 = $this->getLInt();
|
||||
@ -202,7 +232,7 @@ class BinaryStream{
|
||||
$this->putLInt($uuid->getPart(2));
|
||||
}
|
||||
|
||||
public function getSlot(){
|
||||
public function getSlot() : Item{
|
||||
$id = $this->getVarInt();
|
||||
if($id <= 0){
|
||||
return Item::get(0, 0, 0);
|
||||
@ -260,20 +290,11 @@ class BinaryStream{
|
||||
$this->putVarInt(0); //CanDestroy entry count (TODO)
|
||||
}
|
||||
|
||||
public function getString(){
|
||||
return $this->get($this->getUnsignedVarInt());
|
||||
}
|
||||
|
||||
public function putString($v){
|
||||
$this->putUnsignedVarInt(strlen($v));
|
||||
$this->put($v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a 32-bit variable-length unsigned integer from the buffer and returns it.
|
||||
* @return int
|
||||
*/
|
||||
public function getUnsignedVarInt(){
|
||||
public function getUnsignedVarInt() : int{
|
||||
return Binary::readUnsignedVarInt($this->buffer, $this->offset);
|
||||
}
|
||||
|
||||
@ -281,7 +302,7 @@ class BinaryStream{
|
||||
* Writes a 32-bit variable-length unsigned integer to the end of the buffer.
|
||||
* @param int $v
|
||||
*/
|
||||
public function putUnsignedVarInt($v){
|
||||
public function putUnsignedVarInt(int $v){
|
||||
$this->put(Binary::writeUnsignedVarInt($v));
|
||||
}
|
||||
|
||||
@ -289,7 +310,7 @@ class BinaryStream{
|
||||
* Reads a 32-bit zigzag-encoded variable-length integer from the buffer and returns it.
|
||||
* @return int
|
||||
*/
|
||||
public function getVarInt(){
|
||||
public function getVarInt() : int{
|
||||
return Binary::readVarInt($this->buffer, $this->offset);
|
||||
}
|
||||
|
||||
@ -297,7 +318,7 @@ class BinaryStream{
|
||||
* Writes a 32-bit zigzag-encoded variable-length integer to the end of the buffer.
|
||||
* @param int $v
|
||||
*/
|
||||
public function putVarInt($v){
|
||||
public function putVarInt(int $v){
|
||||
$this->put(Binary::writeVarInt($v));
|
||||
}
|
||||
|
||||
@ -333,7 +354,11 @@ class BinaryStream{
|
||||
$this->buffer .= Binary::writeVarLong($v);
|
||||
}
|
||||
|
||||
public function feof(){
|
||||
/**
|
||||
* Returns whether the offset has reached the end of the buffer.
|
||||
* @return bool
|
||||
*/
|
||||
public function feof() : bool{
|
||||
return !isset($this->buffer{$this->offset});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user