-
Notifications
You must be signed in to change notification settings - Fork 6
/
PacketCollection.php
79 lines (70 loc) · 1.73 KB
/
PacketCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/*
* DecMcpe
*
* Copyright (C) 2016 PEMapModder
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PEMapModder
*/
class PacketCollection{
/** @type int */
private $protocolVersion;
/** @type string */
private $protocolVersionHex;
/** @type Packet[] */
private $packets = [];
public function getPackets(){
return $this->packets;
}
public function get($name){
if(!isset($this->packets[$name])){
$this->packets[$name] = new Packet($name);
}
return $this->packets[$name];
}
public function write($file){
ksort($this->packets, SORT_NATURAL | SORT_FLAG_CASE);
$this->free(true);
var_dump($this->packets);
$data = [
"protocolVersion" => $this->protocolVersionHex,
"packets" => $this->packets,
];
file_put_contents($file, json_encode($data, JSON_UNESCAPED_SLASHES | JSON_BIGINT_AS_STRING | JSON_PRETTY_PRINT));
}
/**
* @return int
*/
public function getProtocolVersion(){
return $this->protocolVersion;
}
/**
* @return string
*/
public function getProtocolVersionHex(){
return $this->protocolVersionHex;
}
/**
* @param int $protocolVersion
*/
public function setProtocolVersion($protocolVersion){
$this->protocolVersion = $protocolVersion;
$this->protocolVersionHex = sprintf("0x%x", $protocolVersion);
}
public function free(bool $delete = false){
foreach($this->packets as $k => &$packet){
if($packet instanceof Packet){
if($packet->isReady()){
$packet = $packet->dumpInfo();
}elseif($delete){
unset($this->packets[$k]);
}
}
}
}
}