PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
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\WallConnectionType;
29use function array_flip;
30use function log;
31use function spl_object_id;
32
34 private int $value = 0;
35 private int $offset = 0;
36
37 public function __construct(
38 private int $maxBits
39 ){}
40
41 public function writeInt(int $bits, int $value) : void{
42 if($this->offset + $bits > $this->maxBits){
43 throw new \InvalidArgumentException("Bit buffer cannot be larger than $this->maxBits bits (already have $this->offset bits)");
44 }
45 if(($value & (~0 << $bits)) !== 0){
46 throw new \InvalidArgumentException("Value $value does not fit into $bits bits");
47 }
48
49 $this->value |= ($value << $this->offset);
50 $this->offset += $bits;
51 }
52
53 public function int(int $bits, int &$value) : void{
54 $this->writeInt($bits, $value);
55 }
56
57 private function writeBoundedIntAuto(int $min, int $max, int $value) : void{
58 if($value < $min || $value > $max){
59 throw new \InvalidArgumentException("Value $value is outside the range $min - $max");
60 }
61 $bits = ((int) log($max - $min, 2)) + 1;
62 $this->writeInt($bits, $value - $min);
63 }
64
65 public function boundedIntAuto(int $min, int $max, int &$value) : void{
66 $this->writeBoundedIntAuto($min, $max, $value);
67 }
68
69 protected function writeBool(bool $value) : void{
70 $this->writeInt(1, $value ? 1 : 0);
71 }
72
73 public function bool(bool &$value) : void{
74 $this->writeBool($value);
75 }
76
77 public function horizontalFacing(int &$facing) : void{
78 $this->writeInt(2, match($facing){
79 Facing::NORTH => 0,
80 Facing::EAST => 1,
81 Facing::SOUTH => 2,
82 Facing::WEST => 3,
83 default => throw new \InvalidArgumentException("Invalid horizontal facing $facing")
84 });
85 }
86
90 public function facingFlags(array &$faces) : void{
91 $uniqueFaces = array_flip($faces);
92 foreach(Facing::ALL as $facing){
93 $this->writeBool(isset($uniqueFaces[$facing]));
94 }
95 }
96
100 public function horizontalFacingFlags(array &$faces) : void{
101 $uniqueFaces = array_flip($faces);
102 foreach(Facing::HORIZONTAL as $facing){
103 $this->writeBool(isset($uniqueFaces[$facing]));
104 }
105 }
106
107 public function facing(int &$facing) : void{
108 $this->writeInt(3, match($facing){
109 0 => Facing::DOWN,
110 1 => Facing::UP,
111 2 => Facing::NORTH,
112 3 => Facing::SOUTH,
113 4 => Facing::WEST,
114 5 => Facing::EAST,
115 default => throw new \InvalidArgumentException("Invalid facing $facing")
116 });
117 }
118
119 public function facingExcept(int &$facing, int $except) : void{
120 $this->facing($facing);
121 }
122
123 public function axis(int &$axis) : void{
124 $this->writeInt(2, match($axis){
125 Axis::X => 0,
126 Axis::Z => 1,
127 Axis::Y => 2,
128 default => throw new \InvalidArgumentException("Invalid axis $axis")
129 });
130 }
131
132 public function horizontalAxis(int &$axis) : void{
133 $this->writeInt(1, match($axis){
134 Axis::X => 0,
135 Axis::Z => 1,
136 default => throw new \InvalidArgumentException("Invalid horizontal axis $axis")
137 });
138 }
139
144 public function wallConnections(array &$connections) : void{
145 $packed = 0;
146 $offset = 0;
147 foreach(Facing::HORIZONTAL as $facing){
148 $packed += match($connections[$facing] ?? null){
149 null => 0,
150 WallConnectionType::SHORT => 1,
151 WallConnectionType::TALL => 2,
152 } * (3 ** $offset);
153 $offset++;
154 }
155 $this->writeBoundedIntAuto(0, (3 ** 4) - 1, $packed);
156 }
157
158 public function railShape(int &$railShape) : void{
159 $this->int(4, $railShape);
160 }
161
162 public function straightOnlyRailShape(int &$railShape) : void{
163 $this->int(3, $railShape);
164 }
165
166 public function enum(\UnitEnum &$case) : void{
167 $metadata = RuntimeEnumMetadata::from($case);
168 $this->writeInt($metadata->bits, $metadata->enumToInt($case));
169 }
170
171 public function enumSet(array &$set, array $allCases) : void{
172 foreach($allCases as $case){
173 $this->writeBool(isset($set[spl_object_id($case)]));
174 }
175 }
176
177 public function getValue() : int{ return $this->value; }
178
179 public function getOffset() : int{ return $this->offset; }
180}
boundedIntAuto(int $min, int $max, int &$value)