| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | namespace Clansuite\ServerQuery\ServerProtocols; |
| 14: | |
| 15: | use function is_array; |
| 16: | use function pack; |
| 17: | use function strlen; |
| 18: | use function substr; |
| 19: | use function time; |
| 20: | use function unpack; |
| 21: | use Clansuite\Capture\Protocol\ProtocolInterface; |
| 22: | use Clansuite\Capture\ServerAddress; |
| 23: | use Clansuite\Capture\ServerInfo; |
| 24: | use Clansuite\ServerQuery\CSQuery; |
| 25: | use Override; |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | class Satisfactory extends CSQuery implements ProtocolInterface |
| 35: | { |
| 36: | |
| 37: | |
| 38: | |
| 39: | public string $name = 'Satisfactory'; |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | public array $supportedGames = ['Satisfactory']; |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | public string $protocol = 'Satisfactory'; |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | public array $game_series_list = ['Satisfactory']; |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | protected int $port_diff = 0; |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | public function __construct(?string $address = null, ?int $queryport = null) |
| 74: | { |
| 75: | parent::__construct(); |
| 76: | $this->address = $address; |
| 77: | $this->queryport = $queryport ?? 15777; |
| 78: | } |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | #[Override] |
| 92: | public function query_server(bool $getPlayers = true, bool $getRules = true): bool |
| 93: | { |
| 94: | if ($this->online) { |
| 95: | $this->reset(); |
| 96: | } |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | $cookie = time(); |
| 106: | $payload = pack('VV', $cookie & 0xFFFFFFFF, ($cookie >> 32) & 0xFFFFFFFF); |
| 107: | |
| 108: | $message = pack('v', 0xF6D5) . |
| 109: | pack('C', 0) . |
| 110: | pack('C', 1) . |
| 111: | $payload . |
| 112: | pack('C', 0x1); |
| 113: | |
| 114: | $address = (string) $this->address; |
| 115: | $port = (int) $this->queryport; |
| 116: | |
| 117: | if (($result = $this->sendCommand($address, $port, $message)) === '' || ($result = $this->sendCommand($address, $port, $message)) === '0' || ($result = $this->sendCommand($address, $port, $message)) === false) { |
| 118: | $this->errstr = 'No reply received'; |
| 119: | |
| 120: | return false; |
| 121: | } |
| 122: | |
| 123: | |
| 124: | if (!$this->parseResponse($result)) { |
| 125: | return false; |
| 126: | } |
| 127: | |
| 128: | $this->online = true; |
| 129: | |
| 130: | return true; |
| 131: | } |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | #[Override] |
| 147: | public function query(ServerAddress $addr): ServerInfo |
| 148: | { |
| 149: | $this->address = $addr->ip; |
| 150: | $this->queryport = $addr->port; |
| 151: | $this->query_server(); |
| 152: | |
| 153: | return new ServerInfo( |
| 154: | address: $this->address, |
| 155: | queryport: $this->queryport, |
| 156: | online: $this->online, |
| 157: | gamename: $this->gamename, |
| 158: | gameversion: $this->gameversion, |
| 159: | servertitle: $this->servertitle, |
| 160: | mapname: $this->mapname, |
| 161: | gametype: $this->gametype, |
| 162: | numplayers: $this->numplayers, |
| 163: | maxplayers: $this->maxplayers, |
| 164: | rules: $this->rules, |
| 165: | players: $this->players, |
| 166: | errstr: $this->errstr, |
| 167: | ); |
| 168: | } |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | #[Override] |
| 176: | public function getProtocolName(): string |
| 177: | { |
| 178: | return $this->protocol; |
| 179: | } |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | #[Override] |
| 189: | public function getVersion(ServerInfo $info): string |
| 190: | { |
| 191: | return $info->gameversion ?? 'unknown'; |
| 192: | } |
| 193: | |
| 194: | |
| 195: | |
| 196: | |
| 197: | |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | protected function parseResponse(string $data): bool |
| 204: | { |
| 205: | if (strlen($data) < 5) { |
| 206: | $this->errstr = 'Response too short'; |
| 207: | |
| 208: | return false; |
| 209: | } |
| 210: | |
| 211: | |
| 212: | $tmp = @unpack('v', substr($data, 0, 2)); |
| 213: | |
| 214: | if (!is_array($tmp) || !isset($tmp[1])) { |
| 215: | $this->errstr = 'Invalid response unpack'; |
| 216: | |
| 217: | return false; |
| 218: | } |
| 219: | $magic = $tmp[1]; |
| 220: | |
| 221: | if ($magic !== 0xF6D5) { |
| 222: | $this->errstr = 'Invalid protocol magic'; |
| 223: | |
| 224: | return false; |
| 225: | } |
| 226: | |
| 227: | $tmp = @unpack('C', substr($data, 2, 1)); |
| 228: | |
| 229: | if (!is_array($tmp) || !isset($tmp[1])) { |
| 230: | $this->errstr = 'Invalid response unpack'; |
| 231: | |
| 232: | return false; |
| 233: | } |
| 234: | $messageType = $tmp[1]; |
| 235: | |
| 236: | if ($messageType !== 1) { |
| 237: | $this->errstr = 'Unexpected message type'; |
| 238: | |
| 239: | return false; |
| 240: | } |
| 241: | |
| 242: | $tmp = @unpack('C', substr($data, 3, 1)); |
| 243: | |
| 244: | if (!is_array($tmp) || !isset($tmp[1])) { |
| 245: | $this->errstr = 'Invalid response unpack'; |
| 246: | |
| 247: | return false; |
| 248: | } |
| 249: | $protocolVersion = $tmp[1]; |
| 250: | |
| 251: | if ($protocolVersion !== 1) { |
| 252: | $this->errstr = 'Unsupported protocol version'; |
| 253: | |
| 254: | return false; |
| 255: | } |
| 256: | |
| 257: | $offset = 4; |
| 258: | $offset += 8; |
| 259: | |
| 260: | |
| 261: | $tmp = @unpack('C', substr($data, $offset, 1)); |
| 262: | |
| 263: | if (!is_array($tmp) || !isset($tmp[1])) { |
| 264: | $this->errstr = 'Invalid response unpack'; |
| 265: | |
| 266: | return false; |
| 267: | } |
| 268: | $serverState = $tmp[1]; |
| 269: | $offset++; |
| 270: | |
| 271: | |
| 272: | $tmp = @unpack('V', substr($data, $offset, 4)); |
| 273: | |
| 274: | if (!is_array($tmp) || !isset($tmp[1])) { |
| 275: | $this->errstr = 'Invalid response unpack'; |
| 276: | |
| 277: | return false; |
| 278: | } |
| 279: | $serverNetCL = $tmp[1]; |
| 280: | $offset += 4; |
| 281: | $offset += 8; |
| 282: | |
| 283: | |
| 284: | $tmp = @unpack('C', substr($data, $offset, 1)); |
| 285: | |
| 286: | if (!is_array($tmp) || !isset($tmp[1])) { |
| 287: | $this->errstr = 'Invalid response unpack'; |
| 288: | |
| 289: | return false; |
| 290: | } |
| 291: | $numSubStates = $tmp[1]; |
| 292: | $offset++; |
| 293: | |
| 294: | |
| 295: | for ($i = 0; $i < $numSubStates; $i++) { |
| 296: | |
| 297: | $tmp = @unpack('C', substr($data, $offset, 1)); |
| 298: | |
| 299: | if (!is_array($tmp) || !isset($tmp[1])) { |
| 300: | $this->errstr = 'Invalid response unpack (substate id)'; |
| 301: | |
| 302: | return false; |
| 303: | } |
| 304: | $subStateId = $tmp[1]; |
| 305: | $offset++; |
| 306: | |
| 307: | |
| 308: | $tmp = @unpack('v', substr($data, $offset, 2)); |
| 309: | |
| 310: | if (!is_array($tmp) || !isset($tmp[1])) { |
| 311: | $this->errstr = 'Invalid response unpack (substate version)'; |
| 312: | |
| 313: | return false; |
| 314: | } |
| 315: | $subStateVersion = $tmp[1]; |
| 316: | $offset += 2; |
| 317: | } |
| 318: | |
| 319: | |
| 320: | $tmp = @unpack('v', substr($data, $offset, 2)); |
| 321: | |
| 322: | if (!is_array($tmp) || !isset($tmp[1])) { |
| 323: | $this->errstr = 'Invalid response unpack'; |
| 324: | |
| 325: | return false; |
| 326: | } |
| 327: | $serverNameLength = $tmp[1]; |
| 328: | $offset += 2; |
| 329: | |
| 330: | |
| 331: | $serverName = substr($data, $offset, $serverNameLength); |
| 332: | $offset += $serverNameLength; |
| 333: | |
| 334: | |
| 335: | if ($offset < strlen($data)) { |
| 336: | $tmp = @unpack('C', substr($data, $offset, 1)); |
| 337: | |
| 338: | if (!is_array($tmp) || !isset($tmp[1])) { |
| 339: | $this->errstr = 'Invalid response unpack (terminator)'; |
| 340: | |
| 341: | return false; |
| 342: | } |
| 343: | $terminator = $tmp[1]; |
| 344: | |
| 345: | if ($terminator !== 0x1) { |
| 346: | $this->errstr = 'Invalid terminator'; |
| 347: | |
| 348: | return false; |
| 349: | } |
| 350: | } |
| 351: | |
| 352: | |
| 353: | $this->servertitle = $serverName; |
| 354: | $this->gamename = 'Satisfactory'; |
| 355: | $this->gameversion = (string) $serverNetCL; |
| 356: | |
| 357: | |
| 358: | |
| 359: | if ($serverState === 3 || $serverState === 1) { |
| 360: | $this->online = true; |
| 361: | } else { |
| 362: | $this->online = false; |
| 363: | } |
| 364: | |
| 365: | |
| 366: | $this->numplayers = 0; |
| 367: | $this->maxplayers = 0; |
| 368: | $this->mapname = ''; |
| 369: | $this->gametype = ''; |
| 370: | $this->password = -1; |
| 371: | |
| 372: | return true; |
| 373: | } |
| 374: | } |
| 375: | |