-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restructure NbtSerializer/NbtDeserializer, increased minimum PHP vers…
…ion to 8.0
- Loading branch information
1 parent
65eabcf
commit 3cb087c
Showing
39 changed files
with
729 additions
and
643 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.