ludic/packages/ludic.settings/file.ludic
Orkuncakilkaya 9c24d707bc ludic.settings: the store filled into a kept object in place; texts read from a file kept apart from it
settings_fill_json(v) writes every saved setting into an object the caller keeps, making a Value
only for a key seen the first time (value_set_str / value_set_int): Maroon Lake's Settings pages
rebuilt the whole tree on every change and dropped the old one. A text read from a file is interned,
and so is a muted card's key ludic.hints does not know, so the parsed tree can be let go whole.

Test: the store filled into a kept object changes its Values in place (6 tests pass).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 12:22:07 +03:00

68 lines
3 KiB
Text

# ludic.settings/file.ludic - the store as a JSON object, by key: a switch is 0 or 1, a whole number
# is itself, a float is thousandths (sv_put_float), text is a string. A key the file lacks keeps
# what the store holds, and a key the store does not know is ignored.
export function settings_to_json(settings_st: SettingsState) -> Val {
let v = Value.object()
for i in 0 .. len(settings_st.sg_defs) { settings_put(settings_st, v, i) }
return v
}
# one setting into `v` under its key (a game building its own file adds its extras beside them)
export function settings_put(settings_st: SettingsState, v: Val, id: int) -> void {
let d = settings_st.sg_defs[id]
if not d.saved { return }
if d.kind == SETTING_TEXT {
sv_put_str(v, d.key, settings_st.sg_text[id])
} else if d.kind == SETTING_FLOAT {
sv_put_float(v, d.key, settings_st.sg_num[id])
} else {
sv_put_int(v, d.key, int(settings_st.sg_num[id]))
}
}
# the store written into a kept object in place, in the file's own form: a Value is made only the
# first time its key is seen (a screen reading the settings made a new tree on every change)
export function settings_fill_json(settings_st: SettingsState, v: Val) -> void {
for i in 0 .. len(settings_st.sg_defs) {
let d = settings_st.sg_defs[i]
if not d.saved { continue }
if d.kind == SETTING_TEXT {
value_set_str(v, d.key, settings_st.sg_text[i])
} else if d.kind == SETTING_FLOAT {
value_set_int(v, d.key, int(Math.floor(settings_st.sg_num[i] * 1000.0 + 0.5)))
} else {
value_set_int(v, d.key, int(settings_st.sg_num[i]))
}
}
}
# every key `v` names, held to its range; quiet, since a load is applied whole by its caller
export function settings_from_json(settings_st: mut SettingsState, v: Val) -> void {
if v == null { return }
for i in 0 .. len(settings_st.sg_defs) { sg_read(settings_st, v, i) }
}
function sg_read(settings_st: mut SettingsState, v: Val, id: int) -> void {
let d = settings_st.sg_defs[id]
if not d.saved or Value.kind(v) != 6 or Value.has(v, d.key) == 0 { return }
if d.kind == SETTING_TEXT {
settings_st.sg_text[id] = intern(sv_str(v, d.key, settings_st.sg_text[id])) # the file's tree is let go
} else if d.kind == SETTING_FLOAT {
settings_st.sg_num[id] = settings_held(settings_st, id, sv_float(v, d.key, settings_st.sg_num[id]))
} else {
settings_st.sg_num[id] = settings_held(settings_st, id, float(sv_int(v, d.key, int(settings_st.sg_num[id]))))
}
}
# one setting from a JSON value in the file's own form (a row in a settings screen hands these):
# a change is a fact
export function settings_set_value(settings_st: mut SettingsState, id: int, x: Val) -> void {
let d = settings_st.sg_defs[id]
if d.kind == SETTING_TEXT {
if Value.kind(x) == 4 { settings_set_text(settings_st, id, Value.as_str(x)) }
} else if d.kind == SETTING_FLOAT {
settings_set_float(settings_st, id, float(Value.as_int(x)) / 1000.0)
} else {
settings_set_int(settings_st, id, Value.as_int(x))
}
}