1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | namespace GameQ\Protocols; |
20: | |
21: | use GameQ\Exception\Protocol as Exception; |
22: | use GameQ\Protocol; |
23: | use GameQ\Buffer; |
24: | use GameQ\Result; |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | class Gamespy2 extends Protocol |
36: | { |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | protected $state = self::STATE_BETA; |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | protected $packets = [ |
52: | self::PACKET_DETAILS => "\xFE\xFD\x00\x43\x4F\x52\x59\xFF\x00\x00", |
53: | self::PACKET_PLAYERS => "\xFE\xFD\x00\x43\x4F\x52\x58\x00\xFF\xFF", |
54: | ]; |
55: | |
56: | |
57: | |
58: | |
59: | |
60: | |
61: | protected $responses = [ |
62: | "\x00\x43\x4F\x52\x59" => "processDetails", |
63: | "\x00\x43\x4F\x52\x58" => "processPlayers", |
64: | ]; |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | |
71: | protected $protocol = 'gamespy2'; |
72: | |
73: | |
74: | |
75: | |
76: | |
77: | |
78: | protected $name = 'gamespy2'; |
79: | |
80: | |
81: | |
82: | |
83: | |
84: | |
85: | protected $name_long = "GameSpy2 Server"; |
86: | |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | protected $join_link = null; |
93: | |
94: | |
95: | |
96: | |
97: | |
98: | |
99: | protected $normalize = [ |
100: | |
101: | 'general' => [ |
102: | |
103: | 'dedicated' => 'dedicated', |
104: | 'gametype' => 'gametype', |
105: | 'hostname' => 'hostname', |
106: | 'mapname' => 'mapname', |
107: | 'maxplayers' => 'maxplayers', |
108: | 'mod' => 'mod', |
109: | 'numplayers' => 'numplayers', |
110: | 'password' => 'password', |
111: | ], |
112: | ]; |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | public function processResponse() |
122: | { |
123: | |
124: | |
125: | $packets = []; |
126: | |
127: | |
128: | foreach ($this->packets_response as $response) { |
129: | $buffer = new Buffer($response); |
130: | |
131: | |
132: | $header = $buffer->read(5); |
133: | |
134: | |
135: | $packets[$header][] = $buffer->getBuffer(); |
136: | } |
137: | |
138: | unset($buffer); |
139: | |
140: | $results = []; |
141: | |
142: | |
143: | foreach ($packets as $header => $packetGroup) { |
144: | |
145: | if (!array_key_exists($header, $this->responses)) { |
146: | throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid"); |
147: | } |
148: | |
149: | |
150: | $results = array_merge( |
151: | $results, |
152: | call_user_func_array([$this, $this->responses[$header]], [new Buffer(implode($packetGroup))]) |
153: | ); |
154: | } |
155: | |
156: | unset($packets); |
157: | |
158: | return $results; |
159: | } |
160: | |
161: | |
162: | |
163: | |
164: | |
165: | |
166: | |
167: | |
168: | |
169: | |
170: | |
171: | |
172: | |
173: | protected function processDetails(Buffer $buffer) |
174: | { |
175: | |
176: | |
177: | $result = new Result(); |
178: | |
179: | |
180: | while ($buffer->getLength()) { |
181: | $key = $buffer->readString(); |
182: | if (strlen($key) == 0) { |
183: | break; |
184: | } |
185: | $result->add($key, utf8_encode($buffer->readString())); |
186: | } |
187: | |
188: | unset($buffer); |
189: | |
190: | return $result->fetch(); |
191: | } |
192: | |
193: | |
194: | |
195: | |
196: | |
197: | |
198: | |
199: | |
200: | |
201: | protected function processPlayers(Buffer $buffer) |
202: | { |
203: | |
204: | |
205: | $result = new Result(); |
206: | |
207: | |
208: | $buffer->skip(1); |
209: | |
210: | |
211: | $this->parsePlayerTeam('players', $buffer, $result); |
212: | |
213: | |
214: | $this->parsePlayerTeam('teams', $buffer, $result); |
215: | |
216: | unset($buffer); |
217: | |
218: | return $result->fetch(); |
219: | } |
220: | |
221: | |
222: | |
223: | |
224: | |
225: | |
226: | |
227: | |
228: | |
229: | |
230: | protected function parsePlayerTeam($dataType, Buffer &$buffer, Result &$result) |
231: | { |
232: | |
233: | |
234: | $result->add('num_' . $dataType, $buffer->readInt8()); |
235: | |
236: | |
237: | $varNames = []; |
238: | |
239: | |
240: | while ($buffer->getLength()) { |
241: | $varNames[] = str_replace('_', '', $buffer->readString()); |
242: | |
243: | if ($buffer->lookAhead() === "\x00") { |
244: | $buffer->skip(); |
245: | break; |
246: | } |
247: | } |
248: | |
249: | |
250: | if ($buffer->lookAhead() == "\x00") { |
251: | $buffer->skip(); |
252: | |
253: | return; |
254: | } |
255: | |
256: | |
257: | while ($buffer->getLength() > 4) { |
258: | foreach ($varNames as $varName) { |
259: | $result->addSub($dataType, utf8_encode($varName), utf8_encode($buffer->readString())); |
260: | } |
261: | if ($buffer->lookAhead() === "\x00") { |
262: | $buffer->skip(); |
263: | break; |
264: | } |
265: | } |
266: | |
267: | return; |
268: | } |
269: | } |
270: | |