Skip to content

Commit 82e5da4

Browse files
committed
fix(info_game): refactor key value pairs parsing
split out space and equals delimited
1 parent 65e3a93 commit 82e5da4

File tree

1 file changed

+53
-29
lines changed

1 file changed

+53
-29
lines changed

lgsm/modules/info_game.sh

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ fn_info_game_ini() {
3434
configtype="ini"
3535
}
3636

37-
# Config Type: custom
37+
# Config Type: Key Value Pairs (Equals Delimited)
3838
# Comment: ; or #
39-
# Note: this ini filter does not filter by section. Can cause issues with some games that have multiple sections with the same variable name.
40-
fn_info_game_keyvalue_pairs() {
39+
fn_info_game_keyvalue_pairs_equals() {
4140
# sed is used to process the file.
4241
# -n: Suppresses automatic printing of pattern space.
4342
# /^\<'"${2}"'\>/: Matches lines starting with the word provided as the second argument ($2), considering it as a whole word.
@@ -60,6 +59,31 @@ fn_info_game_keyvalue_pairs() {
6059
configtype="keyvalue_pairs"
6160
}
6261

62+
# Config Type: Key Value Pairs (Space Delimited)
63+
# Comment: ; or #
64+
fn_info_game_keyvalue_pairs_space() {
65+
# sed is used to process the file.
66+
# -n: Suppresses automatic printing of pattern space.
67+
# /^\<'"${2}"'\>/: Matches lines starting with the word provided as the second argument ($2), considering it as a whole word.
68+
# { s/.* *"\?\([^"]*\)"\?/\1/p;q }: Command block executed for lines that match the pattern.
69+
# - s/.* *"\?\([^"]*\)"\?/\1/: Matches and captures the value after an space ( ), possibly surrounded by optional double quotes.
70+
# - .*: Matches any characters before the space.
71+
# - = *"\?: Matches the space and any optional spaces before an optional double quote.
72+
# - \([^"]*\): Captures any characters that are not double quotes.
73+
# - "\?: Matches an optional double quote.
74+
# - /1: Replaces the entire matched pattern with the captured value.
75+
# - p: Prints the modified line.
76+
# - q: Quits processing after modifying and printing the line.
77+
78+
if [ -n "${3}" ]; then
79+
servercfgparse="${3}"
80+
else
81+
servercfgparse="${servercfgfullpath}"
82+
fi
83+
eval "${1}=\"$(sed -n '/^\<'"${2}"'\>/ { s/.* *\"\?\([^"]*\)\"\?/\1/p;q }' "${servercfgparse}" | tr -d '\r')\""
84+
configtype="keyvalue_pairs"
85+
}
86+
6387
# Config Type: QuakeC
6488
# Comment: // or /* */
6589
fn_info_game_quakec() {
@@ -896,11 +920,11 @@ fn_info_game_armar() {
896920
# Filetype: con
897921
fn_info_game_bf1942() {
898922
if [ -f "${servercfgfullpath}" ]; then
899-
fn_info_game_keyvalue_pairs "configip" "game.serverIp"
900-
fn_info_game_keyvalue_pairs "maxplayers" "game.serverMaxPlayers"
901-
fn_info_game_keyvalue_pairs "port" "game.serverPort"
902-
fn_info_game_keyvalue_pairs "servername" "game.serverName"
903-
fn_info_game_keyvalue_pairs "serverpassword" "game.serverPassword"
923+
fn_info_game_keyvalue_pairs_space "configip" "game.serverIP"
924+
fn_info_game_keyvalue_pairs_space "maxplayers" "game.serverMaxPlayers"
925+
fn_info_game_keyvalue_pairs_space "port" "game.serverPort"
926+
fn_info_game_keyvalue_pairs_space "servername" "game.serverName"
927+
fn_info_game_keyvalue_pairs_space "serverpassword" "game.serverPassword"
904928
fi
905929
configip="${configip:-"0.0.0.0"}"
906930
maxplayers="${maxplayers:-"0"}"
@@ -917,11 +941,11 @@ fn_info_game_bf1942() {
917941
# Filetype: con
918942
fn_info_game_bfv() {
919943
if [ -f "${servercfgfullpath}" ]; then
920-
fn_info_game_keyvalue_pairs "configip" "game.serverIp"
921-
fn_info_game_keyvalue_pairs "maxplayers" "game.serverMaxPlayers"
922-
fn_info_game_keyvalue_pairs "port" "game.serverPort"
923-
fn_info_game_keyvalue_pairs "servername" "game.serverName"
924-
fn_info_game_keyvalue_pairs "serverpassword" "game.serverPassword"
944+
fn_info_game_keyvalue_pairs_space "configip" "game.serverIp"
945+
fn_info_game_keyvalue_pairs_space "maxplayers" "game.serverMaxPlayers"
946+
fn_info_game_keyvalue_pairs_space "port" "game.serverPort"
947+
fn_info_game_keyvalue_pairs_space "servername" "game.serverName"
948+
fn_info_game_keyvalue_pairs_space "serverpassword" "game.serverPassword"
925949
fi
926950
configip="${configip:-"0.0.0.0"}"
927951
maxplayers="${maxplayers:-"0"}"
@@ -1750,14 +1774,14 @@ fn_info_game_rust() {
17501774

17511775
fn_info_game_rw() {
17521776
if [ -f "${servercfgfullpath}" ]; then
1753-
fn_info_game_keyvalue_pairs "configip" "Server_IP"
1754-
fn_info_game_keyvalue_pairs "gamemode" "World_GameMode"
1755-
fn_info_game_keyvalue_pairs "maxplayers" "Server_MaxPlayers"
1756-
fn_info_game_keyvalue_pairs "port" "Server_Port"
1757-
fn_info_game_keyvalue_pairs "rconport" "RCON_Port"
1758-
fn_info_game_keyvalue_pairs "seed" "World_Seed"
1759-
fn_info_game_keyvalue_pairs "servername" "Server_Name"
1760-
fn_info_game_keyvalue_pairs "worldname" "World_Name"
1777+
fn_info_game_keyvalue_pairs_equals "configip" "Server_IP"
1778+
fn_info_game_keyvalue_pairs_equals "gamemode" "World_GameMode"
1779+
fn_info_game_keyvalue_pairs_equals "maxplayers" "Server_MaxPlayers"
1780+
fn_info_game_keyvalue_pairs_equals "port" "Server_Port"
1781+
fn_info_game_keyvalue_pairs_equals "rconport" "RCON_Port"
1782+
fn_info_game_keyvalue_pairs_equals "seed" "World_Seed"
1783+
fn_info_game_keyvalue_pairs_equals "servername" "Server_Name"
1784+
fn_info_game_keyvalue_pairs_equals "worldname" "World_Name"
17611785
fi
17621786
configip="${configip:-"0.0.0.0"}"
17631787
gamemode="${gamemode:-"NOT SET"}"
@@ -1994,12 +2018,12 @@ fn_info_game_spark() {
19942018
# Filetype: cfg
19952019
fn_info_game_squad() {
19962020
if [ -f "${servercfgfullpath}" ]; then
1997-
fn_info_game_keyvalue_pairs "servername" "ServerName"
1998-
fn_info_game_keyvalue_pairs "maxplayers" "MaxPlayers"
2021+
fn_info_game_keyvalue_pairs_equals "servername" "ServerName"
2022+
fn_info_game_keyvalue_pairs_equals "maxplayers" "MaxPlayers"
19992023
fi
20002024
if [ -f "${servercfgdir}/Rcon.cfg" ]; then
2001-
fn_info_game_keyvalue_pairs "rconport" "Port" "${servercfgdir}/Rcon.cfg"
2002-
fn_info_game_keyvalue_pairs "rconpassword" "Password" "${servercfgdir}/Rcon.cfg"
2025+
fn_info_game_keyvalue_pairs_equals "rconport" "Port" "${servercfgdir}/Rcon.cfg"
2026+
fn_info_game_keyvalue_pairs_equals "rconpassword" "Password" "${servercfgdir}/Rcon.cfg"
20032027
fi
20042028
maxplayers="${maxplayers:-"0"}"
20052029
port="${port:-"0"}"
@@ -2017,10 +2041,10 @@ fn_info_game_squad() {
20172041
# Filetype: cfg
20182042
fn_info_game_terraria() {
20192043
if [ -f "${servercfgfullpath}" ]; then
2020-
fn_info_game_keyvalue_pairs "maxplayers" "maxplayers"
2021-
fn_info_game_keyvalue_pairs "port" "port"
2022-
fn_info_game_keyvalue_pairs "servername" "worldname"
2023-
fn_info_game_keyvalue_pairs "worldname" "world"
2044+
fn_info_game_keyvalue_pairs_equals "maxplayers" "maxplayers"
2045+
fn_info_game_keyvalue_pairs_equals "port" "port"
2046+
fn_info_game_keyvalue_pairs_equals "servername" "worldname"
2047+
fn_info_game_keyvalue_pairs_equals "worldname" "world"
20242048
fi
20252049
queryport="${port:-"0"}"
20262050
servername="${servername:-"NOT SET"}"

0 commit comments

Comments
 (0)