Server lifecycle
Server only. This page follows a server from the command line to accepting players, then to shutdown. The types are ApplicationStartupManager, GameModeManager, ServerHostingGameMode (all with no namespace) and Alta.Networking.Servers.ServerLifetimePipeline and ServerHandler.
Starting
A TavernLib server is started like this (from the Docker image):
A Township Tale.exe -batchmode -nographics /start_server -1 false 1757 /force_offline /fly /access_token ... /refresh_token ... /identity_token ...
What happens, in order:
- The game logs in with the tokens on the command line (see Command-line flags).
- On login,
ApplicationStartupManager.RunStartupActionsruns. It reads/start_serverand expects three values after it: server id, isHeadless (true/false) and port. Bad values log "Could not parse ..." and the server doesn't start. - Server id
-1means a local server, made fromMenuSettings.LocalGameServerAccess(0). Any other id asks Alta's API for that server, which no longer exists. GameModeManager.StartServermakes aServerHostingGameModeand activates it. It loads the server's scene (ServerInfo.SceneIndex).- If isHeadless is
truebut the process isn't already headless, the game relaunches itself with-batchmode -nographicsand quits (only when/loginor/login_hashis on the command line; otherwise it logs an error and does nothing). TavernLib's servers passfalseand add-batchmodethemselves, so this never happens. ServerLifetimePipeline.AttemptToStartServeropens a server socket on the port (NetworkManager.CreateSocket(isServer: true, ...)), sets NetworkSceneManager.IsServer = true and makes theServerHandler.ServerHandler's constructor setsServerHandler.Current, picks the save folder, loadsServerConfiguration.json, starts the world autosave (if saving is on) and queues its first bootup step.ServerHandler.Bootup()runs the bootup steps one at a time: first the handler's ownInitialize(registers with the API, starts the web server on 1761, raisesGeneralGameEvents.ServerStarted, starts the API ping loop), then the scene'sInitialize(loads the global and void chunks, spawns the scene's embedded entities as server, raisesNetworkScene.ServerStarted).- When the queue is empty,
BootupFinishedis raised and the status becomes "Ready".
Players can connect during bootup. Their join waits: the server sends them the bootup status ("Initializing Scene 1/2" and so on) as ServerStatus messages until it's done.
Events in order
| When | Event | Notes |
|---|---|---|
| Handler made | ServerHandler.Initialized (static) | Raised from the constructor. AddBootupStep and AddConnectionStep don't work yet. Also raised by the main menu's dummy server, and with null when the server stops. |
| First bootup step | GeneralGameEvents.ServerStarted (static) | After the API registration. The scene isn't loaded yet. |
| Scene ready | NetworkSceneManager.Current.ServerStarted | Global chunk loaded, embedded entities spawned. Only exists on NetworkScene, cast NetworkSceneManager.Current to it. |
| All steps done | ServerHandler.Current.BootupFinished | Safe point to call AddConnectionStep. |
| Each player | PlayerJoined / PlayerLeft | See ServerHandler. |
| Stopping | GeneralGameEvents.ServerEnded (static) | From OnServerStopped, after the socket has closed. |
All of these run on Unity's main thread.
Adding your own bootup step
ServerHandler.AddBootupStep(BootupStep step) queues an async step that runs before players are let in. BootupStep is Task (Action<string> progressNotification). The queue only exists between the end of the constructor and the end of Bootup(), so Initialized is too early (the queue isn't made yet and the call logs "Too late to add bootup step"). The game's own advice in that error is to add steps from an embedded entity's InitializeAsServer. From a mod, a postfix on ServerHandler's public constructor works:
using System.Threading.Tasks;
using Alta.Api.Client.HighLevel;
using Alta.Networking;
using Alta.Networking.Servers;
using HarmonyLib;
[HarmonyPatch(typeof(ServerHandler), MethodType.Constructor,
new[] { typeof(ISocket), typeof(IServerAccess), typeof(bool) })]
static class AddMyBootupStep
{
static void Postfix(ServerHandler __instance)
{
__instance.AddBootupStep(async progress =>
{
progress("Loading my mod's data");
await Task.Delay(100);
});
}
}
IServerAccess is in Alta.Api.Client.dll, so add a reference to it. The step runs after the handler's own Initialize and before the scene loads (the scene's step is added after the constructor returns).
Stopping
ServerHandler.StopAsync(ShutdownReason reason):
- Stops the ping loop.
- Saves the world instantly (StreamerManager.Instance.SaveWorld(isInstant: true), which blocks), unless a wipe is running.
- Closes the socket, which disconnects everyone.
- Disposes the web server, raises
GeneralGameEvents.ServerEnded, tells the API it's stopping. - Unloads the save files and sets
ServerHandler.Currenttonull.
ServerHostingGameMode.StopModeAsync calls it, so GameModeManager.StopCurrentModeAsync(reason) stops the server the normal way. ApplicationManager.ExternalOnApplicationQuit(new ShutdownReason(reason, isExpected: true)) quits the whole program, which is what TavernLib's "return to menu" patch does.
A player is saved when they leave (see Saving). Players still connected at shutdown are saved by the instant world save before the socket closes, since it ends with Player.SaveAll().
Other types
| Type | What it is |
|---|---|
GameModeManager | Static. CurrentMode, CurrentGameServerInfo, StartServer, JoinServer, StartLocalTest, StopCurrentModeAsync, event ModeChanged. One mode at a time: starting a second logs "Already have a game mode active". |
GameMode | Abstract base: IsClient, IsServer, ActivateAsync(), StopModeAsync. Subclasses: ServerHostingGameMode, JoinedServerGameMode, LocalTestGameMode. |
ServerLifetimePipeline | Static. Opens the server socket and makes the handler. GetServerRegion(), GetServerFleet(). |
ServerLockCollection, ServerJoinLock | ServerHandler.ServerLocks. While any lock is in it, every join is refused with the highest lock's message plus ", check Discord". TavernLib's chunk merge command uses one. |
GarbageCollectTimer | On the scene. Only runs when the process is headless: every interval it asks for a non-blocking, optimized GC.Collect of every generation. The interval is prefab data (30 s going by TavernLib's notes; the code default is 2 s). TavernLib makes it configurable with garbage_collection_timer in server_settings.json. |