diff --git a/docs/tests/protocols/test_unreal2/test_get_details.rst b/docs/tests/protocols/test_unreal2/test_get_details.rst index af17f42..652c89e 100644 --- a/docs/tests/protocols/test_unreal2/test_get_details.rst +++ b/docs/tests/protocols/test_unreal2/test_get_details.rst @@ -11,11 +11,11 @@ Here are the results for the test method. "game_port": 9980, "query_port": 0, "server_name": "liunai.lt|v1065]|OE/Suicidal/Hard150 LvL| Perks", - "map_name": "", - "game_type": "KF-Arcade-Shopping-4-Hallways", - "num_players": 1195592971, - "max_players": 1415933281, - "ping": 6647929, - "flags": 2, + "map_name": "KF-MysteriousTrees-FIX", + "game_type": "MCGameType", + "num_players": 3, + "max_players": 15, + "ping": 3, + "flags": 10, "skill": "" } diff --git a/docs/tests/protocols/test_unreal2/test_get_players.rst b/docs/tests/protocols/test_unreal2/test_get_players.rst index 19ee94e..ca2db29 100644 --- a/docs/tests/protocols/test_unreal2/test_get_players.rst +++ b/docs/tests/protocols/test_unreal2/test_get_players.rst @@ -7,17 +7,24 @@ Here are the results for the test method. [ { - "id": 139, - "name": "[RUS]Fluffi", - "ping": 76, - "score": 4067, + "id": 974, + "name": "AraMis", + "ping": 192, + "score": 2000, "stats_id": 536870912 }, { - "id": 1, - "name": "Pristine_Gezzy", - "ping": 52, - "score": 15896, + "id": 581, + "name": "[RUS]SKOLSKIY", + "ping": 84, + "score": 767, + "stats_id": 536870912 + }, + { + "id": 580, + "name": "NozSoz", + "ping": 72, + "score": 2002, "stats_id": 536870912 } ] diff --git a/opengsq/protocols/unreal2.py b/opengsq/protocols/unreal2.py index 1b30cec..c58e8fa 100644 --- a/opengsq/protocols/unreal2.py +++ b/opengsq/protocols/unreal2.py @@ -22,10 +22,13 @@ class Unreal2(ProtocolBase): _RULES = 0x01 _PLAYERS = 0x02 - async def get_details(self) -> Status: + async def get_details(self, strip_color=True) -> Status: """ Asynchronously gets the details of a server. + Args: + strip_color (bool, optional): If True, strips color codes from the server name. Defaults to True. + Returns: Status: A Status object containing the details of the server. """ @@ -49,7 +52,7 @@ async def get_details(self) -> Status: server_ip=br.read_string(), game_port=br.read_long(), query_port=br.read_long(), - server_name=self._read_string(br), + server_name=self._read_string(br, strip_color), map_name=self._read_string(br), game_type=self._read_string(br), num_players=br.read_long(), @@ -132,7 +135,7 @@ async def get_players(self) -> list[Player]: return players @staticmethod - def strip_colors(text: str) -> str: + def strip_color(text: str) -> str: """ Strips color codes from a string. @@ -144,7 +147,7 @@ def strip_colors(text: str) -> str: """ return re.compile("\x1b...|[\x00-\x1a]").sub("", text) - def _read_string(self, br: BinaryReader): + def _read_string(self, br: BinaryReader, strip_color=False): """ Reads a string from a BinaryReader object. @@ -161,7 +164,17 @@ def _read_string(self, br: BinaryReader): length = (length & 0x7F) * 2 encoding = "utf-16" - return Unreal2.strip_colors(br.read_bytes(length).decode(encoding, "ignore")) + string = br.read_bytes(length).decode(encoding, "ignore") + + if strip_color: + string = Unreal2.strip_color(string) + else: + string = string.strip("\x00") + + if not br.is_end() and br.read_byte() != 0: + br.stream_position -= 1 + + return string if __name__ == "__main__": @@ -169,7 +182,8 @@ def _read_string(self, br: BinaryReader): async def main_async(): # ut2004 - unreal2 = Unreal2(host="109.230.224.189", port=6970) + # unreal2 = Unreal2(host="109.230.224.189", port=6970) + unreal2 = Unreal2(host="51.195.117.236", port=9981) details = await unreal2.get_details() print(details) rules = await unreal2.get_rules()