https://github.com/sof3/nbtstreams
Stream-style NBT parser/exporter
https://github.com/sof3/nbtstreams
nbt-parser stream-style
Last synced: 12 months ago
JSON representation
Stream-style NBT parser/exporter
- Host: GitHub
- URL: https://github.com/sof3/nbtstreams
- Owner: SOF3
- License: lgpl-3.0
- Created: 2017-08-02T04:15:23.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-08-26T14:24:00.000Z (almost 9 years ago)
- Last Synced: 2025-06-11T15:01:51.278Z (12 months ago)
- Topics: nbt-parser, stream-style
- Language: PHP
- Size: 21.5 KB
- Stars: 8
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
nbtstreams
==========
This library implements an NBT parser and an NBT exporter in a stream style.
Usage (this example uses special indentation for the sake of readability):
```php
$nbtWriter = new NbtWriter("nbtFile.dat");
$nbtWriter
->name("Example")
->startCompound()
->name("aLong")->writeLong(1234)
->name("ReadableString")->writeString("I am readable")
->name("RawByteArray")->writeByteArray("\x00\xff\xff\x00\xfe\xfe\xfe\xfe\xfd\xfd\xfd\xfd\x12\x34\x56\x78")
->name("ByteArrayFromFile")->writeByteArrayFromFile("binary-file.dat")
->name("ByteArrayWithGenerator");
$generator = $nbtWriter->startByteArrayWriterFromFile("binary-file-2.dat");
do{
$generator->send(2048); // copy 2048 bytes
sleep(5);
}while($generator->valid());
$nbtWriter->endCompound()
->close();
$nbt = new NbtReader();
$name = $nbt->readName();
assert($name === "Example");
$nbt->startCompound();
$example = [];
while(($name = $nbt->readName($type)) !== null){
switch($name){
case "aLong":
$example["aLong"] = $nbt->readLong();
break;
case "ReadableString":
$example["string"] = $nbt->readString();
break;
case "RawByteArray":
$example["rawByteArray"] = $nbt->readByteArray();
break;
case "ByteArrayFromFile":
$fh = fopen("binary-file.copy.dat", "wb");
foreach($nbt->generateByteArrayReader() as $buffer){
fwrite($fh, $buffer);
}
fclose($fh);
break;
}
}
$nbt->endCompound();
$nbt->close();
```
Advantages of using nbtstreams over the traditional NBT class from PocketMine:
* **Improves performance** because there is no need to read the whole file before executing parts of it; can spread the file reading into ticks
* **Prevents memory leak** because there is no need to store the whole decompressed file in memory before using/saving it.