Skip to main content

ServerConfigFile<T>

An abstract class in TavernLib, TavernLib.Backend.Server.Configs.

The base class for TavernLib's JSON files. Reads into a T, writes through a temp file with a .bak, and holds a per-object lock so changes don't interleave. Subclass it for your own mod's file.

Constructors​

MemberWhat it is
protected ServerConfigFile(string filePath)Stores the path of the config file.

Fields​

1 private and internal field
MemberWhat it is
private readonly object _syncLock that keeps reads and writes of the file from interleaving.

Properties​

MemberWhat it is
T LastRead { get; private set; }The object from the last good read. new T() before the first one.
bool Readable { get; private set; }False until the file has been read once, and again after any read fails.
2 private and internal properties
MemberWhat it is
private string FileName { get; }The file's name without its folder, used in log messages.
private string FilePath { get; set; }Path of the config file on disk.

Methods​

MemberWhat it is
T Read()Reads the file into a new object and makes it LastRead. A missing file is created with defaults; an empty or broken one throws.
virtual void ReadFromFile()Calls Read. Subclasses may add to it, as ServerSettingsConfig does.
void Update(Func<T, bool> change)Reads the file, passes it to the change, and writes it back if the change returns true, all under the lock.
virtual void WriteToFile()Writes LastRead as it is, which can throw away changes made to the file since. Prefer Update.
protected virtual bool Tidy(T config)Called after each read to fix the object up. Return true to have it written back. Does nothing in the base.
2 private and internal methods
MemberWhat it is
private T Accept(T config)Makes a freshly read config LastRead and marks the file readable.
private void Write(T config)Writes the config to a temp file, then swaps it in and keeps the old file as .bak.