mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-03 08:39:53 +00:00
New NBT parser/writer
This commit is contained in:
parent
515affa1a7
commit
ff99aab6ff
@ -1,17 +1,34 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* Class for reading in NBT-format files.
|
|
||||||
*
|
|
||||||
* @author Justin Martin <frozenfire@thefrozenfire.com>
|
|
||||||
* @version 1.0
|
|
||||||
* MODIFIED BY @shoghicp
|
|
||||||
*
|
|
||||||
* Dependencies:
|
|
||||||
* PHP 4.3+ (5.3+ recommended)
|
|
||||||
*/
|
|
||||||
|
|
||||||
class NBT {
|
/*
|
||||||
public $root = array();
|
|
||||||
|
-
|
||||||
|
/ \
|
||||||
|
/ \
|
||||||
|
/ PocketMine \
|
||||||
|
/ MP \
|
||||||
|
|\ @shoghicp /|
|
||||||
|
|. \ / .|
|
||||||
|
| .. \ / .. |
|
||||||
|
| .. | .. |
|
||||||
|
| .. | .. |
|
||||||
|
\ | /
|
||||||
|
\ | /
|
||||||
|
\ | /
|
||||||
|
\ | /
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
class NBT{
|
||||||
|
public $tree = array();
|
||||||
|
public $binary = b"";
|
||||||
|
private $offset = 0;
|
||||||
|
|
||||||
const TAG_END = 0;
|
const TAG_END = 0;
|
||||||
const TAG_BYTE = 1;
|
const TAG_BYTE = 1;
|
||||||
@ -25,78 +42,172 @@ class NBT {
|
|||||||
const TAG_LIST = 9;
|
const TAG_LIST = 9;
|
||||||
const TAG_COMPOUND = 10;
|
const TAG_COMPOUND = 10;
|
||||||
|
|
||||||
public function loadFile($filename) {
|
public function read($n){
|
||||||
if(is_file($filename)) {
|
if($n <= 0){
|
||||||
$fp = fopen($filename, "rb");
|
return "";
|
||||||
}else{
|
|
||||||
trigger_error("First parameter must be a filename", E_USER_WARNING);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
switch(basename(strtolower($filename), ".dat")){
|
$this->offset += $n;
|
||||||
case "level":
|
return substr($this->binary, $this->offset - $n, $n);
|
||||||
$version = Utils::readLInt(fread($fp, 4));
|
|
||||||
$lenght = Utils::readLInt(fread($fp, 4));
|
|
||||||
break;
|
|
||||||
case "entities":
|
|
||||||
fread($fp, 12);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$this->traverseTag($fp, $this->root);
|
|
||||||
return end($this->root);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function traverseTag($fp, &$tree) {
|
public function write($bin){
|
||||||
if(feof($fp)) {
|
$this->binary .= $bin;
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
$tagType = $this->readType($fp, self::TAG_BYTE); // Read type byte.
|
public function load($str){
|
||||||
if($tagType == self::TAG_END) {
|
$this->offset = 0;
|
||||||
return false;
|
$this->binary = (string) $str;
|
||||||
} else {
|
$this->parseTree($this->tree);
|
||||||
$tagName = $this->readType($fp, self::TAG_STRING);
|
}
|
||||||
$tagData = $this->readType($fp, $tagType);
|
|
||||||
$tree[] = array("type"=>$tagType, "name"=>$tagName, "value"=>$tagData);
|
public function readTAG_BYTE(){
|
||||||
return true;
|
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::writeLFloar($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; ++$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:
|
||||||
|
die("Invalid NBT Tag $tag");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$node[] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function readType($fp, $tagType) {
|
private function parseTree(&$node){
|
||||||
switch($tagType) {
|
while(($tag = ord($this->read(1))) !== self::TAG_END){
|
||||||
case self::TAG_BYTE: // Signed byte (8 bit)
|
$name = $this->readTAG_STRING();
|
||||||
return Utils::readByte(fread($fp, 1));
|
switch($tag){
|
||||||
case self::TAG_SHORT: // Signed short (16 bit, big endian)
|
case self::TAG_BYTE:
|
||||||
return Utils::readLShort(fread($fp, 2));
|
$value = $this->readTAG_BYTE();
|
||||||
case self::TAG_INT: // Signed integer (32 bit, big endian)
|
break;
|
||||||
return Utils::readLInt(fread($fp, 4));
|
case self::TAG_SHORT:
|
||||||
case self::TAG_LONG: // Signed long (64 bit, big endian)
|
$value = $this->readTAG_SHORT();
|
||||||
return Utils::readLLong(fread($fp, 8));
|
break;
|
||||||
case self::TAG_FLOAT: // Floating point value (32 bit, big endian, IEEE 754-2008)
|
case self::TAG_INT:
|
||||||
return Utils::readLFloat(fread($fp, 4));
|
$value = $this->readTAG_INT();
|
||||||
case self::TAG_DOUBLE: // Double value (64 bit, big endian, IEEE 754-2008)
|
break;
|
||||||
return Utils::readLDouble(fread($fp, 8));
|
case self::TAG_LONG:
|
||||||
case self::TAG_BYTE_ARRAY: // Byte array
|
$value = $this->readTAG_LONG();
|
||||||
$arrayLength = $this->readType($fp, self::TAG_INT);
|
break;
|
||||||
$array = array();
|
case self::TAG_FLOAT:
|
||||||
for($i = 0; $i < $arrayLength; $i++) $array[] = $this->readType($fp, self::TAG_BYTE);
|
$value = $this->readTAG_FLOAT();
|
||||||
return $array;
|
break;
|
||||||
case self::TAG_STRING: // String
|
case self::TAG_DOUBLE:
|
||||||
if(!$stringLength = $this->readType($fp, self::TAG_SHORT)) return "";
|
$value = $this->readTAG_DOUBLE();
|
||||||
$string = fread($fp, $stringLength); // Read in number of bytes specified by string length, and decode from utf8.
|
break;
|
||||||
return $string;
|
case self::TAG_BYTE_ARRAY:
|
||||||
case self::TAG_LIST: // List
|
$value = $this->readTAG_BYTE_ARRAY();
|
||||||
$tagID = $this->readType($fp, self::TAG_BYTE);
|
break;
|
||||||
$listLength = $this->readType($fp, self::TAG_INT);
|
case self::TAG_STRING:
|
||||||
$list = array("type"=>$tagID, "value"=>array());
|
$value = $this->readTAG_STRING();
|
||||||
for($i = 0; $i < $listLength; $i++) {
|
break;
|
||||||
if(feof($fp)) break;
|
case self::TAG_LIST:
|
||||||
$list["value"][] = $this->readType($fp, $tagID);
|
$value = array();
|
||||||
}
|
$this->parseList($value, ord($this->read(1)), Utils::readLInt($this->read(4)));
|
||||||
return $list;
|
break;
|
||||||
case self::TAG_COMPOUND: // Compound
|
case self::TAG_COMPOUND:
|
||||||
$tree = array();
|
$value = array();
|
||||||
while($this->traverseTag($fp, $tree));
|
$this->parseTree($value);
|
||||||
return $tree;
|
break;
|
||||||
|
default:
|
||||||
|
die("Invalid NBT Tag $tag");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$node[$name] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
|
Loading…
x
Reference in New Issue
Block a user