304 lines
10 KiB
Bash
Executable File
304 lines
10 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# Container entrypoint: starts the patched Project Zomboid dedicated server
|
|
# with the ZomboidForge agent. Arguments are passed through to
|
|
# zombie.network.GameServer.
|
|
#
|
|
# Environment variables (unset or empty keep the value in the server .ini, or the game default):
|
|
#
|
|
# Server
|
|
# SERVER_NAME Server config name (/data/Server/<name>.ini). Default: zomboidforge
|
|
# SERVER_PASSWORD Password clients need to join (.ini Password)
|
|
# PUBLIC true/false: listed in the in-game server browser (.ini Public)
|
|
# PUBLIC_NAME Name in the server browser (.ini PublicName)
|
|
# PORT Game port (-port). Default: 16261
|
|
# UDP_PORT Direct connection port (-udpport). Default: 16262
|
|
# STEAM 1 = Steam mode (required for official Steam clients), 0 = no Steam. Default: 1
|
|
#
|
|
# Admin account
|
|
# ADMIN_USERNAME Admin account name (-adminusername), passed on every start.
|
|
# Project Zomboid creates the account when it does not exist. Default: admin
|
|
# ADMIN_PASSWORD Password for creating that account (-adminpassword). Required while
|
|
# the account does not exist; not passed once it does.
|
|
#
|
|
# New server
|
|
# SERVER_PRESET Sandbox preset from media/lua/shared/Sandbox (e.g. Apocalypse),
|
|
# copied to <name>_SandboxVars.lua when that file does not exist
|
|
#
|
|
# Mods
|
|
# MODS Semicolon-separated mod IDs (.ini Mods). Build 42 format: \ModA;\ModB
|
|
# WORKSHOP_ITEMS Semicolon-separated Workshop item IDs (.ini WorkshopItems)
|
|
#
|
|
# RCON
|
|
# RCON_PASSWORD Written into the .ini before every start. RCON is disabled when empty.
|
|
#
|
|
# JVM
|
|
# JVM_MIN_HEAP Value for -Xms. Default: JVM default
|
|
# JVM_MAX_HEAP Value for -Xmx. Default: 8g (the vanilla ProjectZomboid64.json value)
|
|
#
|
|
# Maintenance
|
|
# SOFT_RESET true: start in Project Zomboid's soft reset mode (-Dsoftreset).
|
|
# Intended for a single start; unset it afterwards.
|
|
|
|
set -o errexit
|
|
set -o nounset
|
|
set -o pipefail
|
|
|
|
readonly INSTALL_DIR="/opt/pz"
|
|
readonly DATA_DIR="/data"
|
|
readonly AGENT_JAR="${INSTALL_DIR}/zomboidforge-agent.jar"
|
|
readonly JAVA="${INSTALL_DIR}/jre64/bin/java"
|
|
readonly SANDBOX_PRESET_DIR="${INSTALL_DIR}/media/lua/shared/Sandbox"
|
|
|
|
readonly SERVER_NAME="${SERVER_NAME:-zomboidforge}"
|
|
readonly SERVER_PASSWORD="${SERVER_PASSWORD:-}"
|
|
readonly PUBLIC="${PUBLIC:-}"
|
|
readonly PUBLIC_NAME="${PUBLIC_NAME:-}"
|
|
readonly PORT="${PORT:-}"
|
|
readonly UDP_PORT="${UDP_PORT:-}"
|
|
readonly STEAM="${STEAM:-1}"
|
|
readonly ADMIN_USERNAME="${ADMIN_USERNAME:-}"
|
|
readonly ADMIN_PASSWORD="${ADMIN_PASSWORD:-}"
|
|
readonly SERVER_PRESET="${SERVER_PRESET:-}"
|
|
readonly MODS="${MODS:-}"
|
|
readonly WORKSHOP_ITEMS="${WORKSHOP_ITEMS:-}"
|
|
readonly RCON_PASSWORD="${RCON_PASSWORD:-}"
|
|
readonly JVM_MIN_HEAP="${JVM_MIN_HEAP:-}"
|
|
readonly JVM_MAX_HEAP="${JVM_MAX_HEAP:-8g}"
|
|
readonly SOFT_RESET="${SOFT_RESET:-}"
|
|
|
|
readonly SERVER_INI="${DATA_DIR}/Server/${SERVER_NAME}.ini"
|
|
readonly SERVER_SANDBOX_VARS="${DATA_DIR}/Server/${SERVER_NAME}_SandboxVars.lua"
|
|
readonly SERVER_DB="${DATA_DIR}/db/${SERVER_NAME}.db"
|
|
# Target of the /opt/pz/steamapps symlink; Steam stores Workshop downloads there.
|
|
readonly STEAMAPPS_DIR="${DATA_DIR}/steamapps"
|
|
|
|
log() {
|
|
echo "[entrypoint] $*" >&2
|
|
}
|
|
|
|
fail() {
|
|
log "$*"
|
|
exit 1
|
|
}
|
|
|
|
# True for 1/true/yes, case-insensitive.
|
|
is_true() {
|
|
local value="${1,,}"
|
|
[[ "${value}" == "1" || "${value}" == "true" || "${value}" == "yes" ]]
|
|
}
|
|
|
|
# True for 0/false/no, case-insensitive.
|
|
is_false() {
|
|
local value="${1,,}"
|
|
[[ "${value}" == "0" || "${value}" == "false" || "${value}" == "no" ]]
|
|
}
|
|
|
|
# Same lookup as ServerWorldDatabase.create(), which prompts on stdin for a
|
|
# password when the admin account does not exist and no -adminpassword is given.
|
|
admin_account_exists() {
|
|
local username="${ADMIN_USERNAME:-admin}"
|
|
local escaped_username="${username//\'/\'\'}"
|
|
local account_id
|
|
|
|
if [[ ! -f "${SERVER_DB}" ]]; then
|
|
return 1
|
|
fi
|
|
account_id="$(sqlite3 -readonly "${SERVER_DB}" \
|
|
"SELECT id FROM whitelist WHERE username = '${escaped_username}';")"
|
|
[[ -n "${account_id}" ]]
|
|
}
|
|
|
|
# Sets <key>=<value> in the server .ini. Creates the file or the key if missing.
|
|
# Project Zomboid reads a partial .ini and writes the missing defaults on start.
|
|
set_ini_value() {
|
|
local key="$1"
|
|
local value="$2"
|
|
local temp_file="${SERVER_INI}.tmp"
|
|
local key_found=false
|
|
local line
|
|
|
|
mkdir --parents "$(dirname "${SERVER_INI}")"
|
|
: > "${temp_file}"
|
|
|
|
if [[ -f "${SERVER_INI}" ]]; then
|
|
while IFS= read -r line || [[ -n "${line}" ]]; do
|
|
if [[ "${line}" == "${key}="* ]]; then
|
|
printf '%s=%s\n' "${key}" "${value}" >> "${temp_file}"
|
|
key_found=true
|
|
else
|
|
printf '%s\n' "${line}" >> "${temp_file}"
|
|
fi
|
|
done < "${SERVER_INI}"
|
|
fi
|
|
|
|
if [[ "${key_found}" == false ]]; then
|
|
printf '%s=%s\n' "${key}" "${value}" >> "${temp_file}"
|
|
fi
|
|
|
|
mv "${temp_file}" "${SERVER_INI}"
|
|
}
|
|
|
|
# Sets <key>=<value> only when <value> is not empty.
|
|
set_ini_value_if_given() {
|
|
local key="$1"
|
|
local value="$2"
|
|
|
|
if [[ -n "${value}" ]]; then
|
|
set_ini_value "${key}" "${value}"
|
|
fi
|
|
}
|
|
|
|
apply_ini_settings() {
|
|
set_ini_value_if_given "Password" "${SERVER_PASSWORD}"
|
|
set_ini_value_if_given "PublicName" "${PUBLIC_NAME}"
|
|
set_ini_value_if_given "Mods" "${MODS}"
|
|
set_ini_value_if_given "WorkshopItems" "${WORKSHOP_ITEMS}"
|
|
|
|
if [[ -n "${PUBLIC}" ]]; then
|
|
if is_true "${PUBLIC}"; then
|
|
set_ini_value "Public" "true"
|
|
elif is_false "${PUBLIC}"; then
|
|
set_ini_value "Public" "false"
|
|
else
|
|
fail "PUBLIC must be true or false, got '${PUBLIC}'."
|
|
fi
|
|
fi
|
|
|
|
if [[ -n "${RCON_PASSWORD}" ]]; then
|
|
set_ini_value "RCONPassword" "${RCON_PASSWORD}"
|
|
else
|
|
log "RCON_PASSWORD is not set; RCON stays disabled unless the .ini sets RCONPassword."
|
|
fi
|
|
}
|
|
|
|
# Preset names, without SandboxVars.lua (which only references a preset).
|
|
list_sandbox_presets() {
|
|
local file
|
|
local names=()
|
|
|
|
for file in "${SANDBOX_PRESET_DIR}"/*.lua; do
|
|
file="$(basename "${file}" .lua)"
|
|
if [[ "${file}" != "SandboxVars" ]]; then
|
|
names+=("${file}")
|
|
fi
|
|
done
|
|
echo "${names[*]}"
|
|
}
|
|
|
|
# Presets start with "return {" and use CRLF line endings; the server's
|
|
# <name>_SandboxVars.lua starts with "SandboxVars = {".
|
|
install_sandbox_preset() {
|
|
local preset_file="${SANDBOX_PRESET_DIR}/${SERVER_PRESET}.lua"
|
|
local first_line=true
|
|
local line
|
|
|
|
if [[ -z "${SERVER_PRESET}" || -f "${SERVER_SANDBOX_VARS}" ]]; then
|
|
return
|
|
fi
|
|
if [[ "${SERVER_PRESET}" == "SandboxVars" || ! -f "${preset_file}" ]]; then
|
|
fail "Unknown SERVER_PRESET '${SERVER_PRESET}'. Available: $(list_sandbox_presets)"
|
|
fi
|
|
|
|
log "Creating ${SERVER_SANDBOX_VARS} from preset ${SERVER_PRESET}"
|
|
mkdir --parents "$(dirname "${SERVER_SANDBOX_VARS}")"
|
|
while IFS= read -r line || [[ -n "${line}" ]]; do
|
|
line="${line%$'\r'}"
|
|
if [[ "${first_line}" == true ]]; then
|
|
if [[ "${line}" != "return {" ]]; then
|
|
fail "Unexpected first line in ${preset_file}: '${line}'"
|
|
fi
|
|
line="SandboxVars = {"
|
|
first_line=false
|
|
fi
|
|
printf '%s\n' "${line}"
|
|
done < "${preset_file}" > "${SERVER_SANDBOX_VARS}"
|
|
}
|
|
|
|
# Sets needs_admin_password, used by build_server_args.
|
|
check_admin_account() {
|
|
if admin_account_exists; then
|
|
needs_admin_password=false
|
|
if [[ -n "${ADMIN_PASSWORD}" ]]; then
|
|
log "ADMIN_PASSWORD is ignored: the admin account '${ADMIN_USERNAME:-admin}' already exists."
|
|
fi
|
|
else
|
|
needs_admin_password=true
|
|
if [[ -z "${ADMIN_PASSWORD}" ]]; then
|
|
fail "The admin account '${ADMIN_USERNAME:-admin}' of '${SERVER_NAME}' does not exist yet: ADMIN_PASSWORD is required to create it."
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# Same JVM setup as the vanilla ProjectZomboid64.json and start-server.sh.
|
|
# Java runs directly instead of through the native ProjectZomboid64 launcher.
|
|
build_jvm_args() {
|
|
jvm_args=(
|
|
"-javaagent:${AGENT_JAR}"
|
|
"-Djava.awt.headless=true"
|
|
"--enable-native-access=ALL-UNNAMED"
|
|
"-Xmx${JVM_MAX_HEAP}"
|
|
"-XX:+UseZGC"
|
|
"-XX:-OmitStackTraceInFastThrow"
|
|
"-Dzomboid.steam=${STEAM}"
|
|
"-Dzomboid.znetlog=1"
|
|
"-Djava.library.path=linux64/"
|
|
"-Djava.security.egd=file:/dev/urandom"
|
|
"-cp" "java/:java/projectzomboid.jar"
|
|
)
|
|
|
|
if [[ -n "${JVM_MIN_HEAP}" ]]; then
|
|
jvm_args+=("-Xms${JVM_MIN_HEAP}")
|
|
fi
|
|
if [[ -n "${SOFT_RESET}" ]] && is_true "${SOFT_RESET}"; then
|
|
log "SOFT_RESET is set: starting in soft reset mode. Unset it for the next start."
|
|
jvm_args+=("-Dsoftreset")
|
|
fi
|
|
}
|
|
|
|
build_server_args() {
|
|
server_args=(
|
|
"-cachedir=${DATA_DIR}"
|
|
"-servername" "${SERVER_NAME}"
|
|
)
|
|
|
|
if [[ -n "${PORT}" ]]; then
|
|
server_args+=("-port" "${PORT}")
|
|
fi
|
|
if [[ -n "${UDP_PORT}" ]]; then
|
|
server_args+=("-udpport" "${UDP_PORT}")
|
|
fi
|
|
|
|
if [[ -n "${ADMIN_USERNAME}" ]]; then
|
|
server_args+=("-adminusername" "${ADMIN_USERNAME}")
|
|
fi
|
|
# Only while the account does not exist: the value is visible in the process arguments.
|
|
if [[ "${needs_admin_password}" == true ]]; then
|
|
server_args+=("-adminpassword" "${ADMIN_PASSWORD}")
|
|
fi
|
|
}
|
|
|
|
main() {
|
|
mkdir --parents "${STEAMAPPS_DIR}"
|
|
|
|
local needs_admin_password
|
|
check_admin_account
|
|
apply_ini_settings
|
|
install_sandbox_preset
|
|
|
|
local jvm_args
|
|
local server_args
|
|
build_jvm_args
|
|
build_server_args
|
|
|
|
# Native libraries: game (linux64/), Steam (install root), JVM (jre64/lib).
|
|
# libjsig.so chains signal handlers between the JVM and native libraries.
|
|
export LD_LIBRARY_PATH="${INSTALL_DIR}/linux64:${INSTALL_DIR}:${INSTALL_DIR}/jre64/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
|
|
export LD_PRELOAD="libjsig.so${LD_PRELOAD:+:${LD_PRELOAD}}"
|
|
|
|
cd "${INSTALL_DIR}"
|
|
exec "${JAVA}" "${jvm_args[@]}" zombie.network.GameServer "${server_args[@]}" "$@"
|
|
}
|
|
|
|
main "$@"
|