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\Protocol; |
22: | use GameQ\Buffer; |
23: | use GameQ\Result; |
24: | use GameQ\Exception\Protocol as Exception; |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | class Unreal2 extends Protocol |
32: | { |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | protected $packets = [ |
41: | self::PACKET_DETAILS => "\x79\x00\x00\x00\x00", |
42: | self::PACKET_RULES => "\x79\x00\x00\x00\x01", |
43: | self::PACKET_PLAYERS => "\x79\x00\x00\x00\x02", |
44: | ]; |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | protected $responses = [ |
52: | "\x80\x00\x00\x00\x00" => "processDetails", |
53: | "\x80\x00\x00\x00\x01" => "processRules", |
54: | "\x80\x00\x00\x00\x02" => "processPlayers", |
55: | ]; |
56: | |
57: | |
58: | |
59: | |
60: | |
61: | |
62: | protected $protocol = 'unreal2'; |
63: | |
64: | |
65: | |
66: | |
67: | |
68: | |
69: | protected $name = 'unreal2'; |
70: | |
71: | |
72: | |
73: | |
74: | |
75: | |
76: | protected $name_long = "Unreal 2"; |
77: | |
78: | |
79: | |
80: | |
81: | |
82: | |
83: | protected $normalize = [ |
84: | |
85: | 'general' => [ |
86: | |
87: | 'dedicated' => 'ServerMode', |
88: | 'gametype' => 'gametype', |
89: | 'hostname' => 'servername', |
90: | 'mapname' => 'mapname', |
91: | 'maxplayers' => 'maxplayers', |
92: | 'numplayers' => 'numplayers', |
93: | 'password' => 'password', |
94: | ], |
95: | |
96: | 'player' => [ |
97: | 'name' => 'name', |
98: | 'score' => 'score', |
99: | ], |
100: | ]; |
101: | |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | public function processResponse() |
109: | { |
110: | |
111: | |
112: | $packets = []; |
113: | |
114: | |
115: | foreach ($this->packets_response as $response) { |
116: | $buffer = new Buffer($response); |
117: | |
118: | |
119: | $header = $buffer->read(5); |
120: | |
121: | |
122: | $packets[$header][] = $buffer->getBuffer(); |
123: | } |
124: | |
125: | unset($buffer); |
126: | |
127: | $results = []; |
128: | |
129: | |
130: | foreach ($packets as $header => $packetGroup) { |
131: | |
132: | if (!array_key_exists($header, $this->responses)) { |
133: | throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid"); |
134: | } |
135: | |
136: | |
137: | $results = array_merge( |
138: | $results, |
139: | call_user_func_array([$this, $this->responses[$header]], [new Buffer(implode($packetGroup))]) |
140: | ); |
141: | } |
142: | |
143: | unset($packets); |
144: | |
145: | return $results; |
146: | } |
147: | |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | |
154: | |
155: | |
156: | |
157: | |
158: | |
159: | |
160: | protected function processDetails(Buffer $buffer) |
161: | { |
162: | |
163: | |
164: | $result = new Result(); |
165: | |
166: | $result->add('serverid', $buffer->readInt32()); |
167: | $result->add('serverip', $buffer->readPascalString(1)); |
168: | $result->add('gameport', $buffer->readInt32()); |
169: | $result->add('queryport', $buffer->readInt32()); |
170: | $result->add('servername', utf8_encode($buffer->readPascalString(1))); |
171: | $result->add('mapname', utf8_encode($buffer->readPascalString(1))); |
172: | $result->add('gametype', $buffer->readPascalString(1)); |
173: | $result->add('numplayers', $buffer->readInt32()); |
174: | $result->add('maxplayers', $buffer->readInt32()); |
175: | $result->add('ping', $buffer->readInt32()); |
176: | |
177: | unset($buffer); |
178: | |
179: | return $result->fetch(); |
180: | } |
181: | |
182: | |
183: | |
184: | |
185: | |
186: | |
187: | |
188: | |
189: | protected function processPlayers(Buffer $buffer) |
190: | { |
191: | |
192: | |
193: | $result = new Result(); |
194: | |
195: | |
196: | while ($buffer->getLength()) { |
197: | |
198: | if (($id = $buffer->readInt32()) !== 0) { |
199: | |
200: | $result->addPlayer('id', $id); |
201: | $result->addPlayer('name', utf8_encode($buffer->readPascalString(1))); |
202: | $result->addPlayer('ping', $buffer->readInt32()); |
203: | $result->addPlayer('score', $buffer->readInt32()); |
204: | |
205: | |
206: | $buffer->skip(4); |
207: | } |
208: | } |
209: | |
210: | unset($buffer, $id); |
211: | |
212: | return $result->fetch(); |
213: | } |
214: | |
215: | |
216: | |
217: | |
218: | |
219: | |
220: | |
221: | |
222: | protected function processRules(Buffer $buffer) |
223: | { |
224: | |
225: | |
226: | $result = new Result(); |
227: | |
228: | |
229: | $inc = -1; |
230: | while ($buffer->getLength()) { |
231: | |
232: | $key = $buffer->readPascalString(1); |
233: | |
234: | |
235: | if ($key === 'Mutator') { |
236: | $key .= ++$inc; |
237: | } |
238: | |
239: | $result->add(strtolower($key), utf8_encode($buffer->readPascalString(1))); |
240: | } |
241: | |
242: | unset($buffer); |
243: | |
244: | return $result->fetch(); |
245: | } |
246: | } |
247: | |