dayzdockerserver/files/dayzserver

350 lines
9.7 KiB
Bash
Executable file

#!/usr/bin/env bash
# Make sure we don't start collecting core files
# FIXME Set this in Docker somewhere
ulimit -c 0
# Colors
default="\e[0m"
red="\e[31m"
green="\e[32m"
yellow="\e[33m"
lightyellow="\e[93m"
blue="\e[34m"
lightblue="\e[94m"
magenta="\e[35m"
cyan="\e[36m"
# Base directories
CFG_SRC_FILES="/files"
SERVER_FILES="${HOME}/serverfiles"
SERVER_PROFILE="${HOME}/serverprofile"
# Default config file
DEFAULT_CFG="${CFG_SRC_FILES}/default.cfg"
# Battle Eye files
BE_SERVER_FILE="beserver_x64.cfg"
BE_SERVER_DIR="${SERVER_FILES}/battleye"
BE_SERVER_DST="${BE_SERVER_DIR}/${BE_SERVER_FILE}"
BE_SERVER_SRC="${CFG_SRC_FILES}/${BE_SERVER_FILE}"
# Server configuration file
SERVER_CFG_FILE="serverDZ.cfg"
SERVER_CFG_DST="${SERVER_FILES}/${SERVER_CFG_FILE}"
SERVER_CFG_SRC="${CFG_SRC_FILES}/${SERVER_CFG_FILE}"
# Used to check if dayZ is installed
SERVER_INSTALL_FILE="${SERVER_FILES}/DayZServer"
# Steam files
STEAM_LOGIN_DST="${HOME}/steamlogin"
STEAMCMD=steamcmd.sh
# Workshop
WORKSHOP_CFG="${HOME}/workshop.cfg"
fn_prompt_yn(){
echo -n "${1} (y|N) " >&2
read -s -n 1 a
a=$(echo ${a} | tr A-Z a-z)
if [[ "${a}" = "y" ]]
then
echo
return 0
else
echo
return 1
fi
}
fn_loadconfig_dayz(){
source ${DEFAULT_CFG}
# Handle the server configuration file
if [ ! -f ${SERVER_CFG_DST} ]
then
echo "Creating initial server configuration file"
cp "${SERVER_CFG_SRC}" "${SERVER_CFG_DST}"
elif ! diff -q "${SERVER_CFG_SRC}" "${SERVER_CFG_DST}"
then
echo "========================================================================="
diff -Nau --color "${SERVER_CFG_SRC}" "${SERVER_CFG_DST}"
echo "========================================================================="
if fn_prompt_yn "The new server configuration file differs from what's installed. Use it?"
then
echo "Updating the server configuration file"
cp "${SERVER_CFG_SRC}" "${SERVER_CFG_DST}"
else
echo "NOT updating the server configuration file"
fi
fi
}
# Checks for the destination file are already done before calls to this
fn_do_rcon(){
if [ ! -d "${BE_SERVER_DIR}" ]
then
echo
echo -e "${yellow}DayZ is not installed yet. Can't manage RCON until then${default}"
echo
else
echo -n "Creating the Battle Eye configuration file "
cp "${BE_SERVER_SRC}" "${BE_SERVER_DST}"
# Set a random RCON password, unless one's set in the environment
if grep -q RCON_PASSWORD "${BE_SERVER_SRC}"
then
RCON_PASSWORD=$(< /dev/urandom tr -dc 'A-Za-z0-9' | head -c10)
echo -e "using random RCON password ${yellow}${RCON_PASSWORD}${default}"
sed -i "${BE_SERVER_DST}" -e "s/RCON_PASSWORD/${RCON_PASSWORD}/"
else
echo "using the RCON_PASSWORD already set in ${BE_SERVER_SRC}."
fi
fi
}
fn_create_beconfig(){
if [ ! -f "${BE_SERVER_DST}" ]
then
echo -n "Creating Battle Eye RCON file "
fn_do_rcon
elif diff -q "${BE_SERVER_SRC}" "${BE_SERVER_DST}" > /dev/null
then
echo -n "Updating Battle Eye RCON file "
fn_do_rcon
fi
}
fn_start_dayz(){
fn_create_beconfig
fn_loadconfig_dayz
fn_workshop_mods
printf "[ ${green}DayZ${default} ] Starting server...\n"
cd ${SERVER_FILES}
./DayZServer $dayzparameter "$workshop" || (
echo
echo -e "${yellow}========================================== error.log =========================================="
find "${HOME}" -name error.log -exec cat {} \; -exec rm -f {} \;
echo
echo -e "${yellow}========================================== script*.log ========================================"
find "${HOME}" -name "script*.log" -exec cat {} \; -exec rm -f {} \;
echo
echo -e "${yellow}========================================== *.RPT =============================================="
find "${HOME}" -name "*.RPT" -exec cat {} \; -exec rm -f {} \;
echo
echo -e "${yellow}========================================== End crash log ======================================"
)
}
fn_stop_dayz(){
echo "Stopping DayZ server..."
}
fn_steam_login(){
if [ -f "${STEAM_LOGIN_DST}" ]
then
if fn_prompt_yn "The steam login is already set. Reset it?"
then
rm -f "${STEAM_LOGIN_DST}"
else
echo
echo "Not reset. Nothing to do. Exiting..."
exit 0
fi
fi
if [ ! -f "${STEAM_LOGIN_DST}" ]
then
echo "Setting up Steam credentials"
echo -n "Steam Username (anonymous): "
read steamlogin
if [[ "${steamlogin}" = "" ]]
then
echo "Steam login set to 'anonymous'"
steamlogin="anonymous"
fi
echo "steamlogin=${steamlogin}" > "${STEAM_LOGIN_DST}"
${STEAMCMD} +force_install_dir ${SERVER_FILES} +login "${steamlogin}" +quit
fi
}
fn_steamlogin_dayz(){
if [ -f "${STEAM_LOGIN_DST}" ]
then
source "${STEAM_LOGIN_DST}"
else
echo "No cached Steam credentials. Please configure this now: "
fn_steam_login
fi
}
fn_runvalidate_dayz(){
fn_loadconfig_dayz
if [[ "${FAKE_DAYZ_INSTALL}" = "" ]]
then
${STEAMCMD} +force_install_dir ${SERVER_FILES} +login "${steamlogin}" +app_update "${appid}" validate +quit
else
echo
echo -e "${red}Faking DayZ install procedure!!${default}"
echo
fi
}
fn_install_dayz(){
if [ ! -f "${SERVER_INSTALL_FILE}" ]; then
mkdir -p "${SERVER_FILES}"
mkdir -p "${SERVER_PROFILE}"
printf "[ ${yellow}DayZ${default} ] Downloading DayZ Server-Files!\n"
fn_steamlogin_dayz
fn_runvalidate_dayz
else
printf "[ ${lightblue}DayZ${default} ] The Server is already installed.\n"
fi
}
fn_runupdate_dayz(){
fn_loadconfig_dayz
${STEAMCMD} +force_install_dir ${SERVER_FILES} +login "${steamlogin}" +app_update "${appid}" +quit
}
fn_update_dayz(){
fn_steamlogin_dayz
fn_loadconfig_dayz
appmanifestfile=${SERVER_FILES}/steamapps/appmanifest_"${appid}".acf
printf "[ ... ] Checking for update: SteamCMD"
# gets currentbuild
currentbuild=$(grep buildid "${appmanifestfile}" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d \ -f3)
# Removes appinfo.vdf as a fix for not always getting up to date version info from SteamCMD
if [ -f "${HOME}/Steam/appcache/appinfo.vdf" ]; then
rm -f "${HOME}/Steam/appcache/appinfo.vdf"
sleep 1
fi
# check for new build
availablebuild=$(${STEAMCMD} +login "${steamlogin}" +app_info_update 1 +app_info_print "${appid}" +app_info_print \
"${appid}" +quit | sed -n '/branch/,$p' | grep -m 1 buildid | tr -cd '[:digit:]')
if [ -z "${availablebuild}" ]; then
printf "\r[ ${red}FAIL${default} ] Checking for update: SteamCMD\n"
printf "\r[ ${red}FAIL${default} ] Checking for update: SteamCMD: Not returning version info\n"
exit
else
printf "\r[ ${green}OK${default} ] Checking for update: SteamCMD"
fi
# compare builds
if [ "${currentbuild}" != "${availablebuild}" ]; then
printf "\r[ ${green}OK${default} ] Checking for update: SteamCMD: Update available\n"
printf "Update available:\n"
printf "\tCurrent build: ${red}${currentbuild}${default}\n"
printf "\tAvailable build: ${green}${availablebuild}${default}\n"
printf "\thttps://steamdb.info/app/${appid}/\n"
printf "\nApplying update"
# run update
fn_runupdate_dayz
fn_workshop_mods
else
printf "\r[ ${green}OK${default} ] Checking for update: SteamCMD: No update available\n"
printf "\nNo update available:\n"
printf "\tCurrent version: ${green}${currentbuild}${default}\n"
printf "\tAvailable version: ${green}${availablebuild}${default}\n"
printf "\thttps://steamdb.info/app/${appid}/\n\n"
fi
}
fn_workshop_mods(){
fn_steamlogin_dayz
fn_loadconfig_dayz
declare -a workshopID
workshopfolder="${SERVER_FILES}/steamapps/workshop/content/${dayz_id}"
workshoplist=""
if [ ! -f "${WORKSHOP_CFG}" ]
then
echo "No workshop mods..."
return
else
echo "Syncing workshop mods..."
fi
mapfile -t workshopID < "${WORKSHOP_CFG}"
# gather mods
for i in "${workshopID[@]}"
do
if [[ $i =~ ^[0-9] ]] && [ $(expr length $i) -gt 7 ]; then
workshoplist+=" +workshop_download_item "${dayz_id}" "$i""
fi
done
# download mods
${STEAMCMD} +force_install_dir ${SERVER_FILES} +login "${steamlogin}" ${workshoplist} +quit
# link mods
for i in "${workshopID[@]}"
do
if [[ $i =~ ^[0-9] ]] && [ $(expr length $i) -gt 7 ] && [ -d "${workshopfolder}/$i" ]; then
modname=$(cut -d '"' -f 2 <<< $(grep name ${workshopfolder}/$i/meta.cpp))
if [ ! -d "${SERVER_FILES}/@${modname}" ]; then
ln -s ${workshopfolder}/$i "${SERVER_FILES}/@${modname}" &> /dev/null
fi
find "${workshopfolder}/$i" -depth -exec rename -f 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;
fi
done
if ls ${SERVER_FILES}/@* 1> /dev/null 2>&1; then
printf "\n[ ${green}DayZ${default} ] Copy Key Files from Mods...\n"
cp -vu ${SERVER_FILES}/@*/keys/* "${SERVER_FILES}/keys/" > /dev/null 2>&1
fi
}
fn_rcon(){
case "${1}" in
show)
if [ ! -f "${BE_SERVER_DST}" ]
then
fn_do_rcon
fi
if [ -f "${BE_SERVER_DST}" ]
then
echo "=================================================================="
cat "${BE_SERVER_DST}"
echo "=================================================================="
fi
;;
reset)
if fn_prompt_yn "Reset RCON password?"
then
rm -f "${BE_SERVER_DST}"
fn_do_rcon
else
echo
echo "Not reset. Nothing to do. Exiting..."
exit 0
fi
;;
esac
}
fn_backup(){
echo "Creating backup...WIP"
}
case "${1}" in
backup)
fn_backup
;;
install)
fn_install_dayz
;;
login)
fn_steam_login
;;
rcon)
fn_rcon "${2}"
;;
stop)
fn_stop_dayz
;;
update)
fn_update_dayz
;;
workshop)
fn_workshop_mods "${2}"
;;
**)
echo
echo -e "${red}Unknown or missing option: '${1}'${default}"
echo -e "${green}Usage: $(basename $0) [ backup | install | login | rcon [ show | reset ] | stop | update | workshop [ add id | list | remove id ] ]${default}"
echo
;;
esac