From 6c68dd292fa56f957419ee9b372754c5a7142dfd Mon Sep 17 00:00:00 2001 From: Daniel Ceregatti Date: Fri, 24 Feb 2023 15:15:17 -0800 Subject: [PATCH] Make types.sh be able to handle any XML file with types.xml data. --- files/mods/types.sh | 81 ++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 23 deletions(-) diff --git a/files/mods/types.sh b/files/mods/types.sh index 52bcfd6..6e12a29 100755 --- a/files/mods/types.sh +++ b/files/mods/types.sh @@ -1,33 +1,68 @@ #!/usr/bin/env bash -# A generic script to merge a mod's types.xml into all installed missions +# A generic script to manage a mod's types.xml against all installed missions set -eE -cd "$(dirname ${0})" - ID=${1} MODE=${2} +TYPES_FILE="${workshopfolder}/${ID}/extras/types.xml" -echo -if [[ ${MODE} = "uninstall" ]] +if [[ ${3} != "" ]] then - echo "Restoring original types.xml files in missions:" - echo - for file in $(ls *.orig) - do - cp -v $(file} ${HOME}/serverfiles/mpmissions/${file/.orig/}/db/types.xml - done -else - echo "Merging extras/types.xml to server missions directory:" - echo - cp -v ${HOME}/serverfiles/steamapps/workshop/content/221100/${ID}/extras/types.xml . - for dir in $(ls ${HOME}/serverfiles/mpmissions) - do - cp -v ${dir}/db/types.xml ${dir}.orig - head -n-1 ${dir}.orig > /tmp/types.xml - tail -n+2 types.xml >> /tmp/types.xml - xmllint --noout /tmp/types.xml && cp -v /tmp/types.xml ${C}/db/types.xml - done + TYPES_FILE="${3}" fi -echo + +for file in $(find ${SERVER_FILES}/mpmissions -name types.xml -print -prune) +do + if [[ ${MODE} = "uninstall" ]] + then + # Remove the lines that were added by the mod's extras/types.xml from + # every db/types.xml in all mission directories + + # Chop the top tag from the source file + tail -n+2 ${TYPES_FILE} > /tmp/types-tmp.xml + + # Chop the bottom tag from the source file + head -n-1 /tmp/types-tmp.xml > /tmp/types-src.xml + + # Remove that content from the original file + grep -qvxFf /tmp/types-src.xml ${file} + else + # Add the contents of extras/types.xml to every db/types.xml in all + # mission directories + xmllint --noout ${TYPES_FILE} && { + echo -e "${green}${TYPES_FILE} passes XML lint test!" + echo -e "Merging to $file...${default}" + # Chop the bottom tag from the destination file + head -n-1 ${file} > /tmp/types-dst.xml + + # Chop the top 2 tags, xml and types, from the source file + tail -n+2 ${TYPES_FILE} > /tmp/types-src.xml + + # Concatenate the two files back into the source file + cat /tmp/types-dst.xml /tmp/types-src.xml > /tmp/types.xml + + xmllint --noout /tmp/types.xml && { + cp -v /tmp/types.xml ${file} + } || { + # Try again, but chop the top 3 tags, hopefully xml and types, from the source file... + echo "First merge attempt failed, trying again..." + tail -n+3 ${TYPES_FILE} > /tmp/types-src.xml + + # Concatenate the two files back into the source file + cat /tmp/types-dst.xml /tmp/types-src.xml > /tmp/types.xml + + # And lint again. This should probably be a recursive function... + xmllint --noout /tmp/types.xml && { + cp -v /tmp/types.xml ${file} + } || { + echo "XML lint check after merge failed! No files changed!" + } + } + } || { + echo -e "${red}${TYPES_FILE} fails XML lint test!" + echo -e "This will have to be merged by hand!${default}" + } + fi +done