PocketMine-MP 5.33.2 git-919492bdcad8510eb6606439eb77e1c604f1d1ea
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
26use pocketmine\block\utils\WallConnectionType;
30use function get_class;
31use function intdiv;
32use function log;
33use function spl_object_id;
34
36 private int $offset = 0;
37
38 public function __construct(
39 private int $maxBits,
40 private int $value
41 ){}
42
43 public function readInt(int $bits) : int{
44 $bitsLeft = $this->maxBits - $this->offset;
45 if($bits > $bitsLeft){
46 throw new \InvalidArgumentException("No bits left in buffer (need $bits, have $bitsLeft");
47 }
48
49 $value = ($this->value >> $this->offset) & ~(~0 << $bits);
50 $this->offset += $bits;
51 return $value;
52 }
53
54 public function int(int $bits, int &$value) : void{
55 $value = $this->readInt($bits);
56 }
57
58 private function readBoundedIntAuto(int $min, int $max) : int{
59 $bits = ((int) log($max - $min, 2)) + 1;
60 $result = $this->readInt($bits) + $min;
61 if($result < $min || $result > $max){
62 throw new InvalidSerializedRuntimeDataException("Value is outside the range $min - $max");
63 }
64 return $result;
65 }
66
67 public function boundedIntAuto(int $min, int $max, int &$value) : void{
68 $value = $this->readBoundedIntAuto($min, $max);
69 }
70
71 protected function readBool() : bool{
72 return $this->readInt(1) === 1;
73 }
74
75 public function bool(bool &$value) : void{
76 $value = $this->readBool();
77 }
78
79 public function facingExcept(Facing &$facing, Facing $except) : void{
80 $result = Facing::DOWN;
81 $this->enum($result);
82 if($result === $except){
83 throw new InvalidSerializedRuntimeDataException("Illegal facing value");
84 }
85
86 $facing = $result;
87 }
88
89 public function horizontalAxis(Axis &$axis) : void{
90 $axis = match($this->readInt(1)){
91 0 => Axis::X,
92 1 => Axis::Z,
93 default => throw new AssumptionFailedError("Unreachable")
94 };
95 }
96
101 public function wallConnections(array &$connections) : void{
102 $result = [];
103 $offset = 0;
104 $packed = $this->readBoundedIntAuto(0, (3 ** 4) - 1);
105 foreach(Facing::HORIZONTAL as $facing){
106 $type = intdiv($packed, (3 ** $offset)) % 3;
107 if($type !== 0){
108 $result[$facing->value] = match($type){
109 1 => WallConnectionType::SHORT,
110 2 => WallConnectionType::TALL,
111 default => throw new AssumptionFailedError("Unreachable")
112 };
113 }
114 $offset++;
115 }
116
117 $connections = $result;
118 }
119
120 public function enum(\UnitEnum &$case) : void{
121 $metadata = RuntimeEnumMetadata::from($case);
122 $raw = $this->readInt($metadata->bits);
123 $result = $metadata->intToEnum($raw);
124 if($result === null){
125 throw new InvalidSerializedRuntimeDataException("Invalid serialized value $raw for " . get_class($case));
126 }
127
128 $case = $result;
129 }
130
131 public function enumSet(array &$set, array $allCases) : void{
132 $result = [];
133 foreach($allCases as $case){
134 if($this->readBool()){
135 $result[spl_object_id($case)] = $case;
136 }
137 }
138 $set = $result;
139 }
140
141 public function getOffset() : int{ return $this->offset; }
142}
boundedIntAuto(int $min, int $max, int &$value)