|
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
|
|
|
* StarMade Protocol Class |
|
28
|
|
|
* |
|
29
|
|
|
* StarMade server query protocol class |
|
30
|
|
|
* |
|
31
|
|
|
* Credit to Robin Promesberger <[email protected]> for providing Java based querying as a roadmap |
|
32
|
|
|
* |
|
33
|
|
|
* @author Austin Bischoff <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
class Starmade extends Protocol |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Array of packets we want to query. |
|
40
|
|
|
* |
|
41
|
|
|
* @type array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $packets = [ |
|
44
|
|
|
self::PACKET_STATUS => "\x00\x00\x00\x09\x2a\xff\xff\x01\x6f\x00\x00\x00\x00", |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The transport mode for this protocol is TCP |
|
49
|
|
|
* |
|
50
|
|
|
* @type string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $transport = self::TRANSPORT_TCP; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The query protocol used to make the call |
|
56
|
|
|
* |
|
57
|
|
|
* @type string |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $protocol = 'starmade'; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* String name of this protocol class |
|
63
|
|
|
* |
|
64
|
|
|
* @type string |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $name = 'starmade'; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Longer string name of this protocol class |
|
70
|
|
|
* |
|
71
|
|
|
* @type string |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $name_long = "StarMade"; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* The client join link |
|
77
|
|
|
* |
|
78
|
|
|
* @type string |
|
79
|
|
|
*/ |
|
80
|
|
|
protected $join_link = null; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Normalize settings for this protocol |
|
84
|
|
|
* |
|
85
|
|
|
* @type array |
|
86
|
|
|
*/ |
|
87
|
|
|
protected $normalize = [ |
|
88
|
|
|
// General |
|
89
|
|
|
'general' => [ |
|
90
|
|
|
// target => source |
|
91
|
|
|
'dedicated' => 'dedicated', |
|
92
|
|
|
'hostname' => 'hostname', |
|
93
|
|
|
'maxplayers' => 'max_players', |
|
94
|
|
|
'numplayers' => 'num_players', |
|
95
|
|
|
'password' => 'password', |
|
96
|
|
|
], |
|
97
|
|
|
]; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Process the response for the StarMade server |
|
101
|
|
|
* |
|
102
|
|
|
* @return array |
|
103
|
|
|
* @throws \GameQ\Exception\Protocol |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function processResponse() |
|
106
|
|
|
{ |
|
107
|
|
|
|
|
108
|
|
|
// Implode the packets, not sure if there is any split logic for multiple packets |
|
109
|
1 |
|
$buffer = new Buffer(implode('', $this->packets_response), Buffer::NUMBER_TYPE_BIGENDIAN); |
|
110
|
|
|
|
|
111
|
|
|
// Get the passed length in the data side of the packet |
|
112
|
1 |
|
$buffer->readInt32Signed(); |
|
113
|
|
|
|
|
114
|
|
|
// Read off the timestamp (in milliseconds) |
|
115
|
1 |
|
$buffer->readInt64(); |
|
116
|
|
|
|
|
117
|
|
|
// Burn the check id == 42 |
|
118
|
1 |
|
$buffer->readInt8(); |
|
119
|
|
|
|
|
120
|
|
|
// Read packetId, unused |
|
121
|
1 |
|
$buffer->readInt16Signed(); |
|
122
|
|
|
|
|
123
|
|
|
// Read commandId, unused |
|
124
|
1 |
|
$buffer->readInt8Signed(); |
|
125
|
|
|
|
|
126
|
|
|
// Read type, unused |
|
127
|
1 |
|
$buffer->readInt8Signed(); |
|
128
|
|
|
|
|
129
|
1 |
|
$parsed = $this->parseServerParameters($buffer); |
|
130
|
|
|
|
|
131
|
|
|
// Set the result to a new result instance |
|
132
|
1 |
|
$result = new Result(); |
|
133
|
|
|
|
|
134
|
|
|
// Best guess info version is the type of response to expect. As of this commit the version is "2". |
|
135
|
1 |
|
$result->add('info_version', $parsed[0]); |
|
136
|
1 |
|
$result->add('version', $parsed[1]); |
|
137
|
1 |
|
$result->add('hostname', $parsed[2]); |
|
138
|
1 |
|
$result->add('game_descr', $parsed[3]); |
|
139
|
1 |
|
$result->add('start_time', $parsed[4]); |
|
140
|
1 |
|
$result->add('num_players', $parsed[5]); |
|
141
|
1 |
|
$result->add('max_players', $parsed[6]); |
|
142
|
1 |
|
$result->add('dedicated', 1); // All servers are dedicated as far as I can tell |
|
143
|
1 |
|
$result->add('password', 0); // Unsure if you can password servers, cant read that value |
|
144
|
|
|
//$result->add('map', 'Unknown'); |
|
145
|
|
|
|
|
146
|
1 |
|
unset($parsed); |
|
147
|
|
|
|
|
148
|
1 |
|
return $result->fetch(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Parse the server response parameters |
|
153
|
|
|
* |
|
154
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
155
|
|
|
* |
|
156
|
|
|
* @param \GameQ\Buffer $buffer |
|
157
|
|
|
* |
|
158
|
|
|
* @return array |
|
159
|
|
|
* @throws \GameQ\Exception\Protocol |
|
160
|
|
|
*/ |
|
161
|
1 |
|
protected function parseServerParameters(Buffer &$buffer) |
|
162
|
|
|
{ |
|
163
|
|
|
|
|
164
|
|
|
// Init the parsed data array |
|
165
|
1 |
|
$parsed = []; |
|
166
|
|
|
|
|
167
|
|
|
// Read the number of parameters to parse |
|
168
|
1 |
|
$parameterSize = $buffer->readInt32Signed(); |
|
169
|
|
|
|
|
170
|
|
|
// Iterate over the parameter size |
|
171
|
1 |
|
for ($i = 0; $i < $parameterSize; $i++) { |
|
172
|
|
|
// Read the type of return this is |
|
173
|
1 |
|
$dataType = $buffer->readInt8Signed(); |
|
174
|
|
|
|
|
175
|
|
|
switch ($dataType) { |
|
176
|
|
|
// 32-bit int |
|
177
|
1 |
|
case 1: |
|
178
|
1 |
|
$parsed[$i] = $buffer->readInt32Signed(); |
|
179
|
1 |
|
break; |
|
180
|
|
|
|
|
181
|
|
|
// 64-bit int |
|
182
|
1 |
|
case 2: |
|
183
|
1 |
|
$parsed[$i] = $buffer->readInt64(); |
|
184
|
1 |
|
break; |
|
185
|
|
|
|
|
186
|
|
|
// Float |
|
187
|
1 |
|
case 3: |
|
188
|
1 |
|
$parsed[$i] = $buffer->readFloat32(); |
|
189
|
1 |
|
break; |
|
190
|
|
|
|
|
191
|
|
|
// String |
|
192
|
1 |
|
case 4: |
|
193
|
|
|
// The first 2 bytes are the string length |
|
194
|
1 |
|
$strLength = $buffer->readInt16Signed(); |
|
195
|
|
|
|
|
196
|
|
|
// Read the above length from the buffer |
|
197
|
1 |
|
$parsed[$i] = $buffer->read($strLength); |
|
198
|
|
|
|
|
199
|
1 |
|
unset($strLength); |
|
200
|
1 |
|
break; |
|
201
|
|
|
|
|
202
|
|
|
// Boolean |
|
203
|
1 |
|
case 5: |
|
204
|
|
|
$parsed[$i] = (bool)$buffer->readInt8Signed(); |
|
205
|
|
|
break; |
|
206
|
|
|
|
|
207
|
|
|
// 8-bit int |
|
208
|
1 |
|
case 6: |
|
209
|
1 |
|
$parsed[$i] = $buffer->readInt8Signed(); |
|
210
|
1 |
|
break; |
|
211
|
|
|
|
|
212
|
|
|
// 16-bit int |
|
213
|
|
|
case 7: |
|
214
|
|
|
$parsed[$i] = $buffer->readInt16Signed(); |
|
215
|
|
|
break; |
|
216
|
|
|
|
|
217
|
|
|
// Array |
|
218
|
|
|
case 8: |
|
219
|
|
|
// Not implemented |
|
220
|
|
|
throw new Exception("StarMade array parsing is not implemented!"); |
|
221
|
|
|
} |
|
222
|
1 |
|
} |
|
223
|
|
|
|
|
224
|
1 |
|
return $parsed; |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|