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\Buffer; |
22: | use GameQ\Exception\Protocol as Exception; |
23: | use GameQ\Helpers\Str; |
24: | use GameQ\Protocol; |
25: | use GameQ\Result; |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | class Gamespy2 extends Protocol |
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 $normalize = [ |
93: | |
94: | 'general' => [ |
95: | |
96: | 'dedicated' => 'dedicated', |
97: | 'gametype' => 'gametype', |
98: | 'hostname' => 'hostname', |
99: | 'mapname' => 'mapname', |
100: | 'maxplayers' => 'maxplayers', |
101: | 'mod' => 'mod', |
102: | 'numplayers' => 'numplayers', |
103: | 'password' => 'password', |
104: | ], |
105: | ]; |
106: | |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | |
113: | |
114: | public function processResponse() |
115: | { |
116: | |
117: | $packets = []; |
118: | |
119: | |
120: | foreach ($this->packets_response as $response) { |
121: | $buffer = new Buffer($response); |
122: | |
123: | |
124: | $header = $buffer->read(5); |
125: | |
126: | |
127: | $packets[$header][] = $buffer->getBuffer(); |
128: | } |
129: | |
130: | unset($buffer); |
131: | |
132: | $results = []; |
133: | |
134: | |
135: | foreach ($packets as $header => $packetGroup) { |
136: | |
137: | if (!array_key_exists($header, $this->responses)) { |
138: | throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid"); |
139: | } |
140: | |
141: | |
142: | $results = array_merge( |
143: | $results, |
144: | call_user_func_array([$this, $this->responses[$header]], [new Buffer(implode($packetGroup))]) |
145: | ); |
146: | } |
147: | |
148: | unset($packets); |
149: | |
150: | return $results; |
151: | } |
152: | |
153: | |
154: | |
155: | |
156: | |
157: | |
158: | |
159: | |
160: | |
161: | |
162: | |
163: | |
164: | protected function processDetails(Buffer $buffer) |
165: | { |
166: | |
167: | $result = new Result(); |
168: | |
169: | |
170: | while ($buffer->getLength()) { |
171: | $key = $buffer->readString(); |
172: | if (strlen($key) == 0) { |
173: | break; |
174: | } |
175: | $result->add($key, Str::isoToUtf8($buffer->readString())); |
176: | } |
177: | |
178: | unset($buffer); |
179: | |
180: | return $result->fetch(); |
181: | } |
182: | |
183: | |
184: | |
185: | |
186: | |
187: | |
188: | |
189: | |
190: | |
191: | |
192: | protected function processPlayers(Buffer $buffer) |
193: | { |
194: | |
195: | $result = new Result(); |
196: | |
197: | |
198: | $buffer->skip(1); |
199: | |
200: | |
201: | $this->parsePlayerTeam('players', $buffer, $result); |
202: | |
203: | |
204: | $this->parsePlayerTeam('teams', $buffer, $result); |
205: | |
206: | unset($buffer); |
207: | |
208: | return $result->fetch(); |
209: | } |
210: | |
211: | |
212: | |
213: | |
214: | |
215: | |
216: | |
217: | |
218: | |
219: | |
220: | |
221: | protected function parsePlayerTeam($dataType, Buffer &$buffer, Result &$result) |
222: | { |
223: | |
224: | $result->add('num_' . $dataType, $buffer->readInt8()); |
225: | |
226: | |
227: | $varNames = []; |
228: | |
229: | |
230: | while ($buffer->getLength()) { |
231: | $varNames[] = str_replace('_', '', $buffer->readString()); |
232: | |
233: | if ($buffer->lookAhead() === "\x00") { |
234: | $buffer->skip(); |
235: | break; |
236: | } |
237: | } |
238: | |
239: | |
240: | if ($buffer->lookAhead() == "\x00") { |
241: | $buffer->skip(); |
242: | |
243: | return; |
244: | } |
245: | |
246: | |
247: | while ($buffer->getLength() > 4) { |
248: | foreach ($varNames as $varName) { |
249: | $result->addSub($dataType, Str::isoToUtf8($varName), Str::isoToUtf8($buffer->readString())); |
250: | } |
251: | if ($buffer->lookAhead() === "\x00") { |
252: | $buffer->skip(); |
253: | break; |
254: | } |
255: | } |
256: | |
257: | return; |
258: | } |
259: | } |
260: | |