diff --git a/docs/building.md b/docs/building.md index d72b6f94..3b902795 100644 --- a/docs/building.md +++ b/docs/building.md @@ -911,8 +911,16 @@ random frames in which the census part never passes the frame and the parts keep A change to how the on/off switches read their value (`runtime/perf.c` `rt_knob`) passes `python tests\rt_knob_check.py`, which cuts the function out of the file with its warning table and builds it with stand-ins for the settings and the -log: 0, 1 and ab read as before without a line, and any other value reads as 1 and is logged once per switch. No game -files. +log: 0, 1 and ab read as before without a line, false, off and no in any case read as 0 and true, on and yes as 1 +without a line, and any other value reads as 1 and is logged once per switch. No game files. + +A change to how nx2navi.ini and the environment are read (`runtime/cfg.c` `cfg_load`, `cut_comment`, `switch_word`, +`cfg_bool`, `check_value`) passes `python tests\cfg_parse_check.py`, which builds `cfg.c` with stand-ins for what it +links with and loads scratch files: a comment after a value of a setting with a set of values is cut and logged once +with its line, free text, a value in double quotes and the environment keep theirs; switches written as words read as +0 or 1 through every reader; values outside their sets are logged with their line, at most 16 lines and a count; a +full settings table logs one line at load and one for the menu. The launcher's reader (`launcher/gameini.py`) has the +same comment lines in `tests\launcher_test.py` `PlayerIniTest`. No game files. The lifter (`recomp/`) and the CPU headers (`runtime/cpu.h`, `runtime/simd.h`) are checked against Unicorn. Every lifter change passes these, all run with `.venv`: diff --git a/launcher/gameini.py b/launcher/gameini.py index 912fab2f..7c11180b 100644 --- a/launcher/gameini.py +++ b/launcher/gameini.py @@ -3,13 +3,16 @@ Read the way runtime/cfg.c reads the file: a UTF-8 byte order mark is skipped, lines starting with #, ; or [ are not settings, a key is matched without regard to case and with or without its NX2NAVI_ prefix, a value in double quotes loses them, an empty value leaves the setting as it was (its default, or an earlier line's value), and the last -line with a value wins. Sections do not matter to the game; a key the file does not have yet goes under its section, -as the game writes it. +line with a value wins. For a key whose row in runtime/cfg.c has a set of values (choices=True: numbers, ranges, +words), a blank followed by ';' or '#' starts a comment after a value that is not in double quotes, as cfg.c +cut_comment reads it; free text (a path, a name, the list of mods) keeps its whole value. Sections do not matter to +the game; a key the file does not have yet goes under its section, as the game writes it. A change rewrites only the line it touches, through a temporary file: the other lines keep their bytes (the byte order mark, line ends, text that is not UTF-8). A file that exists but cannot be read is never rewritten, as runtime/cfg.c cfg_save_to does not: rebuilding it from nothing would lose its lines. """ import os +import re from pathlib import Path PREFIX = 'NX2NAVI_' @@ -35,33 +38,40 @@ def _text(game_dir): return '' -def _setting(line): - """(key, value) of a settings line, or None.""" +def _setting(line, choices=False): + """(key, value) of a settings line, or None. choices: the key has a set of values, so its value ends at a comment + (the first blank followed by ';' or '#' after the '=', "key = ; note" giving ''), unless it is in double quotes.""" s = line.lstrip(BOM).strip() if not s or s[0] in '#;[' or '=' not in s: return None - k, v = s.split('=', 1) - v = v.strip() + k, raw = s.split('=', 1) + v = raw.strip() + quoted = len(v) >= 2 and v[0] == v[-1] == '"' + if choices and not quoted: + m = re.search(r'[ \t][;#]', raw) + if m: + v = raw[:m.start()].strip() if len(v) >= 2 and v[0] == v[-1] == '"': v = v[1:-1] return _key(k), v -def get(game_dir, key): - """The value the file gives key ('' when it gives none: the game's default applies). Raises OSError when the file - exists but cannot be read.""" +def get(game_dir, key, choices=False): + """The value the file gives key ('' when it gives none: the game's default applies); choices as _setting. Raises + OSError when the file exists but cannot be read.""" want, value = _key(key), '' for line in _text(game_dir).splitlines(): - kv = _setting(line) + kv = _setting(line, choices) if kv and kv[0] == want and kv[1]: value = kv[1] return value -def put(game_dir, section, key, value): - """Write key = value: on the key's last line that has a value (else its last line) when the file has one, else - under [section] (made at the end when the file has none). Raises OSError, the file unchanged, when it exists but - cannot be read or the new one cannot be put in place.""" +def put(game_dir, section, key, value, choices=False): + """Write key = value: on the key's last line that has a value as the game reads it (choices as _setting; else its + last line) when the file has one, else under [section] (made at the end when the file has none). The line is + written again whole, without a comment it had, as runtime/cfg.c cfg_save_to writes it. Raises OSError, the file + unchanged, when it exists but cannot be read or the new one cannot be put in place.""" text = _text(game_dir) bom = BOM if text.startswith(BOM) else '' lines = text[len(bom):].splitlines(keepends=True) @@ -69,7 +79,7 @@ def put(game_dir, section, key, value): entry = f'{key.lower()} = {value}' nl = '\r\n' if lines and lines[0].endswith('\r\n') else '\n' mine = [i for i, line in enumerate(lines) if (_setting(line) or ('',))[0] == want] - valued = [i for i in mine if _setting(lines[i])[1]] + valued = [i for i in mine if _setting(lines[i], choices)[1]] at = (valued or mine or [-1])[-1] if at >= 0: end = lines[at][len(lines[at].rstrip('\r\n')):] or nl diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 14a27ca3..35fe2891 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -1,7 +1,9 @@ # nx2navi.ini: settings for nx2navi.exe. Copy this file to nx2navi.ini next to nx2navi.exe and remove the '#' of a line to change it. # Each line shows the default; on a Steam Deck the device profile ([display]) changes a few of them. A NX2NAVI_* # environment variable of the same name (NX2NAVI_FPS_LIMIT for fps_limit) overrides the file. Keys are case-insensitive; -# '#' and ';' start a comment; [sections] only group lines. +# '#' and ';' start a comment; [sections] only group lines. After a number or one of a setting's words, a space or tab +# then '#' or ';' starts a comment too ("fps_limit = 144 # my monitor" is 144); a path, a name or a size keeps its whole +# text. A setting of 0 or 1 also takes off, false and no for 0 and on, true and yes for 1. # Only the keys in this file are settings. A line with any other key does nothing: the game's log names it at the # start ("no setting of this version") and the settings menu (F1) shows it in its bottom row. # Updates and rebuilds of the game never rewrite nx2navi.ini; the settings menu changes only the lines of the settings diff --git a/runtime/cfg.c b/runtime/cfg.c index a6481433..b387ceb6 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -1,7 +1,10 @@ // nx2navi.ini: file-backed defaults for the NX2NAVI_* environment variables (docs/controls.md, nx2navi.ini.example). // Format: "key = value" lines, '#' or ';' at the start of a line begins a comment, [sections] only group. A key is a // variable name without "NX2NAVI_" (an optional NX2NAVI_ prefix is accepted), case-insensitive. A value is the rest of the -// line, trimmed, optionally in double quotes; an empty value counts as unset. The environment wins over the file. +// line, trimmed, optionally in double quotes; an empty value counts as unset. For a setting with a set of values (a row +// of metas[] with values), a blank followed by ';' or '#' starts a comment after the value too (cut_comment). A switch +// (values 0|1) reads false, off and no as 0 and true, on and yes as 1 (switch_word). The environment wins over the +// file. // // Live settings: cfg_set changes a value in memory (readers see the new pointer; the old string is kept, so a pointer // cfg_get handed out stays valid) and cfg_save writes only the changed keys back, keeping the rest of the file. @@ -553,8 +556,45 @@ static char *norm_key(char *key) { static int entry_line[CFG_MAX_KEYS]; // the file line each entry's value came from (cfg_load's check_values) +// ---- a comment after a value (cfg_load) +// "pace_smooth = 0 ; off" kept the comment in the value, which cfg_bool read as on, and "fps_limit = 144 # monitor" +// gave 60. For a key whose row has a set of values (numbers, ranges, words: none has a blank followed by ';' or '#') +// the value ends at the first blank followed by ';' or '#' in the text after '=' ("key = ; note" is empty), and the +// line is logged once at load. Free text keeps the whole value: a row without a set (window_size, monitor, +// audio_device) and a key without a row (mods_dir, pipeline_cache, the bindings), where a path or a name may hold +// " #". So does a value in double quotes. The menu's save writes a changed key's line again without its comment +// (cfg_save_to), the same way every time. At most CHECK_LINES lines per load, then a count (check_end). +#define CHECK_LINES 16 +static int cuts; + +// raw: the text after '=' as written; returns the value trimmed, cut at its comment when key has a set of values. +static char *cut_comment(const char *key, char *raw, int line) { + char *v = trim(raw); + size_t n = strlen(v); + if (n >= 2 && v[0] == '"' && v[n - 1] == '"') return v; + const CfgMeta *m = cfg_meta(key); + if (!m || !m->values) return v; + char *c = raw; + while (*c && !((*c == ' ' || *c == '\t') && (c[1] == ';' || c[1] == '#'))) c++; + if (!*c) return v; + char note[160]; + snprintf(note, sizeof note, "%s", c + 1); + *c = 0; + v = trim(raw); + if (++cuts <= CHECK_LINES) { + char low[64]; + size_t k = 0; + for (; key[k] && k < sizeof low - 1; k++) low[k] = (char)tolower((unsigned char)key[k]); + low[k] = 0; + rt_log("cfg: nx2navi.ini line %d: %s = '%s', and '%s' after it is a comment", line, low, v, note); + } + return v; +} + +// value: the text after '=' as written (cut_comment trims it). static void add(char *key, char *value, int line) { key = norm_key(key); + value = cut_comment(key, value, line); size_t vl = strlen(value); if (vl >= 2 && value[0] == '"' && value[vl - 1] == '"') value[vl - 1] = 0, value++; if (!*key) return; @@ -580,19 +620,20 @@ static void add(char *key, char *value, int 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 +// ---- values outside their rows' sets, logged at load (the values are read as they are written, a comment after a +// value of a row with a set cut first, cut_comment) +// Hand-edited files had values no consumer takes, and nothing said so: "sharpness = 250" is past 0..100. Only the +// sets that are the consumers' own are checked: 0|1 (with the words switch_word reads), 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. A ';' or '#' after a blank is still named where it +// stays in the value: a free-text row's value, and the environment's. At most CHECK_LINES lines per load, then a count. static int misfits; +static int switch_of(const char *v); // 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; + if (!strcmp(set, "0|1")) return switch_of(v) < 0 ? "is not 0, 1, on, off, true, false, yes or no" : 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 @@ -647,7 +688,8 @@ static void check_value(const char *key, const char *v, int line) { 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; + if (cuts > CHECK_LINES) rt_log("cfg: %d more comments after a value are not listed", cuts - CHECK_LINES); + misfits = cuts = 0; } static void check_values(void) { @@ -727,7 +769,7 @@ int cfg_load(const char *path) { if (!eq) rt_log("cfg: nx2navi.ini line %d: no '=' in '%s'", line, s); else { *eq = 0; - add(trim(s), trim(eq + 1), line); + add(trim(s), eq + 1, line); } } if (!eol) break; @@ -848,11 +890,33 @@ static const char *raw_get(const char *name) { return NULL; } +// A switch written as a word: 0 for false, off and no, 1 for true, on and yes (any case); -1 for anything else but 0 +// and 1, which give themselves. +static int switch_of(const char *v) { + static const char *const word[] = {"false", "off", "no", "true", "on", "yes"}; + if (!strcmp(v, "0") || !strcmp(v, "1")) return v[0] - '0'; + for (int i = 0; i < 6; i++) + if (!strcasecmp(v, word[i])) return i >= 3; + return -1; +} + +// The value of a switch (a row whose set is 0|1) written as a word, as "0" or "1" for every reader: cfg_bool, rt_knob +// and the readers that compare with "0" (frame_rate.c frame_step_cap_ns for rtss) or parse a number (cfg_int). +// "fullscreen = false" and "rtss = off" were on, read as anything but "0". Other values and other rows are handed on +// as they are. +static const char *switch_word(const char *name, const char *v) { + if (!v || !strcmp(v, "0") || !strcmp(v, "1")) return v; + int on = switch_of(v); + if (on < 0) return v; + const CfgMeta *m = cfg_meta(name); + return m && m->values && !strcmp(m->values, "0|1") ? (on ? "1" : "0") : v; +} + static const char *cfg_get_now(const char *name) { const char *v = raw_get(name); if (strncmp(name, PREFIX, PREFIX_LEN)) return v; if (!lowend_of() && lowend_only(name + PREFIX_LEN)) return NULL; // the mode is off: its settings are not read - if (v) return v; + if (v) return switch_word(name, v); if (profile_of()) for (size_t i = 0; i < sizeof DECK / sizeof *DECK; i++) if (!strcasecmp(DECK[i].key, name + PREFIX_LEN)) return DECK[i].value; @@ -1031,7 +1095,11 @@ int cfg_int(const char *name) { return end == v || *end ? (int)default_number(name) : (int)l; } -int cfg_bool(const char *name) { return strcmp(cfg_value(name), "0") != 0; } +// Off for "0" and for false, off and no in any case (switch_of), also for a key whose row is not 0|1; on otherwise. +int cfg_bool(const char *name) { + const char *v = cfg_value(name); + return strcmp(v, "0") != 0 && switch_of(v) != 0; +} // ---- the restart-only settings' values at start (cfg_init), which the running game keeps using whatever the menu sets // later: cfg_pending compares against them, so the menu says "after restart" only while a restart would change diff --git a/runtime/perf.c b/runtime/perf.c index 14b6efed..11ad7c88 100644 --- a/runtime/perf.c +++ b/runtime/perf.c @@ -984,10 +984,11 @@ int rt_ab(void) { return k == 1 || k == 2; } -// rt_knob reads "0" as off, "ab" as the A/B blocks and anything else as on, so "off", "false" or "0 ; a note" turn a -// switch on. A value other than 0, 1 and ab is logged once per switch; the names are string literals, so the table -// holds their pointers (a full table logs no more). The value is still read as on: taking such words as off would -// change what a written setting does. +// rt_knob reads "0", false, off and no (any case) as off, "ab" as the A/B blocks, and anything else as on, as cfg.c +// cfg_bool does; nx2navi.ini's loader has already cut a comment after a value of a key whose row has a set of +// values ("0 ; a note", cfg.c cut_comment). A value other than 0, 1, ab and those words (true, on and yes for on) is +// logged once per switch; the names are string literals, so the table holds their pointers (a full table logs no +// more). #define KNOB_WARNED 32 static const char *knob_warned[KNOB_WARNED]; @@ -995,7 +996,7 @@ static void knob_warn(const char *name, const char *key, const char *value) { for (int i = 0; i < KNOB_WARNED; i++) { const char *cur = __atomic_load_n(&knob_warned[i], __ATOMIC_ACQUIRE); if (!cur && __atomic_compare_exchange_n(&knob_warned[i], &cur, name, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) { - rt_log("cfg: %s = '%s' is not 0, 1 or ab: read as 1", key, value); + rt_log("cfg: %s = '%s' is not 0, 1, ab, on, off, true, false, yes or no: read as 1", key, value); return; } if (cur == name) return; // logged already (another thread may have taken this slot for it just now) @@ -1008,6 +1009,9 @@ int rt_knob(const char *name, const char *def) { const char *e = cfg_get(key); if (!e || !*e) e = def; if (!strcmp(e, "ab")) return 2; + static const char *const word[] = {"false", "off", "no", "true", "on", "yes"}; + for (int i = 0; i < 6; i++) + if (!strcasecmp(e, word[i])) return i >= 3; if (strcmp(e, "0") && strcmp(e, "1")) knob_warn(name, key, e); return strcmp(e, "0") != 0; } diff --git a/tests/cfg_parse_check.c b/tests/cfg_parse_check.c index bbb010c3..5e4c6c54 100644 --- a/tests/cfg_parse_check.c +++ b/tests/cfg_parse_check.c @@ -3,6 +3,8 @@ // 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) // full cfg_load on the file, then two cfg_set of keys the file does not have +// read cfg_load on the file and cfg_init's check of the environment; then what cfg_get, cfg_bool and cfg_int give +// for a list of keys // norow cfg_int and cfg_double of a key without a row whose value (the environment's) does not parse #include #include @@ -81,6 +83,24 @@ int main(int argc, char **argv) { printf("got: vk_present %s\n", cfg_get("NX2NAVI_VK_PRESENT") ? cfg_get("NX2NAVI_VK_PRESENT") : "(unset)"); return 0; } + if (!strcmp(argv[1], "read")) { + if (!cfg_load(ini)) return printf("cfg_load failed\n"), 2; + printf("--- environment\n"); + cfg_init(); + static const char *const keys[] = {"PACE_SMOOTH", "FULLSCREEN", "FPS_LIMIT", "VK_PRESENT", "ASPECT", "RTSS", + "HDR_LIVE", "CLOTH_REALTIME", "WINDOW_SIZE", "DLSS_PRESET", "MODS_DIR", + "DYN_RES", "SHADOW_PCF"}; + for (size_t i = 0; i < sizeof keys / sizeof *keys; i++) { + char name[64]; + snprintf(name, sizeof name, "NX2NAVI_%s", keys[i]); + const char *v = cfg_get(name); + printf("got: %s '%s'", keys[i], v ? v : "(unset)"); + const CfgMeta *m = cfg_meta(name); + if (m && m->values && !strcmp(m->values, "0|1")) printf(" bool %d int %d", cfg_bool(name), cfg_int(name)); + printf("\n"); + } + return 0; + } if (!strcmp(argv[1], "norow")) { if (!cfg_load(ini)) return printf("cfg_load failed\n"), 2; printf("got: cfg_int %d cfg_double %g\n", cfg_int("NX2NAVI_NO_ROW_CHECK"), cfg_double("NX2NAVI_NO_ROW_CHECK")); diff --git a/tests/cfg_parse_check.py b/tests/cfg_parse_check.py index 0cfa221e..304a9e28 100644 --- a/tests/cfg_parse_check.py +++ b/tests/cfg_parse_check.py @@ -1,9 +1,15 @@ """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: +for what it links with. What the readers get and what the log says about the values: +- a comment after a value (cfg.c cut_comment): cut at a blank followed by ';' or '#' for a key whose row has a set of + values, the line logged once with its line number, "key = ; note" left unset; kept whole in free text (a row + without a set, a key without a row), in a value in double quotes and in the environment, where the log still says + it is part of the value; at most 16 lines, then a count; +- switches written as words (cfg.c switch_word, cfg_bool): false, off and no in any case read as 0 and true, on and + yes as 1 through cfg_get, cfg_bool and cfg_int, from the file and from the environment, with no line 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; + word that is none of the menu's choices, 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; - a full settings table (cfg.c CFG_MAX_KEYS): one line at load naming the first key not read, one at the first menu change not kept; - a number read of a key without a row whose set value does not parse gives 0 (cfg.c default_number), not a crash. @@ -53,6 +59,24 @@ color_gamma = 2 vk_present = vsync fps_limit = 144 # my monitor sharpness_typo = 3 +cloth_realtime = disabled +window_size = 1280x720 # small +aspect = ; stretch some day +dlss_preset = "k ; quoted" +''' + +READ = '''[display] +fullscreen = false +rtss = OFF +hdr_live = Yes +cloth_realtime = on ; the default +pace_smooth = No +vk_present = fifo\t; tear-free +fps_limit = 144 # my monitor +window_size = 1280x720 # small +aspect = ; nothing yet +mods_dir = D:\\My Mods #2 +dlss_preset = "k ; quoted" ''' @@ -94,21 +118,48 @@ def main(): 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 + # values that do not: one line each, with the line number; a comment after a value of a row with a set: cut, and + # one line 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 2: pace_smooth = '0', and '; off' after it is a comment", "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", + "line 7: fps_limit = '144', and '# my monitor' after it is a comment", + "line 9: cloth_realtime = 'disabled' is not 0, 1, on, off, true, false, yes or no", + "line 10: window_size = '1280x720 # small' a ';' or '#' after the value is part of it", + "line 11: aspect = '', and '; stretch some day' after it is a comment", + "line 12: dlss_preset = 'k ; quoted' is none of the menu's choices default|k|j; 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 '') + cfg_lines = [l for l in out.splitlines() if l.startswith('log: cfg: nx2navi.ini line') and 'unknown key' not in l] + check(len(cfg_lines) == 9, f'one line each for the nine lines, none for "fullscreen = false" ({len(cfg_lines)})', + out) + check(not any(' line 2: ' in l and 'is not 0' in l for l in cfg_lines), 'a cut value is checked as cut', out) 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) + check("got: pace_smooth '0'" in out, 'the value is read without its comment', out) + + # what the readers get: switches written as words, cut comments, free text kept whole, the environment as written + rc, out = run(exe, env, 'read', READ, work, {'NX2NAVI_DYN_RES': 'TRUE', 'NX2NAVI_SHADOW_PCF': '0 ; env note'}) + got = {l.split()[1]: l.split(' ', 2)[2] for l in out.splitlines() if l.startswith('got: ')} + want = {'PACE_SMOOTH': "'0' bool 0 int 0", 'FULLSCREEN': "'0' bool 0 int 0", 'RTSS': "'0' bool 0 int 0", + 'HDR_LIVE': "'1' bool 1 int 1", 'CLOTH_REALTIME': "'1' bool 1 int 1", 'FPS_LIMIT': "'144'", + 'VK_PRESENT': "'fifo'", 'ASPECT': "'(unset)'", 'WINDOW_SIZE': "'1280x720 # small'", + 'DLSS_PRESET': "'k ; quoted'", 'MODS_DIR': "'D:\\My Mods #2'", 'DYN_RES': "'1' bool 1 int 1", + 'SHADOW_PCF': "'0 ; env note' bool 1 int 0"} + for k, w in want.items(): + check(rc == 0 and got.get(k) == w, f'read: {k} {w}', f'got {got.get(k)!r}\n{out}' if got.get(k) != w else '') + cut = [l for l in out.splitlines() if 'after it is a comment' in l] + check(len(cut) == 4 and any("line 7: vk_present = 'fifo', and '; tear-free'" in l for l in cut), + f'four cut lines, a tab before the comment too ({len(cut)})', out) + check(not any(w in out for w in ('fullscreen =', 'rtss =', 'hdr_live =', 'pace_smooth =', 'NX2NAVI_DYN_RES =')), + 'no line about switches written as words', out) + check("the environment's NX2NAVI_SHADOW_PCF = '0 ; env note' is not 0, 1, on, off, true, false, yes or no; a ';' " + "or '#' after the value is part of it" in out, "the environment's comment stays in its value", 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' @@ -121,6 +172,10 @@ def main(): 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) + rc, out = run(exe, env, 'load', ''.join(f'vk_present = fifo ; note {i}\n' for i in range(20)), work) + listed = sum(1 for l in out.splitlines() if 'after it is a comment' in l) + check(rc == 0 and listed == 16 and '4 more comments after a value are not listed' in out, + f'20 comments after values: {listed} lines and a count', out) # more keys than the table holds: one line at load, one for the menu's changes full = ''.join(f'cheat_{0x1000 + i:08x} = 1\n' for i in range(600)) + 'low_latency = 1\n' rc, out = run(exe, env, 'full', full, work) diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 28ae1933..99385420 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -380,6 +380,32 @@ class PlayerIniTest(Tmp): gameini.put(self.tmp, 'discord', 'discord_rpc', '1') self.assertEqual(ini.read_bytes(), PLAYER_INI + b'\r\n\r\n[discord]\r\ndiscord_rpc = 1\r\n') + def test_a_comment_after_a_value_as_the_game_reads_it(self): + """runtime/cfg.c cut_comment: for a key whose row has a set of values (choices=True), a blank followed by ';' + or '#' starts a comment, "key = ; note" has no value, and a value in double quotes keeps its text; free text, + the mods folder and lists the launcher writes, keeps the whole value. tests/cfg_parse_check.py has the same + lines for the game.""" + ini = self.tmp / 'nx2navi.ini' + body = (b'[frame rate]\r\npace_smooth = 0 ; off\r\nfps_limit = 144 # my monitor\r\nvk_present = fifo\t; x\r\n' + b'aspect = ; later\r\ndlss_preset = "k ; quoted"\r\n[mods]\r\nmods_dir = D:\\My Mods #2\r\n' + b'mods_disabled = a #1|b ; c\r\n') + ini.write_bytes(body) + for key, want in (('pace_smooth', '0'), ('fps_limit', '144'), ('vk_present', 'fifo'), ('aspect', ''), + ('dlss_preset', 'k ; quoted')): + self.assertEqual(gameini.get(self.tmp, key, choices=True), want, key) + self.assertEqual(gameini.get(self.tmp, 'fps_limit'), '144 # my monitor') + self.assertEqual(gameini.get(self.tmp, 'mods_dir'), 'D:\\My Mods #2') + self.assertEqual(gameini.get(self.tmp, 'mods_disabled'), 'a #1|b ; c') + from launcher import mods + self.assertEqual(mods._names(self.tmp, 'MODS_DISABLED'), ['a #1', 'b ; c']) + # a key's line that has only a comment has no value as the game reads it: the change goes on the line before + ini.write_bytes(b'vk_present = fifo\r\nvk_present = ; later\r\n') + gameini.put(self.tmp, 'display', 'vk_present', 'mailbox', choices=True) + self.assertEqual(ini.read_bytes(), b'vk_present = mailbox\r\nvk_present = ; later\r\n') + ini.write_bytes(b'pace_smooth = 0 ; off\r\n') # the line is written again whole, as the game's menu writes it + gameini.put(self.tmp, 'performance', 'pace_smooth', '1', choices=True) + self.assertEqual(ini.read_bytes(), b'pace_smooth = 1\r\n') + def test_the_older_installer_changes_the_language_line_only(self): """installer/ini.py sets the language, in the older installer's last step at every install and update: the one line goes under [language] with the file's own line ends, and every other byte stays (it used to write diff --git a/tests/rt_knob_check.py b/tests/rt_knob_check.py index 5547e31c..595e044d 100644 --- a/tests/rt_knob_check.py +++ b/tests/rt_knob_check.py @@ -1,6 +1,7 @@ """Build and run runtime/perf.c's rt_knob (cut out of the file with its warning table) with stand-ins for the settings and -the log: 0, 1 and ab read as before and log nothing; a value such as "off", "false" or "0 ; a note" still reads as 1 -and is logged once per switch, however often the switch is read. +the log: 0, 1 and ab read as before and log nothing; false, off and no in any case read as 0 and true, on and yes as 1, +without a line; any other value, such as "0 ; a note" from the environment (nx2navi.ini's loader cuts that comment) or +"enabled", reads as 1 and is logged once per switch, however often the switch is read. No GPU, no game files, any Python. @@ -22,7 +23,9 @@ HARNESS = r''' #include static const char *vals[][2] = {{"NX2NAVI_A", "0"}, {"NX2NAVI_B", "1"}, {"NX2NAVI_C", "ab"}, {"NX2NAVI_D", "off"}, - {"NX2NAVI_E", "false"}, {"NX2NAVI_F", "0 ; a note"}, {"NX2NAVI_G", ""}}; + {"NX2NAVI_E", "false"}, {"NX2NAVI_F", "0 ; a note"}, {"NX2NAVI_G", ""}, + {"NX2NAVI_I", "No"}, {"NX2NAVI_J", "OFF"}, {"NX2NAVI_K", "TRUE"}, {"NX2NAVI_L", "on"}, + {"NX2NAVI_M", "Yes"}, {"NX2NAVI_N", "enabled"}}; static const char *cfg_get(const char *key) { for (size_t i = 0; i < sizeof vals / sizeof *vals; i++) if (!strcmp(vals[i][0], key)) return vals[i][1]; @@ -55,18 +58,27 @@ int main(void) { expect("empty, default 0", rt_knob("G", "0"), 0); } expect("lines for 0, 1, ab and defaults", logs, 0); - expect("off", rt_knob("D", "0"), 1); - expect("a line for off", logs, 1); - if (strcmp(last, "cfg: NX2NAVI_D = 'off' is not 0, 1 or ab: read as 1")) - printf("FAIL the line: %%s\n", last), fails++; - for (int i = 0; i < 5; i++) rt_knob("D", "0"); - expect("off read again", logs, 1); - expect("false", rt_knob("E", "1"), 1); + for (int round = 0; round < 2; round++) { + expect("off", rt_knob("D", "1"), 0); + expect("false", rt_knob("E", "1"), 0); + expect("No", rt_knob("I", "1"), 0); + expect("OFF", rt_knob("J", "1"), 0); + expect("TRUE", rt_knob("K", "0"), 1); + expect("on", rt_knob("L", "0"), 1); + expect("Yes", rt_knob("M", "0"), 1); + } + expect("lines for the words", logs, 0); expect("0 with a note", rt_knob("F", "0"), 1); - expect("a line each", logs, 3); - if (!strstr(last, "'0 ; a note'")) printf("FAIL the line: %%s\n", last), fails++; - for (int i = 0; i < 3; i++) rt_knob("E", "1"), rt_knob("F", "0"); - expect("each once", logs, 3); + expect("a line for it", logs, 1); + if (strcmp(last, "cfg: NX2NAVI_F = '0 ; a note' is not 0, 1, ab, on, off, true, false, yes or no: read as 1")) + printf("FAIL the line: %%s\n", last), fails++; + for (int i = 0; i < 5; i++) rt_knob("F", "0"); + expect("read again", logs, 1); + expect("enabled", rt_knob("N", "0"), 1); + expect("a line each", logs, 2); + if (!strstr(last, "'enabled'")) printf("FAIL the line: %%s\n", last), fails++; + for (int i = 0; i < 3; i++) rt_knob("N", "0"), rt_knob("F", "0"); + expect("each once", logs, 2); if (fails) return 1; printf("rt_knob_check: all checks passed\n"); return 0;