TavernLib API
TavernLib is the MelonLoader plugin every Tavern client and server has. Besides keeping servers working without Alta, it has a small API for mods: players' roles, a place to share services between mods, an event for the game's network socket, its folder paths, and read access to its config files and mod manager.
These pages are the reference. For how-tos, see the TavernLib guides: What TavernLib does, Roles, Events and services, Console commands and Folders and files.
They're written from TavernLib 1.5.4's source, with the mod manager, SRV record, server settings and incompatible-mods work included. If a member here is missing from your TavernLib.dll, you have an older build.
Referencing TavernLib
The launcher's Setup installs it at Plugins\TavernLib.dll in the game folder. Add it to your .csproj next to your game references:
<Reference Include="TavernLib" HintPath="$(GameDir)\Plugins\TavernLib.dll" Private="false" />
Private="false" stops your build copying TavernLib next to your DLL. Never ship a copy with your mod. Players already have it, at the version their launcher installed.
Some types need more references:
| You use | Also reference (from A Township Tale_Data\Managed) |
|---|---|
Config types, or your own ServerConfigFile<T> | Newtonsoft.Json |
CommandLineArguments (the game's argument reader) | Alta.CommandLine |
Connection, ISocket, console types | Root.Township (you already have it) |
On GitHub Actions, TavernLib isn't in att-stubs, so download it first. See Depending on TavernLib.
When it's ready
TavernLib is a plugin, so MelonLoader starts it before any mod. It sets itself up in OnEarlyInitializeMelon:
- On a server, it loads console tokens and runs the
/modlistreconcile if there is one. - On both sides, it tells MelonLoader which mod build folders to load (
.serveror.client_windows). A client also sets up its Quick join preference. - On a server, it creates
TavernManager(which reads the config files), starts the roles poll and, unless the launcher runs the server, the log archive and backup timers. - On both sides, it registers
EntranceMessageHandler.
So by your mod's OnInitializeMelon, the services are there. Its Harmony patches are applied with the plugin too, except a handful of game command fixes that wait for OnLateInitializeMelon (see Internals).
Stable API and internals
TavernLib has a lot of public types, because it was simpler to write that way. Most of them aren't meant for mods. This is how to read them:
| Area | Namespace | Status |
|---|---|---|
| Roles, the TavernMod attribute | TavernLib.ModdingApi | Made for mods. Expect it to stay. |
| Services | TavernLib.Services | Made for mods. |
| Events | TavernLib.Utils | Made for mods. |
| Folders, command-line names | TavernLib | Made for mods. |
| Config files | TavernLib.Backend.Api, TavernLib.Backend.Server.Configs | Fine to read. Shapes can change. |
| Client roles, network messages | TavernLib.Backend | Fine to use. Can change. |
| Console answer types | TavernLib.ModulePorts | Lines<T> is fine to use. The modules are TavernLib's. |
| Mod manager | TavernLib.Backend.Mods, TavernLib.Backend.Auth | Fine to read. Mirrors the launcher's Python, changes with it. |
| Everything else | TavernLib.Patches, TavernLib.Debugging, the rest of TavernLib.Backend | TavernLib's inner workings. Don't build on them. |
Anything internal (the logger, the login server, console token checks) isn't reachable without reflection, and can change in any release. Pages say so where it matters.
ModHandshake.Snapshot returned (hash, count, mods) in TavernLib 1.4 and (Hash, Mods, Untracked) now. Canary reads it by reflection and handles both. If you build on a backend type, expect to do the same.
If TavernLib might be missing or older
Setup always installs TavernLib, so a hard reference is normal. If your mod must still load without it (or with a TavernLib from before an API existed), don't reference it. Look it up by name instead, like TavernEssentials does:
using HarmonyLib;
public static class SoftTavernLib
{
// For a mod that should still load if TavernLib is missing or older.
public static bool HasRolesApi => AccessTools.TypeByName("TavernLib.ModdingApi.Roles") != null;
}
A hard reference to a member that isn't there fails when the method that uses it is first compiled (MissingMethodException or TypeLoadException), not when your mod loads.
Which TavernLib is running
TavernLib.Tavern.Version is a const, so the compiler copies its value into your DLL. It tells you the version you built against, not the one running. Ask MelonLoader instead:
using System.Linq;
using MelonLoader;
public static class RunningTavernLib
{
// The version that's actually loaded, not the one you built against.
public static string Version =>
MelonBase.RegisteredMelons.FirstOrDefault(m => m.Info.Name == "TavernLib")?.Info.Version;
}
The same goes for every other const in TavernLib, like ServerSettings.DefaultMaxPlayers and the TavernArgs names.
Server, client or both
Most of the API only does something on a server. A quick map:
| Server | Client | |
|---|---|---|
Roles | Reads users.json, raises RolesChanged | Always empty |
EntranceMessageHandler.LocalUser | Unused | Your roles on the server you're on |
TavernManager and its configs | Yes | Not registered: GetService returns null |
TavernEvents.SocketCreated | Yes | Yes |
TavernDirectories | Yes | Yes |
| Mod manager reads | Yes | Yes (your own Mods folder) |
To check which side you're on, see Command-line arguments.
Pages
| Page | What's on it |
|---|---|
| Roles | Roles, RolesChange, and your roles on the client |
| TavernMod attribute | TavernModAttribute, ModSide, GetModSide |
| Services | TavernServices, IService, what TavernLib registers, TavernMessageHandler |
| Events | TavernEvents, TavernEvent<T> |
| Network messages | The 8-bit message type, TavernLib's message numbers, sending your own |
| Folders | TavernDirectories |
| Config files | TavernManager, ServerConfigFile<T> and each file's class |
| Console | What TavernLib changes about commands, named token paths, Lines<T> |
| Mod manager | Reading installed mods, versions, parity, mod sources |
| Command-line arguments | Every argument TavernLib reads |
| Files | Every file TavernLib reads or writes |
| Internals | The other public types, and TavernLib's own logger |