refactor: use bash functions, change output logic

This commit is contained in:
bastimeyer 2021-12-06 04:51:40 +01:00
parent f6bffde240
commit cc9bc79c4d

View file

@ -25,6 +25,7 @@ LAUNCH=0
SERVER="" SERVER=""
PORT="27016" PORT="27016"
INPUT=() INPUT=()
MODS=()
declare -A DEPS=( declare -A DEPS=(
[gawk]=gawk [gawk]=gawk
@ -132,28 +133,31 @@ check_dir() {
# ---- # ----
check_deps() {
for dep in "${!DEPS[@]}"; do for dep in "${!DEPS[@]}"; do
command -v "${dep}" 2>&1 >/dev/null || err "${DEPS["${dep}"]} is missing. Aborting." command -v "${dep}" 2>&1 >/dev/null || err "${DEPS["${dep}"]} is missing. Aborting."
done done
}
check_dir "${DIR_DAYZ}" query_server_api() {
check_dir "${DIR_WORKSHOP}" [[ -z "${SERVER}" ]] && return
if [[ -n "${SERVER}" ]]; then local query
local response
msg "Querying API for server: ${SERVER%:*}:${PORT}" msg "Querying API for server: ${SERVER%:*}:${PORT}"
query="$(sed -e "s/@ADDRESS@/${SERVER%:*}/" -e "s/@PORT@/${PORT}/" <<< "${API_URL}")" query="$(sed -e "s/@ADDRESS@/${SERVER%:*}/" -e "s/@PORT@/${PORT}/" <<< "${API_URL}")"
debug "Querying ${query}" debug "Querying ${query}"
response="$(curl "${API_PARAMS[@]}" "${query}")" response="$(curl "${API_PARAMS[@]}" "${query}")"
[[ $? > 0 ]] && err "Error while querying API"
debug "Parsing API response" debug "Parsing API response"
jq -e ".mods[]" 2>&1 >/dev/null <<< "${response}" || err "Missing mods data from API response" jq -e ".mods[]" 2>&1 >/dev/null <<< "${response}" || err "Missing mods data from API response"
INPUT+=( $(jq -r ".mods[] | select(.app_id == ${DAYZ_ID}) | .id" <<< "${response}") )
fi
missing=0 INPUT+=( $(jq -r ".mods[] | select(.app_id == ${DAYZ_ID}) | .id" <<< "${response}") )
mods=() }
setup_mods() {
local missing=0
for modid in "${INPUT[@]}"; do for modid in "${INPUT[@]}"; do
modpath="${DIR_WORKSHOP}/${modid}" local modpath="${DIR_WORKSHOP}/${modid}"
if ! [[ -d "${modpath}" ]]; then if ! [[ -d "${modpath}" ]]; then
missing=1 missing=1
msg "Missing mod directory for: ${modid}" msg "Missing mod directory for: ${modid}"
@ -161,10 +165,10 @@ for modid in "${INPUT[@]}"; do
continue continue
fi fi
modmeta="${modpath}/meta.cpp" local modmeta="${modpath}/meta.cpp"
[[ -f "${modmeta}" ]] || err "Missing mod metadata for: ${modid}" [[ -f "${modmeta}" ]] || err "Missing mod metadata for: ${modid}"
modname="$(gawk 'match($0,/name\s*=\s*"(.+)"/,m){print m[1];exit}' "${modmeta}")" local modname="$(gawk 'match($0,/name\s*=\s*"(.+)"/,m){print m[1];exit}' "${modmeta}")"
[[ -n "${modname}" ]] || err "Missing mod name for: ${modid}" [[ -n "${modname}" ]] || err "Missing mod name for: ${modid}"
debug "Mod ${modid} found: ${modname}" debug "Mod ${modid} found: ${modname}"
modname="${modname//\'/}" modname="${modname//\'/}"
@ -174,21 +178,32 @@ for modid in "${INPUT[@]}"; do
ln -sr "${modpath}" "${DIR_DAYZ}/@${modname}" ln -sr "${modpath}" "${DIR_DAYZ}/@${modname}"
fi fi
mods+=("@${modname}") MODS+=("@${modname}")
done done
[[ "${missing}" == 1 ]] && exit 1
cmdline=() return ${missing}
if [[ "${#mods[@]}" -gt 0 ]]; then }
cmdline+=("-mod=$(IFS=";"; echo "${mods[*]}")")
fi
if [[ "${LAUNCH}" == 1 ]] && [[ -n "${SERVER}" ]]; then main() {
cmdline+=("-connect=${SERVER}" -nolauncher -world=empty) check_deps
fi check_dir "${DIR_DAYZ}"
check_dir "${DIR_WORKSHOP}"
query_server_api
setup_mods || exit 1
local mods="$(IFS=";"; echo "${MODS[*]}")"
if [[ "${LAUNCH}" == 1 ]]; then if [[ "${LAUNCH}" == 1 ]]; then
set -x local cmdline=()
steam -applaunch "${DAYZ_ID}" "${cmdline[@]}" [[ -n "${mods}" ]] && cmdline+=("-mod=${mods}")
else [[ -n "${SERVER}" ]] && cmdline+=("-connect=${SERVER}" -nolauncher -world=empty)
echo "${cmdline[@]}" ( set -x; steam -applaunch "${DAYZ_ID}" "${cmdline[@]}"; )
elif [[ -n "${mods}" ]]; then
msg "Add this to your game's launch options, including the quotes:"
echo "\"-mod=${mods}\""
fi fi
}
main