| 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: | class Cs2d extends Protocol |
| 36: | { |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | protected $packets = [ |
| 43: | self::PACKET_STATUS => "\x01\x00\xFB\x01", |
| 44: | |
| 45: | self::PACKET_PLAYERS => "\x01\x00\xFB\x05", |
| 46: | ]; |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | protected $responses = [ |
| 54: | "\x01\x00\xFB\x01" => "processDetails", |
| 55: | "\x01\x00\xFB\x05" => "processPlayers", |
| 56: | ]; |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | protected $protocol = 'cs2d'; |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | protected $name = 'cs2d'; |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | protected $name_long = "Counter-Strike 2d"; |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | protected $join_link = "cs2d://%s:%d/"; |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | protected $normalize = [ |
| 92: | |
| 93: | 'general' => [ |
| 94: | |
| 95: | 'dedicated' => 'dedicated', |
| 96: | 'gametype' => 'game_mode', |
| 97: | 'hostname' => 'hostname', |
| 98: | 'mapname' => 'mapname', |
| 99: | 'maxplayers' => 'max_players', |
| 100: | 'mod' => 'game_dir', |
| 101: | 'numplayers' => 'num_players', |
| 102: | 'password' => 'password', |
| 103: | ], |
| 104: | |
| 105: | 'player' => [ |
| 106: | 'name' => 'name', |
| 107: | 'deaths' => 'deaths', |
| 108: | 'score' => 'score', |
| 109: | ], |
| 110: | ]; |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | public function processResponse() |
| 119: | { |
| 120: | |
| 121: | if (count($this->packets_response) == 1) { |
| 122: | |
| 123: | $buffer = new Buffer($this->packets_response[0]); |
| 124: | |
| 125: | |
| 126: | $packet = (($buffer->lookAhead(4) === $this->packets[self::PACKET_PLAYERS]) ? |
| 127: | self::PACKET_STATUS : self::PACKET_PLAYERS); |
| 128: | |
| 129: | |
| 130: | $responses = explode(substr($this->packets[$packet], 2), $buffer->getData()); |
| 131: | |
| 132: | |
| 133: | $responses[1] = $this->packets[$packet] . ((count($responses) === 2) ? $responses[1] : ""); |
| 134: | |
| 135: | unset($buffer); |
| 136: | } else { |
| 137: | $responses = $this->packets_response; |
| 138: | } |
| 139: | |
| 140: | |
| 141: | $packets = []; |
| 142: | |
| 143: | |
| 144: | foreach ($responses as $response) { |
| 145: | $buffer = new Buffer($response); |
| 146: | |
| 147: | |
| 148: | $header = $buffer->read(4); |
| 149: | |
| 150: | |
| 151: | $packets[$header][] = $buffer->getBuffer(); |
| 152: | } |
| 153: | |
| 154: | unset($buffer); |
| 155: | |
| 156: | $results = []; |
| 157: | |
| 158: | |
| 159: | foreach ($packets as $header => $packetGroup) { |
| 160: | |
| 161: | if (!array_key_exists($header, $this->responses)) { |
| 162: | throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid"); |
| 163: | } |
| 164: | |
| 165: | |
| 166: | $results = array_merge( |
| 167: | $results, |
| 168: | call_user_func_array([$this, $this->responses[$header]], [new Buffer(implode($packetGroup))]) |
| 169: | ); |
| 170: | } |
| 171: | |
| 172: | unset($packets); |
| 173: | |
| 174: | return $results; |
| 175: | } |
| 176: | |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | protected function processDetails(Buffer $buffer) |
| 187: | { |
| 188: | |
| 189: | $result = new Result(); |
| 190: | |
| 191: | |
| 192: | $serverFlags = $buffer->readInt8(); |
| 193: | |
| 194: | |
| 195: | $result->add('password', (int)$this->readFlag($serverFlags, 0)); |
| 196: | $result->add('registered_only', (int)$this->readFlag($serverFlags, 1)); |
| 197: | $result->add('fog_of_war', (int)$this->readFlag($serverFlags, 2)); |
| 198: | $result->add('friendly_fire', (int)$this->readFlag($serverFlags, 3)); |
| 199: | $result->add('bots_enabled', (int)$this->readFlag($serverFlags, 5)); |
| 200: | $result->add('lua_scripts', (int)$this->readFlag($serverFlags, 6)); |
| 201: | |
| 202: | |
| 203: | $result->add('servername', Str::isoToUtf8($buffer->readPascalString(0))); |
| 204: | $result->add('mapname', Str::isoToUtf8($buffer->readPascalString(0))); |
| 205: | $result->add('num_players', $buffer->readInt8()); |
| 206: | $result->add('max_players', $buffer->readInt8()); |
| 207: | $result->add('game_mode', $buffer->readInt8()); |
| 208: | $result->add('num_bots', (($this->readFlag($serverFlags, 5)) ? $buffer->readInt8() : 0)); |
| 209: | $result->add('dedicated', 1); |
| 210: | |
| 211: | unset($buffer); |
| 212: | |
| 213: | return $result->fetch(); |
| 214: | } |
| 215: | |
| 216: | |
| 217: | |
| 218: | |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | protected function processPlayers(Buffer $buffer) |
| 226: | { |
| 227: | |
| 228: | $result = new Result(); |
| 229: | |
| 230: | |
| 231: | $buffer->read(); |
| 232: | |
| 233: | |
| 234: | while ($buffer->getLength()) { |
| 235: | |
| 236: | if (($id = $buffer->readInt8()) !== 0) { |
| 237: | |
| 238: | $result->addPlayer('id', $id); |
| 239: | $result->addPlayer('name', Str::isoToUtf8($buffer->readPascalString(0))); |
| 240: | $result->addPlayer('team', $buffer->readInt8()); |
| 241: | $result->addPlayer('score', $buffer->readInt32()); |
| 242: | $result->addPlayer('deaths', $buffer->readInt32()); |
| 243: | } |
| 244: | } |
| 245: | |
| 246: | unset($buffer, $id); |
| 247: | |
| 248: | return $result->fetch(); |
| 249: | } |
| 250: | |
| 251: | |
| 252: | |
| 253: | |
| 254: | |
| 255: | |
| 256: | |
| 257: | |
| 258: | |
| 259: | protected function readFlag($flags, $offset) |
| 260: | { |
| 261: | return !!($flags & (1 << $offset)); |
| 262: | } |
| 263: | } |
| 264: | |