PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
RuntimeDataWriter.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;
27use pocketmine\block\utils\WallConnectionType;
30use function array_flip;
31use function log;
32use function spl_object_id;
33
35 private int $value = 0;
36 private int $offset = 0;
37
38 public function __construct(
39 private int $maxBits
40 ){}
41
42 public function writeInt(int $bits, int $value) : void{
43 if($this->offset + $bits > $this->maxBits){
44 throw new \InvalidArgumentException("Bit buffer cannot be larger than $this->maxBits bits (already have $this->offset bits)");
45 }
46 if(($value & (~0 << $bits)) !== 0){
47 throw new \InvalidArgumentException("Value $value does not fit into $bits bits");
48 }
49
50 $this->value |= ($value << $this->offset);
51 $this->offset += $bits;
52 }
53
54 public function int(int $bits, int &$value) : void{
55 $this->writeInt($bits, $value);
56 }
57
61 public function boundedInt(int $bits, int $min, int $max, int &$value) : void{
62 $offset = $this->offset;
63 $this->writeBoundedIntAuto($min, $max, $value);
64 $actualBits = $this->offset - $offset;
65 if($actualBits !== $bits){
66 throw new \InvalidArgumentException("Bits should be $actualBits for the given bounds, but received $bits. Use boundedIntAuto() for automatic bits calculation.");
67 }
68 }
69
70 private function writeBoundedIntAuto(int $min, int $max, int $value) : void{
71 if($value < $min || $value > $max){
72 throw new \InvalidArgumentException("Value $value is outside the range $min - $max");
73 }
74 $bits = ((int) log($max - $min, 2)) + 1;
75 $this->writeInt($bits, $value - $min);
76 }
77
78 public function boundedIntAuto(int $min, int $max, int &$value) : void{
79 $this->writeBoundedIntAuto($min, $max, $value);
80 }
81
82 protected function writeBool(bool $value) : void{
83 $this->writeInt(1, $value ? 1 : 0);
84 }
85
86 public function bool(bool &$value) : void{
87 $this->writeBool($value);
88 }
89
90 public function horizontalFacing(int &$facing) : void{
91 $this->writeInt(2, match($facing){
92 Facing::NORTH => 0,
93 Facing::EAST => 1,
94 Facing::SOUTH => 2,
95 Facing::WEST => 3,
96 default => throw new \InvalidArgumentException("Invalid horizontal facing $facing")
97 });
98 }
99
103 public function facingFlags(array &$faces) : void{
104 $uniqueFaces = array_flip($faces);
105 foreach(Facing::ALL as $facing){
106 $this->writeBool(isset($uniqueFaces[$facing]));
107 }
108 }
109
113 public function horizontalFacingFlags(array &$faces) : void{
114 $uniqueFaces = array_flip($faces);
115 foreach(Facing::HORIZONTAL as $facing){
116 $this->writeBool(isset($uniqueFaces[$facing]));
117 }
118 }
119
120 public function facing(int &$facing) : void{
121 $this->writeInt(3, match($facing){
122 0 => Facing::DOWN,
123 1 => Facing::UP,
124 2 => Facing::NORTH,
125 3 => Facing::SOUTH,
126 4 => Facing::WEST,
127 5 => Facing::EAST,
128 default => throw new \InvalidArgumentException("Invalid facing $facing")
129 });
130 }
131
132 public function facingExcept(int &$facing, int $except) : void{
133 $this->facing($facing);
134 }
135
136 public function axis(int &$axis) : void{
137 $this->writeInt(2, match($axis){
138 Axis::X => 0,
139 Axis::Z => 1,
140 Axis::Y => 2,
141 default => throw new \InvalidArgumentException("Invalid axis $axis")
142 });
143 }
144
145 public function horizontalAxis(int &$axis) : void{
146 $this->writeInt(1, match($axis){
147 Axis::X => 0,
148 Axis::Z => 1,
149 default => throw new \InvalidArgumentException("Invalid horizontal axis $axis")
150 });
151 }
152
157 public function wallConnections(array &$connections) : void{
158 $packed = 0;
159 $offset = 0;
160 foreach(Facing::HORIZONTAL as $facing){
161 $packed += match($connections[$facing] ?? null){
162 null => 0,
163 WallConnectionType::SHORT => 1,
164 WallConnectionType::TALL => 2,
165 } * (3 ** $offset);
166 $offset++;
167 }
168 $this->writeBoundedIntAuto(0, (3 ** 4) - 1, $packed);
169 }
170
177 public function brewingStandSlots(array &$slots) : void{
178 $this->enumSet($slots, BrewingStandSlot::cases());
179 }
180
181 public function railShape(int &$railShape) : void{
182 $this->int(4, $railShape);
183 }
184
185 public function straightOnlyRailShape(int &$railShape) : void{
186 $this->int(3, $railShape);
187 }
188
189 public function enum(\UnitEnum &$case) : void{
190 $metadata = RuntimeEnumMetadata::from($case);
191 $this->writeInt($metadata->bits, $metadata->enumToInt($case));
192 }
193
194 public function enumSet(array &$set, array $allCases) : void{
195 foreach($allCases as $case){
196 $this->writeBool(isset($set[spl_object_id($case)]));
197 }
198 }
199
200 public function getValue() : int{ return $this->value; }
201
202 public function getOffset() : int{ return $this->offset; }
203}
boundedIntAuto(int $min, int $max, int &$value)
boundedInt(int $bits, int $min, int $max, int &$value)