Skip to content

Commit

Permalink
restructure NbtSerializer/NbtDeserializer, increased minimum PHP vers…
Browse files Browse the repository at this point in the history
…ion to 8.0
  • Loading branch information
KurtThiemann committed May 6, 2022
1 parent 65eabcf commit 3cb087c
Show file tree
Hide file tree
Showing 39 changed files with 729 additions and 643 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"require": {
"pocketmine/binaryutils": "^0.2.1",
"php": "^7.4 || ^8.0",
"php": ">=8.0",
"php-64bit": "*",
"ext-zlib": "*",
"ext-json": "*"
Expand Down
91 changes: 91 additions & 0 deletions src/Deserializer/BedrockEditionNbtDeserializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Aternos\Nbt\Deserializer;

use Aternos\Nbt\MachineByteOrder;
use Aternos\Nbt\NbtFormat;
use pocketmine\utils\Binary;

class BedrockEditionNbtDeserializer extends NbtDeserializer
{
/**
* @inheritDoc
*/
public function getFormat(): int
{
return NbtFormat::BEDROCK_EDITION;
}

/**
* @inheritDoc
*/
public function readLengthPrefix(): DeserializerIntReadResult
{
$raw = $this->getReader()->read(4);
return new DeserializerIntReadResult(Binary::signInt(Binary::readLInt($raw)), $raw);
}

/**
* @inheritDoc
*/
public function readStringLengthPrefix(): DeserializerIntReadResult
{
$raw = $this->getReader()->read(2);
return new DeserializerIntReadResult(Binary::readLShort($raw), $raw);
}

/**
* @inheritDoc
*/
public function readByte(): DeserializerIntReadResult
{
$raw = $this->getReader()->read(1);
return new DeserializerIntReadResult(Binary::readSignedByte($raw), $raw);
}

/**
* @inheritDoc
*/
public function readShort(): DeserializerIntReadResult
{
$raw = $this->getReader()->read(2);
return new DeserializerIntReadResult(Binary::readSignedLShort($raw), $raw);
}

/**
* @inheritDoc
*/
public function readInt(): DeserializerIntReadResult
{
$raw = $this->getReader()->read(4);
return new DeserializerIntReadResult(Binary::signInt(Binary::readLInt($raw)), $raw);
}

/**
* @inheritDoc
*/
public function readLong(): DeserializerIntReadResult
{
$raw = $this->getReader()->read(8);
$value = @unpack("q", MachineByteOrder::isBigEndian() ? strrev($raw) : $raw)[1] ?? 0;;
return new DeserializerIntReadResult($value, $raw);
}

/**
* @inheritDoc
*/
public function readFloat(): DeserializerFloatReadResult
{
$raw = $this->getReader()->read(4);
return new DeserializerFloatReadResult(Binary::readLFloat($raw), $raw);
}

/**
* @inheritDoc
*/
public function readDouble(): DeserializerFloatReadResult
{
$raw = $this->getReader()->read(8);
return new DeserializerFloatReadResult(Binary::readLDouble($raw), $raw);
}
}
69 changes: 69 additions & 0 deletions src/Deserializer/BedrockEditionNetworkNbtDeserializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Aternos\Nbt\Deserializer;

use Aternos\Nbt\NbtFormat;
use pocketmine\utils\Binary;

class BedrockEditionNetworkNbtDeserializer extends BedrockEditionNbtDeserializer
{
/**
* @inheritDoc
*/
public function getFormat(): int
{
return NbtFormat::BEDROCK_EDITION_NETWORK;
}

/**
* @inheritDoc
*/
public function readLengthPrefix(): DeserializerIntReadResult
{
$reader = $this->getReader();
$raw = $reader->read(5);
$offset = 0;
$value = Binary::readVarInt($raw, $offset);
$reader->returnData(substr($raw, $offset));
return new DeserializerIntReadResult($value, substr($raw, 0, $offset));
}

/**
* @inheritDoc
*/
public function readStringLengthPrefix(): DeserializerIntReadResult
{
$reader = $this->getReader();
$raw = $reader->read(5);
$offset = 0;
$value = Binary::readUnsignedVarInt($raw, $offset);
$reader->returnData(substr($raw, $offset));
return new DeserializerIntReadResult($value, substr($raw, 0, $offset));
}

/**
* @inheritDoc
*/
public function readInt(): DeserializerIntReadResult
{
$reader = $this->getReader();
$raw = $reader->read(5);
$offset = 0;
$value = Binary::readVarInt($raw, $offset);
$reader->returnData(substr($raw, $offset));
return new DeserializerIntReadResult($value, substr($raw, 0, $offset));
}

/**
* @inheritDoc
*/
public function readLong(): DeserializerIntReadResult
{
$reader = $this->getReader();
$raw = $reader->read(10);
$offset = 0;
$value = Binary::readVarLong($raw, $offset);
$reader->returnData(substr($raw, $offset));
return new DeserializerIntReadResult($value, substr($raw, 0, $offset));
}
}
19 changes: 19 additions & 0 deletions src/Deserializer/DeserializerFloatReadResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Aternos\Nbt\Deserializer;

class DeserializerFloatReadResult extends DeserializerReadResult
{
public function __construct(protected float $value, string $rawData)
{
parent::__construct($rawData);
}

/**
* @return float
*/
public function getValue(): float
{
return $this->value;
}
}
19 changes: 19 additions & 0 deletions src/Deserializer/DeserializerIntReadResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Aternos\Nbt\Deserializer;

class DeserializerIntReadResult extends DeserializerReadResult
{
public function __construct(protected int $value, string $rawData)
{
parent::__construct($rawData);
}

/**
* @return int
*/
public function getValue(): int
{
return $this->value;
}
}
18 changes: 18 additions & 0 deletions src/Deserializer/DeserializerReadResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Aternos\Nbt\Deserializer;

abstract class DeserializerReadResult
{
public function __construct(protected string $rawData)
{
}

/**
* @return string
*/
public function getRawData(): string
{
return $this->rawData;
}
}
91 changes: 91 additions & 0 deletions src/Deserializer/JavaEditionNbtDeserializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Aternos\Nbt\Deserializer;

use Aternos\Nbt\MachineByteOrder;
use Aternos\Nbt\NbtFormat;
use pocketmine\utils\Binary;

class JavaEditionNbtDeserializer extends NbtDeserializer
{
/**
* @inheritDoc
*/
public function getFormat(): int
{
return NbtFormat::JAVA_EDITION;
}

/**
* @inheritDoc
*/
public function readLengthPrefix(): DeserializerIntReadResult
{
$raw = $this->getReader()->read(4);
return new DeserializerIntReadResult(Binary::signInt(Binary::readInt($raw)), $raw);
}

/**
* @inheritDoc
*/
public function readStringLengthPrefix(): DeserializerIntReadResult
{
$raw = $this->getReader()->read(2);
return new DeserializerIntReadResult(Binary::readShort($raw), $raw);
}

/**
* @inheritDoc
*/
public function readByte(): DeserializerIntReadResult
{
$raw = $this->getReader()->read(1);
return new DeserializerIntReadResult(Binary::readSignedByte($raw), $raw);
}

/**
* @inheritDoc
*/
public function readShort(): DeserializerIntReadResult
{
$raw = $this->getReader()->read(2);
return new DeserializerIntReadResult(Binary::readSignedShort($raw), $raw);
}

/**
* @inheritDoc
*/
public function readInt(): DeserializerIntReadResult
{
$raw = $this->getReader()->read(4);
return new DeserializerIntReadResult(Binary::signInt(Binary::readInt($raw)), $raw);
}

/**
* @inheritDoc
*/
public function readLong(): DeserializerIntReadResult
{
$raw = $this->getReader()->read(8);
$value = @unpack("q", MachineByteOrder::isLittleEndian() ? strrev($raw) : $raw)[1] ?? 0;
return new DeserializerIntReadResult($value, $raw);
}

/**
* @inheritDoc
*/
public function readFloat(): DeserializerFloatReadResult
{
$raw = $this->getReader()->read(4);
return new DeserializerFloatReadResult(Binary::readFloat($raw), $raw);
}

/**
* @inheritDoc
*/
public function readDouble(): DeserializerFloatReadResult
{
$raw = $this->getReader()->read(8);
return new DeserializerFloatReadResult(Binary::readDouble($raw), $raw);
}
}
69 changes: 69 additions & 0 deletions src/Deserializer/NbtDeserializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Aternos\Nbt\Deserializer;

use Aternos\Nbt\IO\Reader\Reader;

abstract class NbtDeserializer
{
public function __construct(protected Reader $reader)
{
}

/**
* Read an NBT length prefix (TAG_List, TAG_Byte_Array, TAG_Int_Array, and TAG_Long_Array)
*
* @return DeserializerIntReadResult
*/
abstract public function readLengthPrefix(): DeserializerIntReadResult;

/**
* Read an NBT string length prefix
*
* @return DeserializerIntReadResult
*/
abstract public function readStringLengthPrefix(): DeserializerIntReadResult;

/**
* @return DeserializerIntReadResult
*/
abstract public function readByte(): DeserializerIntReadResult;

/**
* @return DeserializerIntReadResult
*/
abstract public function readShort(): DeserializerIntReadResult;

/**
* @return DeserializerIntReadResult
*/
abstract public function readInt(): DeserializerIntReadResult;

/**
* @return DeserializerIntReadResult
*/
abstract public function readLong(): DeserializerIntReadResult;

/**
* @return DeserializerFloatReadResult
*/
abstract public function readFloat(): DeserializerFloatReadResult;

/**
* @return DeserializerFloatReadResult
*/
abstract public function readDouble(): DeserializerFloatReadResult;

/**
* @return int
*/
abstract public function getFormat(): int;

/**
* @return Reader
*/
public function getReader(): Reader
{
return $this->reader;
}
}
Loading

0 comments on commit 3cb087c

Please sign in to comment.