| 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: | |
| 37: | |
| 38: | |
| 39: | class M2mp extends Protocol |
| 40: | { |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | protected $packets = [ |
| 48: | self::PACKET_ALL => "M2MP", |
| 49: | ]; |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | protected $responses = [ |
| 57: | "M2MP" => 'processStatus', |
| 58: | ]; |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | protected $protocol = 'm2mp'; |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | protected $name = 'm2mp'; |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | protected $name_long = "Mafia 2 Multiplayer"; |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | protected $port_diff = 1; |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | protected $normalize = [ |
| 94: | |
| 95: | 'general' => [ |
| 96: | |
| 97: | 'hostname' => 'servername', |
| 98: | 'gametype' => 'gamemode', |
| 99: | 'maxplayers' => 'max_players', |
| 100: | 'numplayers' => 'num_players', |
| 101: | 'password' => 'password', |
| 102: | ], |
| 103: | |
| 104: | 'player' => [ |
| 105: | 'name' => 'name', |
| 106: | ], |
| 107: | ]; |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | public function processResponse() |
| 117: | { |
| 118: | |
| 119: | $buffer = new Buffer(implode('', $this->packets_response)); |
| 120: | |
| 121: | |
| 122: | $header = $buffer->read(4); |
| 123: | |
| 124: | |
| 125: | |
| 126: | if ($header != "M2MP") { |
| 127: | throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid"); |
| 128: | } |
| 129: | |
| 130: | return call_user_func_array([$this, $this->responses[$header]], [$buffer]); |
| 131: | } |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | protected function processStatus(Buffer $buffer) |
| 142: | { |
| 143: | |
| 144: | $results = $this->processServerInfo($buffer); |
| 145: | |
| 146: | $results = array_merge_recursive( |
| 147: | $results, |
| 148: | $this->processPlayers($buffer) |
| 149: | ); |
| 150: | |
| 151: | unset($buffer); |
| 152: | |
| 153: | |
| 154: | return $results; |
| 155: | } |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | protected function processServerInfo(Buffer $buffer) |
| 166: | { |
| 167: | |
| 168: | $result = new Result(); |
| 169: | |
| 170: | |
| 171: | $result->add('dedicated', 1); |
| 172: | |
| 173: | |
| 174: | |
| 175: | $result->add('servername', $buffer->readPascalString(1, true)); |
| 176: | $result->add('num_players', $buffer->readPascalString(1, true)); |
| 177: | $result->add('max_players', $buffer->readPascalString(1, true)); |
| 178: | $result->add('gamemode', $buffer->readPascalString(1, true)); |
| 179: | $result->add('password', (bool) $buffer->readInt8()); |
| 180: | |
| 181: | unset($buffer); |
| 182: | |
| 183: | return $result->fetch(); |
| 184: | } |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | protected function processPlayers(Buffer $buffer) |
| 195: | { |
| 196: | |
| 197: | $result = new Result(); |
| 198: | |
| 199: | |
| 200: | |
| 201: | while ($buffer->getLength()) { |
| 202: | |
| 203: | if ($buffer->getLength() <= 1) { |
| 204: | break; |
| 205: | } |
| 206: | |
| 207: | |
| 208: | |
| 209: | $result->addPlayer('name', Str::isoToUtf8(trim($buffer->readPascalString(1, true)))); |
| 210: | } |
| 211: | |
| 212: | |
| 213: | unset($buffer); |
| 214: | |
| 215: | return $result->fetch(); |
| 216: | } |
| 217: | } |
| 218: | |