1 | <?php |
||
34 | class Gamespy3 extends Protocol |
||
35 | { |
||
36 | |||
37 | /** |
||
38 | * Array of packets we want to look up. |
||
39 | * Each key should correspond to a defined method in this or a parent class |
||
40 | * |
||
41 | * @type array |
||
42 | */ |
||
43 | protected $packets = [ |
||
44 | self::PACKET_CHALLENGE => "\xFE\xFD\x09\x10\x20\x30\x40", |
||
45 | self::PACKET_ALL => "\xFE\xFD\x00\x10\x20\x30\x40%s\xFF\xFF\xFF\x01", |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * The query protocol used to make the call |
||
50 | * |
||
51 | * @type string |
||
52 | */ |
||
53 | protected $protocol = 'gamespy3'; |
||
54 | |||
55 | /** |
||
56 | * String name of this protocol class |
||
57 | * |
||
58 | * @type string |
||
59 | */ |
||
60 | protected $name = 'gamespy3'; |
||
61 | |||
62 | /** |
||
63 | * Longer string name of this protocol class |
||
64 | * |
||
65 | * @type string |
||
66 | */ |
||
67 | protected $name_long = "GameSpy3 Server"; |
||
68 | |||
69 | /** |
||
70 | * The client join link |
||
71 | * |
||
72 | * @type string |
||
73 | */ |
||
74 | protected $join_link = null; |
||
75 | |||
76 | /** |
||
77 | * Parse the challenge response and apply it to all the packet types |
||
78 | * |
||
79 | * @param \GameQ\Buffer $challenge_buffer |
||
80 | * |
||
81 | * @return bool |
||
82 | * @throws \GameQ\Exception\Protocol |
||
83 | */ |
||
84 | public function challengeParseAndApply(Buffer $challenge_buffer) |
||
100 | |||
101 | /** |
||
102 | * Process the response |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | 6 | public function processResponse() |
|
160 | |||
161 | /* |
||
162 | * Internal methods |
||
163 | */ |
||
164 | |||
165 | /** |
||
166 | * Handles cleaning up packets since the responses can be a bit "dirty" |
||
167 | * |
||
168 | * @param array $packets |
||
169 | * |
||
170 | * @return array |
||
171 | */ |
||
172 | 6 | protected function cleanPackets(array $packets = []) |
|
173 | { |
||
174 | |||
175 | // Get the number of packets |
||
176 | 6 | $packetCount = count($packets); |
|
177 | |||
178 | // Compare last var of current packet with first var of next packet |
||
179 | // On a partial match, remove last var from current packet, |
||
180 | // variable header from next packet |
||
181 | 6 | for ($i = 0, $x = $packetCount; $i < $x - 1; $i++) { |
|
182 | // First packet |
||
183 | 3 | $fst = substr($packets[$i], 0, -1); |
|
184 | // Second packet |
||
185 | 3 | $snd = $packets[$i + 1]; |
|
186 | // Get last variable from first packet |
||
187 | 3 | $fstvar = substr($fst, strrpos($fst, "\x00") + 1); |
|
188 | // Get first variable from last packet |
||
189 | 3 | $snd = substr($snd, strpos($snd, "\x00") + 2); |
|
190 | 3 | $sndvar = substr($snd, 0, strpos($snd, "\x00")); |
|
191 | // Check if fstvar is a substring of sndvar |
||
192 | // If so, remove it from the first string |
||
193 | 3 | if (!empty($fstvar) && strpos($sndvar, $fstvar) !== false) { |
|
194 | 1 | $packets[$i] = preg_replace("#(\\x00[^\\x00]+\\x00)$#", "\x00", $packets[$i]); |
|
195 | 1 | } |
|
196 | 3 | } |
|
197 | |||
198 | // Now let's loop the return and remove any dupe prefixes |
||
199 | 6 | for ($x = 1; $x < $packetCount; $x++) { |
|
200 | 3 | $buffer = new Buffer($packets[$x], Buffer::NUMBER_TYPE_BIGENDIAN); |
|
201 | |||
202 | 3 | $prefix = $buffer->readString(); |
|
203 | |||
204 | // Check to see if the return before has the same prefix present |
||
205 | 3 | if ($prefix != null && strstr($packets[($x - 1)], $prefix)) { |
|
206 | // Update the return by removing the prefix plus 2 chars |
||
207 | 1 | $packets[$x] = substr(str_replace($prefix, '', $packets[$x]), 2); |
|
208 | 1 | } |
|
209 | |||
210 | 3 | unset($buffer); |
|
211 | 3 | } |
|
212 | |||
213 | 6 | unset($x, $i, $snd, $sndvar, $fst, $fstvar); |
|
214 | |||
215 | // Return cleaned packets |
||
216 | 6 | return $packets; |
|
217 | } |
||
218 | |||
219 | /** |
||
220 | * Handles processing the details data into a usable format |
||
221 | * |
||
222 | * @param \GameQ\Buffer $buffer |
||
223 | * @param \GameQ\Result $result |
||
224 | */ |
||
225 | 6 | protected function processDetails(Buffer &$buffer, Result &$result) |
|
237 | |||
238 | /** |
||
239 | * Handles processing the player and team data into a usable format |
||
240 | * |
||
241 | * @param \GameQ\Buffer $buffer |
||
242 | * @param \GameQ\Result $result |
||
243 | */ |
||
244 | 6 | protected function processPlayersAndTeams(Buffer &$buffer, Result &$result) |
|
314 | } |
||
315 |