Skip to content

Mod config api #107

@incomplete-tree

Description

@incomplete-tree

This would allow creating a GUI where you can configure mods, without needing to manually edit mod-specific json files. Proposed api:

public abstract class PolyScriptMod
{
    internal void Initialize(string name);
    public string ModName { get; } = modName;
    protected ConfigApiProvider Config { get; } = new ConfigApiProvider(modName, false);
    protected ConfigApiProvider ExposedConfig { get; } = new ConfigApiProvider(modName, true);

    public virtual void OnLoad();
}

public class ConfigApiProvider(string modName, bool exposed)
{
    private JsonObject? CurrentConfig; // populate in constructor and Reload()

    public void Reload();
    public JsonObject GetConfig();
    public void SetDefault(JsonObject defaultValue);
    public void WriteConfig(JsonObject config);
    public void Edit(Action<JsonObject> editor);
    public JsonObject Get(Func<JsonObject, JsonObject> getter);

    public bool SaveChanges()
    {
        JsonObject mods = /*deserialize json object from*/ exposed
            ? "mods.json"
            : Path.Combine("Mods", modName, modName + ".json");

        mods[modName] = this.CurrentConfig;
        // write mods back
    }
}

public class ExampleMod : PolyScriptMod
{
    public override void OnLoad()
    {
        this.ExposedConfig.SetDefault(new JsonObject
        {
            ["example"] = "example"
        });

        this.ExposedConfig.Edit(cfg => cfg["someSection"] = "something");
        var example = this.ExposedConfig.Get(cfg => cfg["example"];
    }
}

public static class MyPatches { }

In addition, GLD mods should be able to specify config via templating. This could be either simple, {{ config.someValue }} (using a value automatically registers it in the UI) or use complex templating, e.g.

{% someVariable = config.value1 + config.value2 %}
        "health" : {{ someVariable }}

(The gld config was wasdeilonn 's idea)

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions