API Basic Config en - LIHACHTETAN/ClassicUO-BadNewbie-BasicIDE GitHub Wiki
JSON / Config.bas
ClassicUO • Basic
Config.bas is a reusable settings module shipped in Scripts/Include. Keep Include/Config.bas beside your main script and write Include "Config.bas". It combines persistent JSON with independent defaults.
Exact syntax
Include "Config.bas"
settings = Config.Load(fileName, defaults)
Config.Save(fileName, settings)
enabled = Config.GetFlag(settings, key, fallback=False)
Config.SetFlag(settings, key, value)
JsonParse(text) / JsonStringify(value[, indented=0])
JsonLoad(fileName[, defaultValue]) / JsonSave(fileName, value[, indented=1])
JsonKind(value) / JsonBoolean(value) / JsonNull() / flag.Value()
Parameters
fileName— Required file path. Relative paths start in the main script folder, including calls from an Include file. Absolute paths are accepted.defaults (Config.Load)— Required Dictionary of defaults for Config.Load. Its deep JSON copy is merged with saved top-level keys. It is not modified. Config.Load does not infer omitted defaults.defaultValue (JsonLoad)— Optional fallback used only when the file or its parent folder is missing. Returned as an independent JSON round-trip copy. Invalid JSON, encoding or access errors are not hidden.settings / value— Value: number, String, array, List, Dictionary, JsonBoolean or JsonNull. Dictionary keys must be Strings; shared references are allowed, circular references are errors.indented— Integer flag: 0 is compact JSON; nonzero adds indentation. Default: Stringify=0, Save=1.key / fallback— Config.Load(fileName, defaults) returns a new Dictionary: saved top-level keys override a deep copy of defaults. Nested objects are replaced whole, not recursively merged. Config.Save(fileName, settings) returns no value and saves explicitly. Config.GetFlag(settings, key, fallback=False) returns 1/True or 0/False; an existing non-Boolean value raises an error. Config.SetFlag(settings, key, value) changes memory only and returns no value. Load/Save require Dictionaries with string keys. Private RequireObject checks the outer kind; JSON conversion validates the complete graph.
Returns
Config.Load(fileName, defaults) returns a new Dictionary: saved top-level keys override a deep copy of defaults. Nested objects are replaced whole, not recursively merged. Config.Save(fileName, settings) returns no value and saves explicitly. Config.GetFlag(settings, key, fallback=False) returns 1/True or 0/False; an existing non-Boolean value raises an error. Config.SetFlag(settings, key, value) changes memory only and returns no value. Load/Save require Dictionaries with string keys. Private RequireObject checks the outer kind; JSON conversion validates the complete graph.
Behavior
- These are local Basic functions without UO.; they send no game packets. JsonParse/JsonStringify work in memory. Basic True serializes as the number 1; use JsonBoolean(True) for JSON true. JsonNull() preserves null separately from 0.
- Numbers must be finite. Integer-valued numbers outside ±9007199254740991 are rejected; store larger identifiers as Strings. Other numbers use Double precision. UTF-8 is strict; an input BOM is accepted, output has no BOM. Invalid Unicode is rejected.
- Limits: 1048576 UTF-16 code units, 4 MiB file/output bytes, 64 nested containers and 100000 value nodes. Over-limit input raises an error. Loading/parsing creates new collections; saving does not clone your in-memory objects.
- Save validates the whole graph, creates parent folders, writes a unique temporary file beside the destination, flushes it, then moves/replaces it. A failed or cancelled pre-commit write keeps the old destination. It cleans its temporary file; an OS cleanup error can leave that file. Already committed saves are not undone.
- Conversion checks pause/stop every 256 values, file I/O between 4096-byte/character chunks and before commit. No background writer or extra thread is created. Individual OS calls cannot be forcibly interrupted. Concurrent saves use last successful replacement; this is not a database transaction.
Examples
1. Independent defaults
# fileName is missing-settings.json; defaults contains delay=350. Load uses defaults only if the file is missing. Changing the returned delay to125 leaves defaults at350. The result is "125:350". Delete or rename a previous sample file before testing missing-file behavior.
Option Explicit On
Include "Config.bas"
Sub Main()
Dim defaults=Dictionary()
defaults["delay"]=350
Dim settings=Config.Load("missing-settings.json", defaults)
settings["delay"]=125
Return CStr(settings["delay"]) & ":" & CStr(defaults["delay"])
End Sub
Parameter and execution notes:
fileName is missing-settings.json; defaults contains delay=350. Load uses defaults only if the file is missing. Changing the returned delay to125 leaves defaults at350. The result is "125:350". Delete or rename a previous sample file before testing missing-file behavior.
Include/Config.bas
Option Explicit On
' Copy Include/Config.bas beside your main script, then Include "Config.bas".
' Relative JSON paths are based on the main script's folder, not this module.
Module Config
Private Sub RequireObject(ByVal value)
If JsonKind(value) <> "object" Then
Throw "Config requires a Dictionary with string keys."
End If
End Sub
' Returns a new Dictionary. Saved top-level keys override independent defaults.
' Missing files use defaults; invalid files raise an error and remain unchanged.
Public Function Load(ByVal fileName, ByVal defaults)
RequireObject(defaults)
Dim result = JsonParse(JsonStringify(defaults))
Dim saved = JsonLoad(fileName, Dictionary())
RequireObject(saved)
For Each entry In saved
result.Set(entry.Key(), entry.Value())
Next
Return result
End Function
' No return value. Validates and writes UTF-8 using same-directory replacement.
Public Sub Save(ByVal fileName, ByVal settings)
RequireObject(settings)
JsonSave(fileName, settings)
End Sub
' A JSON Boolean is distinct from a Basic numeric flag. Convert explicitly.
' Returns 1/True or 0/False; non-Boolean saved values raise an error.
Public Function GetFlag(ByVal settings, ByVal key, Optional ByVal fallback=False)
RequireObject(settings)
Dim flag = settings.Get(key, JsonBoolean(fallback))
If JsonKind(flag) <> "boolean" Then
Throw "Config.GetFlag expects a JSON Boolean for key: " & CStr(key)
End If
Return flag.Value()
End Function
' Changes the Dictionary in memory; call Save to persist it.
Public Sub SetFlag(ByVal settings, ByVal key, ByVal value)
RequireObject(settings)
settings.Set(key, JsonBoolean(value))
End Sub
End Module
2. Save and reload
# fileName is demo-settings.json. SetFlag stores a JSON Boolean; Save creates or replaces this sample file. Load reads it back. GetFlag converts it to Integer1 and delay is350, so Main returns "1:350".
Option Explicit On
Include "Config.bas"
Sub Main()
Dim settings=Dictionary()
settings["delay"]=350
Config.SetFlag(settings, "enabled", True)
Config.Save("demo-settings.json", settings)
Dim loaded=Config.Load("demo-settings.json", Dictionary())
Return CStr(Config.GetFlag(loaded, "enabled")) & ":" & CStr(loaded["delay"])
End Sub
Parameter and execution notes:
fileName is demo-settings.json. SetFlag stores a JSON Boolean; Save creates or replaces this sample file. Load reads it back. GetFlag converts it to Integer1 and delay is350, so Main returns "1:350".
Include/Config.bas
Option Explicit On
' Copy Include/Config.bas beside your main script, then Include "Config.bas".
' Relative JSON paths are based on the main script's folder, not this module.
Module Config
Private Sub RequireObject(ByVal value)
If JsonKind(value) <> "object" Then
Throw "Config requires a Dictionary with string keys."
End If
End Sub
' Returns a new Dictionary. Saved top-level keys override independent defaults.
' Missing files use defaults; invalid files raise an error and remain unchanged.
Public Function Load(ByVal fileName, ByVal defaults)
RequireObject(defaults)
Dim result = JsonParse(JsonStringify(defaults))
Dim saved = JsonLoad(fileName, Dictionary())
RequireObject(saved)
For Each entry In saved
result.Set(entry.Key(), entry.Value())
Next
Return result
End Function
' No return value. Validates and writes UTF-8 using same-directory replacement.
Public Sub Save(ByVal fileName, ByVal settings)
RequireObject(settings)
JsonSave(fileName, settings)
End Sub
' A JSON Boolean is distinct from a Basic numeric flag. Convert explicitly.
' Returns 1/True or 0/False; non-Boolean saved values raise an error.
Public Function GetFlag(ByVal settings, ByVal key, Optional ByVal fallback=False)
RequireObject(settings)
Dim flag = settings.Get(key, JsonBoolean(fallback))
If JsonKind(flag) <> "boolean" Then
Throw "Config.GetFlag expects a JSON Boolean for key: " & CStr(key)
End If
Return flag.Value()
End Function
' Changes the Dictionary in memory; call Save to persist it.
Public Sub SetFlag(ByVal settings, ByVal key, ByVal value)
RequireObject(settings)
settings.Set(key, JsonBoolean(value))
End Sub
End Module
3. Validate saved flags
# The in-memory value enabled=1 is a number, not JSON true. GetFlag rejects it; Catch returns "invalid flag". SetFlag(settings,"enabled",True) would store the correct type. No file is changed.
Option Explicit On
Include "Config.bas"
Sub Main()
Dim settings=Dictionary()
settings["enabled"]=1
Try
Dim enabled=Config.GetFlag(settings, "enabled")
Catch problem
Return "invalid flag"
End Try
Return "unexpected"
End Sub
Parameter and execution notes:
The in-memory value enabled=1 is a number, not JSON true. GetFlag rejects it; Catch returns "invalid flag". SetFlag(settings,"enabled",True) would store the correct type. No file is changed.
Include/Config.bas
Option Explicit On
' Copy Include/Config.bas beside your main script, then Include "Config.bas".
' Relative JSON paths are based on the main script's folder, not this module.
Module Config
Private Sub RequireObject(ByVal value)
If JsonKind(value) <> "object" Then
Throw "Config requires a Dictionary with string keys."
End If
End Sub
' Returns a new Dictionary. Saved top-level keys override independent defaults.
' Missing files use defaults; invalid files raise an error and remain unchanged.
Public Function Load(ByVal fileName, ByVal defaults)
RequireObject(defaults)
Dim result = JsonParse(JsonStringify(defaults))
Dim saved = JsonLoad(fileName, Dictionary())
RequireObject(saved)
For Each entry In saved
result.Set(entry.Key(), entry.Value())
Next
Return result
End Function
' No return value. Validates and writes UTF-8 using same-directory replacement.
Public Sub Save(ByVal fileName, ByVal settings)
RequireObject(settings)
JsonSave(fileName, settings)
End Sub
' A JSON Boolean is distinct from a Basic numeric flag. Convert explicitly.
' Returns 1/True or 0/False; non-Boolean saved values raise an error.
Public Function GetFlag(ByVal settings, ByVal key, Optional ByVal fallback=False)
RequireObject(settings)
Dim flag = settings.Get(key, JsonBoolean(fallback))
If JsonKind(flag) <> "boolean" Then
Throw "Config.GetFlag expects a JSON Boolean for key: " & CStr(key)
End If
Return flag.Value()
End Function
' Changes the Dictionary in memory; call Save to persist it.
Public Sub SetFlag(ByVal settings, ByVal key, ByVal value)
RequireObject(settings)
settings.Set(key, JsonBoolean(value))
End Sub
End Module