|
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
|
|
|
* OpenTTD Protocol Class |
|
28
|
|
|
* |
|
29
|
|
|
* Handles processing Open Transport Tycoon Deluxe servers |
|
30
|
|
|
* |
|
31
|
|
|
* @package GameQ\Protocols |
|
32
|
|
|
* @author Wilson Jesus <> |
|
33
|
|
|
*/ |
|
34
|
|
|
class Openttd extends Protocol |
|
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
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $packets = [ |
|
43
|
|
|
self::PACKET_ALL => "\x03\x00\x00", |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The query protocol used to make the call |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $protocol = 'openttd'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* String name of this protocol class |
|
55
|
|
|
* |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $name = 'openttd'; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Longer string name of this protocol class |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $name_long = "Open Transport Tycoon Deluxe"; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Normalize settings for this protocol |
|
69
|
|
|
* |
|
70
|
|
|
* @var array |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $normalize = [ |
|
73
|
|
|
// General |
|
74
|
|
|
'general' => [ |
|
75
|
|
|
// target => source |
|
76
|
|
|
'hostname' => 'hostname', |
|
77
|
|
|
'mapname' => 'map', |
|
78
|
|
|
'maxplayers' => 'max_clients', |
|
79
|
|
|
'numplayers' => 'clients', |
|
80
|
|
|
'password' => 'password', |
|
81
|
|
|
'dedicated' => 'dedicated', |
|
82
|
|
|
], |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Handle response from the server |
|
87
|
|
|
* |
|
88
|
|
|
* @return mixed |
|
89
|
|
|
* @throws Exception |
|
90
|
|
|
* @throws \GameQ\Exception\Protocol |
|
91
|
|
|
*/ |
|
92
|
12 |
|
public function processResponse() |
|
93
|
|
|
{ |
|
94
|
|
|
// Make a buffer |
|
95
|
12 |
|
$buffer = new Buffer(implode('', $this->packets_response)); |
|
96
|
|
|
|
|
97
|
|
|
// Get the length of the packet |
|
98
|
12 |
|
$packetLength = $buffer->getLength(); |
|
99
|
|
|
|
|
100
|
|
|
// Grab the header |
|
101
|
12 |
|
$length = $buffer->readInt16(); |
|
102
|
|
|
//$type = $buffer->readInt8(); |
|
103
|
12 |
|
$buffer->skip(1); // Skip the "$type" as its not used in the code, and to comply with phpmd it cant be assigned and not used. |
|
104
|
|
|
|
|
105
|
|
|
// Header |
|
106
|
|
|
// Figure out which packet response this is |
|
107
|
12 |
|
if ($packetLength != $length) { |
|
108
|
|
|
throw new Exception(__METHOD__ . " response type '" . bin2hex($length) . "' is not valid"); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
12 |
|
return call_user_func_array([$this, 'processServerInfo'], [$buffer]); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Handle processing the server information |
|
116
|
|
|
* |
|
117
|
|
|
* @param Buffer $buffer |
|
118
|
|
|
* @return array |
|
119
|
|
|
* @throws \GameQ\Exception\Protocol |
|
120
|
|
|
*/ |
|
121
|
12 |
|
protected function processServerInfo(Buffer $buffer) |
|
122
|
|
|
{ |
|
123
|
|
|
// Set the result to a new result instance |
|
124
|
12 |
|
$result = new Result(); |
|
125
|
|
|
|
|
126
|
12 |
|
$protocol_version = $buffer->readInt8(); |
|
127
|
12 |
|
$result->add('protocol_version', $protocol_version); |
|
128
|
|
|
|
|
129
|
|
|
switch ($protocol_version) { |
|
130
|
12 |
|
case 4: |
|
131
|
12 |
|
$num_grfs = $buffer->readInt8(); #number of grfs |
|
132
|
12 |
|
$result->add('num_grfs', $num_grfs); |
|
133
|
|
|
//$buffer->skip ($num_grfs * 20); #skip grfs id and md5 hash |
|
134
|
|
|
|
|
135
|
12 |
|
for ($i=0; $i<$num_grfs; $i++) { |
|
136
|
6 |
|
$result->add('grfs_'.$i.'_ID', strtoupper(bin2hex($buffer->read(4)))); |
|
137
|
6 |
|
$result->add('grfs_'.$i.'_MD5', strtoupper(bin2hex($buffer->read(16)))); |
|
138
|
|
|
} |
|
139
|
|
|
// No break, cascades all the down even if case is meet |
|
140
|
|
|
case 3: |
|
141
|
12 |
|
$result->add('game_date', $buffer->readInt32()); |
|
142
|
12 |
|
$result->add('start_date', $buffer->readInt32()); |
|
143
|
|
|
// Cascades all the way down even if case is meet |
|
144
|
|
|
case 2: |
|
145
|
12 |
|
$result->add('companies_max', $buffer->readInt8()); |
|
146
|
12 |
|
$result->add('companies_on', $buffer->readInt8()); |
|
147
|
12 |
|
$result->add('spectators_max', $buffer->readInt8()); |
|
148
|
|
|
// Cascades all the way down even if case is meet |
|
149
|
|
|
case 1: |
|
150
|
12 |
|
$result->add('hostname', $buffer->readString()); |
|
151
|
12 |
|
$result->add('version', $buffer->readString()); |
|
152
|
|
|
|
|
153
|
12 |
|
$language = $buffer->readInt8(); |
|
154
|
12 |
|
$result->add('language', $language); |
|
155
|
12 |
|
$result->add('language_icon', '//media.openttd.org/images/server/'.$language.'_lang.gif'); |
|
156
|
|
|
|
|
157
|
12 |
|
$result->add('password', $buffer->readInt8()); |
|
158
|
12 |
|
$result->add('max_clients', $buffer->readInt8()); |
|
159
|
12 |
|
$result->add('clients', $buffer->readInt8()); |
|
160
|
12 |
|
$result->add('spectators', $buffer->readInt8()); |
|
161
|
12 |
|
if ($protocol_version < 3) { |
|
162
|
|
|
$days = ( 365 * 1920 + 1920 / 4 - 1920 / 100 + 1920 / 400 ); |
|
163
|
|
|
$result->add('game_date', $buffer->readInt16() + $days); |
|
164
|
|
|
$result->add('start_date', $buffer->readInt16() + $days); |
|
165
|
|
|
} |
|
166
|
12 |
|
$result->add('map', $buffer->readString()); |
|
167
|
12 |
|
$result->add('map_width', $buffer->readInt16()); |
|
168
|
12 |
|
$result->add('map_height', $buffer->readInt16()); |
|
169
|
12 |
|
$result->add('map_type', $buffer->readInt8()); |
|
170
|
12 |
|
$result->add('dedicated', $buffer->readInt8()); |
|
171
|
|
|
// Cascades all the way down even if case is meet |
|
172
|
|
|
} |
|
173
|
12 |
|
unset($buffer); |
|
174
|
|
|
|
|
175
|
12 |
|
return $result->fetch(); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|