Data - Database

Updated a month ago

Database

The Database class provides JSON-based data persistence for V Rising mods. Each database instance creates its own folder and manages file operations with caching.

Overview

using ScarletCore.Data;

var db = new Database("MyModName");

Features

  • JSON serialization with caching
  • Temporary in-memory storage
  • Automatic directory management
  • Cache invalidation based on file timestamps

Methods

Save

Saves data to a JSON file.

db.Save<GuildData>("guild_123", guildData);

Parameters:

  • path - File name without extension
  • data - Object to serialize

Load

Loads data from a JSON file.

var data = db.Load<GuildData>("guild_123");

Parameters:

  • path - File name without extension

Returns: Deserialized object or default value

Get

Gets data from cache or loads from file if not cached.

var data = db.Get<GuildData>("guild_123");

Parameters:

  • path - File name without extension

Returns: Cached or loaded data

Has

Checks if a data file exists.

bool exists = db.Has("guild_123");

Parameters:

  • path - File name without extension

Returns: True if file exists

Delete

Deletes a data file and removes from cache.

bool deleted = db.Delete("guild_123");

Parameters:

  • path - File name without extension

Returns: True if successfully deleted

ClearCache

Clears cached data.

db.ClearCache();           // Clear all cache
db.ClearCache("guild_123"); // Clear specific file cache

Parameters:

  • path - Optional specific path to clear

SaveAll

Saves all cached data to files.

db.SaveAll();

Temporary Storage

Access temporary in-memory storage that persists only during runtime.

var tempData = db.Temp;

Temp Methods

Set

Stores data in memory only.

db.Temp.Set<string>("session_token", "abc123");

Get

Retrieves temporary data.

var token = db.Temp.Get<string>("session_token");

Has

Checks if temporary data exists.

bool exists = db.Temp.Has("session_token");

Remove

Removes temporary data.

bool removed = db.Temp.Remove("session_token");

Clear

Clears all temporary data.

db.Temp.Clear();

GetKeys

Gets all temporary data keys.

var keys = db.Temp.GetKeys();

File Storage

Files are stored in:

BepInEx/config/{DatabaseName}/{path}.json

Caching Behavior

  • Data is cached after load/save operations
  • Cache is invalidated when files are modified externally
  • Cache timestamps track file modification times
  • Temporary storage exists only in memory

Example Usage

using ScarletCore.Data;

[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
[BepInDependency("markvaaz.ScarletCore")]
public class Plugin : BasePlugin {
    public static Database Database { get; private set; }

    public override void Load() {
        Database = new Database(MyPluginInfo.PLUGIN_GUID);
        
        // Use database for persistent storage
        Database.Save("hooks/enabled", true);
        var isEnabled = Database.Load<bool>("hooks/enabled");
        
        // Use temporary storage for runtime data
        Database.Temp.Set("session_active", true);
    }
}