Skip to content

Commit

Permalink
fix: Remove formatting codes from loglines
Browse files Browse the repository at this point in the history
One user has reported the overlay not working when using a mod that
injected these next to usernames.
  • Loading branch information
Amund211 committed Jan 27, 2024
1 parent 39dcabd commit 5c3c02c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/prism/overlay/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@


RANK_REGEX = re.compile(r"\[[a-zA-Z\+]+\] ")
# Minecraft formatting codes (paragraph sign + hex digit(color)/klmnor(formatting))
COLOR_REGEX = re.compile(r"§[0-9a-fklmnor]")


PUNCTUATION_AND_WHITESPACE = ".!:, \t"

Expand Down Expand Up @@ -59,6 +62,11 @@ def strip_until(line: str, *, until: str) -> str:
return line[line.index(until) + len(until) :].strip()


def remove_colors(string: str) -> str:
"""Remove all color codes from a string"""
return COLOR_REGEX.sub("", string)


def remove_ranks(playerstring: str) -> str:
"""Remove all ranks from a string"""
return RANK_REGEX.sub("", playerstring)
Expand Down Expand Up @@ -210,7 +218,7 @@ def parse_chat_message(message: str) -> ChatEvent | None:
# Use lazy printf-style formatting because this message is very common
logger.debug("Chat message: '%s'", message)

message = remove_deduplication_suffix(message)
message = remove_colors(remove_deduplication_suffix(message))

if message.startswith(WHO_PREFIX):
# Info [CHAT] ONLINE: <username1>, <username2>, ..., <usernameN>
Expand Down
33 changes: 33 additions & 0 deletions tests/prism/overlay/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
get_highest_index,
get_lowest_index,
parse_logline,
remove_colors,
remove_deduplication_suffix,
remove_ranks,
strip_until,
Expand Down Expand Up @@ -75,6 +76,33 @@ def test_remove_ranks(rank_string: str, name_string: str) -> None:
assert remove_ranks(rank_string) == name_string


@pytest.mark.parametrize(
"string, expected",
(
("Player1", "Player1"),
*((f"§{char}Player1", "Player1") for char in "0123456789abcdefklmnor"),
*((f"\u00A7{char}Player1", "Player1") for char in "0123456789abcdefklmnor"),
(
"§kPlayer1, §aPlayer2, §bPlayer3, §dPlayer4, §7Player5, §fPlayer6",
"Player1, Player2, Player3, Player4, Player5, Player6",
),
# Rejected
*((f"&{char}Player1", f"&{char}Player1") for char in "0123456789abcdefklmnor"),
*(
(f"§{char}Player1", f"§{char}Player1")
for char in "ghijpqstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
),
*(
(f"\u00A7{char}Player1", f"\u00A7{char}Player1")
for char in "ghijpqstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
),
),
)
def test_remove_colors(string: str, expected: str) -> None:
"""Assert that remove_colors functions properly"""
assert remove_colors(string) == expected


@pytest.mark.parametrize(
"source, substrings, result",
(
Expand Down Expand Up @@ -427,6 +455,11 @@ def test_remove_deduplication_suffix(message: str, result: str) -> None:
)
for count in range(2, 15)
),
(
# Lobby list with colors added from mod (constructed logline)
"[15:03:53] [Client thread/INFO]: [CHAT] ONLINE: §7Player1, §7Player2, §cPlayer3, §7Player4, §7Player5, §7Player6",
LobbyListEvent(usernames=[f"Player{i}" for i in range(1, 7)]),
),
(
# Lobby swap on vanilla
"[Info: 2022-01-07 13:44:01.231411580: GameCallbacks.cpp(162)] Game/avt (Client thread) Info [CHAT] Sending you to mini733G!",
Expand Down

0 comments on commit 5c3c02c

Please sign in to comment.