diff --git a/MOD_DEVELOPMENT.md b/MOD_DEVELOPMENT.md index 4e45b50..277e450 100644 --- a/MOD_DEVELOPMENT.md +++ b/MOD_DEVELOPMENT.md @@ -142,7 +142,9 @@ public final class ShopMod extends Mod @Override public void onLoad() // main thread, registration only { - this.events().onMain(TickEvent.class, event -> { /* every frame, on the main thread */ }); + this.events().onMain(TickEvent.class, event -> { + /* every frame, on the main thread */ + }); } @Override @@ -150,8 +152,12 @@ public final class ShopMod extends Mod { this.modThread().scheduleAtFixedRate(() -> { // on zf-mod-shop: slow work, no game objects - this.main().submit(GameServer::getPlayerCount) // game objects only on the main thread - .thenAcceptAsync(players -> this.logger().log(INFO, players + " online"), this.modThread()); + this.main() + .submit(GameServer::getPlayerCount) // game objects only on the main thread + .thenAcceptAsync( + players -> this.logger().log(INFO, players + " online"), + this.modThread() + ); }, Duration.ZERO, Duration.ofMinutes(1)); } @@ -162,7 +168,7 @@ public final class ShopMod extends Mod ## Services -Methods of `Mod`, usable from the constructor on (also in field initializers): +Methods of `Mod`, usable from the constructor on, and in field initializers: | Method | Returns | Use | |--------------------|------------------|---------------------------------------------------------------| diff --git a/README.md b/README.md index c6e0de2..2c99604 100644 --- a/README.md +++ b/README.md @@ -22,10 +22,10 @@ podman build --platform linux/amd64 -t zomboidforge . # or: docker build ... | `FORGE_VERSION` | `0.0.0-prerelease` | ZomboidForge version (jar manifest, image label). The build stage has no `git`, so the version is never read from a tag there; `ci/release.sh` passes it | | `DISABLED_PATCHES` | | Comma-separated simple class names of build-time patches to skip (debug builds only), e.g. `IsoZombieVehiclePerformancePatch` | -Published images are tagged `-pz` (never overwritten), -`-pz` (the newest hotfix of that minor version), and `latest`. A new image -is built for every ZomboidForge release and every Project Zomboid version, hotfixes included: -the server only admits clients with the same version. +> [!NOTE] +> Published images are tagged `-pz`, `-pz`, and `latest`.
+> A new image is built for every ZomboidForge release and every Project Zomboid version, +> hotfixes included, as the server only admits client with the exact same version. ## Running @@ -38,24 +38,26 @@ podman run -d --name zomboidforge \ Unset variables keep the value in the server `.ini` (or the game default). -| Variable | Default | | -|-------------------|----------------|-----------------------------------------------------------------------------------------------------| -| `SERVER_NAME` | `zomboidforge` | Server config name (`/data/Server/.ini`) | -| `SERVER_PASSWORD` | | Password clients need to join (`.ini` `Password`) | -| `PUBLIC` | | `true`/`false`: listed in the in-game server browser | -| `PUBLIC_NAME` | | Name in the server browser | -| `PORT` | `16261` | Game port (`-port`) | -| `UDP_PORT` | `16262` | Direct connection port (`-udpport`) | -| `ADMIN_USERNAME` | `admin` | First start only | -| `ADMIN_PASSWORD` | | Required on the first start, ignored afterwards | -| `SERVER_PRESET` | | Sandbox preset for a new server: `Apocalypse`, `Extinction`, `Outbreak`, `Rising`, `SixMonthsLater` | -| `MODS` | | Mod IDs, Build 42 format: `\ModA;\ModB` | -| `WORKSHOP_ITEMS` | | Workshop item IDs: `123;456` | -| `RCON_PASSWORD` | | Written into the `.ini` on every start; RCON is disabled without it | -| `JVM_MIN_HEAP` | `JVM_MAX_HEAP` | `-Xms` | -| `JVM_MAX_HEAP` | `8g` | `-Xmx` | -| `STEAM` | `1` | `0` runs the server without Steam | -| `SOFT_RESET` | | `true` starts once in soft reset mode (`-Dsoftreset`); unset it afterwards | +| Variable | Default | | +|----------------------|--------------------------|-----------------------------------------------------------------------------------------------------| +| `SERVER_NAME` | `zomboidforge` | Server config name (`/data/Server/.ini`) | +| `SERVER_PASSWORD` | | Password clients need to join (`.ini` `Password`) | +| `PUBLIC` | | `true`/`false`: listed in the in-game server browser | +| `PUBLIC_NAME` | | Name in the server browser | +| `PORT` | `16261` | Game port (`-port`) | +| `UDP_PORT` | `16262` | Direct connection port (`-udpport`) | +| `UPNP` | | `true`/`false`: UPnP port mapping on the router (`.ini` `UPnP`); `false` recommended (see below) | +| `ADMIN_USERNAME` | `admin` | First start only | +| `ADMIN_PASSWORD` | | Required on the first start, ignored afterwards | +| `SERVER_PRESET` | | Sandbox preset for a new server: `Apocalypse`, `Extinction`, `Outbreak`, `Rising`, `SixMonthsLater` | +| `MODS` | | Mod IDs, Build 42 format: `\ModA;\ModB` | +| `WORKSHOP_ITEMS` | | Workshop item IDs: `123;456` | +| `RCON_PASSWORD` | | Written into the `.ini` on every start; RCON is disabled without it | +| `JVM_MIN_HEAP` | JVM default | `-Xms`; unused heap memory is returned to the system only down to this size | +| `JVM_MAX_HEAP` | 75 % of the memory limit | `-Xmx`; `8g` when the container has no memory limit | +| `JVM_UNCOMMIT_DELAY` | `60` | Seconds after which unused heap memory is returned to the system (`-XX:ZUncommitDelay`) | +| `STEAM` | `1` | `0` runs the server without Steam | +| `SOFT_RESET` | | `true` starts once in soft reset mode (`-Dsoftreset`); unset it afterwards | Notes: @@ -66,10 +68,16 @@ Notes: items (`/data/steamapps`). The server runs as UID 1000. A bind-mounted host directory must be writable by UID 1000 (`chown 1000:1000 `); with rootless Podman, use `--userns=keep-id:uid=1000,gid=1000` to map it to your own user. -- **Stopping:** `SIGTERM` triggers the game's own save-and-quit. Allow enough time for large - worlds (`podman stop -t 120`, `stop_grace_period` in compose). +- **UPnP:** the game enables UPnP by default. In a container the server sees only its own + container address and cannot map the host's ports, and the router search delays every start + by about 12 s; set `UPNP=false` and publish the ports instead. +- **Memory:** with a memory limit (`--memory`, `mem_limit`), the heap gets 75 % of it and the + rest stays for native memory. The startup briefly needs a large heap; with 8 players + exploring, the heap held up to 4.4 GB of live data in tests. A limit below about 6 GB is + not recommended. Without a limit the heap is `8g`, as in vanilla. +- **Stopping:** `SIGTERM` triggers the game's own save-and-quit. Allow enough time for large worlds. -For local testing there is `compose.yaml` (`podman compose up --build`, settings from `.env`). +For local testing use `compose.yaml` (`podman compose up --build`, settings from `.env`). ## Mods @@ -95,24 +103,24 @@ forge/ podman run ... -v ./forge:/forge zomboidforge ``` -Mods are trusted code. They run inside the server process with its full privileges: they can -read and write any file the server can, open network connections, and stop the server. The -class loader isolation and the startup checks catch mistakes; they do not contain a malicious -mod. Install only mods whose authors you trust. - -A mod's settings live in its `config.toml`. When the file is missing, it is created from the -mod's defaults if the folder is writable by UID 1000; otherwise the defaults are used without a -file. Keys missing from `config.toml` take the default. `enabled = false` in its `[forge]` -table switches the mod off; mods that depend on it then fail to load. +> [!WARNING] +> Mods are trusted code.
+> They run inside the server process with its full privileges, which means they can +> read and write any file the server can, open network connections, and stop the server.
+> The class loader isolation and the startup checks catch mistakes; they do not contain +> or restrict a malicious mod. +> +> Install only mods whose authors you trust, or whose code you can verify yourself! Mods keep their data in `zomboidforge.db` in the world's save folder (`/data/Saves/Multiplayer//`). Vanilla backups include it, and deleting the world deletes it. A mod that cannot be loaded stops the server with exit code 1, and the log lists every -problem: a missing or too old dependency, an unsupported game version, game classes or methods -that do not exist in the running version, invalid settings, or an exception in the mod's -startup code. Changes to mods and their settings take effect when the server restarts. +problem. For example, a missing or too old dependency, an unsupported game version, +game classes or methods that do not exist in the running version, invalid settings, +or an exception in the mod's startup code. Changes to mods and their settings take +effect when the server restarts. `forge.toml`, every key optional: @@ -139,9 +147,11 @@ server jar, `lib/server/java/projectzomboid.jar` by default (override with `-Ppz ./gradlew :patcher:patchServerJar # -> patcher/build/server/projectzomboid.jar ``` -Game-specific code lives in `compat/b/`; `api` and `core` are version-independent. -`pz.version` in `gradle.properties` selects the targeted version. `mod-tools` (the build-time -mod checks) and `gradle-plugin` (the plugin for mod authors) are for mod builds, not the server. +> [!IMPORTANT] +> Game-specific code lives in `compat/b/`; `api` and `core` are version-independent.
+> `pz.version` in `gradle.properties` selects the targeted version.
+> `mod-tools` (the build-time mod checks) and `gradle-plugin` (the plugin for mod authors) +> are for mod builds, not the server. Release tags publish `api`, `mod-tools`, and the Gradle plugin to the Gitea Maven registry (`.gitea/workflows/publish.yml`, `ci/publish.sh`); tags with a `-` are only tested. diff --git a/compose.yaml b/compose.yaml index 79ac385..f87453b 100644 --- a/compose.yaml +++ b/compose.yaml @@ -6,20 +6,22 @@ services: image: localhost/zomboidforge:dev platform: linux/amd64 environment: - SERVER_NAME: ${SERVER_NAME:-zomboidforgetest} - SERVER_PASSWORD: ${SERVER_PASSWORD:-} - PUBLIC: ${PUBLIC:-} - PUBLIC_NAME: ${PUBLIC_NAME:-} - PORT: ${PORT:-16261} - UDP_PORT: ${UDP_PORT:-16262} - ADMIN_USERNAME: ${ADMIN_USERNAME:-} - ADMIN_PASSWORD: ${ADMIN_PASSWORD:-} - SERVER_PRESET: ${SERVER_PRESET:-} - MODS: ${MODS:-} - WORKSHOP_ITEMS: ${WORKSHOP_ITEMS:-} - RCON_PASSWORD: ${RCON_PASSWORD:-} - JVM_MIN_HEAP: ${JVM_MIN_HEAP:-} - JVM_MAX_HEAP: ${JVM_MAX_HEAP:-8g} + SERVER_NAME: ${SERVER_NAME:-zomboidforgetest} + SERVER_PASSWORD: ${SERVER_PASSWORD:-} + PUBLIC: ${PUBLIC:-} + PUBLIC_NAME: ${PUBLIC_NAME:-} + PORT: ${PORT:-16261} + UDP_PORT: ${UDP_PORT:-16262} + ADMIN_USERNAME: ${ADMIN_USERNAME:-} + ADMIN_PASSWORD: ${ADMIN_PASSWORD:-} + SERVER_PRESET: ${SERVER_PRESET:-} + MODS: ${MODS:-} + WORKSHOP_ITEMS: ${WORKSHOP_ITEMS:-} + RCON_PASSWORD: ${RCON_PASSWORD:-} + UPNP: ${UPNP:-} + JVM_MIN_HEAP: ${JVM_MIN_HEAP:-} + JVM_MAX_HEAP: ${JVM_MAX_HEAP:-} + JVM_UNCOMMIT_DELAY: ${JVM_UNCOMMIT_DELAY:-} ports: # Host and container ports must be equal: the server reports its own port # to Steam and to clients, so a remapped port is unreachable. diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index a5cc9a6..df94836 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -14,6 +14,10 @@ # 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 +# UPNP true/false: UPnP port mapping on the router (.ini UPnP). false is +# recommended: in a container the server sees only its container address, +# publishing the ports is the container runtime's job, and the router +# discovery delays every start by about 12 s. # # Admin account # ADMIN_USERNAME Admin account name (-adminusername), passed on every start. @@ -33,8 +37,14 @@ # 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) +# JVM_MIN_HEAP Value for -Xms. Default: JVM default. ZGC returns unused heap memory +# only down to this size. +# JVM_MAX_HEAP Value for -Xmx. Default: 75 % of the container memory limit +# (cgroup v2 memory.max); 8g, the vanilla ProjectZomboid64.json value, +# when the container has no limit +# JVM_UNCOMMIT_DELAY +# Seconds of disuse after which ZGC returns heap memory to the system +# (-XX:ZUncommitDelay). Default: 60 (JVM default: 300) # # Maintenance # SOFT_RESET true: start in Project Zomboid's soft reset mode (-Dsoftreset). @@ -58,6 +68,7 @@ readonly PUBLIC_NAME="${PUBLIC_NAME:-}" readonly PORT="${PORT:-}" readonly UDP_PORT="${UDP_PORT:-}" readonly STEAM="${STEAM:-1}" +readonly UPNP="${UPNP:-}" readonly ADMIN_USERNAME="${ADMIN_USERNAME:-}" readonly ADMIN_PASSWORD="${ADMIN_PASSWORD:-}" readonly SERVER_PRESET="${SERVER_PRESET:-}" @@ -65,7 +76,8 @@ 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 JVM_MAX_HEAP="${JVM_MAX_HEAP:-}" +readonly JVM_UNCOMMIT_DELAY="${JVM_UNCOMMIT_DELAY:-60}" readonly SOFT_RESET="${SOFT_RESET:-}" readonly SERVER_INI="${DATA_DIR}/Server/${SERVER_NAME}.ini" @@ -73,6 +85,8 @@ 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" +readonly CGROUP_MEMORY_MAX="/sys/fs/cgroup/memory.max" +readonly UNLIMITED_MAX_HEAP="8g" log() { echo "[entrypoint] $*" >&2 @@ -166,6 +180,16 @@ apply_ini_settings() { fi fi + if [[ -n "${UPNP}" ]]; then + if is_true "${UPNP}"; then + set_ini_value "UPnP" "true" + elif is_false "${UPNP}"; then + set_ini_value "UPnP" "false" + else + fail "UPNP must be true or false, got '${UPNP}'." + fi + fi + if [[ -n "${RCON_PASSWORD}" ]]; then set_ini_value "RCONPassword" "${RCON_PASSWORD}" else @@ -231,16 +255,44 @@ check_admin_account() { fi } +# 75 % of the cgroup v2 memory limit in MiB; UNLIMITED_MAX_HEAP without a limit. +# The rest of the limit is left for native memory (JVM, native libraries, thread stacks). +default_max_heap() { + local limit + + if [[ ! -r "${CGROUP_MEMORY_MAX}" ]]; then + echo "${UNLIMITED_MAX_HEAP}" + return + fi + limit="$(< "${CGROUP_MEMORY_MAX}")" + if [[ "${limit}" == "max" ]]; then + echo "${UNLIMITED_MAX_HEAP}" + return + fi + echo "$(( limit * 3 / 4 / 1024 / 1024 ))m" +} + # 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() { + local max_heap="${JVM_MAX_HEAP}" + + if [[ -z "${max_heap}" ]]; then + max_heap="$(default_max_heap)" + log "JVM_MAX_HEAP is not set: using -Xmx${max_heap}." + fi + if [[ ! "${JVM_UNCOMMIT_DELAY}" =~ ^[0-9]+$ ]]; then + fail "JVM_UNCOMMIT_DELAY must be a number of seconds, got '${JVM_UNCOMMIT_DELAY}'." + fi + jvm_args=( "-javaagent:${AGENT_JAR}" "-Dzomboidforge.dir=${FORGE_DIR}" "-Djava.awt.headless=true" "--enable-native-access=ALL-UNNAMED" - "-Xmx${JVM_MAX_HEAP}" + "-Xmx${max_heap}" "-XX:+UseZGC" + "-XX:ZUncommitDelay=${JVM_UNCOMMIT_DELAY}" "-XX:-OmitStackTraceInFastThrow" "-Dzomboid.steam=${STEAM}" "-Dzomboid.znetlog=1"