|
| 1 | +import struct |
| 2 | + |
| 3 | +from opengsq.binary_reader import BinaryReader |
| 4 | +from opengsq.exceptions import InvalidPacketException |
| 5 | +from opengsq.protocol_base import ProtocolBase |
| 6 | +from opengsq.socket_async import SocketAsync |
| 7 | + |
| 8 | + |
| 9 | +class Satisfactory(ProtocolBase): |
| 10 | + """Satisfactory Protocol""" |
| 11 | + full_name = 'Satisfactory Protocol' |
| 12 | + |
| 13 | + async def get_status(self) -> dict: |
| 14 | + """ |
| 15 | + Retrieves information about the server including state, version, and beacon port |
| 16 | + Server state: 1 - Idle (no game loaded), 2 - currently loading or creating a game, 3 - currently in game |
| 17 | + """ |
| 18 | + # Credit: https://github.com/dopeghoti/SF-Tools/blob/main/Protocol.md |
| 19 | + |
| 20 | + # Send message id, protocol version |
| 21 | + request = struct.pack('2b', 0, 0) + 'opengsq'.encode() |
| 22 | + response = await SocketAsync.send_and_receive(self._address, self._query_port, self._timeout, request) |
| 23 | + br = BinaryReader(response) |
| 24 | + header = br.read_byte() |
| 25 | + |
| 26 | + if header != 1: |
| 27 | + raise InvalidPacketException('Packet header mismatch. Received: {}. Expected: {}.'.format(chr(header), chr(1))) |
| 28 | + |
| 29 | + br.read_byte() # Protocol version |
| 30 | + br.read_bytes(8) # Request data |
| 31 | + |
| 32 | + result = {} |
| 33 | + result['State'] = br.read_byte() |
| 34 | + result['Version'] = br.read_long() |
| 35 | + result['BeaconPort'] = br.read_short() |
| 36 | + |
| 37 | + return result |
| 38 | + |
| 39 | + |
| 40 | +if __name__ == '__main__': |
| 41 | + import asyncio |
| 42 | + import json |
| 43 | + |
| 44 | + async def main_async(): |
| 45 | + satisfactory = Satisfactory(address='delta3.ptse.host', query_port=15777, timeout=5.0) |
| 46 | + status = await satisfactory.get_status() |
| 47 | + print(json.dumps(status, indent=None) + '\n') |
| 48 | + |
| 49 | + asyncio.run(main_async()) |
0 commit comments