PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
LittleEndianNbtSerializer.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20*/
21
22declare(strict_types=1);
23
24namespace pocketmine\nbt;
25
26use function array_values;
27use function assert;
28use function count;
29use function pack;
30use function unpack;
31
33
34 public function readShort() : int{
35 return $this->buffer->getLShort();
36 }
37
38 public function readSignedShort() : int{
39 return $this->buffer->getSignedLShort();
40 }
41
42 public function writeShort(int $v) : void{
43 $this->buffer->putLShort($v);
44 }
45
46 public function readInt() : int{
47 return $this->buffer->getLInt();
48 }
49
50 public function writeInt(int $v) : void{
51 $this->buffer->putLInt($v);
52 }
53
54 public function readLong() : int{
55 return $this->buffer->getLLong();
56 }
57
58 public function writeLong(int $v) : void{
59 $this->buffer->putLLong($v);
60 }
61
62 public function readFloat() : float{
63 return $this->buffer->getLFloat();
64 }
65
66 public function writeFloat(float $v) : void{
67 $this->buffer->putLFloat($v);
68 }
69
70 public function readDouble() : float{
71 return $this->buffer->getLDouble();
72 }
73
74 public function writeDouble(float $v) : void{
75 $this->buffer->putLDouble($v);
76 }
77
78 public function readIntArray() : array{
79 $len = $this->readInt();
80 if($len < 0){
81 throw new NbtDataException("Array length cannot be less than zero ($len < 0)");
82 }
83 $unpacked = unpack("V*", $this->buffer->get($len * 4));
84 assert($unpacked !== false, "The formatting string is valid, and we gave a multiple of 4 bytes");
85 return array_values($unpacked);
86 }
87
88 public function writeIntArray(array $array) : void{
89 $this->writeInt(count($array));
90 $this->buffer->put(pack("V*", ...$array));
91 }
92}