Skip to content

Commit

Permalink
Resolve Source Engine Protocol issue
Browse files Browse the repository at this point in the history
Correct the issue where the GetRules function doesn’t work when the data is compressed.
  • Loading branch information
BattlefieldDuck committed Feb 7, 2024
1 parent f4b0183 commit bc174c3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
5 changes: 3 additions & 2 deletions opengsq/binary_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ def read_short(self, unsigned=True) -> int:

return data

def read_long(self) -> int:
data = struct.unpack('<l', self.__data[self.stream_position:self.stream_position + 4])[0]
def read_long(self, unsigned=False) -> int:
format = 'L' if unsigned else 'l'
data = struct.unpack(f'<{format}', self.__data[self.stream_position:self.stream_position + 4])[0]
self.stream_position += 4

return data
Expand Down
25 changes: 13 additions & 12 deletions opengsq/protocols/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ async def __receive(self, udpClient: UdpClient) -> bytes:

# Packet id
id = br.read_long()
is_compressed = (id & 0x80000000) < 0
is_compressed = id & 0x80000000 != 0
print(is_compressed)

# Check is GoldSource multi-packet response format
if self.__is_gold_source_split(BinaryReader(br.read())):
Expand All @@ -290,15 +291,15 @@ async def __receive(self, udpClient: UdpClient) -> bytes:
# The number of the packet
number = br.read_byte()

# Packet size
br.read_short()

if number == 0 and is_compressed:
# Decompressed size
br.read_long()
br.read_long(unsigned=True)

# CRC32 sum
crc32_checksum = br.read_long()
crc32_checksum = br.read_long(unsigned=True)
else:
# Packet size
br.read_short()

payloads[number] = br.read()

Expand Down Expand Up @@ -377,13 +378,13 @@ async def __parse_gold_source_packet(self, udpClient: UdpClient, packets: list):
import asyncio

async def main_async():
source = Source(host="45.62.160.71", port=27015, timeout=5.0)
info = await source.get_info()
print(info)
source = Source(host="146.19.87.161", port=27015, timeout=5.0)
# info = await source.get_info()
# print(info)

await asyncio.sleep(1)
players = await source.get_players()
print(players)
# await asyncio.sleep(1)
# players = await source.get_players()
# print(players)

await asyncio.sleep(1)
rules = await source.get_rules()
Expand Down

0 comments on commit bc174c3

Please sign in to comment.