Add ServerLoadPatch, ServerStoppingPatch, and ServerTickPatch to enhance server lifecycle handling in GameServer. Include tests, update B42PatchSet, and integrate lifecycle management utilities.

This commit is contained in:
Overlord committed 2026-10-01 20:03:10 +02:00
1 parent 600373dc61
commit 837367047c
11 files changed
+787 -3

No files matched your search

@@ -1,17 +1,84 @@
package dev.overlord.zomboidforge.compat.b42.hooks;
import dev.overlord.zomboidforge.api.ZomboidForge;
import dev.overlord.zomboidforge.core.Lifecycle;
/** Hooks called by patched {@code zombie.network.GameServer} code. */
/**
* Hooks called by patched {@code zombie.network.GameServer} code and its shutdown hook
* {@code GameServer$1}. Exceptions are caught here: the main loop logs repeated exceptions only
* a limited number of times, and an exception in the shutdown hook would skip the world save.
*/
@SuppressWarnings("CallToPrintStackTrace")
public final class ServerHooks
{
private static final Lifecycle LIFECYCLE = new Lifecycle();
private ServerHooks() {}
/**
* Called once on the main thread in {@code GameServer.main}, directly after
* {@code ServerOptions.instance.init()}: logging is set up, and the server name and cache
* directory are known. The Workshop download, the server database, Lua, and the world come
* later.
*
* @return {@code false} to stop the server; the patched code then removes the vanilla
* shutdown hook and exits with code 1
*/
public static boolean onLoad()
{
try
{
ServerHooks.LIFECYCLE.loaded();
System.out.println("[ZomboidForge] " + ZomboidForge.VERSION + " load phase.");
return true;
}
catch (Throwable t)
{
System.out.println("[ZomboidForge] Startup failed, stopping the server.");
t.printStackTrace();
return false;
}
}
/**
* Called once on the main thread, after the world is loaded, the network port is open, and
* RCON is initialized; directly before the main loop starts.
*/
public static void onServerStarted() {
System.out.println("[ZomboidForge] Server started (ZomboidForge " + ZomboidForge.VERSION + ").");
public static void onServerStarted()
{
try
{
ServerHooks.LIFECYCLE.started();
System.out.println("[ZomboidForge] Server started (ZomboidForge " + ZomboidForge.VERSION + ").");
}
catch (RuntimeException e) { e.printStackTrace(); }
}
/**
* Called on the main thread for every frame of the main loop, directly after
* {@code ServerMap.instance.preupdate()}, inside the {@code frameStep} profiling probe. Not
* called while the update limiter skips a frame.
*/
public static void onTick() {}
/**
* Called once on the shutdown hook thread ({@code GameServer$1}), after
* {@code GameServer.done = true} and before {@code ServerMap.instance.QueuedQuit()} saves the
* world. Both ways the server stops lead here: the {@code quit} command makes
* {@code ServerMap.preupdate()} call {@code System.exit(0)}, and SIGTERM or SIGINT start the
* JVM shutdown directly. In the second case the main thread may still be inside a frame. Not
* called in soft-reset mode.
*/
public static void onStopping()
{
try
{
if (!ServerHooks.LIFECYCLE.stopping()) return;
System.out.println("[ZomboidForge] Server stopping.");
}
catch (RuntimeException e) { e.printStackTrace(); }
}
}