diff --git a/src/nbt/NBT.php b/src/nbt/NBT.php index 6ca8a12dd6..61e3f89013 100644 --- a/src/nbt/NBT.php +++ b/src/nbt/NBT.php @@ -26,10 +26,10 @@ class NBT{ private $buffer; private $offset; private $endianness; - private $data = array(); + private $data; public function get($len){ - if($len <= 0){ + if($len < 0){ $this->offset = strlen($this->buffer) - 1; return ""; } @@ -56,24 +56,21 @@ class NBT{ public function read($buffer){ $this->offset = 0; $this->buffer = $buffer; - $this->readTag(); + $this->data = $this->readTag(); } public function write(){ $this->offset = 0; if($this->data instanceof NBTTag_Compound){ $this->writeTag($this->data); - return true; + return $this->buffer; }else{ return false; } } - protected function readTag(){ + public function readTag(){ switch($this->getByte()){ - case NBTTag::TAG_End: //No named tag - $tag = new NBTTag_End; - break; case NBTTag::TAG_Byte: $tag = new NBTTag_Byte($this->getString()); $tag->read($this); @@ -123,8 +120,10 @@ class NBT{ $tag->read($this); break; + case NBTTag::TAG_End: //No named tag default: - return false; + $tag = new NBTTag_End; + break; } return $tag; } diff --git a/src/nbt/tags/TAG_Compound.php b/src/nbt/tags/TAG_Compound.php index 01a4e22e30..ec95ebf84b 100644 --- a/src/nbt/tags/TAG_Compound.php +++ b/src/nbt/tags/TAG_Compound.php @@ -29,7 +29,7 @@ class NBTTag_Compound extends NamedNBTTag{ $this->value = array(); do{ $tag = $nbt->readTag(); - if($tag instanceof NamedNBTTag){ + if($tag instanceof NamedNBTTag and $tag->getName() !== ""){ $this->value[$tag->getName()] = $tag; }else{ $this->value[] = $tag; diff --git a/src/nbt/tags/TAG_List.php b/src/nbt/tags/TAG_List.php index 54dfecf57c..2a72a8c52d 100644 --- a/src/nbt/tags/TAG_List.php +++ b/src/nbt/tags/TAG_List.php @@ -34,62 +34,62 @@ class NBTTag_List extends NamedNBTTag{ switch($tagId){ case NBTTag::TAG_Byte: $tag = new NBTTag_Byte(false); - $tag->read($this); + $tag->read($nbt); $this->value[] = $tag; break; case NBTTag::TAG_Byte: $tag = new NBTTag_Byte(false); - $tag->read($this); + $tag->read($nbt); $this->value[] = $tag; break; case NBTTag::TAG_Short: $tag = new NBTTag_Short(false); - $tag->read($this); + $tag->read($nbt); $this->value[] = $tag; break; case NBTTag::TAG_Int: $tag = new NBTTag_Int(false); - $tag->read($this); + $tag->read($nbt); $this->value[] = $tag; break; case NBTTag::TAG_Long: $tag = new NBTTag_Long(false); - $tag->read($this); + $tag->read($nbt); $this->value[] = $tag; break; case NBTTag::TAG_Float: $tag = new NBTTag_Float(false); - $tag->read($this); + $tag->read($nbt); $this->value[] = $tag; break; case NBTTag::TAG_Double: $tag = new NBTTag_Double(false); - $tag->read($this); + $tag->read($nbt); $this->value[] = $tag; break; case NBTTag::TAG_Byte_Array: $tag = new NBTTag_Byte_Array(false); - $tag->read($this); + $tag->read($nbt); $this->value[] = $tag; break; case NBTTag::TAG_String: $tag = new NBTTag_String(false); - $tag->read($this); + $tag->read($nbt); $this->value[] = $tag; break; case NBTTag::TAG_List: $tag = new NBTTag_List(false); - $tag->read($this); + $tag->read($nbt); $this->value[] = $tag; break; case NBTTag::TAG_Compound: $tag = new NBTTag_Compound(false); - $tag->read($this); + $tag->read($nbt); $this->value[] = $tag; break; case NBTTag::TAG_Int_Array: $tag = new NBTTag_Int_Array(false); - $tag->read($this); + $tag->read($nbt); $this->value[] = $tag; break; } diff --git a/src/utils/NBT.php b/src/utils/NBT.php deleted file mode 100644 index daa6b39327..0000000000 --- a/src/utils/NBT.php +++ /dev/null @@ -1,213 +0,0 @@ -offset += $n; - return substr($this->binary, $this->offset - $n, $n); - } - - private function feof(){ - return !isset($this->binary{$this->offset}); - } - - public function write($bin){ - $this->binary .= $bin; - } - - public function load($str){ - $this->offset = 0; - $this->binary = (string) $str; - $this->parseTree($this->tree); - } - - public function readTAG_BYTE(){ - return Utils::readByte($this->read(1)); - } - - public function readTAG_SHORT(){ - return Utils::readLShort($this->read(2)); - } - - public function readTAG_INT(){ - return Utils::readLInt($this->read(4)); - } - - public function readTAG_LONG(){ - return Utils::readLLong($this->read(8)); - } - - public function readTAG_FLOAT(){ - return Utils::readLFloat($this->read(4)); - } - - public function readTAG_DOUBLE(){ - return Utils::readLDouble($this->read(8)); - } - - public function readTAG_BYTE_ARRAY(){ - return $this->read($this->readTAG_INT()); - } - - public function readTAG_STRING(){ - return $this->read(Utils::readLShort($this->read(2), false)); - } - - public function writeTAG_BYTE($v){ - $this->binary .= chr($v); - } - - public function writeTAG_SHORT($v){ - $this->binary .= Utils::writeLShort($v); - } - - public function writeTAG_INT($v){ - $this->binary .= Utils::writeLInt($v); - } - - public function writeTAG_LONG($v){ - $this->binary .= Utils::writeLLong($v); - } - - public function writeTAG_FLOAT($v){ - $this->binary .= Utils::writeLFloat($v); - } - - public function writeTAG_DOUBLE($v){ - $this->binary .= Utils::writeLDouble($v); - } - - public function writeTAG_BYTE_ARRAY($v){ - $this->binary .= $this->writeTAG_INT(strlen($v)).$v; - } - - public function writeTAG_STRING($v){ - $this->binary .= $this->writeTAG_SHORT(strlen($v)).$v; - } - - private function parseList(&$node, $tag, $cnt){ - for($i = 0; $i < $cnt and !$this->feof(); ++$i){ - switch($tag){ - case self::TAG_BYTE: - $value = $this->readTAG_BYTE(); - break; - case self::TAG_SHORT: - $value = $this->readTAG_SHORT(); - break; - case self::TAG_INT: - $value = $this->readTAG_INT(); - break; - case self::TAG_LONG: - $value = $this->readTAG_LONG(); - break; - case self::TAG_FLOAT: - $value = $this->readTAG_FLOAT(); - break; - case self::TAG_DOUBLE: - $value = $this->readTAG_DOUBLE(); - break; - case self::TAG_BYTE_ARRAY: - $value = $this->readTAG_BYTE_ARRAY(); - break; - case self::TAG_STRING: - $value = $this->readTAG_STRING(); - break; - case self::TAG_LIST: - $value = array(); - $this->parseList($value, ord($this->read(1)), Utils::readLInt($this->read(4))); - break; - case self::TAG_COMPOUND: - $value = array(); - $this->parseTree($value); - break; - default: - echo bin2hex(substr($this->binary, $this->offset - 1)).PHP_EOL.PHP_EOL; - die("Invalid NBT Tag $tag"); - break; - } - $node[] = $value; - } - } - - private function parseTree(&$node){ - while(($tag = ord($this->read(1))) !== self::TAG_END and !$this->feof()){ - $name = $this->readTAG_STRING(); - switch($tag){ - case self::TAG_BYTE: - $value = $this->readTAG_BYTE(); - break; - case self::TAG_SHORT: - $value = $this->readTAG_SHORT(); - break; - case self::TAG_INT: - $value = $this->readTAG_INT(); - break; - case self::TAG_LONG: - $value = $this->readTAG_LONG(); - break; - case self::TAG_FLOAT: - $value = $this->readTAG_FLOAT(); - break; - case self::TAG_DOUBLE: - $value = $this->readTAG_DOUBLE(); - break; - case self::TAG_BYTE_ARRAY: - $value = $this->readTAG_BYTE_ARRAY(); - break; - case self::TAG_STRING: - $value = $this->readTAG_STRING(); - break; - case self::TAG_LIST: - $value = array(); - $this->parseList($value, ord($this->read(1)), Utils::readLInt($this->read(4))); - break; - case self::TAG_COMPOUND: - $value = array(); - $this->parseTree($value); - break; - default: - echo bin2hex(substr($this->binary, $this->offset - 1)).PHP_EOL.PHP_EOL; - die("Invalid NBT Tag $tag"); - break; - } - $node[$name] = $value; - } - } -} \ No newline at end of file