| 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: | use GameQ\Server; |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | class Samp extends Protocol |
| 37: | { |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | protected $packets = [ |
| 45: | self::PACKET_STATUS => "SAMP%si", |
| 46: | self::PACKET_PLAYERS => "SAMP%sd", |
| 47: | self::PACKET_RULES => "SAMP%sr", |
| 48: | ]; |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | protected $responses = [ |
| 56: | "\x69" => "processStatus", |
| 57: | "\x64" => "processPlayers", |
| 58: | "\x72" => "processRules", |
| 59: | ]; |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | protected $protocol = 'samp'; |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | protected $name = 'samp'; |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | protected $name_long = "San Andreas Multiplayer"; |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | protected $server_code = null; |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | protected $join_link = "samp://%s:%d/"; |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | protected $normalize = [ |
| 102: | |
| 103: | 'general' => [ |
| 104: | |
| 105: | 'dedicated' => 'dedicated', |
| 106: | 'hostname' => ['hostname', 'servername'], |
| 107: | 'mapname' => 'mapname', |
| 108: | 'maxplayers' => 'max_players', |
| 109: | 'numplayers' => 'num_players', |
| 110: | 'password' => 'password', |
| 111: | ], |
| 112: | |
| 113: | 'player' => [ |
| 114: | 'name' => 'name', |
| 115: | 'score' => 'score', |
| 116: | 'ping' => 'ping', |
| 117: | ], |
| 118: | ]; |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | public function beforeSend(Server $server) |
| 126: | { |
| 127: | |
| 128: | $this->server_code = implode('', array_map('chr', explode('.', $server->ip()))) . |
| 129: | pack("S", $server->portClient()); |
| 130: | |
| 131: | |
| 132: | foreach ($this->packets as $packetType => $packet) { |
| 133: | |
| 134: | $this->packets[$packetType] = sprintf($packet, $this->server_code); |
| 135: | } |
| 136: | } |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | public function processResponse() |
| 145: | { |
| 146: | |
| 147: | $results = []; |
| 148: | |
| 149: | |
| 150: | $serverCodeLength = strlen($this->server_code); |
| 151: | |
| 152: | |
| 153: | foreach ($this->packets_response as $response) { |
| 154: | |
| 155: | $buffer = new Buffer($response); |
| 156: | |
| 157: | |
| 158: | if (($header = $buffer->read(4)) !== 'SAMP') { |
| 159: | throw new Exception(__METHOD__ . " header response '{$header}' is not valid"); |
| 160: | } |
| 161: | |
| 162: | |
| 163: | if ($buffer->read($serverCodeLength) !== $this->server_code) { |
| 164: | throw new Exception(__METHOD__ . " code check failed."); |
| 165: | } |
| 166: | |
| 167: | |
| 168: | $response_type = $buffer->read(1); |
| 169: | |
| 170: | |
| 171: | if (!array_key_exists($response_type, $this->responses)) { |
| 172: | throw new Exception(__METHOD__ . " response type '{$response_type}' is not valid"); |
| 173: | } |
| 174: | |
| 175: | |
| 176: | $results = array_merge( |
| 177: | $results, |
| 178: | call_user_func_array([$this, $this->responses[$response_type]], [$buffer]) |
| 179: | ); |
| 180: | |
| 181: | unset($buffer); |
| 182: | } |
| 183: | |
| 184: | return $results; |
| 185: | } |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | |
| 195: | |
| 196: | protected function processStatus(Buffer $buffer) |
| 197: | { |
| 198: | |
| 199: | $result = new Result(); |
| 200: | |
| 201: | |
| 202: | $result->add('dedicated', 1); |
| 203: | |
| 204: | |
| 205: | $result->add('password', $buffer->readInt8()); |
| 206: | $result->add('num_players', $buffer->readInt16()); |
| 207: | $result->add('max_players', $buffer->readInt16()); |
| 208: | |
| 209: | |
| 210: | $result->add('servername', Str::isoToUtf8($buffer->read($buffer->readInt32()))); |
| 211: | $result->add('gametype', $buffer->read($buffer->readInt32())); |
| 212: | $result->add('language', $buffer->read($buffer->readInt32())); |
| 213: | |
| 214: | unset($buffer); |
| 215: | |
| 216: | return $result->fetch(); |
| 217: | } |
| 218: | |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | |
| 226: | protected function processPlayers(Buffer $buffer) |
| 227: | { |
| 228: | |
| 229: | $result = new Result(); |
| 230: | |
| 231: | |
| 232: | $result->add('num_players', $buffer->readInt16()); |
| 233: | |
| 234: | |
| 235: | while ($buffer->getLength()) { |
| 236: | $result->addPlayer('id', $buffer->readInt8()); |
| 237: | $result->addPlayer('name', Str::isoToUtf8($buffer->readPascalString())); |
| 238: | $result->addPlayer('score', $buffer->readInt32()); |
| 239: | $result->addPlayer('ping', $buffer->readInt32()); |
| 240: | } |
| 241: | |
| 242: | unset($buffer); |
| 243: | |
| 244: | return $result->fetch(); |
| 245: | } |
| 246: | |
| 247: | |
| 248: | |
| 249: | |
| 250: | |
| 251: | |
| 252: | |
| 253: | |
| 254: | protected function processRules(Buffer $buffer) |
| 255: | { |
| 256: | |
| 257: | $result = new Result(); |
| 258: | |
| 259: | |
| 260: | $result->add('num_rules', $buffer->readInt16()); |
| 261: | |
| 262: | |
| 263: | while ($buffer->getLength()) { |
| 264: | $result->add($buffer->readPascalString(), $buffer->readPascalString()); |
| 265: | } |
| 266: | |
| 267: | unset($buffer); |
| 268: | |
| 269: | return $result->fetch(); |
| 270: | } |
| 271: | } |
| 272: | |