PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
RuntimeDataReader.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\data\runtime;
25
26use pocketmine\block\utils\BrewingStandSlot;
28use pocketmine\block\utils\WallConnectionType;
32use function get_class;
33use function intdiv;
34use function log;
35use function spl_object_id;
36
38 use LegacyRuntimeEnumDescriberTrait;
39
40 private int $offset = 0;
41
42 public function __construct(
43 private int $maxBits,
44 private int $value
45 ){}
46
47 public function readInt(int $bits) : int{
48 $bitsLeft = $this->maxBits - $this->offset;
49 if($bits > $bitsLeft){
50 throw new \InvalidArgumentException("No bits left in buffer (need $bits, have $bitsLeft");
51 }
52
53 $value = ($this->value >> $this->offset) & ~(~0 << $bits);
54 $this->offset += $bits;
55 return $value;
56 }
57
58 public function int(int $bits, int &$value) : void{
59 $value = $this->readInt($bits);
60 }
61
65 public function boundedInt(int $bits, int $min, int $max, int &$value) : void{
66 $offset = $this->offset;
67 $this->boundedIntAuto($min, $max, $value);
68 $actualBits = $this->offset - $offset;
69 if($this->offset !== $offset + $bits){
70 throw new \InvalidArgumentException("Bits should be $actualBits for the given bounds, but received $bits. Use boundedIntAuto() for automatic bits calculation.");
71 }
72 }
73
74 private function readBoundedIntAuto(int $min, int $max) : int{
75 $bits = ((int) log($max - $min, 2)) + 1;
76 $result = $this->readInt($bits) + $min;
77 if($result < $min || $result > $max){
78 throw new InvalidSerializedRuntimeDataException("Value is outside the range $min - $max");
79 }
80 return $result;
81 }
82
83 public function boundedIntAuto(int $min, int $max, int &$value) : void{
84 $value = $this->readBoundedIntAuto($min, $max);
85 }
86
87 protected function readBool() : bool{
88 return $this->readInt(1) === 1;
89 }
90
91 public function bool(bool &$value) : void{
92 $value = $this->readBool();
93 }
94
95 public function horizontalFacing(int &$facing) : void{
96 $facing = match($this->readInt(2)){
97 0 => Facing::NORTH,
98 1 => Facing::EAST,
99 2 => Facing::SOUTH,
100 3 => Facing::WEST,
101 default => throw new AssumptionFailedError("Unreachable")
102 };
103 }
104
108 public function facingFlags(array &$faces) : void{
109 $result = [];
110 foreach(Facing::ALL as $facing){
111 if($this->readBool()){
112 $result[$facing] = $facing;
113 }
114 }
115
116 $faces = $result;
117 }
118
122 public function horizontalFacingFlags(array &$faces) : void{
123 $result = [];
124 foreach(Facing::HORIZONTAL as $facing){
125 if($this->readBool()){
126 $result[$facing] = $facing;
127 }
128 }
129
130 $faces = $result;
131 }
132
133 public function facing(int &$facing) : void{
134 $facing = match($this->readInt(3)){
135 0 => Facing::DOWN,
136 1 => Facing::UP,
137 2 => Facing::NORTH,
138 3 => Facing::SOUTH,
139 4 => Facing::WEST,
140 5 => Facing::EAST,
141 default => throw new InvalidSerializedRuntimeDataException("Invalid facing value")
142 };
143 }
144
145 public function facingExcept(int &$facing, int $except) : void{
146 $result = 0;
147 $this->facing($result);
148 if($result === $except){
149 throw new InvalidSerializedRuntimeDataException("Illegal facing value");
150 }
151
152 $facing = $result;
153 }
154
155 public function axis(int &$axis) : void{
156 $axis = match($this->readInt(2)){
157 0 => Axis::X,
158 1 => Axis::Z,
159 2 => Axis::Y,
160 default => throw new InvalidSerializedRuntimeDataException("Invalid axis value")
161 };
162 }
163
164 public function horizontalAxis(int &$axis) : void{
165 $axis = match($this->readInt(1)){
166 0 => Axis::X,
167 1 => Axis::Z,
168 default => throw new AssumptionFailedError("Unreachable")
169 };
170 }
171
176 public function wallConnections(array &$connections) : void{
177 $result = [];
178 $offset = 0;
179 $packed = $this->readBoundedIntAuto(0, (3 ** 4) - 1);
180 foreach(Facing::HORIZONTAL as $facing){
181 $type = intdiv($packed, (3 ** $offset)) % 3;
182 if($type !== 0){
183 $result[$facing] = match($type){
184 1 => WallConnectionType::SHORT,
185 2 => WallConnectionType::TALL,
186 default => throw new AssumptionFailedError("Unreachable")
187 };
188 }
189 $offset++;
190 }
191
192 $connections = $result;
193 }
194
201 public function brewingStandSlots(array &$slots) : void{
202 $this->enumSet($slots, BrewingStandSlot::cases());
203 }
204
205 public function railShape(int &$railShape) : void{
206 $result = $this->readInt(4);
207 if(!isset(RailConnectionInfo::CONNECTIONS[$result]) && !isset(RailConnectionInfo::CURVE_CONNECTIONS[$result])){
208 throw new InvalidSerializedRuntimeDataException("Invalid rail shape $result");
209 }
210
211 $railShape = $result;
212 }
213
214 public function straightOnlyRailShape(int &$railShape) : void{
215 $result = $this->readInt(3);
216 if(!isset(RailConnectionInfo::CONNECTIONS[$result])){
217 throw new InvalidSerializedRuntimeDataException("No rail shape matches meta $result");
218 }
219
220 $railShape = $result;
221 }
222
223 public function enum(\UnitEnum &$case) : void{
224 $metadata = RuntimeEnumMetadata::from($case);
225 $raw = $this->readInt($metadata->bits);
226 $result = $metadata->intToEnum($raw);
227 if($result === null){
228 throw new InvalidSerializedRuntimeDataException("Invalid serialized value $raw for " . get_class($case));
229 }
230
231 $case = $result;
232 }
233
234 public function enumSet(array &$set, array $allCases) : void{
235 $result = [];
236 foreach($allCases as $case){
237 if($this->readBool()){
238 $result[spl_object_id($case)] = $case;
239 }
240 }
241 $set = $result;
242 }
243
244 public function getOffset() : int{ return $this->offset; }
245}
boundedIntAuto(int $min, int $max, int &$value)
boundedInt(int $bits, int $min, int $max, int &$value)