浏览代码

refactor(workshop): edit Barotrauma config_player.xml with sed instead of Python (#5017)

Daniel Gibbs 1 周之前
父节点
当前提交
f3026d3143
共有 1 个文件被更改,包括 8 次插入 和 19 次删除
  1. 8 19
      lgsm/modules/workshop_core.sh

+ 8 - 19
lgsm/modules/workshop_core.sh

@@ -404,34 +404,23 @@ fn_workshop_remove() {
 # Barotrauma only loads mods listed in config_player.xml. Replaces LinuxGSM's entries with the installed mods, in order.
 fn_workshop_bt_packages() {
 	local config="${serverfiles}/config_player.xml"
-	local item managed=() enabled=()
+	local item entries=""
 	if [ ! -f "${config}" ]; then
 		fn_print_warn_nl "${config} not found. Start the server once, then run ./${selfname} workshop-update to enable Workshop mods"
 		fn_script_log_warn "${config} not found, Workshop mods not enabled"
 		return
 	fi
-	managed=("${workshopremovals[@]}" "${workshopitems[@]}")
+	sed -z -i 's#<regularpackages />#<regularpackages>\n    </regularpackages>#' "${config}"
+	for item in "${workshopremovals[@]}" "${workshopitems[@]}"; do
+		sed -z -i -E "s#[[:space:]]*<package[[:space:]]+path=\"LocalMods/${item}/filelist\\.xml\"[[:space:]]*/>##g" "${config}"
+	done
 	for item in "${workshopitems[@]}"; do
 		if [ -f "${workshopdir}/${item}/filelist.xml" ]; then
-			enabled+=("${item}")
+			entries+="\\n      <package\\n        path=\"LocalMods/${item}/filelist.xml\" />"
 		fi
 	done
-	python3 - "${config}" "${managed[*]}" "${enabled[*]}" << 'PYEOF'
-import re
-import sys
-
-config, managed, enabled = sys.argv[1], sys.argv[2].split(), sys.argv[3].split()
-with open(config, encoding="utf-8-sig", newline="") as f:
-    text = f.read()
-text = text.replace("<regularpackages />", "<regularpackages>\n    </regularpackages>")
-for item in managed:
-    text = re.sub(r'\s*<package\b[^>]*?path="LocalMods/' + re.escape(item) + r'/filelist\.xml"[^>]*/>', "", text)
-entries = "".join('\n      <package\n        path="LocalMods/' + item + '/filelist.xml" />' for item in enabled)
-text = re.sub(r"(\s*)</regularpackages>", entries + r"\1</regularpackages>", text, count=1)
-text = re.sub(r"<regularpackages>\s*</regularpackages>", "<regularpackages />", text)
-with open(config, "w", encoding="utf-8", newline="") as f:
-    f.write(text)
-PYEOF
+	sed -z -i -E "s#([[:space:]]*)</regularpackages>#${entries}\\1</regularpackages>#" "${config}"
+	sed -z -i -E 's#<regularpackages>[[:space:]]*</regularpackages>#<regularpackages />#' "${config}"
 }
 
 # Installs downloaded items and removes unused ones. Downloads happen first, so the server only stops for this step.