Skip to main content

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 useAlso 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 typesRoot.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:

  1. On a server, it loads console tokens and runs the /modlist reconcile if there is one.
  2. On both sides, it tells MelonLoader which mod build folders to load (.server or .client_windows). A client also sets up its Quick join preference.
  3. 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.
  4. 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:

AreaNamespaceStatus
Roles, the TavernMod attributeTavernLib.ModdingApiMade for mods. Expect it to stay.
ServicesTavernLib.ServicesMade for mods.
EventsTavernLib.UtilsMade for mods.
Folders, command-line namesTavernLibMade for mods.
Config filesTavernLib.Backend.Api, TavernLib.Backend.Server.ConfigsFine to read. Shapes can change.
Client roles, network messagesTavernLib.BackendFine to use. Can change.
Console answer typesTavernLib.ModulePortsLines<T> is fine to use. The modules are TavernLib's.
Mod managerTavernLib.Backend.Mods, TavernLib.Backend.AuthFine to read. Mirrors the launcher's Python, changes with it.
Everything elseTavernLib.Patches, TavernLib.Debugging, the rest of TavernLib.BackendTavernLib'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.

Internals do change

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:

ServerClient
RolesReads users.json, raises RolesChangedAlways empty
EntranceMessageHandler.LocalUserUnusedYour roles on the server you're on
TavernManager and its configsYesNot registered: GetService returns null
TavernEvents.SocketCreatedYesYes
TavernDirectoriesYesYes
Mod manager readsYesYes (your own Mods folder)

To check which side you're on, see Command-line arguments.

Pages​

PageWhat's on it
RolesRoles, RolesChange, and your roles on the client
TavernMod attributeTavernModAttribute, ModSide, GetModSide
ServicesTavernServices, IService, what TavernLib registers, TavernMessageHandler
EventsTavernEvents, TavernEvent<T>
Network messagesThe 8-bit message type, TavernLib's message numbers, sending your own
FoldersTavernDirectories
Config filesTavernManager, ServerConfigFile<T> and each file's class
ConsoleWhat TavernLib changes about commands, named token paths, Lines<T>
Mod managerReading installed mods, versions, parity, mod sources
Command-line argumentsEvery argument TavernLib reads
FilesEvery file TavernLib reads or writes
InternalsThe other public types, and TavernLib's own logger