Explorar el Código

fix(info_game): refactor key value pairs parsing

split out space and equals delimited
Daniel Gibbs hace 2 años
padre
commit
82e5da4b1d
Se han modificado 1 ficheros con 53 adiciones y 29 borrados
  1. 53 29
      lgsm/modules/info_game.sh

+ 53 - 29
lgsm/modules/info_game.sh

@@ -34,10 +34,9 @@ fn_info_game_ini() {
 	configtype="ini"
 }
 
-# Config Type: custom
+# Config Type: Key Value Pairs (Equals Delimited)
 # Comment: ; or #
-# Note: this ini filter does not filter by section. Can cause issues with some games that have multiple sections with the same variable name.
-fn_info_game_keyvalue_pairs() {
+fn_info_game_keyvalue_pairs_equals() {
 	# sed is used to process the file.
 	# -n: Suppresses automatic printing of pattern space.
 	# /^\<'"${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() {
 	configtype="keyvalue_pairs"
 }
 
+# Config Type: Key Value Pairs (Space Delimited)
+# Comment: ; or #
+fn_info_game_keyvalue_pairs_space() {
+	# sed is used to process the file.
+	# -n: Suppresses automatic printing of pattern space.
+	# /^\<'"${2}"'\>/: Matches lines starting with the word provided as the second argument ($2), considering it as a whole word.
+	# { s/.*  *"\?\([^"]*\)"\?/\1/p;q }: Command block executed for lines that match the pattern.
+	#   - s/.*  *"\?\([^"]*\)"\?/\1/: Matches and captures the value after an space ( ), possibly surrounded by optional double quotes.
+	#     - .*: Matches any characters before the space.
+	#     - = *"\?: Matches the space and any optional spaces before an optional double quote.
+	#     - \([^"]*\): Captures any characters that are not double quotes.
+	#     - "\?: Matches an optional double quote.
+	#     - /1: Replaces the entire matched pattern with the captured value.
+	#   - p: Prints the modified line.
+	#   - q: Quits processing after modifying and printing the line.
+
+	if [ -n "${3}" ]; then
+		servercfgparse="${3}"
+	else
+		servercfgparse="${servercfgfullpath}"
+	fi
+	eval "${1}=\"$(sed -n '/^\<'"${2}"'\>/ { s/.*  *\"\?\([^"]*\)\"\?/\1/p;q }' "${servercfgparse}" | tr -d '\r')\""
+	configtype="keyvalue_pairs"
+}
+
 # Config Type: QuakeC
 # Comment: // or /* */
 fn_info_game_quakec() {
@@ -896,11 +920,11 @@ fn_info_game_armar() {
 # Filetype: con
 fn_info_game_bf1942() {
 	if [ -f "${servercfgfullpath}" ]; then
-		fn_info_game_keyvalue_pairs "configip" "game.serverIp"
-		fn_info_game_keyvalue_pairs "maxplayers" "game.serverMaxPlayers"
-		fn_info_game_keyvalue_pairs "port" "game.serverPort"
-		fn_info_game_keyvalue_pairs "servername" "game.serverName"
-		fn_info_game_keyvalue_pairs "serverpassword" "game.serverPassword"
+		fn_info_game_keyvalue_pairs_space "configip" "game.serverIP"
+		fn_info_game_keyvalue_pairs_space "maxplayers" "game.serverMaxPlayers"
+		fn_info_game_keyvalue_pairs_space "port" "game.serverPort"
+		fn_info_game_keyvalue_pairs_space "servername" "game.serverName"
+		fn_info_game_keyvalue_pairs_space "serverpassword" "game.serverPassword"
 	fi
 	configip="${configip:-"0.0.0.0"}"
 	maxplayers="${maxplayers:-"0"}"
@@ -917,11 +941,11 @@ fn_info_game_bf1942() {
 # Filetype: con
 fn_info_game_bfv() {
 	if [ -f "${servercfgfullpath}" ]; then
-		fn_info_game_keyvalue_pairs "configip" "game.serverIp"
-		fn_info_game_keyvalue_pairs "maxplayers" "game.serverMaxPlayers"
-		fn_info_game_keyvalue_pairs "port" "game.serverPort"
-		fn_info_game_keyvalue_pairs "servername" "game.serverName"
-		fn_info_game_keyvalue_pairs "serverpassword" "game.serverPassword"
+		fn_info_game_keyvalue_pairs_space "configip" "game.serverIp"
+		fn_info_game_keyvalue_pairs_space "maxplayers" "game.serverMaxPlayers"
+		fn_info_game_keyvalue_pairs_space "port" "game.serverPort"
+		fn_info_game_keyvalue_pairs_space "servername" "game.serverName"
+		fn_info_game_keyvalue_pairs_space "serverpassword" "game.serverPassword"
 	fi
 	configip="${configip:-"0.0.0.0"}"
 	maxplayers="${maxplayers:-"0"}"
@@ -1750,14 +1774,14 @@ fn_info_game_rust() {
 
 fn_info_game_rw() {
 	if [ -f "${servercfgfullpath}" ]; then
-		fn_info_game_keyvalue_pairs "configip" "Server_IP"
-		fn_info_game_keyvalue_pairs "gamemode" "World_GameMode"
-		fn_info_game_keyvalue_pairs "maxplayers" "Server_MaxPlayers"
-		fn_info_game_keyvalue_pairs "port" "Server_Port"
-		fn_info_game_keyvalue_pairs "rconport" "RCON_Port"
-		fn_info_game_keyvalue_pairs "seed" "World_Seed"
-		fn_info_game_keyvalue_pairs "servername" "Server_Name"
-		fn_info_game_keyvalue_pairs "worldname" "World_Name"
+		fn_info_game_keyvalue_pairs_equals "configip" "Server_IP"
+		fn_info_game_keyvalue_pairs_equals "gamemode" "World_GameMode"
+		fn_info_game_keyvalue_pairs_equals "maxplayers" "Server_MaxPlayers"
+		fn_info_game_keyvalue_pairs_equals "port" "Server_Port"
+		fn_info_game_keyvalue_pairs_equals "rconport" "RCON_Port"
+		fn_info_game_keyvalue_pairs_equals "seed" "World_Seed"
+		fn_info_game_keyvalue_pairs_equals "servername" "Server_Name"
+		fn_info_game_keyvalue_pairs_equals "worldname" "World_Name"
 	fi
 	configip="${configip:-"0.0.0.0"}"
 	gamemode="${gamemode:-"NOT SET"}"
@@ -1994,12 +2018,12 @@ fn_info_game_spark() {
 # Filetype: cfg
 fn_info_game_squad() {
 	if [ -f "${servercfgfullpath}" ]; then
-		fn_info_game_keyvalue_pairs "servername" "ServerName"
-		fn_info_game_keyvalue_pairs "maxplayers" "MaxPlayers"
+		fn_info_game_keyvalue_pairs_equals "servername" "ServerName"
+		fn_info_game_keyvalue_pairs_equals "maxplayers" "MaxPlayers"
 	fi
 	if [ -f "${servercfgdir}/Rcon.cfg" ]; then
-		fn_info_game_keyvalue_pairs "rconport" "Port" "${servercfgdir}/Rcon.cfg"
-		fn_info_game_keyvalue_pairs "rconpassword" "Password" "${servercfgdir}/Rcon.cfg"
+		fn_info_game_keyvalue_pairs_equals "rconport" "Port" "${servercfgdir}/Rcon.cfg"
+		fn_info_game_keyvalue_pairs_equals "rconpassword" "Password" "${servercfgdir}/Rcon.cfg"
 	fi
 	maxplayers="${maxplayers:-"0"}"
 	port="${port:-"0"}"
@@ -2017,10 +2041,10 @@ fn_info_game_squad() {
 # Filetype: cfg
 fn_info_game_terraria() {
 	if [ -f "${servercfgfullpath}" ]; then
-		fn_info_game_keyvalue_pairs "maxplayers" "maxplayers"
-		fn_info_game_keyvalue_pairs "port" "port"
-		fn_info_game_keyvalue_pairs "servername" "worldname"
-		fn_info_game_keyvalue_pairs "worldname" "world"
+		fn_info_game_keyvalue_pairs_equals "maxplayers" "maxplayers"
+		fn_info_game_keyvalue_pairs_equals "port" "port"
+		fn_info_game_keyvalue_pairs_equals "servername" "worldname"
+		fn_info_game_keyvalue_pairs_equals "worldname" "world"
 	fi
 	queryport="${port:-"0"}"
 	servername="${servername:-"NOT SET"}"