PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
BigEndianNbtSerializer.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->getShort();
36 }
37
38 public function readSignedShort() : int{
39 return $this->buffer->getSignedShort();
40 }
41
42 public function writeShort(int $v) : void{
43 $this->buffer->putShort($v);
44 }
45
46 public function readInt() : int{
47 return $this->buffer->getInt();
48 }
49
50 public function writeInt(int $v) : void{
51 $this->buffer->putInt($v);
52 }
53
54 public function readLong() : int{
55 return $this->buffer->getLong();
56 }
57
58 public function writeLong(int $v) : void{
59 $this->buffer->putLong($v);
60 }
61
62 public function readFloat() : float{
63 return $this->buffer->getFloat();
64 }
65
66 public function writeFloat(float $v) : void{
67 $this->buffer->putFloat($v);
68 }
69
70 public function readDouble() : float{
71 return $this->buffer->getDouble();
72 }
73
74 public function writeDouble(float $v) : void{
75 $this->buffer->putDouble($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("N*", $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("N*", ...$array));
91 }
92}