PocketMine-MP 5.44.3 git-327e8a1690982f1ac3634944d705ebad5d91f4ad
Loading...
Searching...
No Matches
DynamicValueMap.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol\types\cereal;
16
17use pmmp\encoding\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\LE;
20use pmmp\encoding\VarInt;
22use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait;
23use function count;
24
25final class DynamicValueMap extends DynamicValue{
26 use GetTypeIdFromConstTrait;
27
28 public const ID = DynamicValueType::MAP;
29
34 public function __construct(
35 private array $value
36 ){}
37
42 public function getValue() : array{ return $this->value; }
43
44 protected static function readValue(ByteBufferReader $in) : self{
45 $value = [];
46
47 for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; $i++){
48 $key = CommonTypes::getString($in);
49 //YIKES! unchecked recursion ?!?!?! thank god this never gets sent by the client...
50 $type = LE::readUnsignedInt($in);
51 $value[$key] = DynamicValue::read($in, $type);
52 }
53
54 return new self($value);
55 }
56
57 protected function writeValue(ByteBufferWriter $out) : void{
58 VarInt::writeUnsignedInt($out, count($this->value));
59 foreach($this->value as $key => $value){
60 CommonTypes::putString($out, (string) $key); //make sure we don't get any unexpected strings casted to int
61 LE::writeUnsignedInt($out, $value?->getTypeId() ?? DynamicValueType::NULL);
62 $value?->write($out);
63 }
64 }
65}