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\Buffer;
22: use GameQ\Exception\Protocol as Exception;
23: use GameQ\Protocol;
24: use GameQ\Result;
25:
26: /**
27: * Battlefield 3 Protocol Class
28: *
29: * Good place for doc status and info is http://www.fpsadmin.com/forum/showthread.php?t=24134
30: *
31: * @package GameQ\Protocols
32: * @author Austin Bischoff <austin@codebeard.com>
33: */
34: class Bf3 extends Protocol
35: {
36: /**
37: * Array of packets we want to query.
38: *
39: * @var array
40: */
41: protected $packets = [
42: self::PACKET_STATUS => "\x00\x00\x00\x21\x1b\x00\x00\x00\x01\x00\x00\x00\x0a\x00\x00\x00serverInfo\x00",
43: self::PACKET_VERSION => "\x00\x00\x00\x22\x18\x00\x00\x00\x01\x00\x00\x00\x07\x00\x00\x00version\x00",
44: self::PACKET_PLAYERS =>
45: "\x00\x00\x00\x23\x24\x00\x00\x00\x02\x00\x00\x00\x0b\x00\x00\x00listPlayers\x00\x03\x00\x00\x00\x61ll\x00",
46: ];
47:
48: /**
49: * Use the response flag to figure out what method to run
50: *
51: * @var array
52: */
53: protected $responses = [
54: 1627389952 => "processDetails", // a
55: 1644167168 => "processVersion", // b
56: 1660944384 => "processPlayers", // c
57: ];
58:
59: /**
60: * The transport mode for this protocol is TCP
61: *
62: * @var string
63: */
64: protected $transport = self::TRANSPORT_TCP;
65:
66: /**
67: * The query protocol used to make the call
68: *
69: * @var string
70: */
71: protected $protocol = 'bf3';
72:
73: /**
74: * String name of this protocol class
75: *
76: * @var string
77: */
78: protected $name = 'bf3';
79:
80: /**
81: * Longer string name of this protocol class
82: *
83: * @var string
84: */
85: protected $name_long = "Battlefield 3";
86:
87: /**
88: * query_port = client_port + 22000
89: * 47200 = 25200 + 22000
90: *
91: * @var int
92: */
93: protected $port_diff = 22000;
94:
95: /**
96: * Normalize settings for this protocol
97: *
98: * @var array
99: */
100: protected $normalize = [
101: // General
102: 'general' => [
103: // target => source
104: 'dedicated' => 'dedicated',
105: 'hostname' => 'hostname',
106: 'mapname' => 'map',
107: 'maxplayers' => 'max_players',
108: 'numplayers' => 'num_players',
109: 'password' => 'password',
110: ],
111: 'player' => [
112: 'name' => 'name',
113: 'score' => 'score',
114: 'ping' => 'ping',
115: ],
116: 'team' => [
117: 'score' => 'tickets',
118: ],
119: ];
120:
121: /**
122: * Process the response for the StarMade server
123: *
124: * @return array
125: * @throws \GameQ\Exception\Protocol
126: */
127: public function processResponse()
128: {
129: // Holds the results sent back
130: $results = [];
131:
132: // Holds the processed packets after having been reassembled
133: $processed = [];
134:
135: // Start up the index for the processed
136: $sequence_id_last = 0;
137:
138: foreach ($this->packets_response as $packet) {
139: // Create a new buffer
140: $buffer = new Buffer($packet);
141:
142: // Each "good" packet begins with sequence_id (32-bit)
143: $sequence_id = $buffer->readInt32();
144:
145: // Sequence id is a response
146: if (array_key_exists($sequence_id, $this->responses)) {
147: $processed[$sequence_id] = $buffer->getBuffer();
148: $sequence_id_last = $sequence_id;
149: } else {
150: // This is a continuation of the previous packet, reset the buffer and append
151: $buffer->jumpto(0);
152:
153: // Append
154: $processed[$sequence_id_last] .= $buffer->getBuffer();
155: }
156: }
157:
158: unset($buffer, $sequence_id_last, $sequence_id);
159:
160: // Iterate over the combined packets and do some work
161: foreach ($processed as $sequence_id => $data) {
162: // Create a new buffer
163: $buffer = new Buffer($data);
164:
165: // Get the length of the packet
166: $packetLength = $buffer->getLength();
167:
168: // Check to make sure the expected length matches the real length
169: // Subtract 4 for the sequence_id pulled out earlier
170: if ($packetLength != ($buffer->readInt32() - 4)) {
171: throw new Exception(__METHOD__ . " packet length does not match expected length!");
172: }
173:
174: // Now we need to call the proper method
175: $results = array_merge(
176: $results,
177: call_user_func_array([$this, $this->responses[$sequence_id]], [$buffer])
178: );
179: }
180:
181: return $results;
182: }
183:
184: // Internal Methods
185:
186: /**
187: * Decode the buffer into a usable format
188: *
189: * @param \GameQ\Buffer $buffer
190: *
191: * @return array
192: * @throws \GameQ\Exception\Protocol
193: */
194: protected function decode(Buffer $buffer)
195: {
196: $items = [];
197:
198: // Get the number of words in this buffer
199: $itemCount = $buffer->readInt32();
200:
201: // Loop over the number of items
202: for ($i = 0; $i < $itemCount; $i++) {
203: // Length of the string
204: $buffer->readInt32();
205:
206: // Just read the string
207: $items[$i] = $buffer->readString();
208: }
209:
210: return $items;
211: }
212:
213: /**
214: * Process the server details
215: *
216: * @param \GameQ\Buffer $buffer
217: *
218: * @return array
219: */
220: protected function processDetails(Buffer $buffer)
221: {
222: // Decode into items
223: $items = $this->decode($buffer);
224:
225: // Set the result to a new result instance
226: $result = new Result();
227:
228: // Server is always dedicated
229: $result->add('dedicated', 1);
230:
231: // These are the same no matter what mode the server is in
232: $result->add('hostname', $items[1]);
233: $result->add('num_players', (int)$items[2]);
234: $result->add('max_players', (int)$items[3]);
235: $result->add('gametype', $items[4]);
236: $result->add('map', $items[5]);
237: $result->add('roundsplayed', (int)$items[6]);
238: $result->add('roundstotal', (int)$items[7]);
239: $result->add('num_teams', (int)$items[8]);
240:
241: // Set the current index
242: $index_current = 9;
243:
244: // Pull the team count
245: $teamCount = $result->get('num_teams');
246:
247: // Loop for the number of teams found, increment along the way
248: for ($id = 1; $id <= $teamCount; $id++, $index_current++) {
249: // Shows the tickets
250: $result->addTeam('tickets', $items[$index_current]);
251: // We add an id so we know which team this is
252: $result->addTeam('id', $id);
253: }
254:
255: // Get and set the rest of the data points.
256: $result->add('targetscore', (int)$items[$index_current]);
257: $result->add('online', 1); // Forced true, it seems $words[$index_current + 1] is always empty
258: $result->add('ranked', (int)$items[$index_current + 2]);
259: $result->add('punkbuster', (int)$items[$index_current + 3]);
260: $result->add('password', (int)$items[$index_current + 4]);
261: $result->add('uptime', (int)$items[$index_current + 5]);
262: $result->add('roundtime', (int)$items[$index_current + 6]);
263: // Added in R9
264: $result->add('ip_port', $items[$index_current + 7]);
265: $result->add('punkbuster_version', $items[$index_current + 8]);
266: $result->add('join_queue', (int)$items[$index_current + 9]);
267: $result->add('region', $items[$index_current + 10]);
268: $result->add('pingsite', $items[$index_current + 11]);
269: $result->add('country', $items[$index_current + 12]);
270: // Added in R29, No docs as of yet
271: $result->add('quickmatch', (int)$items[$index_current + 13]); // Guessed from research
272:
273: unset($items, $index_current, $teamCount, $buffer);
274:
275: return $result->fetch();
276: }
277:
278: /**
279: * Process the server version
280: *
281: * @param \GameQ\Buffer $buffer
282: *
283: * @return array
284: * @throws \GameQ\Exception\Protocol
285: */
286: protected function processVersion(Buffer $buffer)
287: {
288: // Decode into items
289: $items = $this->decode($buffer);
290:
291: // Set the result to a new result instance
292: $result = new Result();
293:
294: $result->add('version', $items[2]);
295:
296: unset($buffer, $items);
297:
298: return $result->fetch();
299: }
300:
301: /**
302: * Process the players
303: *
304: * @param \GameQ\Buffer $buffer
305: *
306: * @return array
307: * @throws \GameQ\Exception\Protocol
308: */
309: protected function processPlayers(Buffer $buffer)
310: {
311: // Decode into items
312: $items = $this->decode($buffer);
313:
314: // Set the result to a new result instance
315: $result = new Result();
316:
317: // Number of data points per player
318: $numTags = $items[1];
319:
320: // Grab the tags for each player
321: $tags = array_slice($items, 2, $numTags);
322:
323: // Get the player count
324: $playerCount = $items[$numTags + 2];
325:
326: // Iterate over the index until we run out of players
327: for ($i = 0, $x = $numTags + 3; $i < $playerCount; $i++, $x += $numTags) {
328: // Loop over the player tags and extract the info for that tag
329: foreach ($tags as $index => $tag) {
330: $result->addPlayer($tag, $items[($x + $index)]);
331: }
332: }
333:
334: return $result->fetch();
335: }
336: }
337: