Log the nx2navi.ini and environment values that are outside their settings' sets, with the line they came from.

cfg.c takes a value as written and logged nothing about it: "pace_smooth = 0 ; off" keeps the comment in the value
and cfg_bool reads it as on, "fullscreen = false" is on too, and "sharpness = 250" is past 0..100 (the audit's
probe on cfg.c printed exactly that, with 0 log lines during the load). A player who edits the file by hand had no
way to see why a setting did not take.

After cfg_load (and after migrate, so a value it rewrites is checked as read) each file value is checked against
its row: a 0|1 switch with any other value, a value that is not a number in a lo..hi range, and a word that is
none of a list of words, case aside. A list with a number in it is left alone: it is the menu's presets and its
consumer takes more (fps_limit any rate from 30 to 240). A list of words is reported as "none of the menu's
choices" only, since a consumer may take more (dlss_preset a..m). A value holding " ;" or " #" is noted on any row:
the comment is part of it. cfg_init checks the environment's NX2NAVI_ values the same way. At most 16 lines a load,
then a count. The values read are unchanged; reading words for off or cutting comments (F-8) needs approval.

Not reproduced before the change by a game run; the new tests/cfg_parse_check.py builds cfg.c with stand-ins and
checks each kind of line with its line number, the environment's, the cap of 16 and a count, and no line for a file
whose values fit (including fps_limit 144, render_scale 300, dlss_preset k, pad_layout xbox_prompts). Every value
nx2navi.ini.example shows passes without a line. launcher_test PlayerIniTest, the cfg rows check and the compile
checks pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 17:23:19 -05:00
parent 87fd385b7b
commit 8c1ec42406
3 changed files with 290 additions and 1 deletions

View File

