Refactor everything the relies on getting a mod by index in a listing. This had to account for context such needing to maintain mod ordering when starting the server.

Moved a lot of the functions out of scripts and into common and consolidated.
Fix some output formatting.
Make key copying only happen in a server.
This commit is contained in:
Daniel Ceregatti 2023-08-30 18:08:08 -07:00
parent 09b08a9978
commit 65840a79c5
3 changed files with 107 additions and 124 deletions

View file

@ -32,6 +32,9 @@ release_server_appid=1042420
# DayZ release client SteamID. This is for mods, as only the release client has them. # DayZ release client SteamID. This is for mods, as only the release client has them.
release_client_appid=221100 release_client_appid=221100
# Server container profile directory
SERVER_PROFILE="/profiles"
# Common container base directories # Common container base directories
FILES="/files" FILES="/files"
SERVER_FILES="/serverfiles" SERVER_FILES="/serverfiles"
@ -66,16 +69,95 @@ list(){
X=1 X=1
C="${green}" C="${green}"
spaces=" " spaces=" "
echo "Installed mods:" echo
echo -e " ID Name URL Size" echo -e " ID Name URL Size"
echo "------------------------------------------------------------------------------------------------------------------------" echo "------------------------------------------------------------------------------------------------------------------------"
for dir in $(ls -tr ${WORKSHOP_DIR}) for link in $(ls -d ${SERVER_FILES}/@* | sort)
do do
ID=${dir} ID=$(readlink ${link} | awk -F/ '{print $NF}')
NAME=$(grep name "${WORKSHOP_DIR}/${dir}/meta.cpp" | cut -d '"' -f2 | tr -cd [:alnum:]) MODNAME=$(get_mod_name ${ID})
SIZE=$(du -sh "${WORKSHOP_DIR}/${dir}" | awk '{print $1}') SIZE=$(du -sh "${WORKSHOP_DIR}/${ID}" | awk '{print $1}')
printf "${C}%.3d %s %.30s %s https://steamcommunity.com/sharedfiles/filedetails/?id=%s %s${default}\n" ${X} ${ID} "${NAME}" "${spaces:${#NAME}+1}" ${ID} ${SIZE} printf "${C}%.3d %s %.30s %s https://steamcommunity.com/sharedfiles/filedetails/?id=%s %s${default}\n" ${X} ${ID} "${MODNAME}" "${spaces:${#NAME}+1}" ${ID} ${SIZE}
X=$((X+1)) X=$((X+1))
done done
echo echo
} }
# Get mod name by ID or index
get_mod_name(){
# Check for an ID
if ! [ -d "${WORKSHOP_DIR}/${1}" ]
then
echo "Mod ID ${1} doesn't exist" >&2
exit 1
fi
NAME=$(grep name ${WORKSHOP_DIR}/${1}/meta.cpp | cut -d '"' -f2 | tr -cd [:alnum:])
echo -n ${NAME}
}
get_mod_id(){
# If we were passed a valid mod id, just return it
if [ -d "${WORKSHOP_DIR}/${1}" ]
then
echo -n ${1}
return
fi
# If we have a second argument, we want to iterate over active server mods
DIR=${SERVER_FILES}
ARG="-d"
if [[ ${2} = "0" ]]
then
ARG="-tdr"
DIR=${SERVER_PROFILE}
fi
# echo "DIR: ${DIR}, ARG: ${ARG}" >&2
X=1
# Loop over mods
for link in $(ls ${ARG} ${DIR}/@* 2> /dev/null)
do
ID=$(readlink ${link} | awk -F/ '{print $NF}')
if [[ ${X} = ${1} ]]
then
echo -n ${ID}
return
fi
X=$((X+1))
done
}
get_mods(){
workshoplist=""
for link in $(ls -d ${SERVER_FILES}/@* 2> /dev/null | sort)
do
ID=$(readlink ${link} | awk -F/ '{print $NF}')
MODNAME=$(get_mod_name ${ID})
workshoplist+=" +workshop_download_item "${release_client_appid}" "${ID}
done
get_mod_command_line
}
get_mod_command_line(){
mod_command_line=""
for link in $(ls -tdr ${SERVER_PROFILE}/@* 2> /dev/null)
do
ID=$(readlink ${link} | awk -F/ '{print $NF}')
MODNAME=$(get_mod_name ${ID})
mod_command_line+="@${MODNAME};"
done
if [[ ${mod_command_line} != "" ]]
then
mod_command_line='-mod='${mod_command_line::-1}
fi
}
# Copy mod keys
copy_keys(){
if [[ ${1} = 1 ]]
then
echo -n "Copying key file(s): "
find ${WORKSHOP_DIR}/${2} -name "*.bikey" -exec cp -v {} "${SERVER_FILES}/keys/" \;
else
echo -n "Removing key file(s): "
find ${WORKSHOP_DIR}/${2} -name "*.bikey" -execdir rm -vf "${SERVER_FILES}/keys/{}" \;
fi
}

