From cf3d8f449e05f5f540f88ce50609dff372a01445 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Sun, 27 Sep 2015 19:48:42 +0200 Subject: [PATCH] Added some asserts --- src/pocketmine/Player.php | 1 - src/pocketmine/Server.php | 7 ++-- src/pocketmine/entity/Entity.php | 9 +++-- src/pocketmine/entity/Item.php | 4 ++ src/pocketmine/level/Position.php | 4 +- src/pocketmine/level/generator/Generator.php | 39 ++++++-------------- src/pocketmine/nbt/tag/CompoundTag.php | 2 + src/pocketmine/resources/pocketmine.yml | 1 + src/pocketmine/tile/Tile.php | 4 +- 9 files changed, 30 insertions(+), 41 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index c876ead2c..1bd21903b 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -2487,7 +2487,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade $pk = new EntityEventPacket(); $pk->eid = $this->getId(); $pk->event = EntityEventPacket::USE_ITEM; - $pk; $this->dataPacket($pk); Server::broadcastPacket($this->getViewers(), $pk); diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 786d69036..658eb01df 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -1518,6 +1518,9 @@ class Server{ } define('pocketmine\DEBUG', (int) $this->getProperty("debug.level", 1)); + + ini_set('assert.exception', 1); + if($this->logger instanceof MainLogger){ $this->logger->setLogDebug(\pocketmine\DEBUG > 1); } @@ -1867,10 +1870,6 @@ class Server{ * @throws \Exception */ public function dispatchCommand(CommandSender $sender, $commandLine){ - if(!($sender instanceof CommandSender)){ - throw new ServerException("CommandSender is not valid"); - } - if($this->commandMap->dispatch($sender, $commandLine)){ return true; } diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index b302f6220..92527456a 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -206,9 +206,8 @@ abstract class Entity extends Location implements Metadatable{ public function __construct(FullChunk $chunk, CompoundTag $nbt){ - if($chunk === null or $chunk->getProvider() === null){ - throw new ChunkException("Invalid garbage Chunk given to Entity"); - } + + assert($chunk !== null and $chunk->getProvider() !== null); $this->timings = Timings::getEntityTimings($this); @@ -240,6 +239,8 @@ abstract class Entity extends Location implements Metadatable{ ); $this->setMotion($this->temporalVector->setComponents($this->namedtag["Motion"][0], $this->namedtag["Motion"][1], $this->namedtag["Motion"][2])); + assert(!is_nan($this->x) and !is_infinite($this->x) and !is_nan($this->y) and !is_infinite($this->y) and !is_nan($this->z) and !is_infinite($this->z)); + if(!isset($this->namedtag->FallDistance)){ $this->namedtag->FallDistance = new FloatTag("FallDistance", 0); } @@ -501,6 +502,8 @@ abstract class Entity extends Location implements Metadatable{ } protected function initEntity(){ + assert($this->namedtag instanceof CompoundTag); + if(isset($this->namedtag->ActiveEffects)){ foreach($this->namedtag->ActiveEffects->getValue() as $e){ $effect = Effect::getEffect($e["Id"]); diff --git a/src/pocketmine/entity/Item.php b/src/pocketmine/entity/Item.php index e0df967bf..76269d569 100644 --- a/src/pocketmine/entity/Item.php +++ b/src/pocketmine/entity/Item.php @@ -30,6 +30,7 @@ use pocketmine\item\Item as ItemItem; use pocketmine\nbt\NBT; +use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\ShortTag; use pocketmine\nbt\tag\StringTag; use pocketmine\network\Network; @@ -70,6 +71,9 @@ class Item extends Entity{ if(isset($this->namedtag->Thrower)){ $this->thrower = $this->namedtag["Thrower"]; } + + assert($this->namedtag->Item instanceof CompoundTag); + if(!isset($this->namedtag->Item)){ $this->close(); return; diff --git a/src/pocketmine/level/Position.php b/src/pocketmine/level/Position.php index 05c323a9a..d581ab0da 100644 --- a/src/pocketmine/level/Position.php +++ b/src/pocketmine/level/Position.php @@ -78,9 +78,7 @@ class Position extends Vector3{ * @throws LevelException */ public function getSide($side, $step = 1){ - if(!$this->isValid()){ - throw new LevelException("Undefined Level reference"); - } + assert($this->isValid()); return Position::fromObject(parent::getSide($side, $step), $this->level); } diff --git a/src/pocketmine/level/generator/Generator.php b/src/pocketmine/level/generator/Generator.php index bd7e6c432..007c82751 100644 --- a/src/pocketmine/level/generator/Generator.php +++ b/src/pocketmine/level/generator/Generator.php @@ -118,15 +118,10 @@ abstract class Generator{ * @return \SplFixedArray */ public static function getFastNoise2D(Noise $noise, $xSize, $zSize, $samplingRate, $x, $y, $z){ - if($samplingRate === 0){ - throw new \InvalidArgumentException("samplingRate cannot be 0"); - } - if ($xSize % $samplingRate !== 0) { - throw new \InvalidArgumentCountException("xSize % samplingRate must return 0"); - } - if ($zSize % $samplingRate !== 0) { - throw new \InvalidArgumentCountException("zSize % samplingRate must return 0"); - } + assert($samplingRate !== 0, new \InvalidArgumentException("samplingRate cannot be 0")); + + assert($xSize % $samplingRate === 0, new \InvalidArgumentCountException("xSize % samplingRate must return 0")); + assert($zSize % $samplingRate === 0, new \InvalidArgumentCountException("zSize % samplingRate must return 0")); $noiseArray = new \SplFixedArray($xSize + 1); @@ -173,24 +168,14 @@ abstract class Generator{ * @return \SplFixedArray */ public static function getFastNoise3D(Noise $noise, $xSize, $ySize, $zSize, $xSamplingRate, $ySamplingRate, $zSamplingRate, $x, $y, $z){ - if($xSamplingRate === 0){ - throw new \InvalidArgumentException("xSamplingRate cannot be 0"); - } - if($zSamplingRate === 0){ - throw new \InvalidArgumentException("zSamplingRate cannot be 0"); - } - if($ySamplingRate === 0){ - throw new \InvalidArgumentException("ySamplingRate cannot be 0"); - } - if ($xSize % $xSamplingRate !== 0) { - throw new \InvalidArgumentCountException("xSize % xSamplingRate must return 0"); - } - if ($zSize % $zSamplingRate !== 0) { - throw new \InvalidArgumentCountException("zSize % zSamplingRate must return 0"); - } - if ($ySize % $ySamplingRate !== 0) { - throw new \InvalidArgumentCountException("ySize % ySamplingRate must return 0"); - } + + assert($xSamplingRate !== 0, new \InvalidArgumentException("xSamplingRate cannot be 0")); + assert($zSamplingRate !== 0, new \InvalidArgumentException("zSamplingRate cannot be 0")); + assert($ySamplingRate !== 0, new \InvalidArgumentException("ySamplingRate cannot be 0")); + + assert($xSize % $xSamplingRate === 0, new \InvalidArgumentCountException("xSize % xSamplingRate must return 0")); + assert($zSize % $zSamplingRate === 0, new \InvalidArgumentCountException("zSize % zSamplingRate must return 0")); + assert($ySize % $ySamplingRate === 0, new \InvalidArgumentCountException("ySize % ySamplingRate must return 0")); $noiseArray = array_fill(0, $xSize + 1, array_fill(0, $zSize + 1, [])); diff --git a/src/pocketmine/nbt/tag/CompoundTag.php b/src/pocketmine/nbt/tag/CompoundTag.php index 7c8ba10a0..ebef4fadd 100644 --- a/src/pocketmine/nbt/tag/CompoundTag.php +++ b/src/pocketmine/nbt/tag/CompoundTag.php @@ -62,6 +62,8 @@ class CompoundTag extends NamedTag implements \ArrayAccess{ } } + assert(false, "Offset $offset not found"); + return null; } diff --git a/src/pocketmine/resources/pocketmine.yml b/src/pocketmine/resources/pocketmine.yml index 6cdb388f7..4695bf704 100644 --- a/src/pocketmine/resources/pocketmine.yml +++ b/src/pocketmine/resources/pocketmine.yml @@ -84,6 +84,7 @@ network: upnp-forwarding: false debug: + #To enable assertion execution, set zend.assertions in your php.ini to 1 #If > 1, it will show debug messages in the console level: 1 #Enables /status, /gc diff --git a/src/pocketmine/tile/Tile.php b/src/pocketmine/tile/Tile.php index 450288160..180b3a21c 100644 --- a/src/pocketmine/tile/Tile.php +++ b/src/pocketmine/tile/Tile.php @@ -111,9 +111,7 @@ abstract class Tile extends Position{ } public function __construct(FullChunk $chunk, CompoundTag $nbt){ - if($chunk === null or $chunk->getProvider() === null){ - throw new ChunkException("Invalid garbage Chunk given to Tile"); - } + assert($chunk !== null and $chunk->getProvider() !== null); $this->timings = Timings::getTileEntityTimings($this);