Fixed extremely stupid zero-length bug in BinaryStream

pls don't kill me 😢
This commit is contained in:
Dylan K. Taylor 2017-06-10 18:33:54 +01:00
parent 4765242397
commit 2024e9ecdf
6 changed files with 23 additions and 10 deletions

View File

@ -46,7 +46,7 @@ class BatchPacket extends DataPacket{
} }
public function decode(){ public function decode(){
$this->payload = $this->get(0); $this->payload = $this->getRemaining();
} }
public function encode(){ public function encode(){

View File

@ -38,7 +38,7 @@ class BlockEntityDataPacket extends DataPacket{
public function decode(){ public function decode(){
$this->getBlockPosition($this->x, $this->y, $this->z); $this->getBlockPosition($this->x, $this->y, $this->z);
$this->namedtag = $this->get(0); $this->namedtag = $this->getRemaining();
} }
public function encode(){ public function encode(){

View File

@ -49,7 +49,7 @@ class CommandStepPacket extends DataPacket{
$this->inputJson = json_decode($this->getString()); $this->inputJson = json_decode($this->getString());
$this->outputJson = json_decode($this->getString()); $this->outputJson = json_decode($this->getString());
$this->get(0); //TODO: read command origin data $this->getRemaining(); //TODO: read command origin data
} }
public function encode(){ public function encode(){

View File

@ -45,7 +45,7 @@ class UnknownPacket extends DataPacket{
public function decode(){ public function decode(){
$this->offset -= 1; //Rewind one byte so we can read the PID $this->offset -= 1; //Rewind one byte so we can read the PID
$this->payload = $this->get(0); $this->payload = $this->getRemaining();
} }
public function encode(){ public function encode(){

View File

@ -53,7 +53,7 @@ class UpdateTradePacket extends DataPacket{
$this->traderEid = $this->getEntityUniqueId(); $this->traderEid = $this->getEntityUniqueId();
$this->playerEid = $this->getEntityUniqueId(); $this->playerEid = $this->getEntityUniqueId();
$this->displayName = $this->getString(); $this->displayName = $this->getString();
$this->offers = $this->get(0); $this->offers = $this->getRemaining();
} }
public function encode(){ public function encode(){

View File

@ -57,19 +57,32 @@ class BinaryStream{
return $this->buffer; return $this->buffer;
} }
public function get(int $len) : string{ /**
if($len < 0){ * @param int|bool $len
$this->offset = strlen($this->buffer) - 1; *
return ""; * @return string
}elseif($len === 0){ */
public function get($len) : string{
if($len === true){
$str = substr($this->buffer, $this->offset); $str = substr($this->buffer, $this->offset);
$this->offset = strlen($this->buffer); $this->offset = strlen($this->buffer);
return $str; return $str;
}elseif($len < 0){
$this->offset = strlen($this->buffer) - 1;
return "";
}elseif($len === 0){
return "";
} }
return $len === 1 ? $this->buffer{$this->offset++} : substr($this->buffer, ($this->offset += $len) - $len, $len); return $len === 1 ? $this->buffer{$this->offset++} : substr($this->buffer, ($this->offset += $len) - $len, $len);
} }
public function getRemaining() : string{
$str = substr($this->buffer, $this->offset);
$this->offset = strlen($this->buffer);
return $str;
}
public function put(string $str){ public function put(string $str){
$this->buffer .= $str; $this->buffer .= $str;
} }