Skip to content

Commit 9fa1960

Browse files
Refactor and clean up protocol implementations
Refactored multiple protocol files for improved readability and consistency, including formatting, string handling, and error management. Removed unused imports, standardized byte and string operations, and enhanced code clarity across AoE1, AoE2, CoD1, CoD4, CoD5, DirectPlay, and related modules. No functional changes were made to protocol logic.
1 parent 10112a7 commit 9fa1960

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1726
-1359
lines changed

opengsq/binary_reader.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def prepend_bytes(self, data):
1616
self.__data = data + self.__data
1717

1818
def read(self) -> bytes:
19-
return self.__data[self.stream_position:]
19+
return self.__data[self.stream_position :]
2020

2121
def read_byte(self) -> int:
2222
data = self.__data[self.stream_position]
@@ -25,39 +25,49 @@ def read_byte(self) -> int:
2525
return data
2626

2727
def read_bytes(self, count: int) -> bytes:
28-
data = self.__data[self.stream_position:self.stream_position + count]
28+
data = self.__data[self.stream_position : self.stream_position + count]
2929
self.stream_position += count
3030

3131
return data
3232

3333
def read_short(self, unsigned=True) -> int:
34-
format = 'H' if unsigned else 'h'
35-
data = struct.unpack(f'<{format}', self.__data[self.stream_position:self.stream_position + 2])[0]
34+
format = "H" if unsigned else "h"
35+
data = struct.unpack(
36+
f"<{format}", self.__data[self.stream_position : self.stream_position + 2]
37+
)[0]
3638
self.stream_position += 2
3739

3840
return data
3941

4042
def read_long(self, unsigned=False) -> int:
41-
format = 'L' if unsigned else 'l'
42-
data = struct.unpack(f'<{format}', self.__data[self.stream_position:self.stream_position + 4])[0]
43+
format = "L" if unsigned else "l"
44+
data = struct.unpack(
45+
f"<{format}", self.__data[self.stream_position : self.stream_position + 4]
46+
)[0]
4347
self.stream_position += 4
4448

4549
return data
4650

4751
def read_long_long(self) -> int:
48-
data = struct.unpack('<q', self.__data[self.stream_position:self.stream_position + 8])[0]
52+
data = struct.unpack(
53+
"<q", self.__data[self.stream_position : self.stream_position + 8]
54+
)[0]
4955
self.stream_position += 8
5056

5157
return data
5258

5359
def read_float(self) -> float:
54-
data = struct.unpack('<f', self.__data[self.stream_position:self.stream_position + 4])[0]
60+
data = struct.unpack(
61+
"<f", self.__data[self.stream_position : self.stream_position + 4]
62+
)[0]
5563
self.stream_position += 4
5664

5765
return data
5866

59-
def read_string(self, delimiters=[b'\x00'], encoding='utf-8', errors='ignore') -> str:
60-
bytes_string = b''
67+
def read_string(
68+
self, delimiters=[b"\x00"], encoding="utf-8", errors="ignore"
69+
) -> str:
70+
bytes_string = b""
6171

6272
while self.remaining_bytes() > 0:
6373
stream_byte = bytes([self.read_byte()])
@@ -69,7 +79,9 @@ def read_string(self, delimiters=[b'\x00'], encoding='utf-8', errors='ignore') -
6979

7080
return str(bytes_string, encoding=encoding, errors=errors)
7181

72-
def read_pascal_string(self, encoding='utf-8', errors='ignore'):
82+
def read_pascal_string(self, encoding="utf-8", errors="ignore"):
7383
length = self.read_byte()
74-
pascal_string = str(self.read_bytes(length - 1), encoding=encoding, errors=errors)
84+
pascal_string = str(
85+
self.read_bytes(length - 1), encoding=encoding, errors=errors
86+
)
7587
return pascal_string

opengsq/protocol_socket.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ class SocketKind(Enum):
1010
SOCK_DGRAM = auto()
1111

1212

13-
class Socket():
13+
class Socket:
1414
@staticmethod
1515
async def gethostbyname(hostname: str):
16-
return await asyncio.get_running_loop().run_in_executor(None, socket.gethostbyname, hostname)
16+
return await asyncio.get_running_loop().run_in_executor(
17+
None, socket.gethostbyname, hostname
18+
)
1719

1820
class Protocol(asyncio.Protocol):
1921
def __init__(self, timeout: float):
@@ -78,13 +80,17 @@ async def __connect(self, remote_addr):
7880
lambda: self.__protocol,
7981
host=remote_addr[0],
8082
port=remote_addr[1],
81-
local_addr=('0.0.0.0', self.__local_port) if self.__local_port else None
83+
local_addr=("0.0.0.0", self.__local_port)
84+
if self.__local_port
85+
else None,
8286
)
8387
else:
8488
self.__transport, _ = await loop.create_datagram_endpoint(
8589
lambda: self.__protocol,
8690
remote_addr=remote_addr,
87-
local_addr=('0.0.0.0', self.__local_port) if self.__local_port else None
91+
local_addr=("0.0.0.0", self.__local_port)
92+
if self.__local_port
93+
else None,
8894
)
8995

9096
def close(self):
@@ -156,8 +162,8 @@ async def communicate(protocol: ProtocolBase, data: bytes, source_port: int = No
156162
loop = asyncio.get_running_loop()
157163
transport, protocol_instance = await loop.create_datagram_endpoint(
158164
lambda: Socket.Protocol(protocol._timeout), # Use public Protocol class
159-
local_addr=('0.0.0.0', source_port if source_port else 0),
160-
allow_broadcast=protocol._allow_broadcast
165+
local_addr=("0.0.0.0", source_port if source_port else 0),
166+
allow_broadcast=protocol._allow_broadcast,
161167
)
162168

163169
try:
@@ -183,13 +189,15 @@ def __init__(self):
183189
super().__init__(SocketKind.SOCK_STREAM)
184190

185191

186-
if __name__ == '__main__':
192+
if __name__ == "__main__":
193+
187194
async def test_socket_async():
188195
with Socket() as socket_async:
189196
socket_async.settimeout(5)
190-
await socket_async.connect(('122.128.109.245', 27015))
197+
await socket_async.connect(("122.128.109.245", 27015))
191198
socket_async.send(
192-
b'\xFF\xFF\xFF\xFFTSource Engine Query\x00\xFF\xFF\xFF\xFF')
199+
b"\xff\xff\xff\xffTSource Engine Query\x00\xff\xff\xff\xff"
200+
)
193201
print(await socket_async.recv())
194202

195203
loop = asyncio.get_event_loop()

0 commit comments

Comments
 (0)