PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
Loading...
Searching...
No Matches
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
27use pocketmine\block\utils\WallConnectionType;
31use function get_class;
32use function intdiv;
33use function log;
34use function spl_object_id;
35
37 private int $offset = 0;
38
39 public function __construct(
40 private int $maxBits,
41 private int $value
42 ){}
43
44 public function readInt(int $bits) : int{
45 $bitsLeft = $this->maxBits - $this->offset;
46 if($bits > $bitsLeft){
47 throw new \InvalidArgumentException("No bits left in buffer (need $bits, have $bitsLeft");
48 }
49
50 $value = ($this->value >> $this->offset) & ~(~0 << $bits);
51 $this->offset += $bits;
52 return $value;
53 }
54
55 public function int(int $bits, int &$value) : void{
56 $value = $this->readInt($bits);
57 }
58
59 private function readBoundedIntAuto(int $min, int $max) : int{
60 $bits = ((int) log($max - $min, 2)) + 1;
61 $result = $this->readInt($bits) + $min;
62 if($result < $min || $result > $max){
63 throw new InvalidSerializedRuntimeDataException("Value is outside the range $min - $max");
64 }
65 return $result;
66 }
67
68 public function boundedIntAuto(int $min, int $max, int &$value) : void{
69 $value = $this->readBoundedIntAuto($min, $max);
70 }
71
72 protected function readBool() : bool{
73 return $this->readInt(1) === 1;
74 }
75
76 public function bool(bool &$value) : void{
77 $value = $this->readBool();
78 }
79
80 public function horizontalFacing(int &$facing) : void{
81 $facing = match($this->readInt(2)){
82 0 => Facing::NORTH,
83 1 => Facing::EAST,
84 2 => Facing::SOUTH,
85 3 => Facing::WEST,
86 default => throw new AssumptionFailedError("Unreachable")
87 };
88 }
89
93 public function facingFlags(array &$faces) : void{
94 $result = [];
95 foreach(Facing::ALL as $facing){
96 if($this->readBool()){
97 $result[$facing] = $facing;
98 }
99 }
100
101 $faces = $result;
102 }
103
107 public function horizontalFacingFlags(array &$faces) : void{
108 $result = [];
109 foreach(Facing::HORIZONTAL as $facing){
110 if($this->readBool()){
111 $result[$facing] = $facing;
112 }
113 }
114
115 $faces = $result;
116 }
117
118 public function facing(int &$facing) : void{
119 $facing = match($this->readInt(3)){
120 0 => Facing::DOWN,
121 1 => Facing::UP,
122 2 => Facing::NORTH,
123 3 => Facing::SOUTH,
124 4 => Facing::WEST,
125 5 => Facing::EAST,
126 default => throw new InvalidSerializedRuntimeDataException("Invalid facing value")
127 };
128 }
129
130 public function facingExcept(int &$facing, int $except) : void{
131 $result = 0;
132 $this->facing($result);
133 if($result === $except){
134 throw new InvalidSerializedRuntimeDataException("Illegal facing value");
135 }
136
137 $facing = $result;
138 }
139
140 public function axis(int &$axis) : void{
141 $axis = match($this->readInt(2)){
142 0 => Axis::X,
143 1 => Axis::Z,
144 2 => Axis::Y,
145 default => throw new InvalidSerializedRuntimeDataException("Invalid axis value")
146 };
147 }
148
149 public function horizontalAxis(int &$axis) : void{
150 $axis = match($this->readInt(1)){
151 0 => Axis::X,
152 1 => Axis::Z,
153 default => throw new AssumptionFailedError("Unreachable")
154 };
155 }
156
161 public function wallConnections(array &$connections) : void{
162 $result = [];
163 $offset = 0;
164 $packed = $this->readBoundedIntAuto(0, (3 ** 4) - 1);
165 foreach(Facing::HORIZONTAL as $facing){
166 $type = intdiv($packed, (3 ** $offset)) % 3;
167 if($type !== 0){
168 $result[$facing] = match($type){
169 1 => WallConnectionType::SHORT,
170 2 => WallConnectionType::TALL,
171 default => throw new AssumptionFailedError("Unreachable")
172 };
173 }
174 $offset++;
175 }
176
177 $connections = $result;
178 }
179
180 public function railShape(int &$railShape) : void{
181 $result = $this->readInt(4);
182 if(!isset(RailConnectionInfo::CONNECTIONS[$result]) && !isset(RailConnectionInfo::CURVE_CONNECTIONS[$result])){
183 throw new InvalidSerializedRuntimeDataException("Invalid rail shape $result");
184 }
185
186 $railShape = $result;
187 }
188
189 public function straightOnlyRailShape(int &$railShape) : void{
190 $result = $this->readInt(3);
191 if(!isset(RailConnectionInfo::CONNECTIONS[$result])){
192 throw new InvalidSerializedRuntimeDataException("No rail shape matches meta $result");
193 }
194
195 $railShape = $result;
196 }
197
198 public function enum(\UnitEnum &$case) : void{
199 $metadata = RuntimeEnumMetadata::from($case);
200 $raw = $this->readInt($metadata->bits);
201 $result = $metadata->intToEnum($raw);
202 if($result === null){
203 throw new InvalidSerializedRuntimeDataException("Invalid serialized value $raw for " . get_class($case));
204 }
205
206 $case = $result;
207 }
208
209 public function enumSet(array &$set, array $allCases) : void{
210 $result = [];
211 foreach($allCases as $case){
212 if($this->readBool()){
213 $result[spl_object_id($case)] = $case;
214 }
215 }
216 $set = $result;
217 }
218
219 public function getOffset() : int{ return $this->offset; }
220}
boundedIntAuto(int $min, int $max, int &$value)