View file

@ -3,7 +3,6 @@
source dz-common source dz-common
# Server container base directories # Server container base directories
SERVER_PROFILE="/profiles"
MPMISSIONS="${SERVER_FILES}/mpmissions" MPMISSIONS="${SERVER_FILES}/mpmissions"
mkdir -p ${SERVER_PROFILE}/battleye mkdir -p ${SERVER_PROFILE}/battleye
@ -109,23 +108,6 @@ EOF
> ~/py3rcon.config.json > ~/py3rcon.config.json
} }
get_mods(){
workshoplist=""
mod_command_line=""
for link in $(ls -tdr ${SERVER_PROFILE}/@* 2> /dev/null)
do
ID=$(readlink ${link} | awk -F/ '{print $NF}')
MODNAME=$(get_mod_name ${ID})
workshoplist+=" +workshop_download_item "${release_client_appid}" "${ID}
mod_command_line+="@${MODNAME};"
done
# Remove the trailing semi-colon. This is necessary.
if [[ ${mod_command_line} != "" ]]
then
mod_command_line="-mod=${mod_command_line::-1}"
fi
}
# Make sure to clean up and report on exit, as these files remain in the container's volume # Make sure to clean up and report on exit, as these files remain in the container's volume
report() { report() {
rm -f /tmp/mod_command_line /tmp/parameters rm -f /tmp/mod_command_line /tmp/parameters
@ -261,33 +243,6 @@ config(){
fi fi
} }
get_mod_id_by_index(){
X=1
# Loop over mods
for link in $(ls -tdr ${SERVER_FILES}/@* 2> /dev/null)
do
ID=$(readlink ${link} | awk -F/ '{print $NF}')
if [[ ${X} = ${1} ]]
then
echo -n ${ID}
return
fi
X=$((X+1))
done
}
# Get mod name by ID or index
get_mod_name(){
# Check for an ID
if ! [ -d "${WORKSHOP_DIR}/${ID}" ]
then
echo "Mod ID ${1} doesn't exist" >&2
exit 1
fi
NAME=$(grep name ${WORKSHOP_DIR}/${ID}/meta.cpp | cut -d '"' -f2 | tr -cd [:alnum:])
echo -n ${NAME}
}
# Activate / Deactivate a mod # Activate / Deactivate a mod
activate(){ activate(){
W=${1} W=${1}
@ -297,20 +252,26 @@ activate(){
if [[ ${W} = 0 ]] if [[ ${W} = 0 ]]
then then
WW="de" WW="de"
UU="un"
COLOR="${red}" COLOR="${red}"
fi fi
ID=$(get_mod_id_by_index ${1}) ID=$(get_mod_id ${1} ${W})
MODNAME=$(get_mod_name ${ID}) MODNAME=$(get_mod_name ${ID})
# echo "ID: ${ID}, MODNAME: ${MODNAME}"
# exit 0
# Toggle state or report nothing burger # Toggle state or report nothing burger
pushd "${SERVER_PROFILE}" > /dev/null pushd "${SERVER_PROFILE}" > /dev/null
if [ -L "${SERVER_PROFILE}/@${MODNAME}" ] if [[ ${W} = 0 ]] && [ -L "${SERVER_PROFILE}/@${MODNAME}" ]
then then
echo -n "Removing mod symlink: "
rm -vf "${SERVER_PROFILE}/@${MODNAME}" rm -vf "${SERVER_PROFILE}/@${MODNAME}"
else elif [[ ${W} = 1 ]]
ln -s "${WORKSHOP_DIR}/${ID}" "${SERVER_PROFILE}/@${MODNAME}" then
# echo -e "Mod id ${ID} - ${COLOR}${MODNAME}${default} - is already ${WW}active" echo -n "Creating mod symlink: "
ln -sfv "${WORKSHOP_DIR}/${ID}" "${SERVER_PROFILE}/@${MODNAME}"
else
echo -e "Mod id ${ID} - ${COLOR}${MODNAME}${default} - is already ${WW}active"
fi fi
copy_keys ${W} ${ID}
echo -e "Mod id ${ID} - ${COLOR}${MODNAME}${default} ${WW}activated" echo -e "Mod id ${ID} - ${COLOR}${MODNAME}${default} ${WW}activated"
popd > /dev/null popd > /dev/null
status status
@ -341,7 +302,10 @@ activelist(){
printf "${C}%.3d %s %.23s %s https://steamcommunity.com/sharedfiles/filedetails/?id=%s %s${default}\n" ${X} ${ID} "${MODNAME}" "${spaces:${#MODNAME}+1}" ${ID} ${SIZE} printf "${C}%.3d %s %.23s %s https://steamcommunity.com/sharedfiles/filedetails/?id=%s %s${default}\n" ${X} ${ID} "${MODNAME}" "${spaces:${#MODNAME}+1}" ${ID} ${SIZE}
X=$((X+1)) X=$((X+1))
done done
echo if [[ ${have} = "no" ]]
then
echo -ne "${red}none${default}"
fi
} }
# Display the status of everything # Display the status of everything
@ -389,10 +353,6 @@ Server files installed: ${INSTALLED}"
echo -ne " echo -ne "
Active mods: " Active mods: "
activelist activelist
if [[ ${MODS} == "" ]]
then
echo -n "none"
fi
echo -e "${MODS} echo -e "${MODS}
Server running: ${RUNNING} Server running: ${RUNNING}
Working parameters: ${parameters} Working parameters: ${parameters}

View file

@ -34,54 +34,12 @@ ${default}"
exit 1 exit 1
} }
get_mod_id(){
# If we were passed a valid mod id, just return it
if [ -d "${WORKSHOP_DIR}/${1}" ]
then
echo -n ${1}
return
fi
X=1
# Loop over mods
for dir in $(ls -tr ${WORKSHOP_DIR})
do
ID=${dir}
if [[ ${X} = ${1} ]]
then
echo -n ${ID}
return
fi
X=$((X+1))
done
}
# Get mod name by ID or index
get_mod_name(){
ID=$(get_mod_id ${1})
if ! [ -d "${WORKSHOP_DIR}/${ID}" ]
then
echo "Mod ID ${1} doesn't exist" >&2
exit 1
fi
NAME=$(grep name ${WORKSHOP_DIR}/${ID}/meta.cpp | cut -d '"' -f2 | tr -cd [:alnum:])
echo -n ${NAME}
}
# "Manage" XML files. # "Manage" XML files.
xml(){ xml(){
/files/bin/xml.sh ${1} /files/bin/xml.sh ${1}
mergexml ${1} mergexml ${1}
} }
# Copy mod keys
copy_keys(){
if [[ ${1} = 1 ]]
then
echo "Copying key files..."
find ${WORKSHOP_DIR}/${2} -name "*.bikey" -exec cp -v {} "${SERVER_FILES}/keys/" \;
fi
}
# Manage the mod symlink # Manage the mod symlink
symlink(){ symlink(){
W=${1} W=${1}
@ -158,7 +116,7 @@ add(){
# Lower case all the files in mod directories. # Lower case all the files in mod directories.
find "${WORKSHOP_DIR}/${1}" -depth -exec rename -f 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \; find "${WORKSHOP_DIR}/${1}" -depth -exec rename -f 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;
# Copy the key files # Copy the key files
copy_keys 1 ${1} # copy_keys 1 ${1}
echo -e "Mod id ${1} - ${green}${MODNAME}${default} - added" echo -e "Mod id ${1} - ${green}${MODNAME}${default} - added"
mergexml ${ID} mergexml ${ID}
# checkTypesXML ${1} install # checkTypesXML ${1} install
@ -278,23 +236,6 @@ update(){
fi fi
} }
get_mods(){
workshoplist=""
mod_command_line=""
for link in $(ls -tdr ${SERVER_FILES}/@* 2> /dev/null)
do
ID=$(readlink ${link} | awk -F/ '{print $NF}')
MODNAME=$(get_mod_name ${ID})
workshoplist+=" +workshop_download_item "${release_client_appid}" "${ID}
mod_command_line+="@${MODNAME};"
done
if [[ ${mod_command_line} != "" ]]
then
mod_command_line='-mod='${mod_command_line::-1}
fi
}
# Update mods # Update mods
modupdate(){ modupdate(){
echo "Updating mods..." echo "Updating mods..."
@ -388,7 +329,7 @@ case "${C}" in
r|remove) r|remove)
remove "${@}" remove "${@}"
;; ;;
s|status) l|s|status)
status "${@}" status "${@}"
;; ;;
u|update) u|update)