|
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
|
|
|
* Mafia 2 Multiplayer Protocol Class |
|
28
|
|
|
* |
|
29
|
|
|
* Loosely based on SAMP protocol |
|
30
|
|
|
* |
|
31
|
|
|
* Query port = server port + 1 |
|
32
|
|
|
* |
|
33
|
|
|
* Handles processing Mafia 2 Multiplayer servers |
|
34
|
|
|
* |
|
35
|
|
|
* @package GameQ\Protocols |
|
36
|
|
|
* @author Wilson Jesus <> |
|
37
|
|
|
*/ |
|
38
|
|
|
class M2mp extends Protocol |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* Array of packets we want to look up. |
|
42
|
|
|
* Each key should correspond to a defined method in this or a parent class |
|
43
|
|
|
* |
|
44
|
|
|
* @type array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $packets = [ |
|
47
|
|
|
self::PACKET_ALL => "M2MP", |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Use the response flag to figure out what method to run |
|
52
|
|
|
* |
|
53
|
|
|
* @type array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $responses = [ |
|
56
|
|
|
"M2MP" => 'processStatus', |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* The query protocol used to make the call |
|
61
|
|
|
* |
|
62
|
|
|
* @type string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $protocol = 'm2mp'; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* String name of this protocol class |
|
68
|
|
|
* |
|
69
|
|
|
* @type string |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $name = 'm2mp'; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Longer string name of this protocol class |
|
75
|
|
|
* |
|
76
|
|
|
* @type string |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $name_long = "Mafia 2 Multiplayer"; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* The client join link |
|
82
|
|
|
* |
|
83
|
|
|
* @type string |
|
84
|
|
|
*/ |
|
85
|
|
|
protected $join_link = null; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* The difference between the client port and query port |
|
89
|
|
|
* |
|
90
|
|
|
* @type int |
|
91
|
|
|
*/ |
|
92
|
|
|
protected $port_diff = 1; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Normalize settings for this protocol |
|
96
|
|
|
* |
|
97
|
|
|
* @type array |
|
98
|
|
|
*/ |
|
99
|
|
|
protected $normalize = [ |
|
100
|
|
|
// General |
|
101
|
|
|
'general' => [ |
|
102
|
|
|
// target => source |
|
103
|
|
|
'hostname' => 'servername', |
|
104
|
|
|
'gametype' => 'gamemode', |
|
105
|
|
|
'maxplayers' => 'max_players', |
|
106
|
|
|
'numplayers' => 'num_players', |
|
107
|
|
|
'password' => 'password', |
|
108
|
|
|
], |
|
109
|
|
|
// Individual |
|
110
|
|
|
'player' => [ |
|
111
|
|
|
'name' => 'name', |
|
112
|
|
|
], |
|
113
|
|
|
]; |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Handle response from the server |
|
117
|
|
|
* |
|
118
|
|
|
* @return mixed |
|
119
|
|
|
* @throws Exception |
|
120
|
|
|
*/ |
|
121
|
6 |
|
public function processResponse() |
|
122
|
|
|
{ |
|
123
|
|
|
// Make a buffer |
|
124
|
6 |
|
$buffer = new Buffer(implode('', $this->packets_response)); |
|
125
|
|
|
|
|
126
|
|
|
// Grab the header |
|
127
|
6 |
|
$header = $buffer->read(4); |
|
128
|
|
|
|
|
129
|
|
|
// Header |
|
130
|
|
|
// Figure out which packet response this is |
|
131
|
6 |
|
if ($header != "M2MP") { |
|
132
|
|
|
throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid"); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
6 |
|
return call_user_func_array([$this, $this->responses[$header]], [$buffer]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Process the status response |
|
140
|
|
|
* |
|
141
|
|
|
* @param Buffer $buffer |
|
142
|
|
|
* |
|
143
|
|
|
* @return array |
|
144
|
|
|
*/ |
|
145
|
6 |
|
protected function processStatus(Buffer $buffer) |
|
146
|
|
|
{ |
|
147
|
|
|
// We need to split the data and offload |
|
148
|
6 |
|
$results = $this->processServerInfo($buffer); |
|
149
|
|
|
|
|
150
|
6 |
|
$results = array_merge_recursive( |
|
151
|
|
|
$results, |
|
152
|
6 |
|
$this->processPlayers($buffer) |
|
153
|
|
|
); |
|
154
|
|
|
|
|
155
|
6 |
|
unset($buffer); |
|
156
|
|
|
|
|
157
|
|
|
// Return results |
|
158
|
6 |
|
return $results; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Handle processing the server information |
|
163
|
|
|
* |
|
164
|
|
|
* @param Buffer $buffer |
|
165
|
|
|
* |
|
166
|
|
|
* @return array |
|
167
|
|
|
*/ |
|
168
|
6 |
|
protected function processServerInfo(Buffer $buffer) |
|
169
|
|
|
{ |
|
170
|
|
|
// Set the result to a new result instance |
|
171
|
6 |
|
$result = new Result(); |
|
172
|
|
|
|
|
173
|
|
|
// Always dedicated |
|
174
|
6 |
|
$result->add('dedicated', 1); |
|
175
|
|
|
|
|
176
|
|
|
// Pull out the server information |
|
177
|
|
|
// Note the length information is incorrect, we correct using offset options in pascal method |
|
178
|
6 |
|
$result->add('servername', $buffer->readPascalString(1, true)); |
|
179
|
6 |
|
$result->add('num_players', $buffer->readPascalString(1, true)); |
|
180
|
6 |
|
$result->add('max_players', $buffer->readPascalString(1, true)); |
|
181
|
6 |
|
$result->add('gamemode', $buffer->readPascalString(1, true)); |
|
182
|
6 |
|
$result->add('password', (bool) $buffer->readInt8()); |
|
183
|
|
|
|
|
184
|
6 |
|
unset($buffer); |
|
185
|
|
|
|
|
186
|
6 |
|
return $result->fetch(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Handle processing of player data |
|
191
|
|
|
* |
|
192
|
|
|
* @param Buffer $buffer |
|
193
|
|
|
* |
|
194
|
|
|
* @return array |
|
195
|
|
|
*/ |
|
196
|
6 |
|
protected function processPlayers(Buffer $buffer) |
|
197
|
|
|
{ |
|
198
|
|
|
// Set the result to a new result instance |
|
199
|
6 |
|
$result = new Result(); |
|
200
|
|
|
|
|
201
|
|
|
// Parse players |
|
202
|
|
|
// Read the player info, it's in the same query response for some odd reason. |
|
203
|
6 |
|
while ($buffer->getLength()) { |
|
204
|
|
|
// Check to see if we ran out of info, length bug from response |
|
205
|
6 |
|
if ($buffer->getLength() <= 1) { |
|
206
|
6 |
|
break; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
// Only player name information is available |
|
210
|
|
|
// Add player name, encoded |
|
211
|
6 |
|
$result->addPlayer('name', utf8_encode(trim($buffer->readPascalString(1, true)))); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
// Clear |
|
215
|
6 |
|
unset($buffer); |
|
216
|
|
|
|
|
217
|
6 |
|
return $result->fetch(); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|