"""Project Zomboid support: container settings, Steam Workshop lookups and mod info. Uses the ghcr.io/gameserver-images/project-zomboid image, which is configured entirely through environment variables (INI_ for pzserver.ini, WORKSHOP_IDS, MEMORY, ...). It downloads workshop items itself, adds mod maps to Map= and logs "Warning:"/"Error:" lines about mod problems. """ import json import re import time import urllib.parse import urllib.request IMAGE = "ghcr.io/gameserver-images/project-zomboid:1" WORKSHOP_APP_ID = 108600 # Project Zomboid's Steam app (the dedicated server is 380870) BRANCHES = {"public": "Build 42", "legacy41": "Build 41"} SERVERNAME = "pzserver" MEMORY_OVERHEAD_MB = 768 # JVM and native memory on top of the Java heap MOD_ID_RE = re.compile(r"^[^;\\\r\n]{1,100}$") STEAM_API = "https://api.steampowered.com/ISteamRemoteStorage" # Mods offered as checkboxes when creating a Build 42 server. Workshop and mod IDs were checked with # `list-mods` on build 42.21.0 (2026-10-16). Each entry carries its required items, in load order; # "main" names the entry's own items (the rest are dependencies) for its "last updated" date; # "includes" names those dependencies for the checklist. PRESET_MODS = [ {"key": "common_sense", "name": "Common Sense", "note": "B42.20+ update", "items": ["3750253491"], "mods": ["VB_CommonSense"]}, {"key": "better_sorting", "name": "Better Sorting", "items": ["2313387159"], "mods": ["BetterSortCC"]}, {"key": "proximity_inventory", "name": "Proximity Inventory", "items": ["2847184718"], "mods": ["ProximityInventory"]}, {"key": "has_been_read", "name": "Has Been Read", "items": ["2544353492"], "mods": ["P4HasBeenRead"]}, {"key": "skill_recovery_journal", "name": "Skill Recovery Journal", "includes": ["errorMagnifier", "Mod Update and Alert System"], "items": ["3077900375", "2896041179", "2503622437"], "main": ["2503622437"], "mods": ["ChuckleberryFinnAlertSystem", "errorMagnifier", "SkillRecoveryJournal"]}, {"key": "clean_ui", "name": "CleanUI + Clean HotBar", "includes": ["NeatUI Framework"], "items": ["3508537032", "3437629766", "3461263912"], "main": ["3437629766", "3461263912"], "mods": ["NeatUI_Framework", "CleanUI", "CleanHotBar"]}, {"key": "better_auto_mechanics", "name": "Better Auto Mechanics", "items": ["3635856965"], "mods": ["better-auto-mechanics"]}, {"key": "combat_text", "name": "Combat Text", "items": ["2286124931"], "mods": ["CombatText"]}, {"key": "kill_count", "name": "KillCount", "items": ["2553809727"], "mods": ["KillCount"]}, {"key": "map_legend_ui", "name": "Map Legend UI", "items": ["2710167561"], "mods": ["MapLegendUI"]}, ] PRESET_BRANCH = "public" # the preset is Build 42 only def preset_selection(keys): """(workshop_ids, mods) for the chosen preset keys, without duplicates, in load order.""" chosen = [m for m in PRESET_MODS if m["key"] in set(keys)] if len(chosen) != len(set(keys)): raise ValueError("Unknown mod in the selection") items = list(dict.fromkeys(i for m in chosen for i in m["items"])) mods = list(dict.fromkeys(i for m in chosen for i in m["mods"])) return items, mods _presets_cache = {"t": 0, "data": None} def presets_with_updates(): """PRESET_MODS plus "updated" (unix time of the newest main item's last Workshop update). Looked up on Steam at most once an hour; without Steam the list is returned without dates. """ if _presets_cache["data"] and time.time() - _presets_cache["t"] < 3600: return _presets_cache["data"] try: details = workshop_details([i for m in PRESET_MODS for i in m["items"]]) except Exception: return PRESET_MODS data = [] for m in PRESET_MODS: times = [details.get(i, {}).get("updated") for i in m.get("main", m["items"])] data.append({**m, "updated": max((t for t in times if t), default=None)}) _presets_cache.update(t=time.time(), data=data) return data def container_env(cfg): """Environment for the image from a server's stored config.""" mods = cfg.get("mods", []) if cfg.get("branch", "public") != "legacy41": mods = ["\\" + m for m in mods] # Build 42 writes mod IDs with a backslash in front return { "SERVERNAME": SERVERNAME, "GAME_BRANCH": cfg.get("branch", "public"), "ADMINPASSWORD": cfg["admin_password"], "INI_PublicName": cfg["server_name"], "INI_Public": "true", "INI_Password": cfg.get("password", ""), "INI_MaxPlayers": str(cfg.get("max_players", 8)), "INI_BackupsOnVersionChange": "true", "PORT": str(cfg["port"]), "UDPPORT": str(cfg["port"] + 1), "MEMORY": f"{cfg['memory_mb']}m", # Always set, even when empty: the image keeps a removed variable's last value in the ini. "WORKSHOP_IDS": ";".join(cfg.get("workshop_ids", [])), "INI_Mods": ";".join(mods), "MOD_UPDATE_CHECK": "15m" if cfg.get("auto_update", True) else "", "TZ": "UTC", } # ---------- Steam Workshop ---------- def parse_workshop_ids(text): """Workshop IDs from links (…?id=123) and bare numbers, in order, without duplicates.""" ids = re.findall(r"(?