Improve/add initial project structure for ZomboidForge with build system, runtime, and patching logic.

Added version support for building images.
This commit is contained in:
Overlord committed 2026-09-29 16:51:13 +02:00
1 parent bf9228d2be
commit 015e9bb2c8
32 files changed
+1268 -58

No files matched your search

+210 -27
View File
@@ -4,12 +4,41 @@
# with the ZomboidForge agent. Arguments are passed through to
# zombie.network.GameServer.
#
# Environment variables:
# SERVER_NAME Server config name (/data/Server/<name>.ini). Default: zomboidforge
# ADMIN_PASSWORD Admin account password. Required on the first start only.
# RCON_PASSWORD Written into the server .ini before every start. RCON is disabled when empty.
# JVM_MAX_HEAP Value for -Xmx. Default: 8g (the vanilla ProjectZomboid64.json value)
# STEAM 1 = Steam mode (required for official Steam clients), 0 = no Steam. Default: 1
# 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
@@ -19,20 +48,67 @@ 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 ADMIN_PASSWORD="${ADMIN_PASSWORD:-}"
readonly RCON_PASSWORD="${RCON_PASSWORD:-}"
readonly JVM_MAX_HEAP="${JVM_MAX_HEAP:-8g}"
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() {
@@ -63,30 +139,101 @@ set_ini_value() {
mv "${temp_file}" "${SERVER_INI}"
}
apply_rcon_password() {
if [[ -z "${RCON_PASSWORD}" ]]; then
# 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
set_ini_value "RCONPassword" "${RCON_PASSWORD}"
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}"
}
# Without -adminpassword, Project Zomboid blocks on an interactive prompt to
# create the admin account on the first start.
check_admin_password() {
if [[ -z "${ADMIN_PASSWORD}" && ! -f "${SERVER_DB}" ]]; then
log "First start of '${SERVER_NAME}': ADMIN_PASSWORD is required to create the admin account."
exit 1
# 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
}
main() {
apply_rcon_password
check_admin_password
# Same JVM setup as the vanilla ProjectZomboid64.json and start-server.sh.
# Java runs directly instead of through the native ProjectZomboid64 launcher.
local jvm_args=(
# 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"
@@ -100,13 +247,49 @@ main() {
"-cp" "java/:java/projectzomboid.jar"
)
local server_args=(
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 "${ADMIN_PASSWORD}" ]]; then
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.
+48 -3
View File
@@ -4,8 +4,14 @@
# with steamcmd, using an anonymous login. Runs in the steamcmd image during
# the Docker build.
#
# Usage: install-server.sh <install-dir> <branch>
# <branch> Steam branch; "public" is the default branch.
# Usage: install-server.sh <install-dir> <branch> <steamclient>
# <branch> Steam branch: "public" (default branch), "unstable", or a
# version branch such as "42.19".
# <steamclient> "bundled" keeps the steamclient.so files shipped with the
# server; "steamcmd" replaces them with steamcmd's own, newer
# copies (a known workaround for failing Workshop downloads).
#
# Writes the installed Steam build ID to <install-dir>/steam-buildid.
set -o errexit
set -o nounset
@@ -13,15 +19,17 @@ set -o pipefail
readonly APP_ID="380870"
readonly MAX_ATTEMPTS=3
readonly STEAMCMD_DIR="${HOME}/.local/share/Steam/steamcmd"
readonly INSTALL_DIR="$1"
readonly BRANCH="$2"
readonly STEAMCLIENT="$3"
log() {
echo "[install-server] $*" >&2
}
main() {
install_app() {
local branch_args=()
if [[ "${BRANCH}" != "public" ]]; then
branch_args=("-beta" "${BRANCH}")
@@ -47,6 +55,43 @@ main() {
log "Installation failed: ${INSTALL_DIR}/java/projectzomboid.jar not found"
exit 1
fi
}
# Copies the build ID from Steam's app manifest, which is removed afterwards.
record_build_id() {
local manifest="${INSTALL_DIR}/steamapps/appmanifest_${APP_ID}.acf"
local build_id
build_id="$(grep --max-count=1 '"buildid"' "${manifest}" | tr --delete --complement '0-9')"
if [[ -z "${build_id}" ]]; then
log "No buildid found in ${manifest}"
exit 1
fi
echo "${build_id}" > "${INSTALL_DIR}/steam-buildid"
log "Installed Steam build ${build_id}"
}
# The install root holds the 32-bit steamclient.so, linux64/ the 64-bit one.
replace_steamclient() {
case "${STEAMCLIENT}" in
bundled)
;;
steamcmd)
log "Replacing the bundled steamclient.so files with steamcmd's"
cp "${STEAMCMD_DIR}/linux32/steamclient.so" "${INSTALL_DIR}/steamclient.so"
cp "${STEAMCMD_DIR}/linux64/steamclient.so" "${INSTALL_DIR}/linux64/steamclient.so"
;;
*)
log "Unknown steamclient option '${STEAMCLIENT}' (expected: bundled, steamcmd)"
exit 1
;;
esac
}
main() {
install_app
record_build_id
replace_steamclient
# Steam's install metadata; not needed at runtime.
rm --recursive --force "${INSTALL_DIR}/steamapps"