Skip to content

Module Storage

Алексей . edited this page Sep 29, 2026 · 3 revisions

Module Storage: Config and Assets

Each module gets three storage handles from IModuleContext:

Property Backing store Typical use
Context.Assets IModuleStorage Read-write files shipped with the module (textures, Lua tables, presets)
Context.UserData IModuleStorage Per-user mutable files (saves, caches, logs)
Context.Config IModuleConfig Typed JSON config stored inside UserData

All three are lazy. They are not created on the filesystem until the module actually touches them.

Paths

Resolved by SFPaths at startup from Environment.ProcessPath.

Root Location Purpose
SFPaths.GameDirectory Directory of gta_sa.exe Base for assets
SFPaths.AssetsRoot <GameDir>\SF\ Shared assets root
SFPaths.UserDataRoot <MyDocuments>\GTA San Andreas User Files\SF\ Shared user data root

Per-module folders: <AssetsRoot>\<moduleId>\ and <UserDataRoot>\<moduleId>\. They are created on first write.

Example for Arizona installed at C:\Games\Arizona\bin\Arizona\gta_sa.exe and a module with id my-module:

  • Assets: C:\Games\Arizona\bin\Arizona\SF\my-module\
  • User data: C:\Users\<user>\Documents\GTA San Andreas User Files\SF\my-module\
  • Config: C:\Users\<user>\Documents\GTA San Andreas User Files\SF\my-module\config.json

IModuleStorage

Unified file API for both Assets and UserData. All paths are relative to the module root and sanitized against path traversal.

IModuleStorage assets = Context.Assets;

if (assets.Exists("preset.json"))
{
    string json = assets.ReadAllText("preset.json");
}

assets.WriteAllText("output\\dump.txt", "hello");

foreach (string file in assets.EnumerateFiles("presets", "*.json", recursive: true))
{
    // relative paths under the module root
}

Members:

Member Description
Root Absolute module root directory
IsReadOnly false for the default implementation
Exists(relativePath) Check file existence
GetFullPath(relativePath) Resolve to absolute path
OpenRead / OpenWrite Raw Stream access
ReadAllBytes / ReadAllText / ReadAllLines Whole-file reads
WriteAllBytes / WriteAllText / WriteAllLines Whole-file writes, create parent dirs
Delete(relativePath) Deletes a single file, returns true if it was removed
EnumerateFiles(dir, pattern, recursive) Enumerate relative file paths

Attempts to escape the module root (..\..\etc) throw UnauthorizedAccessException.

IModuleConfig

Typed JSON config on top of UserData. Location: <UserDataRoot>\<moduleId>\config.json.

The IModuleConfig API accepts JsonTypeInfo<T>; use source-generated metadata. This is a contract requirement, not a NativeAOT deployment requirement.

public sealed class MyConfig
{
    public string Target { get; set; } = "";
    public int PollIntervalMs { get; set; } = 250;
}

[JsonSerializable(typeof(MyConfig))]
internal sealed partial class MyConfigContext : JsonSerializerContext;

Loading a config, supplying defaults on first launch:

MyConfig cfg = Context.Config.Load(MyConfigContext.Default.MyConfig, static () => new MyConfig());
cfg.PollIntervalMs = 500;
Context.Config.Save(MyConfigContext.Default.MyConfig, cfg);

TryLoad returns false if the file is missing or malformed so the caller can decide between falling back and aborting:

if (!Context.Config.TryLoad(MyConfigContext.Default.MyConfig, out MyConfig cfg))
{
    Log.LogWarning("config missing, using defaults");
    cfg = new MyConfig();
}

Other members: Exists, Location, Delete().

Replacing storage from another module

Note

Provider replacement is host-only Runtime configuration. Context.SF.Modules in an external plugin exposes the public catalog, not mutable storage settings. SF.Modules.DefaultStorage returns the active default provider, or null after it is replaced.

SF.Modules.Storage exposes a mutable IModuleStorageProvider. A module can replace the provider wholesale or override a single module's storage before that module starts.

public sealed class ConfigOverrideModule : SFModuleBase
{
    protected override Task OnStartingAsync()
    {
        DefaultModuleStorageProvider provider = SF.Modules.DefaultStorage
            ?? throw new InvalidOperationException("The default provider is not active.");
        provider.OverrideConfig("my-module", new EncryptedConfig(...));
        provider.OverrideAssets("my-module", new ReadOnlyAssetBundle(...));
        return Task.CompletedTask;
    }

    protected override Task ExecuteAsync(CancellationToken ct) => Task.CompletedTask;
}

Order the override module before its target via [SFModule(Order = ...)] so the override is installed before the target's first call to Context.Assets / Context.Config.

To replace the entire provider:

SF.Modules.Storage = new MyCustomStorageProvider();

If an override must keep using the original default provider after replacement, retain that provider explicitly before assigning SF.Modules.Storage.

Cheat sheet

// read/write an asset next to gta_sa.exe
byte[] blob = Context.Assets.ReadAllBytes("data\\table.bin");
Context.Assets.WriteAllText("data\\out.txt", text);

// read/write user data in My Documents
Context.UserData.WriteAllLines("log.txt", lines);

// typed config
MyConfig cfg = Context.Config.Load(MyConfigContext.Default.MyConfig, static () => new MyConfig());
Context.Config.Save(MyConfigContext.Default.MyConfig, cfg);

// absolute path if you need it for another API
string abs = Context.Assets.GetFullPath("native.dll");

Clone this wiki locally