From 398fbbfb314cf570bc8859122254a40d1073cdb7 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Thu, 27 Feb 2014 00:03:27 +0100 Subject: [PATCH] Added array things to NBT --- src/nbt/NBT.php | 29 +++++++++++--- src/nbt/NBTTag.php | 4 +- src/nbt/tags/TAG_Compound.php | 54 +++++++++++++++++++++++--- src/nbt/tags/TAG_List.php | 73 ++++++++++++++++++++++++++++++----- 4 files changed, 137 insertions(+), 23 deletions(-) diff --git a/src/nbt/NBT.php b/src/nbt/NBT.php index b13153c74..fadd82594 100644 --- a/src/nbt/NBT.php +++ b/src/nbt/NBT.php @@ -19,7 +19,7 @@ * */ -class NBT{ +class NBT implements ArrayAccess{ const LITTLE_ENDIAN = 0; const BIG_ENDIAN = 1; @@ -194,26 +194,43 @@ class NBT{ $this->buffer .= $v; } - public function __get($name){ - return $this->data instanceof NBTTag_Compound ? $this->data->{$name} : false; + public function &__get($name){ + $ret = $this->data instanceof NBTTag_Compound ? $this->data[$name] : false; + return $ret; } public function __set($name, $value){ if($this->data instanceof NBTTag_Compound){ - $this->data->{$name} = $value; + $this->data[$name] = $value; } } public function __isset($name){ - return $this->data instanceof NBTTag_Compound ? isset($this->data->{$name}) : false; + return $this->data instanceof NBTTag_Compound ? isset($this->data[$name]) : false; } public function __unset($name){ if($this->data instanceof NBTTag_Compound){ - unset($this->data->{$name}); + unset($this->data[$name]); } } + public function offsetExists($name){ + return $this->__isset($name); + } + + public function &offsetGet($name){ + return $this->__get($name); + } + + public function offsetSet($name, $value){ + $this->__set($name, $value); + } + + public function offsetUnset($name){ + $this->__unset($name); + } + public function getData(){ return $this->data; } diff --git a/src/nbt/NBTTag.php b/src/nbt/NBTTag.php index 55acec543..34877c337 100644 --- a/src/nbt/NBTTag.php +++ b/src/nbt/NBTTag.php @@ -33,9 +33,9 @@ abstract class NBTTag{ const TAG_Compound = 10; const TAG_Int_Array = 11; - protected $value = 0; + protected $value; - public function getValue(){ + public function &getValue(){ return $this->value; } diff --git a/src/nbt/tags/TAG_Compound.php b/src/nbt/tags/TAG_Compound.php index 3f54e5c10..d39c27131 100644 --- a/src/nbt/tags/TAG_Compound.php +++ b/src/nbt/tags/TAG_Compound.php @@ -19,18 +19,62 @@ * */ -class NBTTag_Compound extends NamedNBTTag{ +class NBTTag_Compound extends NamedNBTTag implements ArrayAccess, Iterator{ public function getType(){ return NBTTag::TAG_Compound; } - public function __get($name){ - return isset($this->value[$name]) ? $this->value[$name]->getValue() : false; + public function rewind(){ + reset($this->value); } - + + public function current(){ + return current($this->value); + } + + public function key(){ + return key($this->value); + } + + public function next(){ + return next($this->value); + } + + public function valid(){ + $key = key($this->value); + return $key !== null and $key !== false; + } + + public function offsetExists($name){ + return $this->__isset($name); + } + + public function &offsetGet($name){ + return $this->__get($name); + } + + public function offsetSet($name, $value){ + $this->__set($name, $value); + } + + public function offsetUnset($name){ + $this->__unset($name); + } + + public function &__get($name){ + $ret = isset($this->value[$name]) ? $this->value[$name] : false; + if(!is_object($ret) or $ret instanceof ArrayAccess){ + return $ret; + }else{ + return $ret->getValue(); + } + } + public function __set($name, $value){ - if(isset($this->value[$name])){ + if($value instanceof NBTTag){ + $this->value[$name] = $value; + }elseif(isset($this->value[$name])){ if($value instanceof NamedNBTTag and $value->getName() !== "" and $value->getName() !== false){ $this->value[$value->getName()]->setValue($value); }else{ diff --git a/src/nbt/tags/TAG_List.php b/src/nbt/tags/TAG_List.php index bc431b784..e54f8c65d 100644 --- a/src/nbt/tags/TAG_List.php +++ b/src/nbt/tags/TAG_List.php @@ -19,18 +19,72 @@ * */ -class NBTTag_List extends NamedNBTTag{ +class NBTTag_List extends NamedNBTTag implements ArrayAccess, Iterator{ + + private $tagType; public function getType(){ return NBTTag::TAG_List; } - public function __get($name){ - return isset($this->value[$name]) ? $this->value[$name]->getValue() : false; + public function setTagType($type){ + $this->tagType = $type; + } + + public function getTagType(){ + return $this->tagType; + } + + public function rewind(){ + reset($this->value); + } + + public function current(){ + return current($this->value); + } + + public function key(){ + return key($this->value); + } + + public function next(){ + return next($this->value); + } + + public function valid(){ + $key = key($this->value); + return $key !== null and $key !== false; + } + + public function offsetExists($name){ + return $this->__isset($name); + } + + public function &offsetGet($name){ + return $this->__get($name); + } + + public function offsetSet($name, $value){ + $this->__set($name, $value); + } + + public function offsetUnset($name){ + $this->__unset($name); + } + + public function &__get($name){ + $ret = isset($this->value[$name]) ? $this->value[$name] : false; + if(!is_object($ret) or $ret instanceof ArrayAccess){ + return $ret; + }else{ + return $ret->getValue(); + } } public function __set($name, $value){ - if(isset($this->value[$name])){ + if($value instanceof NBTTag){ + $this->value[$name] = $value; + }elseif(isset($this->value[$name])){ $this->value[$name]->setValue($value); } } @@ -45,11 +99,10 @@ class NBTTag_List extends NamedNBTTag{ public function read(NBT $nbt){ $this->value = array(); - $tagId = $nbt->getByte(); - $this->value[-1] = $tagId; + $this->tagType = $nbt->getByte(); $size = $nbt->getInt(); for($i = 0; $i < $size and !$nbt->feof(); ++$i){ - switch($tagId){ + switch($this->tagType){ case NBTTag::TAG_Byte: $tag = new NBTTag_Byte(false); $tag->read($nbt); @@ -115,11 +168,11 @@ class NBTTag_List extends NamedNBTTag{ } public function write(NBT $nbt){ - $nbt->putByte($this->value[-1]); - $nbt->putInt(count($this->value) - 1); + $nbt->putByte($this->tagType); + $nbt->putInt(count($this->value)); foreach($this->value as $tag){ if($tag instanceof NBTTag){ - $nbt->writeTag($tag); + $tag->write($nbt); } } }