New NBT tags

This commit is contained in:
Shoghi Cervantes 2014-02-26 13:08:53 +01:00
parent 85f8613803
commit 28d1da1286
15 changed files with 812 additions and 0 deletions

205
src/nbt/NBT.php Normal file
View File

@ -0,0 +1,205 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class NBT{
const LITTLE_ENDIAN = 0;
const BIG_ENDIAN = 1;
private $buffer;
private $offset;
private $endianness;
private $data = array();
public function get($len){
if($len <= 0){
$this->offset = strlen($this->buffer) - 1;
return "";
}
if($len === true){
return substr($this->buffer, $this->offset);
}
$this->offset += $len;
return substr($this->buffer, $this->offset - $len, $len);
}
public function put($v){
$this->buffer .= $v;
}
public function feof(){
return !isset($this->buffer{$this->offset});
}
public function __construct($endianness = NBT::BIG_ENDIAN){
$this->offset = 0;
$this->endianness = $endianness & 0x01;
}
public function read($buffer){
$this->offset = 0;
$this->buffer = $buffer;
$this->readTag();
}
public function write(){
$this->offset = 0;
if($this->data instanceof NBTTag_Compound){
$this->writeTag($this->data);
return true;
}else{
return false;
}
}
protected 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);
break;
case NBTTag::TAG_Byte:
$tag = new NBTTag_Byte($this->getString());
$tag->read($this);
break;
case NBTTag::TAG_Short:
$tag = new NBTTag_Short($this->getString());
$tag->read($this);
break;
case NBTTag::TAG_Int:
$tag = new NBTTag_Int($this->getString());
$tag->read($this);
break;
case NBTTag::TAG_Long:
$tag = new NBTTag_Long($this->getString());
$tag->read($this);
break;
case NBTTag::TAG_Float:
$tag = new NBTTag_Float($this->getString());
$tag->read($this);
break;
case NBTTag::TAG_Double:
$tag = new NBTTag_Double($this->getString());
$tag->read($this);
break;
case NBTTag::TAG_Byte_Array:
$tag = new NBTTag_Byte_Array($this->getString());
$tag->read($this);
break;
case NBTTag::TAG_String:
$tag = new NBTTag_String($this->getString());
$tag->read($this);
break;
case NBTTag::TAG_List:
$tag = new NBTTag_List($this->getString());
$tag->read($this);
break;
case NBTTag::TAG_Compound:
$tag = new NBTTag_Compound($this->getString());
$tag->read($this);
break;
case NBTTag::TAG_Int_Array:
$tag = new NBTTag_Int_Array($this->getString());
$tag->read($this);
break;
default:
return false;
}
return $tag;
}
public function writeTag(NBTTag $tag){
$this->putByte($tag->getType());
if($tag instanceof NamedNBTTag and $tag->getName() !== false){
$this->putString($tag->getName());
}
$tag->write($this);
}
public function getByte(){
return Utils::readByte($this->get(1), true);
}
public function putByte($v){
$this->buffer .= Utils::writeByte($v);
}
public function getShort(){
return $this->endianness === self::BIG_ENDIAN ? Utils::readShort($this->get(2)) : Utils::readLShort($this->get(2));
}
public function putShort($v){
$this->buffer .= $this->endianness === self::BIG_ENDIAN ? Utils::writeShort($v) : Utils::writeLShort($v);
}
public function getInt(){
return $this->endianness === self::BIG_ENDIAN ? Utils::readInt($this->get(4)) : Utils::readLInt($this->get(4));
}
public function putInt($v){
$this->buffer .= $this->endianness === self::BIG_ENDIAN ? Utils::writeInt($v) : Utils::writeLInt($v);
}
public function getLong(){
return $this->endianness === self::BIG_ENDIAN ? Utils::readLong($this->get(8)) : Utils::readLLong($this->get(8));
}
public function putLong($v){
$this->buffer .= $this->endianness === self::BIG_ENDIAN ? Utils::writeLong($v) : Utils::writeLLong($v);
}
public function getFloat(){
return $this->endianness === self::BIG_ENDIAN ? Utils::readFloat($this->get(4)) : Utils::readLFloat($this->get(4));
}
public function putFloat($v){
$this->buffer .= $this->endianness === self::BIG_ENDIAN ? Utils::writeFloat($v) : Utils::writeLFloat($v);
}
public function getDouble(){
return $this->endianness === self::BIG_ENDIAN ? Utils::readDouble($this->get(8)) : Utils::readLDouble($this->get(8));
}
public function putDouble($v){
$this->buffer .= $this->endianness === self::BIG_ENDIAN ? Utils::writeDouble($v) : Utils::writeLDouble($v);
}
public function getString(){
return $this->get($this->getShort());
}
public function putString($v){
$this->putShort(strlen($v));
$this->buffer .= $v;
}
public function getData(){
return $this->data;
}
public function setData(NBTTag_Compound $data){
$this->data = $data;
}
}

