TavernManager
TavernLib.Backend.Api.TavernManager in TavernLib.dll. Server only.
The service that owns the server's config files. Get it from the service registry. It isn't there on a client.
var manager = TavernServices.GetService<TavernManager>();
Members
public class TavernManager : IService
| Member | File | Page |
|---|---|---|
| UserConfigFile UserConfig { get; } | users.json | User config |
| ServerSettingsConfig ServerConfig { get; } | server_settings.json | Server settings |
| TavernServerConfig TavernConfig { get; } | tavern_server.json | Ports |
| WhitelistRequestsConfigFile WhitelistRequests { get; } | whitelist_requests.json | Other config files |
Each one is a ServerConfigFile<T>: LastRead is the file as last read, Read() reads it again, Update changes it safely.
It also has two internal members, the login server (AuthManager) and the Community Servers listing (ListingController). Mods can't reach them without reflection.
When it's made
TavernLib creates it in its OnEarlyInitializeMelon on a server, so it's ready before your mod's OnInitializeMelon. The constructor:
- Reads all four files. A missing file is created with the defaults. One that can't be read is logged (
TavernLib keeps running without users.json; joins that need it are refused until it reads.) and skipped, so itsLastReadholds the defaults and itsReadableisfalse. - Starts the listing if
community_listedis on. Changing it later needs a restart. - Starts the login server on the auth port, unless the server was started with
/launcherauth. The configs are there either way.
Don't create a TavernManager yourself. A second one would try to start a second login server on the same port.
How fresh is LastRead?
TavernLib reads some files again as it goes, and each read replaces LastRead:
| File | Read again |
|---|---|
users.json | On every login and every join. |
server_settings.json | On every login request and join, on each console login (for console_owner_local_only), and when the game's GarbageCollectTimer starts. |
tavern_server.json | Never. Ports are read once at start. |
whitelist_requests.json | When a player asks to join a whitelisted server. |
So on a busy server, LastRead is rarely more than a join old. On an empty one it can be hours old. If you need the file as it is now, call Read() (see ServerConfigFile).
Roles have their own copy that's refreshed every second: use Roles for those, not UserConfig.
Example
using System;
using MelonLoader;
using TavernLib.Backend.Api;
using TavernLib.Backend.Server.Configs;
using TavernLib.Services;
public static class ReadSettings
{
public static void Log(MelonLogger.Instance log)
{
var manager = TavernServices.GetService<TavernManager>();
if (manager == null) return;
ServerSettings settings = manager.ServerConfig.LastRead;
log.Msg($"{settings.Name}: up to {settings.PlayerLimit} players, password {(settings.PasswordHash.Length > 0 ? "on" : "off")}");
TavernServer ports = manager.TavernConfig.LastRead;
log.Msg($"Game port {ports.Game}, auth {ports.Auth}, web {ports.Web}, console {ports.Console}");
}
public static ServerSettings Fresh()
{
var config = TavernServices.GetService<TavernManager>().ServerConfig;
try
{
return config.Read();
}
catch (Exception)
{
return config.LastRead; // unreadable right now: keep what was read before
}
}
}
These types use Newtonsoft.Json attributes, so reference Newtonsoft.Json from the game's Managed folder too.
See also
- Config reference for what each setting means to a host
- Files