diff --git a/seedValueTest b/seedValueTest new file mode 100644 index 0000000..5337184 --- /dev/null +++ b/seedValueTest @@ -0,0 +1 @@ +žò\Öð$Ž \ No newline at end of file diff --git a/src/MachineByteOrder.php b/src/MachineByteOrder.php new file mode 100644 index 0000000..9870c32 --- /dev/null +++ b/src/MachineByteOrder.php @@ -0,0 +1,36 @@ +> 32; - $secondHalf = $value & 0xFFFFFFFF; - return strrev(pack('VV', $firstHalf, $secondHalf)); + $packed = pack("q", $value); + return MachineByteOrder::isBigEndian() ? strrev($packed) : $packed; } /** diff --git a/src/Serializer/JavaEditionNbtSerializer.php b/src/Serializer/JavaEditionNbtSerializer.php index a2e54c5..5c4fede 100644 --- a/src/Serializer/JavaEditionNbtSerializer.php +++ b/src/Serializer/JavaEditionNbtSerializer.php @@ -3,6 +3,7 @@ namespace Aternos\Nbt\Serializer; use Aternos\Nbt\IO\Reader\Reader; +use Aternos\Nbt\MachineByteOrder; use Aternos\Nbt\NbtFormat; use pocketmine\utils\Binary; @@ -96,15 +97,7 @@ public function encodeInt(int $value): string */ public function decodeLong(string $data): int { - $firstHalf = Binary::readInt(substr($data, 0, 4)); - $secondHalf = Binary::readInt(substr($data, 4)); - - $negative = boolval($firstHalf & pow(2, 31)); - - $firstHalf &= (pow(2, 31) - 1); - $result = ($firstHalf << 32) + $secondHalf; - - return $negative ? $result*-1 : $result; + return @unpack("q", MachineByteOrder::isLittleEndian() ? strrev($data) : $data)[1] ?? 0; } /** @@ -112,9 +105,8 @@ public function decodeLong(string $data): int */ public function encodeLong(int $value): string { - $firstHalf = ($value & 0xFFFFFFFF00000000) >> 32; - $secondHalf = $value & 0xFFFFFFFF; - return pack('NN', $firstHalf, $secondHalf); + $packed = pack("q", $value); + return MachineByteOrder::isLittleEndian() ? strrev($packed) : $packed; } /**