Address more error cases, some minor cleanup

This commit is contained in:
Dylan K. Taylor 2019-01-16 21:30:31 +00:00
parent 44ef9fc577
commit fc98f4c42b
7 changed files with 105 additions and 14 deletions

View File

@ -31,6 +31,7 @@ use pocketmine\item\Item;
use pocketmine\item\ItemFactory; use pocketmine\item\ItemFactory;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\nbt\LittleEndianNbtSerializer; use pocketmine\nbt\LittleEndianNbtSerializer;
use pocketmine\network\BadPacketException;
use pocketmine\network\mcpe\protocol\types\CommandOriginData; use pocketmine\network\mcpe\protocol\types\CommandOriginData;
use pocketmine\network\mcpe\protocol\types\EntityLink; use pocketmine\network\mcpe\protocol\types\EntityLink;
use pocketmine\utils\BinaryDataException; use pocketmine\utils\BinaryDataException;
@ -43,6 +44,10 @@ class NetworkBinaryStream extends BinaryStream{
/** @var LittleEndianNbtSerializer */ /** @var LittleEndianNbtSerializer */
private static $itemNbtSerializer = null; private static $itemNbtSerializer = null;
/**
* @return string
* @throws BinaryDataException
*/
public function getString() : string{ public function getString() : string{
return $this->get($this->getUnsignedVarInt()); return $this->get($this->getUnsignedVarInt());
} }
@ -52,6 +57,10 @@ class NetworkBinaryStream extends BinaryStream{
$this->put($v); $this->put($v);
} }
/**
* @return UUID
* @throws BinaryDataException
*/
public function getUUID() : UUID{ public function getUUID() : UUID{
//This is actually two little-endian longs: UUID Most followed by UUID Least //This is actually two little-endian longs: UUID Most followed by UUID Least
$part1 = $this->getLInt(); $part1 = $this->getLInt();
@ -69,6 +78,12 @@ class NetworkBinaryStream extends BinaryStream{
$this->putLInt($uuid->getPart(2)); $this->putLInt($uuid->getPart(2));
} }
/**
* @return Item
*
* @throws BadPacketException
* @throws BinaryDataException
*/
public function getSlot() : Item{ public function getSlot() : Item{
$id = $this->getVarInt(); $id = $this->getVarInt();
if($id === 0){ if($id === 0){
@ -88,7 +103,11 @@ class NetworkBinaryStream extends BinaryStream{
if(self::$itemNbtSerializer === null){ if(self::$itemNbtSerializer === null){
self::$itemNbtSerializer = new LittleEndianNbtSerializer(); self::$itemNbtSerializer = new LittleEndianNbtSerializer();
} }
$compound = self::$itemNbtSerializer->read($this->get($nbtLen)); try{
$compound = self::$itemNbtSerializer->read($this->get($nbtLen));
}catch(\UnexpectedValueException $e){
throw new BadPacketException($e->getMessage(), 0, $e);
}
} }
//TODO //TODO
@ -104,7 +123,7 @@ class NetworkBinaryStream extends BinaryStream{
try{ try{
return ItemFactory::get($id, $data, $cnt, $compound); return ItemFactory::get($id, $data, $cnt, $compound);
}catch(\InvalidArgumentException $e){ }catch(\InvalidArgumentException $e){
throw new BinaryDataException($e->getMessage(), 0, $e); throw new BadPacketException($e->getMessage(), 0, $e);
} }
} }
@ -146,6 +165,9 @@ class NetworkBinaryStream extends BinaryStream{
* @param bool $types Whether to include metadata types along with values in the returned array * @param bool $types Whether to include metadata types along with values in the returned array
* *
* @return array * @return array
*
* @throws BadPacketException
* @throws BinaryDataException
*/ */
public function getEntityMetadata(bool $types = true) : array{ public function getEntityMetadata(bool $types = true) : array{
$count = $this->getUnsignedVarInt(); $count = $this->getUnsignedVarInt();
@ -184,7 +206,7 @@ class NetworkBinaryStream extends BinaryStream{
$value = $this->getVector3(); $value = $this->getVector3();
break; break;
default: default:
throw new BinaryDataException("Invalid data type " . $type); throw new BadPacketException("Unknown entity metadata type " . $type);
} }
if($types){ if($types){
$data[$key] = [$type, $value]; $data[$key] = [$type, $value];
@ -249,7 +271,8 @@ class NetworkBinaryStream extends BinaryStream{
* Reads a list of Attributes from the stream. * Reads a list of Attributes from the stream.
* @return Attribute[] * @return Attribute[]
* *
* @throws BinaryDataException if reading an attribute with an unrecognized name * @throws BadPacketException if reading an attribute with an unrecognized name
* @throws BinaryDataException
*/ */
public function getAttributeList() : array{ public function getAttributeList() : array{
$list = []; $list = [];
@ -271,7 +294,7 @@ class NetworkBinaryStream extends BinaryStream{
$list[] = $attr; $list[] = $attr;
}else{ }else{
throw new BinaryDataException("Unknown attribute type \"$id\""); throw new BadPacketException("Unknown attribute type \"$id\"");
} }
} }
@ -297,6 +320,8 @@ class NetworkBinaryStream extends BinaryStream{
/** /**
* Reads and returns an EntityUniqueID * Reads and returns an EntityUniqueID
* @return int * @return int
*
* @throws BinaryDataException
*/ */
public function getEntityUniqueId() : int{ public function getEntityUniqueId() : int{
return $this->getVarLong(); return $this->getVarLong();
@ -314,6 +339,8 @@ class NetworkBinaryStream extends BinaryStream{
/** /**
* Reads and returns an EntityRuntimeID * Reads and returns an EntityRuntimeID
* @return int * @return int
*
* @throws BinaryDataException
*/ */
public function getEntityRuntimeId() : int{ public function getEntityRuntimeId() : int{
return $this->getUnsignedVarLong(); return $this->getUnsignedVarLong();
@ -334,6 +361,8 @@ class NetworkBinaryStream extends BinaryStream{
* @param int &$x * @param int &$x
* @param int &$y * @param int &$y
* @param int &$z * @param int &$z
*
* @throws BinaryDataException
*/ */
public function getBlockPosition(&$x, &$y, &$z) : void{ public function getBlockPosition(&$x, &$y, &$z) : void{
$x = $this->getVarInt(); $x = $this->getVarInt();
@ -360,6 +389,8 @@ class NetworkBinaryStream extends BinaryStream{
* @param int &$x * @param int &$x
* @param int &$y * @param int &$y
* @param int &$z * @param int &$z
*
* @throws BinaryDataException
*/ */
public function getSignedBlockPosition(&$x, &$y, &$z) : void{ public function getSignedBlockPosition(&$x, &$y, &$z) : void{
$x = $this->getVarInt(); $x = $this->getVarInt();
@ -384,6 +415,8 @@ class NetworkBinaryStream extends BinaryStream{
* Reads a floating-point Vector3 object with coordinates rounded to 4 decimal places. * Reads a floating-point Vector3 object with coordinates rounded to 4 decimal places.
* *
* @return Vector3 * @return Vector3
*
* @throws BinaryDataException
*/ */
public function getVector3() : Vector3{ public function getVector3() : Vector3{
return new Vector3( return new Vector3(
@ -424,6 +457,10 @@ class NetworkBinaryStream extends BinaryStream{
$this->putLFloat($vector->z); $this->putLFloat($vector->z);
} }
/**
* @return float
* @throws BinaryDataException
*/
public function getByteRotation() : float{ public function getByteRotation() : float{
return (float) ($this->getByte() * (360 / 256)); return (float) ($this->getByte() * (360 / 256));
} }
@ -437,6 +474,9 @@ class NetworkBinaryStream extends BinaryStream{
* TODO: implement this properly * TODO: implement this properly
* *
* @return array, members are in the structure [name => [type, value]] * @return array, members are in the structure [name => [type, value]]
*
* @throws BadPacketException
* @throws BinaryDataException
*/ */
public function getGameRules() : array{ public function getGameRules() : array{
$count = $this->getUnsignedVarInt(); $count = $this->getUnsignedVarInt();
@ -456,7 +496,7 @@ class NetworkBinaryStream extends BinaryStream{
$value = $this->getLFloat(); $value = $this->getLFloat();
break; break;
default: default:
throw new BinaryDataException("Unknown gamerule type $type"); throw new BadPacketException("Unknown gamerule type $type");
} }
$rules[$name] = [$type, $value]; $rules[$name] = [$type, $value];
@ -494,6 +534,8 @@ class NetworkBinaryStream extends BinaryStream{
/** /**
* @return EntityLink * @return EntityLink
*
* @throws BinaryDataException
*/ */
protected function getEntityLink() : EntityLink{ protected function getEntityLink() : EntityLink{
$link = new EntityLink(); $link = new EntityLink();
@ -516,6 +558,10 @@ class NetworkBinaryStream extends BinaryStream{
$this->putBool($link->immediate); $this->putBool($link->immediate);
} }
/**
* @return CommandOriginData
* @throws BinaryDataException
*/
protected function getCommandOriginData() : CommandOriginData{ protected function getCommandOriginData() : CommandOriginData{
$result = new CommandOriginData(); $result = new CommandOriginData();

View File

@ -191,9 +191,13 @@ class AddEntityPacket extends DataPacket{
$attr = Attribute::getAttribute($id); $attr = Attribute::getAttribute($id);
if($attr !== null){ if($attr !== null){
$attr->setMinValue($min); try{
$attr->setMaxValue($max); $attr->setMinValue($min);
$attr->setValue($current); $attr->setMaxValue($max);
$attr->setValue($current);
}catch(\InvalidArgumentException $e){
throw new BadPacketException($e->getMessage(), 0, $e); //TODO: address this properly
}
$this->attributes[] = $attr; $this->attributes[] = $attr;
}else{ }else{
throw new BadPacketException("Unknown attribute type \"$id\""); throw new BadPacketException("Unknown attribute type \"$id\"");

View File

@ -30,6 +30,7 @@ use pocketmine\network\mcpe\handler\SessionHandler;
use pocketmine\network\mcpe\protocol\types\CommandData; use pocketmine\network\mcpe\protocol\types\CommandData;
use pocketmine\network\mcpe\protocol\types\CommandEnum; use pocketmine\network\mcpe\protocol\types\CommandEnum;
use pocketmine\network\mcpe\protocol\types\CommandParameter; use pocketmine\network\mcpe\protocol\types\CommandParameter;
use pocketmine\utils\BinaryDataException;
use function array_flip; use function array_flip;
use function array_keys; use function array_keys;
use function array_map; use function array_map;
@ -140,6 +141,11 @@ class AvailableCommandsPacket extends DataPacket{
} }
} }
/**
* @return CommandEnum
* @throws BadPacketException
* @throws BinaryDataException
*/
protected function getEnum() : CommandEnum{ protected function getEnum() : CommandEnum{
$retval = new CommandEnum(); $retval = new CommandEnum();
$retval->enumName = $this->getString(); $retval->enumName = $this->getString();
@ -156,6 +162,10 @@ class AvailableCommandsPacket extends DataPacket{
return $retval; return $retval;
} }
/**
* @return CommandEnum
* @throws BinaryDataException
*/
protected function getSoftEnum() : CommandEnum{ protected function getSoftEnum() : CommandEnum{
$retval = new CommandEnum(); $retval = new CommandEnum();
$retval->enumName = $this->getString(); $retval->enumName = $this->getString();
@ -191,6 +201,10 @@ class AvailableCommandsPacket extends DataPacket{
} }
} }
/**
* @return int
* @throws BinaryDataException
*/
protected function getEnumValueIndex() : int{ protected function getEnumValueIndex() : int{
if($this->enumValuesCount < 256){ if($this->enumValuesCount < 256){
return $this->getByte(); return $this->getByte();
@ -211,6 +225,11 @@ class AvailableCommandsPacket extends DataPacket{
} }
} }
/**
* @return CommandData
* @throws BadPacketException
* @throws BinaryDataException
*/
protected function getCommandData() : CommandData{ protected function getCommandData() : CommandData{
$retval = new CommandData(); $retval = new CommandData();
$retval->commandName = $this->getString(); $retval->commandName = $this->getString();

View File

@ -28,6 +28,7 @@ namespace pocketmine\network\mcpe\protocol;
use pocketmine\network\mcpe\handler\SessionHandler; use pocketmine\network\mcpe\handler\SessionHandler;
use pocketmine\network\mcpe\protocol\types\CommandOriginData; use pocketmine\network\mcpe\protocol\types\CommandOriginData;
use pocketmine\network\mcpe\protocol\types\CommandOutputMessage; use pocketmine\network\mcpe\protocol\types\CommandOutputMessage;
use pocketmine\utils\BinaryDataException;
use function count; use function count;
class CommandOutputPacket extends DataPacket{ class CommandOutputPacket extends DataPacket{
@ -58,6 +59,10 @@ class CommandOutputPacket extends DataPacket{
} }
} }
/**
* @return CommandOutputMessage
* @throws BinaryDataException
*/
protected function getCommandMessage() : CommandOutputMessage{ protected function getCommandMessage() : CommandOutputMessage{
$message = new CommandOutputMessage(); $message = new CommandOutputMessage();

View File

@ -26,6 +26,7 @@ namespace pocketmine\network\mcpe\protocol;
#include <rules/DataPacket.h> #include <rules/DataPacket.h>
use pocketmine\network\mcpe\handler\SessionHandler; use pocketmine\network\mcpe\handler\SessionHandler;
use pocketmine\utils\BinaryDataException;
class MoveEntityDeltaPacket extends DataPacket{ class MoveEntityDeltaPacket extends DataPacket{
public const NETWORK_ID = ProtocolInfo::MOVE_ENTITY_DELTA_PACKET; public const NETWORK_ID = ProtocolInfo::MOVE_ENTITY_DELTA_PACKET;
@ -54,6 +55,12 @@ class MoveEntityDeltaPacket extends DataPacket{
/** @var float */ /** @var float */
public $zRot = 0.0; public $zRot = 0.0;
/**
* @param int $flag
*
* @return int
* @throws BinaryDataException
*/
private function maybeReadCoord(int $flag) : int{ private function maybeReadCoord(int $flag) : int{
if($this->flags & $flag){ if($this->flags & $flag){
return $this->getVarInt(); return $this->getVarInt();
@ -61,6 +68,12 @@ class MoveEntityDeltaPacket extends DataPacket{
return 0; return 0;
} }
/**
* @param int $flag
*
* @return float
* @throws BinaryDataException
*/
private function maybeReadRotation(int $flag) : float{ private function maybeReadRotation(int $flag) : float{
if($this->flags & $flag){ if($this->flags & $flag){
return $this->getByteRotation(); return $this->getByteRotation();

View File

@ -31,6 +31,7 @@ use pocketmine\item\Item;
use pocketmine\network\BadPacketException; use pocketmine\network\BadPacketException;
use pocketmine\network\mcpe\NetworkBinaryStream; use pocketmine\network\mcpe\NetworkBinaryStream;
use pocketmine\Player; use pocketmine\Player;
use pocketmine\utils\BinaryDataException;
class NetworkInventoryAction{ class NetworkInventoryAction{
public const SOURCE_CONTAINER = 0; public const SOURCE_CONTAINER = 0;
@ -96,7 +97,8 @@ class NetworkInventoryAction{
* @param NetworkBinaryStream $packet * @param NetworkBinaryStream $packet
* *
* @return $this * @return $this
* @throws \UnexpectedValueException *
* @throws BinaryDataException
* @throws BadPacketException * @throws BadPacketException
*/ */
public function read(NetworkBinaryStream $packet) : NetworkInventoryAction{ public function read(NetworkBinaryStream $packet) : NetworkInventoryAction{

View File

@ -23,7 +23,9 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\protocol\types; namespace pocketmine\network\mcpe\protocol\types;
use pocketmine\network\BadPacketException;
use pocketmine\network\mcpe\NetworkBinaryStream; use pocketmine\network\mcpe\NetworkBinaryStream;
use pocketmine\utils\BinaryDataException;
use function count; use function count;
abstract class TransactionData{ abstract class TransactionData{
@ -45,8 +47,8 @@ abstract class TransactionData{
/** /**
* @param NetworkBinaryStream $stream * @param NetworkBinaryStream $stream
* *
* @throws \OutOfBoundsException * @throws BinaryDataException
* @throws \UnexpectedValueException * @throws BadPacketException
*/ */
final public function decode(NetworkBinaryStream $stream) : void{ final public function decode(NetworkBinaryStream $stream) : void{
$actionCount = $stream->getUnsignedVarInt(); $actionCount = $stream->getUnsignedVarInt();
@ -59,8 +61,8 @@ abstract class TransactionData{
/** /**
* @param NetworkBinaryStream $stream * @param NetworkBinaryStream $stream
* *
* @throws \OutOfBoundsException * @throws BinaryDataException
* @throws \UnexpectedValueException * @throws BadPacketException
*/ */
abstract protected function decodeData(NetworkBinaryStream $stream) : void; abstract protected function decodeData(NetworkBinaryStream $stream) : void;