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