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() |
|
156 | { |
||
157 | |||
158 | 63 | $results = [ ]; |
|
159 | |||
160 | 63 | $packets = [ ]; |
|
161 | |||
162 | // We need to pre-sort these for split packets so we can do extra work where needed |
||
163 | 63 | foreach ($this->packets_response as $response) { |
|
164 | 63 | $buffer = new Buffer($response); |
|
165 | |||
166 | // Get the header of packet (long) |
||
167 | 63 | $header = $buffer->readInt32Signed(); |
|
168 | |||
169 | // Single packet |
||
170 | 63 | if ($header == -1) { |
|
171 | // We need to peek and see what kind of engine this is for later processing |
||
172 | 63 | if ($buffer->lookAhead(1) == "\x6d") { |
|
173 | 4 | $this->source_engine = self::GOLDSOURCE_ENGINE; |
|
174 | 4 | } |
|
175 | |||
176 | 63 | $packets[] = $buffer->getBuffer(); |
|
177 | 63 | continue; |
|
178 | } else { |
||
179 | // Split packet |
||
180 | |||
181 | // Packet Id (long) |
||
182 | 25 | $packet_id = $buffer->readInt32Signed(); |
|
183 | |||
184 | // Add the buffer to the packet as another array |
||
185 | 25 | $packets[$packet_id][] = $buffer->getBuffer(); |
|
186 | } |
||
187 | 63 | } |
|
188 | |||
189 | // Now that we have the packets sorted we need to iterate and process them |
||
190 | 63 | foreach ($packets as $packet_id => $packet) { |
|
191 | // We first need to off load split packets to combine them |
||
192 | 63 | if (is_array($packet)) { |
|
193 | 23 | $buffer = new Buffer($this->processPackets($packet_id, $packet)); |
|
194 | 23 | } else { |
|
195 | 63 | $buffer = new Buffer($packet); |
|
196 | } |
||
197 | |||
198 | // Figure out what packet response this is for |
||
199 | 63 | $response_type = $buffer->read(1); |
|
200 | |||
201 | // Figure out which packet response this is |
||
202 | 63 | if (!array_key_exists($response_type, $this->responses)) { |
|
203 | 2 | throw new Exception(__METHOD__ . " response type '{$response_type}' is not valid"); |
|
204 | } |
||
205 | |||
206 | // Now we need to call the proper method |
||
207 | 61 | $results = array_merge( |
|
208 | 61 | $results, |
|
209 | 61 | call_user_func_array([ $this, $this->responses[$response_type] ], [ $buffer ]) |
|
210 | 61 | ); |
|
211 | |||
212 | 61 | unset($buffer); |
|
213 | 61 | } |
|
214 | |||
215 | 61 | unset($packets, $packet); |
|
216 | |||
217 | 61 | return $results; |
|
218 | } |
||
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) |
|
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 |