Performance improvements in NBT reading/writing

This commit is contained in:
Shoghi Cervantes 2014-08-24 13:59:37 +02:00
parent 93a2bd36e0
commit 84ce5f1c73
2 changed files with 28 additions and 6 deletions

View File

@ -62,7 +62,7 @@ class NBT{
private $buffer; private $buffer;
private $offset; private $offset;
private $endianness; public $endianness;
private $data; private $data;
public function get($len){ public function get($len){
@ -73,7 +73,7 @@ class NBT{
}elseif($len === true){ }elseif($len === true){
return substr($this->buffer, $this->offset); return substr($this->buffer, $this->offset);
} }
if($len > 1024){ if($len > 16){
return substr($this->buffer, ($this->offset += $len) - $len, $len); return substr($this->buffer, ($this->offset += $len) - $len, $len);
} }
$buffer = ""; $buffer = "";

View File

@ -22,6 +22,7 @@
namespace pocketmine\nbt\tag; namespace pocketmine\nbt\tag;
use pocketmine\nbt\NBT; use pocketmine\nbt\NBT;
use pocketmine\utils\Binary;
class IntArray extends NamedTag{ class IntArray extends NamedTag{
@ -32,15 +33,36 @@ class IntArray extends NamedTag{
public function read(NBT $nbt){ public function read(NBT $nbt){
$this->value = []; $this->value = [];
$size = $nbt->getInt(); $size = $nbt->getInt();
for($i = 0; $i < $size and !$nbt->feof(); ++$i){ $ints = $nbt->get($size * 4);
$this->value[] = $nbt->getInt(); $offset = 0;
if($nbt->endianness === NBT::LITTLE_ENDIAN){
for($i = 0; $i < $size and isset($ints{$offset}); ++$i){
$this->value[$i] = Binary::readLInt(substr($ints, $offset, 4));
$offset += 4;
}
}else{
for($i = 0; $i < $size and isset($ints{$offset}); ++$i){
$this->value[$i] = Binary::readInt(substr($ints, $offset, 4));
$offset += 4;
}
} }
} }
public function write(NBT $nbt){ public function write(NBT $nbt){
$nbt->putInt(count($this->value)); $nbt->putInt(count($this->value));
foreach($this->value as $v){
$nbt->putInt($v); $ints = "";
if($nbt->endianness === NBT::LITTLE_ENDIAN){
foreach($this->value as $v){
$ints .= Binary::writeLInt($v);
}
}else{
foreach($this->value as $v){
$ints .= Binary::writeInt($v);
}
} }
$nbt->put($ints);
} }
} }