Skip to content

Commit

Permalink
Resolve Unreal2 Protocol Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
BattlefieldDuck committed Feb 2, 2024
1 parent 7096598 commit 07dcee6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
12 changes: 6 additions & 6 deletions docs/tests/protocols/test_unreal2/test_get_details.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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": ""
}
23 changes: 15 additions & 8 deletions docs/tests/protocols/test_unreal2/test_get_players.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
26 changes: 20 additions & 6 deletions opengsq/protocols/unreal2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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(),
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -161,15 +164,26 @@ 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__":
import asyncio

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()
Expand Down

0 comments on commit 07dcee6

Please sign in to comment.