Skip to content

3. Creating, saving and loading configs

one-nora edited this page May 16, 2026 · 3 revisions

Scheme

WKLib config is built around folders, files, and typed values.

  • ConfigFolder owns folders and files
  • ConfigFile reads and writes JSON
  • ConfigValue<T> binds a typed value to a JSON key

Basic flow

  1. Create ConfigValue<T> objects using your WKLibAPI instance
  2. Modify values when needed
  3. Call SaveSync() or SaveAsync() to save changes

This flow uses the default config file. To make more files check the full doc.

Config folder

Represents a folder directory, when created it will use the "BepInEx/config" directory.

For example, doing new ConfigFolder("MyMod") will use the "BepInEx/config/MyMod" directory.

By default the WKLibAPI instance creates a config folder using the DisplayName field (which would look like so "BepInEx/config/{DisplayName}").

Constructors

ConfigFolder(string folderName)
ConfigFolder(ConfigFolder parent, string folderName)

Examples

var myFolder = new ConfigFolder("MyFolder");

Creating a nested folder

var subFolder = new ConfigFolder(myFolder, "MySubFolder");

Members

  • BasePath full folder path
  • FolderName folder name only

Folder methods

  • GetSubFolder(string folderName)
  • CreateSubFolder(string folderName)
  • GetOrCreateSubFolder(string folderName)
  • RegisterConfigFolder(string folderName)

File methods

  • GetConfigFile(string fileName)
  • CreateConfigFile(string fileName)
  • GetOrCreateConfigFile(string fileName)
  • RegisterConfigFile(string fileName)

Other methods

  • LoadFiles() scans for JSON files and subfolders
  • GetConfigFolders() returns a copy of subfolders
  • GetConfigFiles() returns a copy of config files
  • EnumerateFiles() recursively enumerates files

Config file

Represents one JSON file on disk.

Constructors

ConfigFile(ConfigFolder configFolder, string fileName)
ConfigFile(WKLibAPI api, string fileName)

Examples

// Will be created on ../BepInEx/config/FolderName/Settings.json
var file = new ConfigFile(folder, "Settings");
// This needs the API instance to be created
// Will be created on ../BepInEx/config/ModName/Settings.json
var file = new ConfigFile(WKLibAPI, "Settings");

Members

  • FilePath full path to the JSON file
  • FullFileName file name with extension
  • FileName file name without extension

Loading and saving

  • LoadFromDisk() reads the file and loads the values
  • SaveAsync() writes the current values to disk
  • SaveSync() synchronous wrapper for SaveAsync()

Value access

  • TryGetParsedToken(string key, out JToken token)
  • TryGetParsedValueObject(string key, out object value)

Notes

  • The .json extension is appended automatically when needed

Config Value

ConfigValue<T> stores a typed value bound to a ConfigFile.

Constructors

ConfigValue(ConfigFile file, string key, T currentValue, bool loadOnCreation = true)
ConfigValue(WKLibAPI api, string key, T currentValue, bool loadOnCreation = true)

Examples

public static ConfigValue<float> speed = new ConfigValue<float>(file, "MoveSpeed", 5f);
// Uses the default config file
public static ConfigValue<float> speed = new ConfigValue<float>(WKLibAPI, "MoveSpeed", 5f);

Members

  • Value current value
  • RefValue gives ref access to the stored value

Notes

  • JSON parse errors are ignored
  • implicit conversion to Value

Clone this wiki locally