@@ -508,6 +508,8 @@ static char *norm_key(char *key) {
return strncmp(key, PREFIX, PREFIX_LEN) ? key : key + PREFIX_LEN;
}
static int entry_line[CFG_MAX_KEYS]; // the file line each entry's value came from (cfg_load's check_values)
static void add(char *key, char *value, int line) {
key = norm_key(key);
size_t vl = strlen(value);
@@ -522,12 +524,90 @@ static void add(char *key, char *value, int line) {
if (!strcmp(entries[i].key, key)) { // a repeated key: the later line wins
free(entries[i].value);
entries[i].value = strdup(value);
entry_line[i] = line;
return;
}
if (n_entries == CFG_MAX_KEYS) return;
entry_line[n_entries] = line;
entries[n_entries++] = (CfgEntry){strdup(key), strdup(value), 0, 0};
}
// ---- values outside their rows' sets, logged at load (the values are read as they are written)
// Hand-edited files had values no consumer takes, and nothing said so: "pace_smooth = 0 ; off" keeps the comment in
// the value, which cfg_bool reads as on; "fullscreen = false" is on too; "sharpness = 250" is past 0..100. Only the
// sets that are the consumers' own are checked: 0|1, ranges lo..hi, and lists of words. A list with a number in it is
// the menu's presets, and its consumer takes more (fps_limit any rate from 30 to 240, render_scale 25..400), so it is
// left alone. A list of words names the menu's choices; a consumer may take more (dlss_preset any letter a..m), so the
// line says no more than that. At most CHECK_LINES lines per load, then a count.
#define CHECK_LINES 16
static int misfits;
// NULL when v fits the set, else what the log line says of it (in buf).
static const char *misfit(const char *set, const char *v, char *buf, size_t cap) {
if (!strcmp(set, "0|1")) return strcmp(v, "0") && strcmp(v, "1") ? "is not 0 or 1" : NULL;
const char *dots = strstr(set, "..");
if (dots && !strchr(set, '|')) {
char lo_s[32], *end; // the low end apart: strtod would take "0." of "0..100" for a number
size_t ln = (size_t)(dots - set);
if (!ln || ln >= sizeof lo_s) return NULL;
memcpy(lo_s, set, ln), lo_s[ln] = 0;
double lo = strtod(lo_s, &end);
if (*end) return NULL;
double hi = strtod(dots + 2, &end);
if (end == dots + 2 || *end) return NULL;
// any number: some rows with whole ends take fractions (dyn_res_target 16.7 in 1..100)
double x = strtod(v, &end);
if (end != v && !*end && x == x && x >= lo && x <= hi) return NULL;
snprintf(buf, cap, "is not a number in %s", set);
return buf;
}
for (const char *m = set; *m;) { // a member that is a number: the menu's presets, not checked
char *end;
size_t n = strcspn(m, "|");
strtod(m, &end);
if (end == m + n) return NULL;
m += n + (m[n] == '|');
}
for (const char *m = set; *m;) {
size_t n = strcspn(m, "|");
if (strlen(v) == n && !strncasecmp(m, v, n)) return NULL;
m += n + (m[n] == '|');
}
snprintf(buf, cap, "is none of the menu's choices %s", set);
return buf;
}
// key: upper case without the prefix; line 0: the value comes from the environment.
static void check_value(const char *key, const char *v, int line) {
const CfgMeta *m = cfg_meta(key);
if (!m) return;
char buf[160];
const char *bad = m->values ? misfit(m->values, v, buf, sizeof buf) : NULL;
int comment = strstr(v, " ;") || strstr(v, " #") || strstr(v, "\t;") || strstr(v, "\t#");
if (!bad && !comment) return;
if (++misfits > CHECK_LINES) return;
char low[64], where[96];
size_t n = 0;
for (; key[n] && n < sizeof low - 1; n++) low[n] = (char)tolower((unsigned char)key[n]);
low[n] = 0;
if (line) snprintf(where, sizeof where, "nx2navi.ini line %d: %s", line, low);
else snprintf(where, sizeof where, "the environment's " PREFIX "%s", key);
rt_log("cfg: %s = '%s' %s%s%s", where, v, bad ? bad : "", bad && comment ? "; " : "",
comment ? "a ';' or '#' after the value is part of it (a comment goes on a line of its own)" : "");
}
static void check_end(void) {
if (misfits > CHECK_LINES)
rt_log("cfg: %d more values outside their settings' choices are not listed", misfits - CHECK_LINES);
misfits = 0;
}
static void check_values(void) {
for (int i = 0; i < n_entries; i++)
if (entries[i].value && entry_line[i]) check_value(entries[i].key, entries[i].value, entry_line[i]);
check_end();
}
// 0.1.9 had the Xbox prompts as a pad layout: pad_layout = xbox_prompts is pad_layout = nintendo with prompts = xbox.
// Read as those two, which the menu shows and the next cfg_save writes; a prompts line of the file's own wins. The
// value still works from the environment (input_pad_layout_pick, input_prompts_pick).
@@ -541,7 +621,8 @@ static void migrate(void) {
free(layout->value);
layout->value = strdup("nintendo");
layout->dirty = 1;
if (!prompts && n_entries < CFG_MAX_KEYS) entries[n_entries++] = (CfgEntry){strdup("PROMPTS"), strdup("xbox"), 1, 0};
if (!prompts && n_entries < CFG_MAX_KEYS)
entry_line[n_entries] = 0, entries[n_entries++] = (CfgEntry){strdup("PROMPTS"), strdup("xbox"), 1, 0};
rt_log("cfg: nx2navi.ini pad_layout = xbox_prompts is read as pad_layout = nintendo and prompts = %s",
prompts ? prompts->value : "xbox");
}
@@ -604,6 +685,7 @@ int cfg_load(const char *path) {
}
free(data);
migrate();
check_values(); // after migrate: a value it rewrites is checked as the game reads it
return 1;
}
@@ -1243,6 +1325,13 @@ static void load_ini(void) {
void cfg_init(void) {
load_ini();
for (size_t i = 0; i < sizeof metas / sizeof *metas; i++) { // the environment's values, as the file's (cfg_load)
char var[80];
snprintf(var, sizeof var, PREFIX "%s", metas[i].key);
const char *e = getenv(var);
if (e && *e) check_value(metas[i].key, e, 0);
}
check_end();
__atomic_store_n(&lowend, -1, __ATOMIC_RELEASE); // asked before the file was read: decided again (lowend_of)
if (profile_of())
rt_log("cfg: device profile Steam Deck: defaults for the keys nx2navi.ini does not set (NX2NAVI_DEVICE_PROFILE=desktop "

74
tests/cfg_parse_check.c Normal file
View File

@@ -0,0 +1,74 @@
// runtime/cfg.c's reading of nx2navi.ini and the environment, with stand-ins for what cfg.c links against. Built and
// run by tests/cfg_parse_check.py: `cfg_parse_check <case> <ini>` loads the file the script wrote and prints every line
// cfg.c logged ("log: ...") and what the case reads back ("got: ...").
// load cfg_load on the file; then cfg_init's check of the environment (the variables the script set)
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cfg.h"
#include "host.h"
#include "platform/platform.h"
void rt_log(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
printf("log: ");
vprintf(fmt, ap);
printf("\n");
va_end(ap);
}
void rt_fatal(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
printf("FATAL: ");
vprintf(fmt, ap);
printf("\n");
va_end(ap);
exit(3);
}
const char *bugcap_replay_dir(void) { return NULL; }
void perf_size(PerfSizes *s, const char *name, uint64_t count, uint64_t limit, uint64_t bytes) {
(void)s, (void)name, (void)count, (void)limit, (void)bytes;
}
static const char *ini;
int plat_exe_path(char *out, size_t n) { (void)out, (void)n; return 0; }
int plat_file_exists(const char *p) {
FILE *f = fopen(p, "rb");
if (f) fclose(f);
return f != NULL;
}
uint8_t *plat_file_read(const char *path, uint64_t *size) {
FILE *f = fopen(path, "rb");
if (!f) return NULL;
fseek(f, 0, SEEK_END);
long n = ftell(f);
fseek(f, 0, SEEK_SET);
uint8_t *d = malloc((size_t)n + 1);
if (fread(d, 1, (size_t)n, f) != (size_t)n) n = 0;
d[n] = 0;
fclose(f);
*size = (uint64_t)n;
return d;
}
int64_t plat_file_size(const char *p) { (void)p; return -1; }
int plat_file_write_atomic(const char *p, const void *d, size_t n) { (void)p, (void)d, (void)n; return 1; }
void plat_mutex_lock(PlatMutex *m) { (void)m; }
void plat_mutex_unlock(PlatMutex *m) { (void)m; }
int plat_setenv(const char *n, const char *v) { (void)n, (void)v; return 1; }
int main(int argc, char **argv) {
setvbuf(stdout, NULL, _IONBF, 0);
if (argc < 3) return 2;
ini = argv[2];
if (!strcmp(argv[1], "load")) {
if (!cfg_load(ini)) return printf("cfg_load failed\n"), 2;
printf("got: pace_smooth '%s'\n", cfg_get("NX2NAVI_PACE_SMOOTH") ? cfg_get("NX2NAVI_PACE_SMOOTH") : "(unset)");
printf("--- environment\n");
cfg_init(); // finds no nx2navi.ini next to the stand-in exe; checks the environment's values
return 0;
}
return 2;
}

126
tests/cfg_parse_check.py Normal file
View File

@@ -0,0 +1,126 @@
"""Build and run tests/cfg_parse_check.c: runtime/cfg.c's reading of nx2navi.ini and the environment, against stand-ins
for what it links with. The values stay as written; what is checked is what the log says about them:
- values outside their rows' sets (cfg.c check_value): a 0|1 switch with another value, a number outside its range, a
word that is none of the menu's choices, and a ';' or '#' comment kept in a value, each named with its line; none
for a file whose values fit, nor for a list with numbers in it (the menu's presets); at most 16 lines, then a count.
No game files.
Usage: cfg_parse_check.py
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
def compiler():
if os.environ.get('CC'):
return os.environ['CC']
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
return str(MSYS_BIN / 'clang.exe')
return shutil.which('clang') or 'clang'
CLEAN = '''[frame rate]
pace_smooth = 1
fps_limit = 144
; a comment line
# another
[display]
sharpness = 80
color_gamma = 1.1
vk_present = FIFO
render_scale = 300
dlss_preset = k
pad_layout = xbox_prompts
monitor = 2
'''
BAD = '''[frame rate]
pace_smooth = 0 ; off
fullscreen = false
sharpness = 250
color_gamma = 2
vk_present = vsync
fps_limit = 144 # my monitor
sharpness_typo = 3
'''
def run(exe, env, case, text, work, extra_env=None):
ini = work / f'{case}_{len(text)}.ini'
ini.write_text(text, encoding='utf-8')
e = dict(env)
for k in list(e):
if k.upper().startswith('NX2NAVI_'):
del e[k]
e.update(extra_env or {})
p = subprocess.run([str(exe), case, str(ini)], env=e, capture_output=True, text=True, errors='replace', timeout=60)
if p.returncode:
print(p.stdout, p.stderr)
return p.returncode, p.stdout
def main():
work = ROOT / 'build' / 'test' / 'cfg_parse'
work.mkdir(parents=True, exist_ok=True)
exe = work / ('cfg_parse_check.exe' if os.name == 'nt' else 'cfg_parse_check')
env = dict(os.environ)
if os.name == 'nt' and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
rt = ROOT / 'runtime'
subprocess.check_call([compiler(), '-std=gnu11', '-O1', '-g', '-Wall', '-Wno-unused-function', '-I', str(rt),
str(ROOT / 'tests' / 'cfg_parse_check.c'), str(rt / 'cfg.c'), '-o', str(exe)], env=env)
bad = 0
def check(ok, what, out=''):
nonlocal bad
print(f'{what}: {"ok" if ok else "FAIL"}', flush=True)
if not ok:
bad += 1
print(out)
# values that fit: no line about them
rc, out = run(exe, env, 'load', CLEAN, work)
lines = [l for l in out.splitlines() if l.startswith('log: cfg:') and '=' in l and 'line' in l]
check(rc == 0 and not lines, 'a file whose values fit logs nothing about them', out)
# values that do not: one line each, with the line number
rc, out = run(exe, env, 'load', BAD, work, {'NX2NAVI_SHARPNESS': '500', 'NX2NAVI_VK_PRESENT': 'fifo'})
want = [
"line 2: pace_smooth = '0 ; off' is not 0 or 1; a ';' or '#' after the value is part of it",
"line 3: fullscreen = 'false' is not 0 or 1",
"line 4: sharpness = '250' is not a number in 0..100",
"line 5: color_gamma = '2' is not a number in 0.8..1.2",
"line 6: vk_present = 'vsync' is none of the menu's choices mailbox|fifo|immediate",
"line 7: fps_limit = '144 # my monitor' a ';' or '#' after the value is part of it",
"the environment's NX2NAVI_SHARPNESS = '500' is not a number in 0..100",
]
for w in want:
check(rc == 0 and w in out, f'logged: {w}', out if w not in out else '')
check('NX2NAVI_VK_PRESENT' not in out, 'an environment value that fits is not logged', out)
check("pace_smooth '0 ; off'" in out, 'the value itself is read as written', out)
# at most 16 lines, then a count
many = '\n'.join(f'sharpness = {200 + i}' if i % 2 else f'fullscreen = no{i}' for i in range(2)) + '\n'
many = ''.join(f'[s{i}]\nsharpness = {200 + i}\n' for i in range(1)) + ''.join(
f'{k} = 7\n' for k in ('fullscreen', 'pace_smooth', 'vk_present', 'sharpness', 'color_gamma', 'perf_overlay',
'upscaler', 'standin_filter', 'dlss', 'dlss_preset', 'framegen', 'aa', 'aspect',
'texture_detail', 'color_preset', 'device_profile', 'mods_rebuild', 'operation_mode',
'text_speed', 'sprint_mode'))
rc, out = run(exe, env, 'load', many, work)
listed = sum(1 for l in out.splitlines() if l.startswith('log: cfg: nx2navi.ini line') and "= '7'" in l)
check(rc == 0 and listed == 16 and 'more values outside their settings' in out,
f'20 values outside their sets: {listed} lines and a count', out)
print(f'cfg_parse_check: {"FAILED" if bad else "ok"}')
return 1 if bad else 0
if __name__ == '__main__':
sys.exit(main())