Added configuration function, resolved overwriting Game.ini

This commit is contained in:
Price Hiller 2021-08-27 06:54:18 -05:00
parent 53d62dca6c
commit 6ca51153a2

View File

@ -192,7 +192,7 @@ kill_server() {
[[ -z "${server_id}" ]] && log "error" "No server id passed" && exit 1
tmux kill-session -t "${prefix}-Server-${server_id}" > /dev/null 2>&1
tmux kill-session -t "${prefix}-Server-${server_id}" >/dev/null 2>&1
tmux_response="${?}"
if [ "${tmux_response}" == "0" ]; then
@ -232,6 +232,29 @@ should_kill() {
return 0
}
run_and_kill() {
local prefix
local server_id
prefix="Mordhau"
server_id="${1}"
local server_directory
server_directory="${BASE_DIR}/Server-${server_id}"
log "info" "Launching ${prefix}-Server-${server_id} to install any missing configurations..."
tmux new-session -d -s "${prefix}-Server-${server_id}" \
"${server_directory}/MordhauServer.sh"
sleep 5
tmux kill-session -t "${prefix}-Server-${server_id}" &&
log "info" "Successfully ran the Mordhau session and stopped the session"
return 0
}
generate_default_configs() {
mkdir -p "${BASE_DIR}" && log "info" "Created base directory if it didn't exist"
@ -257,7 +280,7 @@ generate_default_configs() {
# For example:
# Mods=1121745
__EOF__
log "info" "Created a new mods.conf with no mods"
log "info" "Created a new mods configuration with no mods at ${MODS_CONFIG}"
fi
if [ ! -f "${ADMINS_CONFIG}" ]; then
@ -268,12 +291,136 @@ __EOF__
# For example:
# Admins=5E92E0B55E90869C
__EOF__
log "info" "Created a new mods.conf with no mods"
log "info" "Created a new admins configurations with no admins at ${ADMINS_CONFIG}"
fi
log "info" "Generated all configs if they did not exist"
}
configure() {
local server_id
local list
local verbose
local edit
server_id=""
list=0
verbose=0
edit=0
while :; do
case ${1} in
-h | -\? | --help)
printf "Usage: %s\n" \
"configure [options]
--server <server id: int> | -s <server id: int>
Lists the path to the Game-Primary.ini for the given server, useful for cat and other scripting. Required if --list not passed
Example:
--server 3
--list | -l
List all paths to the Game-Primary.ini configs for all servers, very useful for cat and other scripting. Required if --server not passed
Example:
--list
--edit | -e
Opens each Game-Primary.ini in nano
Example:
--edit
--verbose | -v
Shows further output -- useful for a glance, should not be used with scripts
Example:
--verbose"
exit
;;
--) # End of all options.
break
;;
--server | -s)
shift
server_id="${1}"
[[ -z "${server_id}" ]] && log "error" "No server id passed" && exit 1
;;
--list | -l)
list=1
;;
--verbose | -v)
verbose=1
;;
--edit | -e)
edit=1
;;
-?*)
printf 'Unknown option: %s\n' "$1" >&2
;;
*) # Default case: No more options, so break out of the loop.
break ;;
esac
shift
done
display_verbose_output() {
# First argument is the server number
echo "$(important "Server-${1}") ($(important "Mordhau-Server-${1}"))"
echo "=================================="
}
if [ -z "${server_id}" ] && [ "${list}" -eq 0 ]; then
log "error" "Neither list nor server option passed, check configure --help"
exit 1
fi
if [ -n "${server_id}" ]; then
local server_directory
local primary_server_config
server_directory="${BASE_DIR}/Server-${server_id}"
server_num="${server_id}"
primary_server_config="${server_directory}/Mordhau/Saved/Config/LinuxServer/Game-Primary.ini"
[[ ! -f "${primary_server_config}" ]] &&
log "error" "Unable to find a config for $(important "Server-${server_num}") (${primary_server_config})" &&
exit 1
if [ "${verbose}" -eq 1 ]; then
display_verbose_output "${server_num}"
fi
echo "${primary_server_config}"
if [[ "${edit}" -eq 1 ]]; then
log "info" "Editing $(important "Server-${server_num}")'s config located at ${primary_server_config}..."
nano "${primary_server_config}"
fi
fi
if [[ "${list}" -eq 1 ]]; then
for server in "${BASE_DIR}/Server-"*; do
local server_directory
local primary_server_config
server_directory="${server}"
server_num="${server: -1}"
primary_server_config="${server_directory}/Mordhau/Saved/Config/LinuxServer/Game-Primary.ini"
[[ ! -f "${primary_server_config}" ]] &&
log "error" "Unable to find a config for $(important "Server-${server_num}") (${primary_server_config})" &&
continue
if [[ "${verbose}" -eq 1 ]]; then
echo ""
display_verbose_output "${server_num}"
fi
echo "${primary_server_config}"
if [[ "${edit}" -eq 1 ]]; then
log "info" "Editing $(important "Server-${server_num}")'s config located at ${primary_server_config}..."
sleep 2
nano "${primary_server_config}"
fi
done
fi
}
start() {
local server_id
local can_kill
@ -325,14 +472,15 @@ start() {
local server_directory
local server_config
server_directory="${BASE_DIR}/Server-${server_id}"
server_config="${server_directory}/Mordhau/Saved/Config/LinuxServer/Game.ini"
server_config_directory="${server_directory}/Mordhau/Saved/Config/LinuxServer"
server_config="${server_config_directory}/Game.ini"
[[ ! -d "${server_directory}" ]] &&
log "error" "Unable to find the server directory for ${server_id}, verify the server exists at ${server_directory}" &&
exit 1
[[ ! -f "${server_config}" ]] &&
log "error" "Unable to find a valid Game.ini for ${server_id}, verify the installation is correct" &&
log "error" "Unable to find a valid Game.ini for Server-${server_id}, verify the installation is correct" &&
exit 1
local rcon_pass
@ -382,19 +530,78 @@ start() {
beacon_port=$(("${START_PORT_RANGE}" + $(("${server_id}" * 4)) + 2))
rcon_port=$(("${START_PORT_RANGE}" + $(("${server_id}" * 4)) + 3))
while read -r line; do
if [[ "${line}" == "RconPort="* ]]; then
echo "RconPort=${rcon_port}"
elif [[ "${line}" == "RconPassword="* ]]; then
echo "RconPassword=${rcon_pass}"
elif [[ "${line}" == *"Mordhau.MordhauGameSession"* ]]; then
echo "${mods}"
echo "${admins}"
elif [[ "${line}" == "Mods="* ]]; then
:
fi
done <"${server_config}" >"serv.temp" && mv "serv.temp" "${server_config}"
log "info" "Updating Game-Primary.ini"
local backup_extension
backup_extension="$(date +%Y-%m-%dT%H:%M:%S%z)"
# Necessary as the external config to Game.ini
local game_primary
game_primary="${server_config_directory}/Game-Primary.ini"
[[ ! -f "${game_primary}" ]] &&
cp "${server_config}" "${game_primary}" &&
log "info" "Created a Game-Primary.ini config as it did not exist"
log "info" "Creating Game-Primary.ini backup as Game-Primary.ini.${backup_extension}"
cp "${game_primary}" "${game_primary}.${backup_extension}"
log "info" "Created backup of Game-Primary.ini located at ${game_primary}.${backup_extension}"
# The below booleans are used to avoid duplicating lines, a sort of failsafe
local updated_rcon_port
local updated_rcon_pass
updated_rcon_port=0
updated_rcon_pass=0
# Loading primary config bool is incredibly important as it controls loading the external configurations to the server
local primary_config_admins_mods
local loading_primary_config
local loaded_primary_config
loading_primary_config=0
loaded_primary_config=0
primary_config_admins_mods="$(
cat <<__EOF__
# BEGIN PRIMARY CONFIG - DO NOT INCLUDE SERVER SPECIFIC CONFIGURATION BETWEEN THIS LINE AND "END PRIMARY CONFIG"
# Primary config Mods
${mods}
# Primary config Admins
${admins}
# END PRIMARY CONFIG - DO NOT INCLUDE SERVER SPECIFIC CONFIGURATION BETWEEN THIS LINE AND "BEGIN PRIMARY CONFIG"
__EOF__
)"
log "info" "Overwriting Game.ini..."
while read -r line; do
if [[ "${line}" == "RconPort="* ]] && [ "${updated_rcon_port}" -eq 0 ]; then
echo "RconPort=${rcon_port}"
updated_rcon_port=1
elif [[ "${line}" == "RconPassword="* ]] && [ "${updated_rcon_pass}" -eq 0 ]; then
echo "RconPassword=${rcon_pass}"
updated_rcon_pass=1
elif [[ "${line}" == *"BEGIN PRIMARY CONFIG"* ]]; then
loading_primary_config=1
elif [[ "${line}" == *"END PRIMARY CONFIG"* ]]; then
loading_primary_config=0
echo "${primary_config_admins_mods}"
loaded_primary_config=1
elif [ "${loading_primary_config}" -eq 0 ]; then
echo "${line}"
fi
done <"${game_primary}" >"serv.temp"
if [ "${loaded_primary_config}" -eq 0 ]; then
log "info" "Primary config not injected, loading it now..."
echo "${primary_config_admins_mods}" >>"serv.temp"
log "info" "Successfully injected primary config"
fi
mv "serv.temp" "${game_primary}"
cat "${game_primary}" >"${server_config}"
log "info" "Finished updating Game-Primary.ini ($(important "${game_primary}"))"
log "info" "Ports:
$(important " Game Port: ${game_port}")
@ -414,8 +621,8 @@ start() {
-QueryPort="${query_port}" \
-BeaconPort="${beacon_port}" \
-LOG \
-USEALLAVAILABLE \
&& log "info" "Successfully started Mordhau-Server-${server_id}"
-USEALLAVAILABLE &&
log "info" "Successfully started Mordhau-Server-${server_id}"
}
@ -477,7 +684,9 @@ update() {
log "info" "Verifying and updating server"
steamcmd +login anonymous +force_install_dir "${server_directory}" +app_update 629800 validate +quit
run_and_kill "${server_id}"
log "info" "Successfully verified and updated $(important "Server-${server_id}")"
}
install() {
@ -529,6 +738,7 @@ install() {
log "error" "A server already exists at ${server_directory}, delete it and try again" &&
exit 1
kill_server -s "${server_id}" >/dev/null 2>&1
mkdir -p "${server_directory}" && log "info" "Created server directory ${server_directory}"
steamcmd +login anonymous +force_install_dir "${server_directory}" +app_update 629800 validate +quit
@ -536,13 +746,13 @@ install() {
log "info" "Starting server to install default configuration files, please wait..."
tmux new-session -d -s "${prefix}-Server-${server_id}" \
"${server_directory}/MordhauServer.sh"
run_and_kill "${server_id}"
sleep 5
cp "${server_config}" "Game-Primary.ini" &&
log "info" "Created the Game-Primary.ini file"
log "info" "Finished setting up Server-${server_id}"
tmux send-keys -t "${prefix}-Server-${server_id}" "C-c" ENTER >/dev/null 2>&1 \
&& log "info" "Successfully ran the Mordhau session and stopped the session"
}
usage() {
@ -566,7 +776,9 @@ usage() {
install
Exposes options to install Mordhau Servers, pass -h to it for details
update
Exposes options to update Mordhau Servers, pass -h to it for details"
Exposes options to update Mordhau Servers, pass -h to it for details
configure
Exposes options to configure Mordhau Servers, pass -h to it for details"
}
parse_args() {
@ -612,9 +824,15 @@ parse_args() {
update "$@"
break
;;
configure | c)
shift
configure "$@"
break
;;
-?*)
printf 'Unknown option: %s\n' "$1" >&2
usage
exit 1
;;
*) # Default case: No more options, so break out of the loop.
break ;;