|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of GameQ. |
|
4
|
|
|
* |
|
5
|
|
|
* GameQ is free software; you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
|
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* GameQ is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Lesser General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace GameQ\Protocols; |
|
20
|
|
|
|
|
21
|
|
|
use GameQ\Protocol; |
|
22
|
|
|
use GameQ\Buffer; |
|
23
|
|
|
use GameQ\Result; |
|
24
|
|
|
use GameQ\Exception\Protocol as Exception; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Lost Heaven Protocol class |
|
28
|
|
|
* |
|
29
|
|
|
* Reference: http://lh-mp.eu/wiki/index.php/Query_System |
|
30
|
|
|
* |
|
31
|
|
|
* @author Austin Bischoff <[email protected]> |
|
32
|
|
|
*/ |
|
33
|
|
|
class Lhmp extends Protocol |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Array of packets we want to look up. |
|
38
|
|
|
* Each key should correspond to a defined method in this or a parent class |
|
39
|
|
|
* |
|
40
|
|
|
* @type array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $packets = [ |
|
43
|
|
|
self::PACKET_DETAILS => "LHMPo", |
|
44
|
|
|
self::PACKET_PLAYERS => "LHMPp", |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Use the response flag to figure out what method to run |
|
49
|
|
|
* |
|
50
|
|
|
* @type array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $responses = [ |
|
53
|
|
|
"LHMPo" => "processDetails", |
|
54
|
|
|
"LHMPp" => "processPlayers", |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* The query protocol used to make the call |
|
59
|
|
|
* |
|
60
|
|
|
* @type string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $protocol = 'lhmp'; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* String name of this protocol class |
|
66
|
|
|
* |
|
67
|
|
|
* @type string |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $name = 'lhmp'; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Longer string name of this protocol class |
|
73
|
|
|
* |
|
74
|
|
|
* @type string |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $name_long = "Lost Heaven"; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* query_port = client_port + 1 |
|
80
|
|
|
* |
|
81
|
|
|
* @type int |
|
82
|
|
|
*/ |
|
83
|
|
|
protected $port_diff = 1; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Normalize settings for this protocol |
|
87
|
|
|
* |
|
88
|
|
|
* @type array |
|
89
|
|
|
*/ |
|
90
|
|
|
protected $normalize = [ |
|
91
|
|
|
// General |
|
92
|
|
|
'general' => [ |
|
93
|
|
|
// target => source |
|
94
|
|
|
'gametype' => 'gamemode', |
|
95
|
|
|
'hostname' => 'servername', |
|
96
|
|
|
'mapname' => 'mapname', |
|
97
|
|
|
'maxplayers' => 'maxplayers', |
|
98
|
|
|
'numplayers' => 'numplayers', |
|
99
|
|
|
'password' => 'password', |
|
100
|
|
|
], |
|
101
|
|
|
// Individual |
|
102
|
|
|
'player' => [ |
|
103
|
|
|
'name' => 'name', |
|
104
|
|
|
], |
|
105
|
|
|
]; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Process the response |
|
109
|
|
|
* |
|
110
|
|
|
* @return array |
|
111
|
|
|
* @throws \GameQ\Exception\Protocol |
|
112
|
|
|
*/ |
|
113
|
2 |
|
public function processResponse() |
|
114
|
|
|
{ |
|
115
|
|
|
// Will hold the packets after sorting |
|
116
|
2 |
|
$packets = []; |
|
117
|
|
|
|
|
118
|
|
|
// We need to pre-sort these for split packets so we can do extra work where needed |
|
119
|
2 |
|
foreach ($this->packets_response as $response) { |
|
120
|
2 |
|
$buffer = new Buffer($response); |
|
121
|
|
|
|
|
122
|
|
|
// Pull out the header |
|
123
|
2 |
|
$header = $buffer->read(5); |
|
124
|
|
|
|
|
125
|
|
|
// Add the packet to the proper section, we will combine later |
|
126
|
2 |
|
$packets[$header][] = $buffer->getBuffer(); |
|
127
|
2 |
|
} |
|
128
|
|
|
|
|
129
|
2 |
|
unset($buffer); |
|
130
|
|
|
|
|
131
|
2 |
|
$results = []; |
|
132
|
|
|
|
|
133
|
|
|
// Now let's iterate and process |
|
134
|
2 |
|
foreach ($packets as $header => $packetGroup) { |
|
135
|
|
|
// Figure out which packet response this is |
|
136
|
2 |
|
if (!array_key_exists($header, $this->responses)) { |
|
137
|
2 |
|
throw new Exception(__METHOD__ . " response type '{$header}' is not valid"); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// Now we need to call the proper method |
|
141
|
2 |
|
$results = array_merge( |
|
142
|
2 |
|
$results, |
|
143
|
2 |
|
call_user_func_array([$this, $this->responses[$header]], [new Buffer(implode($packetGroup))]) |
|
144
|
2 |
|
); |
|
145
|
2 |
|
} |
|
146
|
|
|
|
|
147
|
|
|
unset($packets); |
|
148
|
|
|
|
|
149
|
|
|
return $results; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/* |
|
153
|
|
|
* Internal methods |
|
154
|
|
|
*/ |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Handles processing the details data into a usable format |
|
158
|
|
|
* |
|
159
|
|
|
* @param Buffer $buffer |
|
160
|
|
|
* |
|
161
|
|
|
* @return array |
|
162
|
|
|
* @throws Exception |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function processDetails(Buffer $buffer) |
|
165
|
|
|
{ |
|
166
|
|
|
|
|
167
|
|
|
// Set the result to a new result instance |
|
168
|
|
|
$result = new Result(); |
|
169
|
|
|
|
|
170
|
|
|
$result->add('protocol', $buffer->readString()); |
|
171
|
|
|
$result->add('password', $buffer->readString()); |
|
172
|
|
|
$result->add('numplayers', $buffer->readInt16()); |
|
173
|
|
|
$result->add('maxplayers', $buffer->readInt16()); |
|
174
|
|
|
$result->add('servername', utf8_encode($buffer->readPascalString())); |
|
175
|
|
|
$result->add('gamemode', $buffer->readPascalString()); |
|
176
|
|
|
$result->add('website', utf8_encode($buffer->readPascalString())); |
|
177
|
|
|
$result->add('mapname', utf8_encode($buffer->readPascalString())); |
|
178
|
|
|
|
|
179
|
|
|
unset($buffer); |
|
180
|
|
|
|
|
181
|
|
|
return $result->fetch(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Handles processing the player data into a usable format |
|
186
|
|
|
* |
|
187
|
|
|
* @param Buffer $buffer |
|
188
|
|
|
* |
|
189
|
|
|
* @return array |
|
190
|
|
|
*/ |
|
191
|
2 |
|
protected function processPlayers(Buffer $buffer) |
|
192
|
|
|
{ |
|
193
|
|
|
|
|
194
|
|
|
// Set the result to a new result instance |
|
195
|
2 |
|
$result = new Result(); |
|
196
|
|
|
|
|
197
|
|
|
// Get the number of players |
|
198
|
2 |
|
$result->add('numplayers', $buffer->readInt16()); |
|
199
|
|
|
|
|
200
|
|
|
// Parse players |
|
201
|
2 |
|
while ($buffer->getLength()) { |
|
202
|
|
|
// Player id |
|
203
|
|
|
if (($id = $buffer->readInt16()) !== 0) { |
|
204
|
|
|
// Add the results |
|
205
|
|
|
$result->addPlayer('id', $id); |
|
206
|
|
|
$result->addPlayer('name', utf8_encode($buffer->readPascalString())); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
2 |
|
unset($buffer, $id); |
|
211
|
|
|
|
|
212
|
2 |
|
return $result->fetch(); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|