1 | <?php |
||
36 | class Source extends Protocol |
||
37 | { |
||
38 | |||
39 | /* |
||
40 | * Source engine type constants |
||
41 | */ |
||
42 | const SOURCE_ENGINE = 0, |
||
43 | GOLDSOURCE_ENGINE = 1; |
||
44 | |||
45 | /** |
||
46 | * Array of packets we want to look up. |
||
47 | * Each key should correspond to a defined method in this or a parent class |
||
48 | * |
||
49 | * @type array |
||
50 | */ |
||
51 | protected $packets = [ |
||
52 | self::PACKET_CHALLENGE => "\xFF\xFF\xFF\xFF\x56\x00\x00\x00\x00", |
||
53 | self::PACKET_DETAILS => "\xFF\xFF\xFF\xFFTSource Engine Query\x00", |
||
54 | self::PACKET_PLAYERS => "\xFF\xFF\xFF\xFF\x55%s", |
||
55 | self::PACKET_RULES => "\xFF\xFF\xFF\xFF\x56%s", |
||
56 | ]; |
||
57 | |||
58 | /** |
||
59 | * Use the response flag to figure out what method to run |
||
60 | * |
||
61 | * @type array |
||
62 | */ |
||
63 | protected $responses = [ |
||
64 | "\x49" => "processDetails", // I |
||
65 | "\x6d" => "processDetailsGoldSource", // m, goldsource |
||
66 | "\x44" => "processPlayers", // D |
||
67 | "\x45" => "processRules", // E |
||
68 | ]; |
||
69 | |||
70 | /** |
||
71 | * The query protocol used to make the call |
||
72 | * |
||
73 | * @type string |
||
74 | */ |
||
75 | protected $protocol = 'source'; |
||
76 | |||
77 | /** |
||
78 | * String name of this protocol class |
||
79 | * |
||
80 | * @type string |
||
81 | */ |
||
82 | protected $name = 'source'; |
||
83 | |||
84 | /** |
||
85 | * Longer string name of this protocol class |
||
86 | * |
||
87 | * @type string |
||
88 | */ |
||
89 | protected $name_long = "Source Server"; |
||
90 | |||
91 | /** |
||
92 | * Define the Source engine type. By default it is assumed to be Source |
||
93 | * |
||
94 | * @type int |
||
95 | */ |
||
96 | protected $source_engine = self::SOURCE_ENGINE; |
||
97 | |||
98 | /** |
||
99 | * The client join link |
||
100 | * |
||
101 | * @type string |
||
102 | */ |
||
103 | protected $join_link = "steam://connect/%s:%d/"; |
||
104 | |||
105 | /** |
||
106 | * Normalize settings for this protocol |
||
107 | * |
||
108 | * @type array |
||
109 | */ |
||
110 | protected $normalize = [ |
||
111 | // General |
||
112 | 'general' => [ |
||
113 | // target => source |
||
114 | 'dedicated' => 'dedicated', |
||
115 | 'gametype' => 'game_descr', |
||
116 | 'hostname' => 'hostname', |
||
117 | 'mapname' => 'map', |
||
118 | 'maxplayers' => 'max_players', |
||
119 | 'mod' => 'game_dir', |
||
120 | 'numplayers' => 'num_players', |
||
121 | 'password' => 'password', |
||
122 | ], |
||
123 | // Individual |
||
124 | 'player' => [ |
||
125 | 'name' => 'name', |
||
126 | 'score' => 'score', |
||
127 | 'time' => 'time', |
||
128 | ], |
||
129 | ]; |
||
130 | |||
131 | /** |
||
132 | * Parse the challenge response and apply it to all the packet types |
||
133 | * |
||
134 | * @param \GameQ\Buffer $challenge_buffer |
||
135 | * |
||
136 | * @return bool |
||
137 | * @throws \GameQ\Exception\Protocol |
||
138 | */ |
||
139 | 1 | public function challengeParseAndApply(Buffer $challenge_buffer) |
|
148 | |||
149 | /** |
||
150 | * Process the response |
||
151 | * |
||
152 | * @return array |
||
153 | * @throws \GameQ\Exception\Protocol |
||
154 | */ |
||
155 | 63 | public function processResponse() |
|
219 | |||
220 | /* |
||
221 | * Internal methods |
||
222 | */ |
||
223 | |||
224 | /** |
||
225 | * Process the split packets and decompress if necessary |
||
226 | * |
||
227 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
228 | * |
||
229 | * @param $packet_id |
||
230 | * @param array $packets |
||
231 | * |
||
232 | * @return string |
||
233 | * @throws \GameQ\Exception\Protocol |
||
234 | */ |
||
235 | 23 | protected function processPackets($packet_id, array $packets = [ ]) |
|
321 | |||
322 | /** |
||
323 | * Handles processing the details data into a usable format |
||
324 | * |
||
325 | * @param \GameQ\Buffer $buffer |
||
326 | * |
||
327 | * @return mixed |
||
328 | * @throws \GameQ\Exception\Protocol |
||
329 | */ |
||
330 | 59 | protected function processDetails(Buffer $buffer) |
|
331 | { |
||
332 | |||
333 | // Set the result to a new result instance |
||
334 | 59 | $result = new Result(); |
|
335 | |||
336 | 59 | $result->add('protocol', $buffer->readInt8()); |
|
337 | 59 | $result->add('hostname', $buffer->readString()); |
|
338 | 59 | $result->add('map', $buffer->readString()); |
|
339 | 59 | $result->add('game_dir', $buffer->readString()); |
|
340 | 59 | $result->add('game_descr', $buffer->readString()); |
|
341 | 59 | $result->add('steamappid', $buffer->readInt16()); |
|
342 | 59 | $result->add('num_players', $buffer->readInt8()); |
|
343 | 59 | $result->add('max_players', $buffer->readInt8()); |
|
344 | 59 | $result->add('num_bots', $buffer->readInt8()); |
|
345 | 59 | $result->add('dedicated', $buffer->read()); |
|
346 | 59 | $result->add('os', $buffer->read()); |
|
347 | 59 | $result->add('password', $buffer->readInt8()); |
|
348 | 59 | $result->add('secure', $buffer->readInt8()); |
|
349 | |||
350 | // Special result for The Ship only (appid=2400) |
||
351 | 59 | if ($result->get('steamappid') == 2400) { |
|
352 | 3 | $result->add('game_mode', $buffer->readInt8()); |
|
353 | 3 | $result->add('witness_count', $buffer->readInt8()); |
|
354 | 3 | $result->add('witness_time', $buffer->readInt8()); |
|
355 | } |
||
356 | |||
357 | 59 | $result->add('version', $buffer->readString()); |
|
358 | |||
359 | // Because of php 5.4... |
||
360 | 59 | $edfCheck = $buffer->lookAhead(1); |
|
361 | |||
362 | // Extra data flag |
||
363 | 59 | if (!empty($edfCheck)) { |
|
364 | 56 | $edf = $buffer->readInt8(); |
|
365 | |||
366 | 56 | if ($edf & 0x80) { |
|
367 | 56 | $result->add('port', $buffer->readInt16Signed()); |
|
368 | } |
||
369 | |||
370 | 56 | if ($edf & 0x10) { |
|
371 | 54 | $result->add('steam_id', $buffer->readInt64()); |
|
372 | } |
||
373 | |||
374 | 56 | if ($edf & 0x40) { |
|
375 | 1 | $result->add('sourcetv_port', $buffer->readInt16Signed()); |
|
376 | 1 | $result->add('sourcetv_name', $buffer->readString()); |
|
377 | } |
||
378 | |||
379 | 56 | if ($edf & 0x20) { |
|
380 | 44 | $result->add('keywords', $buffer->readString()); |
|
381 | } |
||
382 | |||
383 | 56 | if ($edf & 0x01) { |
|
384 | 54 | $result->add('game_id', $buffer->readInt64()); |
|
385 | } |
||
386 | |||
387 | 56 | unset($edf); |
|
388 | } |
||
389 | |||
390 | 59 | unset($buffer); |
|
391 | |||
392 | 59 | return $result->fetch(); |
|
393 | } |
||
394 | |||
395 | /** |
||
396 | * Handles processing the server details from goldsource response |
||
397 | * |
||
398 | * @param \GameQ\Buffer $buffer |
||
399 | * |
||
400 | * @return array |
||
401 | * @throws \GameQ\Exception\Protocol |
||
402 | */ |
||
403 | 4 | protected function processDetailsGoldSource(Buffer $buffer) |
|
442 | |||
443 | /** |
||
444 | * Handles processing the player data into a usable format |
||
445 | * |
||
446 | * @param \GameQ\Buffer $buffer |
||
447 | * |
||
448 | * @return mixed |
||
449 | */ |
||
450 | 58 | protected function processPlayers(Buffer $buffer) |
|
479 | |||
480 | /** |
||
481 | * Handles processing the rules data into a usable format |
||
482 | * |
||
483 | * @param \GameQ\Buffer $buffer |
||
484 | * |
||
485 | * @return mixed |
||
486 | */ |
||
487 | 50 | protected function processRules(Buffer $buffer) |
|
508 | } |
||
509 |