55
src/nbt/NBTTag.php Normal file
View File

@ -0,0 +1,55 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
abstract class NBTTag{
const TAG_End = 0;
const TAG_Byte = 1;
const TAG_Short = 2;
const TAG_Int = 3;
const TAG_Long = 4;
const TAG_Float = 5;
const TAG_Double = 6;
const TAG_Byte_Array = 7;
const TAG_String = 8;
const TAG_List = 9;
const TAG_Compound = 10;
const TAG_Int_Array = 11;
protected $value = 0;
public function getValue(){
return $this->value;
}
public abstract function getType();
public function setValue($value){
$this->value = $value;
}
abstract public function write(NBT $nbt);
abstract public function read(NBT $nbt);
public final function __toString(){
return $this->value;
}
}

40
src/nbt/NamedNBTTag.php Normal file
View File

@ -0,0 +1,40 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
/***REM_START***/
require_once("NBTTag.php");
/***REM_END***/
class NamedNBTTag{
protected $name;
public function __construct($name = ""){
$this->name = $name;
}
public function getName(){
return $this->name;
}
public function setName($name){
$this->name = $name;
}
}

35
src/nbt/tags/TAG_Byte.php Normal file
View File

@ -0,0 +1,35 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class NBTTag_Byte extends NamedNBTTag{
public function getType(){
return self::TAG_Byte;
}
public function read(NBT $nbt){
$this->value = $nbt->getByte();
}
public function write(NBT $nbt){
$nbt->putByte($this->value);
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class NBTTag_Byte_Array extends NamedNBTTag{
public function getType(){
return self::TAG_Byte_Array;
}
public function read(NBT $nbt){
$this->value = $nbt->get($this->getInt());
}
public function write(NBT $nbt){
$nbt->putInt(strlen($this->value));
$nbt->put($this->value);
}
}

View File

@ -0,0 +1,45 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class NBTTag_Compound extends NamedNBTTag{
public function getType(){
return self::TAG_Compound;
}
public function read(NBT $nbt){
$this->value = array();
do{
$tag = $nbt->readTag();
if($tag instanceof NamedNBTTag){
$this->value[$tag->getName()] = $tag;
}else{
$this->value[] = $tag;
}
}while(!($tag instanceof NBTTag_End) and !$nbt->feof());
}
public function write(NBT $nbt){
foreach($this->value as $tag){
$nbt->writeTag($tag);
}
}
}

View File

@ -0,0 +1,35 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class NBTTag_Double extends NamedNBTTag{
public function getType(){
return self::TAG_Double;
}
public function read(NBT $nbt){
$this->value = $nbt->getDouble();
}
public function write(NBT $nbt){
$nbt->putDouble($this->value);
}
}

35
src/nbt/tags/TAG_End.php Normal file
View File

@ -0,0 +1,35 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class NBTTag_End extends NBTTag{
public function getType(){
return self::TAG_End;
}
public function read(NBT $nbt){
}
public function write(NBT $nbt){
}
}

View File

@ -0,0 +1,35 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class NBTTag_Float extends NamedNBTTag{
public function getType(){
return self::TAG_Float;
}
public function read(NBT $nbt){
$this->value = $nbt->getFloat();
}
public function write(NBT $nbt){
$nbt->putFloat($this->value);
}
}

35
src/nbt/tags/TAG_Int.php Normal file
View File

@ -0,0 +1,35 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class NBTTag_Int extends NamedNBTTag{
public function getType(){
return self::TAG_Int;
}
public function read(NBT $nbt){
$this->value = $nbt->getInt();
}
public function write(NBT $nbt){
$nbt->putInt($this->value);
}
}

View File

@ -0,0 +1,42 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class NBTTag_Int_Array extends NamedNBTTag{
public function getType(){
return self::TAG_Int_Array;
}
public function read(NBT $nbt){
$this->value = array();
$size = $nbt->getInt();
for($i = 0; $i < $size and !$nbt->feof(); ++$i){
$this->value[] = $nbt->getInt();
}
}
public function write(NBT $nbt){
$nbt->putInt(count($this->value));
foreach($this->value as $v){
$nbt->putInt($v);
}
}
}

108
src/nbt/tags/TAG_List.php Normal file
View File

@ -0,0 +1,108 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class NBTTag_List extends NamedNBTTag{
public function getType(){
return self::TAG_List;
}
public function read(NBT $nbt){
$this->value = array();
$tagId = $nbt->getByte();
$this->value[-1] = $tagId;
$size = $nbt->getInt();
for($i = 0; $i < $size and !$nbt->feof(); ++$i){
switch($tagId){
case NBTTag::TAG_Byte:
$tag = new NBTTag_Byte(false);
$tag->read($this);
$this->value[] = $tag;
break;
case NBTTag::TAG_Byte:
$tag = new NBTTag_Byte(false);
$tag->read($this);
$this->value[] = $tag;
break;
case NBTTag::TAG_Short:
$tag = new NBTTag_Short(false);
$tag->read($this);
$this->value[] = $tag;
break;
case NBTTag::TAG_Int:
$tag = new NBTTag_Int(false);
$tag->read($this);
$this->value[] = $tag;
break;
case NBTTag::TAG_Long:
$tag = new NBTTag_Long(false);
$tag->read($this);
$this->value[] = $tag;
break;
case NBTTag::TAG_Float:
$tag = new NBTTag_Float(false);
$tag->read($this);
$this->value[] = $tag;
break;
case NBTTag::TAG_Double:
$tag = new NBTTag_Double(false);
$tag->read($this);
$this->value[] = $tag;
break;
case NBTTag::TAG_Byte_Array:
$tag = new NBTTag_Byte_Array(false);
$tag->read($this);
$this->value[] = $tag;
break;
case NBTTag::TAG_String:
$tag = new NBTTag_String(false);
$tag->read($this);
$this->value[] = $tag;
break;
case NBTTag::TAG_List:
$tag = new NBTTag_List(false);
$tag->read($this);
$this->value[] = $tag;
break;
case NBTTag::TAG_Compound:
$tag = new NBTTag_Compound(false);
$tag->read($this);
$this->value[] = $tag;
break;
case NBTTag::TAG_Int_Array:
$tag = new NBTTag_Int_Array(false);
$tag->read($this);
$this->value[] = $tag;
break;
}
}
}
public function write(NBT $nbt){
$nbt->putByte($this->value[-1]);
$nbt->putInt(count($this->value) - 1);
foreach($this->value as $tag){
if($tag instanceof NBTTag){
$nbt->writeTag($tag);
}
}
}
}

35
src/nbt/tags/TAG_Long.php Normal file
View File

@ -0,0 +1,35 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class NBTTag_Long extends NamedNBTTag{
public function getType(){
return self::TAG_Long;
}
public function read(NBT $nbt){
$this->value = $nbt->getLong();
}
public function write(NBT $nbt){
$nbt->putLong($this->value);
}
}

View File

@ -0,0 +1,35 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class NBTTag_Short extends NamedNBTTag{
public function getType(){
return self::TAG_Short;
}
public function read(NBT $nbt){
$this->value = $nbt->getShort();
}
public function write(NBT $nbt){
$nbt->putShort($this->value);
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class NBTTag_String extends NamedNBTTag{
public function getType(){
return self::TAG_String;
}
public function read(NBT $nbt){
$this->value = $nbt->get($this->getShort());
}
public function write(NBT $nbt){
$nbt->putShort(strlen($this->value));
$nbt->put($this->value);
}
}