| 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: | class Unreal2 extends Protocol |
| 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: | $packets = []; |
| 112: | |
| 113: | |
| 114: | foreach ($this->packets_response as $response) { |
| 115: | $buffer = new Buffer($response); |
| 116: | |
| 117: | |
| 118: | $header = $buffer->read(5); |
| 119: | |
| 120: | |
| 121: | $packets[$header][] = $buffer->getBuffer(); |
| 122: | } |
| 123: | |
| 124: | unset($buffer); |
| 125: | |
| 126: | $results = []; |
| 127: | |
| 128: | |
| 129: | foreach ($packets as $header => $packetGroup) { |
| 130: | |
| 131: | if (!array_key_exists($header, $this->responses)) { |
| 132: | throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid"); |
| 133: | } |
| 134: | |
| 135: | |
| 136: | $results = array_merge( |
| 137: | $results, |
| 138: | call_user_func_array([$this, $this->responses[$header]], [new Buffer(implode($packetGroup))]) |
| 139: | ); |
| 140: | } |
| 141: | |
| 142: | unset($packets); |
| 143: | |
| 144: | return $results; |
| 145: | } |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | protected function processDetails(Buffer $buffer) |
| 158: | { |
| 159: | |
| 160: | $result = new Result(); |
| 161: | |
| 162: | $result->add('serverid', $buffer->readInt32()); |
| 163: | $result->add('serverip', $buffer->readPascalString(1)); |
| 164: | $result->add('gameport', $buffer->readInt32()); |
| 165: | $result->add('queryport', $buffer->readInt32()); |
| 166: | $result->add('servername', Str::isoToUtf8($buffer->readPascalString(1))); |
| 167: | $result->add('mapname', Str::isoToUtf8($buffer->readPascalString(1))); |
| 168: | $result->add('gametype', $buffer->readPascalString(1)); |
| 169: | $result->add('numplayers', $buffer->readInt32()); |
| 170: | $result->add('maxplayers', $buffer->readInt32()); |
| 171: | $result->add('ping', $buffer->readInt32()); |
| 172: | |
| 173: | unset($buffer); |
| 174: | |
| 175: | return $result->fetch(); |
| 176: | } |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | protected function processPlayers(Buffer $buffer) |
| 186: | { |
| 187: | |
| 188: | $result = new Result(); |
| 189: | |
| 190: | |
| 191: | while ($buffer->getLength()) { |
| 192: | |
| 193: | if (($id = $buffer->readInt32()) !== 0) { |
| 194: | |
| 195: | $result->addPlayer('id', $id); |
| 196: | $result->addPlayer('name', Str::isoToUtf8($buffer->readPascalString(1))); |
| 197: | $result->addPlayer('ping', $buffer->readInt32()); |
| 198: | $result->addPlayer('score', $buffer->readInt32()); |
| 199: | |
| 200: | |
| 201: | $buffer->skip(4); |
| 202: | } |
| 203: | } |
| 204: | |
| 205: | unset($buffer, $id); |
| 206: | |
| 207: | return $result->fetch(); |
| 208: | } |
| 209: | |
| 210: | |
| 211: | |
| 212: | |
| 213: | |
| 214: | |
| 215: | |
| 216: | |
| 217: | protected function processRules(Buffer $buffer) |
| 218: | { |
| 219: | |
| 220: | $result = new Result(); |
| 221: | |
| 222: | |
| 223: | $inc = -1; |
| 224: | while ($buffer->getLength()) { |
| 225: | |
| 226: | $key = $buffer->readPascalString(1); |
| 227: | |
| 228: | |
| 229: | if ($key === 'Mutator') { |
| 230: | $key .= ++$inc; |
| 231: | } |
| 232: | |
| 233: | $result->add(strtolower($key), Str::isoToUtf8($buffer->readPascalString(1))); |
| 234: | } |
| 235: | |
| 236: | unset($buffer); |
| 237: | |
| 238: | return $result->fetch(); |
| 239: | } |
| 240: | } |
| 241: | |