Skip to content
This repository was archived by the owner on Aug 8, 2026. It is now read-only.

Commit ef091dc

Browse files
committed
ext-encoding 0.5.x support
1 parent 95fb625 commit ef091dc

7 files changed

Lines changed: 86 additions & 92 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ jobs:
88
if: "!contains(github.event.head_commit.message, '[ci skip]')"
99
strategy:
1010
matrix:
11-
php: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4']
11+
php: ['8.1', '8.2', '8.3', '8.4']
1212
name: PHP ${{ matrix.php }}
1313
steps:
1414
- uses: actions/checkout@v4
1515
- name: Setup PHP
16-
uses: shivammathur/setup-php@2.31.1
16+
uses: shivammathur/setup-php@2.35.4
1717
with:
1818
php-version: ${{ matrix.php }}
1919
ini-values: xdebug.max_nesting_level=3000
20+
extensions: encoding-https://github.com/pmmp/ext-encoding@0.5.1
2021
- name: Cache Composer packages
2122
id: composer-cache
2223
uses: actions/cache@v4

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"description": "PHP library for working with Named Binary Tags",
44
"type": "library",
55
"require": {
6-
"php": "^7.4 || ^8.0",
6+
"php": "^8.1",
77
"php-64bit": "*",
8-
"ext-encoding": "~0.4.0"
8+
"ext-encoding": "~0.5.0"
99
},
1010
"require-dev": {
1111
"phpstan/phpstan": "2.1.0",

src/BaseNbtSerializer.php

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
namespace pocketmine\nbt;
2525

2626
use pmmp\encoding\Byte;
27-
use pmmp\encoding\ByteBuffer;
27+
use pmmp\encoding\ByteBufferReader;
28+
use pmmp\encoding\ByteBufferWriter;
2829
use pmmp\encoding\DataDecodeException;
2930
use pocketmine\nbt\tag\Tag;
3031
use function strlen;
@@ -33,18 +34,15 @@
3334
* Base Named Binary Tag encoder/decoder
3435
*/
3536
abstract class BaseNbtSerializer implements NbtStreamReader, NbtStreamWriter{
36-
protected ByteBuffer $buffer;
37-
38-
public function __construct(){
39-
$this->buffer = new ByteBuffer();
40-
}
37+
protected ByteBufferReader $reader;
38+
protected ByteBufferWriter $writer;
4139

4240
/**
4341
* @throws DataDecodeException
4442
* @throws NbtDataException
4543
*/
4644
private function readRoot(int $maxDepth) : TreeRoot{
47-
$type = Byte::readUnsigned($this->buffer);
45+
$type = Byte::readUnsigned($this->reader);
4846
if($type === NBT::TAG_End){
4947
throw new NbtDataException("Found TAG_End at the start of buffer");
5048
}
@@ -61,15 +59,15 @@ private function readRoot(int $maxDepth) : TreeRoot{
6159
* @throws NbtDataException
6260
*/
6361
public function read(string $buffer, int &$offset = 0, int $maxDepth = 0) : TreeRoot{
64-
$this->buffer = new ByteBuffer($buffer);
65-
$this->buffer->setReadOffset($offset);
62+
$this->reader = new ByteBufferReader($buffer);
63+
$this->reader->setOffset($offset);
6664

6765
try{
6866
$data = $this->readRoot($maxDepth);
6967
}catch(DataDecodeException $e){
7068
throw new NbtDataException($e->getMessage(), 0, $e);
7169
}
72-
$offset = $this->buffer->getReadOffset();
70+
$offset = $this->reader->getOffset();
7371

7472
return $data;
7573
}
@@ -85,11 +83,11 @@ public function read(string $buffer, int &$offset = 0, int $maxDepth = 0) : Tree
8583
* @throws NbtDataException
8684
*/
8785
public function readHeadless(string $buffer, int $rootType, int &$offset = 0, int $maxDepth = 0) : Tag{
88-
$this->buffer = new ByteBuffer($buffer);
89-
$this->buffer->setReadOffset($offset);
86+
$this->reader = new ByteBufferReader($buffer);
87+
$this->reader->setOffset($offset);
9088

9189
$data = NBT::createTag($rootType, $this, new ReaderTracker($maxDepth));
92-
$offset = $this->buffer->getReadOffset();
90+
$offset = $this->reader->getOffset();
9391

9492
return $data;
9593
}
@@ -104,11 +102,12 @@ public function readHeadless(string $buffer, int $rootType, int &$offset = 0, in
104102
* @throws NbtDataException
105103
*/
106104
public function readMultiple(string $buffer, int $maxDepth = 0) : array{
107-
$this->buffer = new ByteBuffer($buffer);
105+
$this->reader = new ByteBufferReader($buffer);
108106

109107
$retval = [];
110108

111-
while($this->buffer->getReadOffset() < $this->buffer->getUsedLength()){
109+
$length = strlen($this->reader->getData());
110+
while($this->reader->getOffset() < $length){
112111
try{
113112
$retval[] = $this->readRoot($maxDepth);
114113
}catch(DataDecodeException $e){
@@ -120,17 +119,17 @@ public function readMultiple(string $buffer, int $maxDepth = 0) : array{
120119
}
121120

122121
private function writeRoot(TreeRoot $root) : void{
123-
Byte::writeUnsigned($this->buffer, $root->getTag()->getType());
122+
Byte::writeUnsigned($this->writer, $root->getTag()->getType());
124123
$this->writeString($root->getName());
125124
$root->getTag()->write($this);
126125
}
127126

128127
public function write(TreeRoot $data) : string{
129-
$this->buffer = new ByteBuffer();
128+
$this->writer = new ByteBufferWriter();
130129

131130
$this->writeRoot($data);
132131

133-
return $this->buffer->toString();
132+
return $this->writer->getData();
134133
}
135134

136135
/**
@@ -140,45 +139,45 @@ public function write(TreeRoot $data) : string{
140139
* @see BaseNbtSerializer::readHeadless()
141140
*/
142141
public function writeHeadless(Tag $data) : string{
143-
$this->buffer = new ByteBuffer();
142+
$this->writer = new ByteBufferWriter();
144143
$data->write($this);
145-
return $this->buffer->toString();
144+
return $this->writer->getData();
146145
}
147146

148147
/**
149148
* @param TreeRoot[] $data
150149
*/
151150
public function writeMultiple(array $data) : string{
152-
$this->buffer = new ByteBuffer();
151+
$this->writer = new ByteBufferWriter();
153152
foreach($data as $root){
154153
$this->writeRoot($root);
155154
}
156-
return $this->buffer->toString();
155+
return $this->writer->getData();
157156
}
158157

159158
public function readByte() : int{
160-
return Byte::readUnsigned($this->buffer);
159+
return Byte::readUnsigned($this->reader);
161160
}
162161

163162
public function readSignedByte() : int{
164-
return Byte::readSigned($this->buffer);
163+
return Byte::readSigned($this->reader);
165164
}
166165

167166
public function writeByte(int $v) : void{
168-
Byte::writeUnsigned($this->buffer, $v);
167+
Byte::writeUnsigned($this->writer, $v);
169168
}
170169

171170
public function readByteArray() : string{
172171
$length = $this->readInt();
173172
if($length < 0){
174173
throw new NbtDataException("Array length cannot be less than zero ($length < 0)");
175174
}
176-
return $this->buffer->readByteArray($length);
175+
return $this->reader->readByteArray($length);
177176
}
178177

179178
public function writeByteArray(string $v) : void{
180179
$this->writeInt(strlen($v)); //TODO: overflow
181-
$this->buffer->writeByteArray($v);
180+
$this->writer->writeByteArray($v);
182181
}
183182

184183
/**
@@ -202,14 +201,14 @@ protected static function checkWriteStringLength(int $len) : int{
202201
}
203202

204203
public function readString() : string{
205-
return $this->buffer->readByteArray(self::checkReadStringLength($this->readShort()));
204+
return $this->reader->readByteArray(self::checkReadStringLength($this->readShort()));
206205
}
207206

208207
/**
209208
* @throws \InvalidArgumentException if the string is too long
210209
*/
211210
public function writeString(string $v) : void{
212211
$this->writeShort(self::checkWriteStringLength(strlen($v)));
213-
$this->buffer->writeByteArray($v);
212+
$this->writer->writeByteArray($v);
214213
}
215214
}

src/BigEndianNbtSerializer.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,71 +24,64 @@
2424
namespace pocketmine\nbt;
2525

2626
use pmmp\encoding\BE;
27-
use function array_values;
28-
use function assert;
2927
use function count;
30-
use function pack;
31-
use function unpack;
3228

3329
class BigEndianNbtSerializer extends BaseNbtSerializer{
3430

3531
public function readShort() : int{
36-
return BE::readUnsignedShort($this->buffer);
32+
return BE::readUnsignedShort($this->reader);
3733
}
3834

3935
public function readSignedShort() : int{
40-
return BE::readSignedShort($this->buffer);
36+
return BE::readSignedShort($this->reader);
4137
}
4238

4339
public function writeShort(int $v) : void{
44-
BE::writeUnsignedShort($this->buffer, $v);
40+
BE::writeUnsignedShort($this->writer, $v);
4541
}
4642

4743
public function readInt() : int{
48-
return BE::readSignedInt($this->buffer);
44+
return BE::readSignedInt($this->reader);
4945
}
5046

5147
public function writeInt(int $v) : void{
52-
BE::writeSignedInt($this->buffer, $v);
48+
BE::writeSignedInt($this->writer, $v);
5349
}
5450

5551
public function readLong() : int{
56-
return BE::readSignedLong($this->buffer);
52+
return BE::readSignedLong($this->reader);
5753
}
5854

5955
public function writeLong(int $v) : void{
60-
BE::writeSignedLong($this->buffer, $v);
56+
BE::writeSignedLong($this->writer, $v);
6157
}
6258

6359
public function readFloat() : float{
64-
return BE::readFloat($this->buffer);
60+
return BE::readFloat($this->reader);
6561
}
6662

6763
public function writeFloat(float $v) : void{
68-
BE::writeFloat($this->buffer, $v);
64+
BE::writeFloat($this->writer, $v);
6965
}
7066

7167
public function readDouble() : float{
72-
return BE::readDouble($this->buffer);
68+
return BE::readDouble($this->reader);
7369
}
7470

7571
public function writeDouble(float $v) : void{
76-
BE::writeDouble($this->buffer, $v);
72+
BE::writeDouble($this->writer, $v);
7773
}
7874

7975
public function readIntArray() : array{
8076
$len = $this->readInt();
8177
if($len < 0){
8278
throw new NbtDataException("Array length cannot be less than zero ($len < 0)");
8379
}
84-
/** @var array<int>|false $unpacked */
85-
$unpacked = unpack("N*", $this->buffer->readByteArray($len * 4));
86-
assert($unpacked !== false, "The formatting string is valid, and we gave a multiple of 4 bytes");
87-
return array_values($unpacked);
80+
return BE::readSignedIntArray($this->reader, $len);
8881
}
8982

9083
public function writeIntArray(array $array) : void{
9184
$this->writeInt(count($array));
92-
$this->buffer->writeByteArray(pack("N*", ...$array));
85+
BE::writeSignedIntArray($this->writer, $array);
9386
}
9487
}

0 commit comments

Comments
 (0)