From 093cb5b39ef04390f250e87ca71bcb33604447db Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 17 Feb 2018 14:29:20 +0000 Subject: [PATCH] Updated PocketMine-NBT dependency --- composer.json | 2 +- composer.lock | 10 +++++----- src/pocketmine/Player.php | 9 ++++++--- src/pocketmine/Server.php | 13 +++++++------ src/pocketmine/item/Item.php | 7 ++----- .../level/format/io/BaseLevelProvider.php | 13 ++++++------- .../level/format/io/leveldb/LevelDB.php | 18 ++++++------------ .../level/format/io/region/Anvil.php | 14 +++++--------- .../level/format/io/region/McRegion.php | 19 +++++++------------ src/pocketmine/tile/Spawnable.php | 3 +-- 10 files changed, 46 insertions(+), 62 deletions(-) diff --git a/composer.json b/composer.json index a236eba5e..c38a2bebf 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "pocketmine/raklib": "dev-master#9b50878021ca545684fc1fd2d9333911ab2988e1", "pocketmine/pocketmine-spl": "^0.2.0", "pocketmine/pocketmine-binaryutils": "dev-master#a7cd5303a3b215d26bf9be76682ce9311f40e887", - "pocketmine/pocketmine-nbt": "dev-master#7186c4a47243f7eb374cbafc31cc797686b61db6" + "pocketmine/pocketmine-nbt": "dev-master#33aaaebab10b501d233b6208d6a170b82f40c3b4" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 9586d03d0..f5817d148 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "c7e83c674d1c7756afa995b7f595d790", + "content-hash": "b649f9dd4cf9477cfe9027cdb75858e0", "packages": [ { "name": "pocketmine/pocketmine-binaryutils", @@ -45,12 +45,12 @@ "source": { "type": "git", "url": "https://github.com/pmmp/PocketMine-NBT.git", - "reference": "7186c4a47243f7eb374cbafc31cc797686b61db6" + "reference": "33aaaebab10b501d233b6208d6a170b82f40c3b4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/pmmp/PocketMine-NBT/zipball/7186c4a47243f7eb374cbafc31cc797686b61db6", - "reference": "7186c4a47243f7eb374cbafc31cc797686b61db6", + "url": "https://api.github.com/repos/pmmp/PocketMine-NBT/zipball/33aaaebab10b501d233b6208d6a170b82f40c3b4", + "reference": "33aaaebab10b501d233b6208d6a170b82f40c3b4", "shasum": "" }, "require": { @@ -76,7 +76,7 @@ "source": "https://github.com/pmmp/PocketMine-NBT/tree/master", "issues": "https://github.com/pmmp/PocketMine-NBT/issues" }, - "time": "2018-02-14T17:39:47+00:00" + "time": "2018-02-17T14:20:32+00:00" }, { "name": "pocketmine/pocketmine-spl", diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index b0845dd67..f4d8371c4 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -2789,9 +2789,12 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $t = $this->level->getTile($pos); if($t instanceof Spawnable){ $nbt = new NetworkLittleEndianNBTStream(); - $nbt->read($packet->namedtag); - $nbt = $nbt->getData(); - if(!$t->updateCompoundTag($nbt, $this)){ + $compound = $nbt->read($packet->namedtag); + + if(!($compound instanceof CompoundTag)){ + throw new \InvalidArgumentException("Expected " . CompoundTag::class . " in block entity NBT, got " . (is_object($compound) ? get_class($compound) : gettype($compound))); + } + if(!$t->updateCompoundTag($compound, $this)){ $t->spawnTo($this); } } diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 3e5485fea..590b398b9 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -752,9 +752,12 @@ class Server{ if(file_exists($path . "$name.dat")){ try{ $nbt = new BigEndianNBTStream(); - $nbt->readCompressed(file_get_contents($path . "$name.dat")); + $compound = $nbt->readCompressed(file_get_contents($path . "$name.dat")); + if(!($compound instanceof CompoundTag)){ + throw new \RuntimeException("Invalid data found in \"$name.dat\", expected " . CompoundTag::class . ", got " . (is_object($compound) ? get_class($compound) : gettype($compound))); + } - return $nbt->getData(); + return $compound; }catch(\Throwable $e){ //zlib decode error / corrupt data rename($path . "$name.dat", $path . "$name.dat.bak"); $this->logger->notice($this->getLanguage()->translateString("pocketmine.data.playerCorrupted", [$name])); @@ -819,12 +822,10 @@ class Server{ if(!$ev->isCancelled()){ $nbt = new BigEndianNBTStream(); try{ - $nbt->setData($ev->getSaveData()); - if($async){ - $this->getScheduler()->scheduleAsyncTask(new FileWriteTask($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed())); + $this->getScheduler()->scheduleAsyncTask(new FileWriteTask($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed($ev->getSaveData()))); }else{ - file_put_contents($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed()); + file_put_contents($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed($ev->getSaveData())); } }catch(\Throwable $e){ $this->logger->critical($this->getLanguage()->translateString("pocketmine.data.saveError", [$name, $e->getMessage()])); diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php index 03751187b..78d307e91 100644 --- a/src/pocketmine/item/Item.php +++ b/src/pocketmine/item/Item.php @@ -66,9 +66,7 @@ class Item implements ItemIds, \JsonSerializable{ self::$cachedParser = new LittleEndianNBTStream(); } - self::$cachedParser->read($tag); - $data = self::$cachedParser->getData(); - + $data = self::$cachedParser->read($tag); if(!($data instanceof CompoundTag)){ throw new \InvalidArgumentException("Invalid item NBT string given, it could not be deserialized"); } @@ -81,8 +79,7 @@ class Item implements ItemIds, \JsonSerializable{ self::$cachedParser = new LittleEndianNBTStream(); } - self::$cachedParser->setData($tag); - return self::$cachedParser->write(); + return self::$cachedParser->write($tag); } /** diff --git a/src/pocketmine/level/format/io/BaseLevelProvider.php b/src/pocketmine/level/format/io/BaseLevelProvider.php index b006df61a..3ddb211fc 100644 --- a/src/pocketmine/level/format/io/BaseLevelProvider.php +++ b/src/pocketmine/level/format/io/BaseLevelProvider.php @@ -43,14 +43,14 @@ abstract class BaseLevelProvider implements LevelProvider{ mkdir($this->path, 0777, true); } $nbt = new BigEndianNBTStream(); - $nbt->readCompressed(file_get_contents($this->getPath() . "level.dat")); - $levelData = $nbt->getData()->getCompoundTag("Data"); - if($levelData !== null){ - $this->levelData = $levelData; - }else{ + $levelData = $nbt->readCompressed(file_get_contents($this->getPath() . "level.dat")); + + if(!($levelData instanceof CompoundTag) or !$levelData->hasTag("Data", CompoundTag::class)){ throw new LevelException("Invalid level.dat"); } + $this->levelData = $levelData->getCompoundTag("Data"); + if(!$this->levelData->hasTag("generatorName", StringTag::class)){ $this->levelData->setString("generatorName", (string) Generator::getGenerator("DEFAULT"), true); } @@ -107,10 +107,9 @@ abstract class BaseLevelProvider implements LevelProvider{ public function saveLevelData(){ $nbt = new BigEndianNBTStream(); - $nbt->setData(new CompoundTag("", [ + $buffer = $nbt->writeCompressed(new CompoundTag("", [ $this->levelData ])); - $buffer = $nbt->writeCompressed(); file_put_contents($this->getPath() . "level.dat", $buffer); } diff --git a/src/pocketmine/level/format/io/leveldb/LevelDB.php b/src/pocketmine/level/format/io/leveldb/LevelDB.php index 8d6a4e02c..514217859 100644 --- a/src/pocketmine/level/format/io/leveldb/LevelDB.php +++ b/src/pocketmine/level/format/io/leveldb/LevelDB.php @@ -95,8 +95,7 @@ class LevelDB extends BaseLevelProvider{ mkdir($this->path, 0777, true); } $nbt = new LittleEndianNBTStream(); - $nbt->read(substr(file_get_contents($this->getPath() . "level.dat"), 8)); - $levelData = $nbt->getData(); + $levelData = $nbt->read(substr(file_get_contents($this->getPath() . "level.dat"), 8)); if($levelData instanceof CompoundTag){ $this->levelData = $levelData; }else{ @@ -210,8 +209,7 @@ class LevelDB extends BaseLevelProvider{ ]); $nbt = new LittleEndianNBTStream(); - $nbt->setData($levelData); - $buffer = $nbt->write(); + $buffer = $nbt->write($levelData); file_put_contents($path . "level.dat", Binary::writeLInt(self::CURRENT_STORAGE_VERSION) . Binary::writeLInt(strlen($buffer)) . $buffer); @@ -240,8 +238,7 @@ class LevelDB extends BaseLevelProvider{ $this->levelData->setInt("StorageVersion", self::CURRENT_STORAGE_VERSION); $nbt = new LittleEndianNBTStream(); - $nbt->setData($this->levelData); - $buffer = $nbt->write(); + $buffer = $nbt->write($this->levelData); file_put_contents($this->getPath() . "level.dat", Binary::writeLInt(self::CURRENT_STORAGE_VERSION) . Binary::writeLInt(strlen($buffer)) . $buffer); } @@ -371,8 +368,7 @@ class LevelDB extends BaseLevelProvider{ /** @var CompoundTag[] $entities */ $entities = []; if(($entityData = $this->db->get($index . self::TAG_ENTITY)) !== false and strlen($entityData) > 0){ - $nbt->read($entityData, true); - $entities = $nbt->getData(); + $entities = $nbt->read($entityData, true); if(!is_array($entities)){ $entities = [$entities]; } @@ -387,8 +383,7 @@ class LevelDB extends BaseLevelProvider{ $tiles = []; if(($tileData = $this->db->get($index . self::TAG_BLOCK_ENTITY)) !== false and strlen($tileData) > 0){ - $nbt->read($tileData, true); - $tiles = $nbt->getData(); + $tiles = $nbt->read($tileData, true); if(!is_array($tiles)){ $tiles = [$tiles]; } @@ -506,8 +501,7 @@ class LevelDB extends BaseLevelProvider{ private function writeTags(array $targets, string $index){ if(!empty($targets)){ $nbt = new LittleEndianNBTStream(); - $nbt->setData($targets); - $this->db->put($index, $nbt->write()); + $this->db->put($index, $nbt->write($targets)); }else{ $this->db->delete($index); } diff --git a/src/pocketmine/level/format/io/region/Anvil.php b/src/pocketmine/level/format/io/region/Anvil.php index e6e3ac085..c21edead7 100644 --- a/src/pocketmine/level/format/io/region/Anvil.php +++ b/src/pocketmine/level/format/io/region/Anvil.php @@ -85,10 +85,7 @@ class Anvil extends McRegion{ //TODO: TileTicks $writer = new BigEndianNBTStream(); - $nbt->setName("Level"); - $writer->setData(new CompoundTag("", [$nbt])); - - return $writer->writeCompressed(ZLIB_ENCODING_DEFLATE, RegionLoader::$COMPRESSION_LEVEL); + return $writer->writeCompressed(new CompoundTag("", [$nbt]), ZLIB_ENCODING_DEFLATE, RegionLoader::$COMPRESSION_LEVEL); } protected function serializeSubChunk(SubChunk $subChunk) : CompoundTag{ @@ -103,14 +100,13 @@ class Anvil extends McRegion{ public function nbtDeserialize(string $data){ $nbt = new BigEndianNBTStream(); try{ - $nbt->readCompressed($data); - - $chunk = $nbt->getData()->getCompoundTag("Level"); - - if($chunk === null){ + $chunk = $nbt->readCompressed($data); + if(!($chunk instanceof CompoundTag) or !$chunk->hasTag("Level")){ throw new ChunkException("Invalid NBT format"); } + $chunk = $chunk->getCompoundTag("Level"); + $subChunks = []; $subChunksTag = $chunk->getListTag("Sections") ?? []; foreach($subChunksTag as $subChunk){ diff --git a/src/pocketmine/level/format/io/region/McRegion.php b/src/pocketmine/level/format/io/region/McRegion.php index 01401da2e..f462a7b88 100644 --- a/src/pocketmine/level/format/io/region/McRegion.php +++ b/src/pocketmine/level/format/io/region/McRegion.php @@ -101,10 +101,7 @@ class McRegion extends BaseLevelProvider{ $nbt->setTag(new ListTag("TileEntities", $tiles, NBT::TAG_Compound)); $writer = new BigEndianNBTStream(); - $nbt->setName("Level"); - $writer->setData(new CompoundTag("", [$nbt])); - - return $writer->writeCompressed(ZLIB_ENCODING_DEFLATE, RegionLoader::$COMPRESSION_LEVEL); + return $writer->writeCompressed(new CompoundTag("", [$nbt]), ZLIB_ENCODING_DEFLATE, RegionLoader::$COMPRESSION_LEVEL); } /** @@ -115,14 +112,13 @@ class McRegion extends BaseLevelProvider{ public function nbtDeserialize(string $data){ $nbt = new BigEndianNBTStream(); try{ - $nbt->readCompressed($data); - - $chunk = $nbt->getData()->getCompoundTag("Level"); - - if($chunk === null){ - throw new ChunkException("Invalid NBT format, 'Level' key not found"); + $chunk = $nbt->readCompressed($data); + if(!($chunk instanceof CompoundTag) or !$chunk->hasTag("Level")){ + throw new ChunkException("Invalid NBT format"); } + $chunk = $chunk->getCompoundTag("Level"); + $subChunks = []; $fullIds = $chunk->hasTag("Blocks", ByteArrayTag::class) ? $chunk->getByteArray("Blocks") : str_repeat("\x00", 32768); $fullData = $chunk->hasTag("Data", ByteArrayTag::class) ? $chunk->getByteArray("Data") : str_repeat("\x00", 16384); @@ -257,10 +253,9 @@ class McRegion extends BaseLevelProvider{ new CompoundTag("GameRules", []) ]); $nbt = new BigEndianNBTStream(); - $nbt->setData(new CompoundTag("", [ + $buffer = $nbt->writeCompressed(new CompoundTag("", [ $levelData ])); - $buffer = $nbt->writeCompressed(); file_put_contents($path . "level.dat", $buffer); } diff --git a/src/pocketmine/tile/Spawnable.php b/src/pocketmine/tile/Spawnable.php index b88873939..9de350d20 100644 --- a/src/pocketmine/tile/Spawnable.php +++ b/src/pocketmine/tile/Spawnable.php @@ -95,8 +95,7 @@ abstract class Spawnable extends Tile{ self::$nbtWriter = new NetworkLittleEndianNBTStream(); } - self::$nbtWriter->setData($this->getSpawnCompound()); - $this->spawnCompoundCache = self::$nbtWriter->write(); + $this->spawnCompoundCache = self::$nbtWriter->write($this->getSpawnCompound()); } return $this->spawnCompoundCache;