From cb08092a70b165e2b71a8c5ee5f814cb21ae6427 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Fri, 13 Nov 2026 13:43:42 -0500 Subject: [PATCH 1/5] Converter search: ignore lists for upscaler DLLs another installer placed Every search for a file to convert (model data and Ray Reconstruction data; the window, install, model convert, rr-data convert) now skips files named in an ignore list: %LOCALAPPDATA%\WrongGPU\ignore-files.txt (the one an installer writes to), ignore-files.txt in the launcher's own data folder, and the --ignore-list= / --ignore= options. An entry is a full path, a path with its SHA-256, or a SHA-256 alone; paths match the same file (case, slashes, . and .., links and junctions, hard links). An empty list or none never changes which file wins. - A skipped file is named with its reason in the log, the console and the JSON result; a chosen file on a list is not converted either, and the player is told which list and line. - A list that exists but can't be read fails closed (exit 10) after short retries on a sharing violation. - A conversion is reused only while its recorded source (path and, since this change, SHA-256 in model-source.txt) is not on a list. - The uninstalls keep ignore-files.txt and say so. - The shared-folder cleanup picks a target name no earlier cleanup uses and retries a sharing violation or access denied a bounded number of times. The format is documented in clean/installer/README.md ("Ignore lists"). Tests: convert-ignore-tests (ctest convert-ignore, now in CI); run_convert_ignore_tests.py and launcher_ignore_cases.py drive the launcher. Fixes #21 Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- .github/workflows/ci.yml | 4 +- clean/installer/CMakeLists.txt | 8 +- clean/installer/README.md | 103 +++ clean/installer/src/app/cli.cpp | 108 ++- clean/installer/src/app/ui_words.cpp | 7 +- clean/installer/src/wg_core/checks.cpp | 6 +- clean/installer/src/wg_core/context.h | 5 + .../installer/src/wg_core/convert_ignore.cpp | 327 ++++++++ clean/installer/src/wg_core/convert_ignore.h | 101 +++ clean/installer/src/wg_core/install.cpp | 1 + clean/installer/src/wg_core/launcher.cpp | 29 +- clean/installer/src/wg_core/launcher.h | 3 +- clean/installer/src/wg_core/model.cpp | 107 ++- clean/installer/src/wg_core/model.h | 10 + clean/installer/src/wg_core/record.cpp | 23 +- clean/installer/src/wg_core/record.h | 10 +- clean/installer/src/wg_core/rr.cpp | 92 ++- clean/installer/src/wg_core/rr.h | 13 +- .../installer/tests/convert_ignore_tests.cpp | 387 ++++++++++ .../installer/tests/launcher_ignore_cases.py | 94 +++ .../tests/run_convert_ignore_tests.py | 726 ++++++++++++++++++ clean/installer/tests/run_launcher_tests.py | 1 + 22 files changed, 2104 insertions(+), 61 deletions(-) create mode 100644 clean/installer/src/wg_core/convert_ignore.cpp create mode 100644 clean/installer/src/wg_core/convert_ignore.h create mode 100644 clean/installer/tests/convert_ignore_tests.cpp create mode 100644 clean/installer/tests/launcher_ignore_cases.py create mode 100644 clean/installer/tests/run_convert_ignore_tests.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1cc10255..4c2a577e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -77,8 +77,8 @@ jobs: run: | $PSNativeCommandUseErrorActionPreference = $true cmake -S clean/installer -B out/launcher-tests -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded -DWRONGGPU_PAYLOAD_DIR="$PWD/out/no-payload" -DWRONGGPU_TESTING=OFF -DWRONGGPU_PRESET_K=OFF -DWRONGGPU_DIRECT_ROUTE=OFF - cmake --build out/launcher-tests --target wg-core-tests launcher-core-tests coexist-core-tests - ctest --test-dir out/launcher-tests --output-on-failure -R "^(wg-core|launcher-core|coexist-core|source-size|wording)$" + cmake --build out/launcher-tests --target wg-core-tests launcher-core-tests coexist-core-tests convert-ignore-tests + ctest --test-dir out/launcher-tests --output-on-failure -R "^(wg-core|launcher-core|coexist-core|convert-ignore|source-size|wording)$" - name: Ray Reconstruction plugin (build and CPU tests) run: | $PSNativeCommandUseErrorActionPreference = $true diff --git a/clean/installer/CMakeLists.txt b/clean/installer/CMakeLists.txt index d9c5f402..92b299a2 100644 --- a/clean/installer/CMakeLists.txt +++ b/clean/installer/CMakeLists.txt @@ -105,7 +105,7 @@ add_library(wg_core STATIC src/wg_core/model.cpp src/wg_core/checks.cpp src/wg_core/install.cpp src/wg_core/logs.cpp src/wg_core/profile.cpp src/wg_core/logtail.cpp src/wg_core/launcher.cpp src/wg_core/fingerprint.cpp src/wg_core/optikeys.cpp src/wg_core/leftovers.cpp src/wg_core/rr.cpp src/wg_core/rrprep.cpp src/wg_core/gamerules.cpp - src/wg_core/rrconfig.cpp) + src/wg_core/rrconfig.cpp src/wg_core/convert_ignore.cpp) target_include_directories(wg_core PUBLIC src) target_compile_definitions(wg_core PUBLIC ${PRESET_K_DEFINITION} ${DIRECT_ROUTE_DEFINITION}) if(WRONGGPU_TESTING) @@ -188,6 +188,11 @@ add_executable(coexist-core-tests tests/coexist_tests.cpp) target_link_libraries(coexist-core-tests PRIVATE wg_core) # real front-end log excerpts (trimmed, paths replaced) for the Last run parser (UI review r3 #2) target_compile_definitions(coexist-core-tests PRIVATE WG_TEST_LOGS="${CMAKE_CURRENT_SOURCE_DIR}/tests/logs") +# The converter search's ignore lists (src/wg_core/convert_ignore.h): the list format, bad lines, path and hash matching +# (case, slashes, '.' and '..', hard link, junction, symbolic link where the account may make one), the well-known lists, +# and an empty list keeping every candidate set as it was (synthetic files in the temp folder). +add_executable(convert-ignore-tests tests/convert_ignore_tests.cpp) +target_link_libraries(convert-ignore-tests PRIVATE wg_core) enable_testing() add_test(NAME source-size COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/check_source_size.py) # The launcher's words never claim the same output as the original ("bit for bit", "identical", "(native)": rr-notes.md) @@ -195,6 +200,7 @@ add_test(NAME wording COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/ add_test(NAME wg-core COMMAND wg-core-tests) add_test(NAME launcher-core COMMAND launcher-core-tests) add_test(NAME coexist-core COMMAND coexist-core-tests) +add_test(NAME convert-ignore COMMAND convert-ignore-tests) if(WRONGGPU_TESTING) add_test(NAME wizard-policy COMMAND wizard-policy-tests) endif() diff --git a/clean/installer/README.md b/clean/installer/README.md index b0871357..637ad74e 100644 --- a/clean/installer/README.md +++ b/clean/installer/README.md @@ -319,6 +319,8 @@ ctest `source-size`. - **Command line** (a verb as the first argument; `src/app/cli.cpp`): `scan`, `status [game]`, `install`, `repair`, `update`, `uninstall`, `rollback`, `adopt [game | --all]`, `set key=value ...`, `set --global key=value`, `get [key]`, `get --global`, `collect [--zip=]`, `hipcheck`, `model status|convert`, + `rr-data status|convert [--dll=]` (both converts also take `--ignore=`, `--ignore-list=` and `--library=`: + "Ignore lists" below), `self-install [--dir=] [--startmenu=|-]`, `self-uninstall [--keep-games]`, `play `, `update-check`, `apply --job= --result=` (the elevated worker), `version`, `ui-dump --dump= [--shots=]` (the window's pages painted offscreen, never shown: their widgets as JSON, PNGs, for tests), `ui-render --out-dir= @@ -370,6 +372,84 @@ keep the games, which then get their own entries back); its data (`launcher.ini` `%LOCALAPPDATA%\WrongGPU\model` in both modes (the runtime reads it there). `managed-games.txt` in that folder lists the managed games, so an uninstaller keeps the model while another game uses it. +**Ignore lists** (`src/wg_core/convert_ignore.*`): WrongGPU converts only the vendor DLL files that came with the player's +own games, never a copy that another program's installer placed in a game. Such an installer registers the files it placed +in a list the launcher always reads, and the player does nothing. Every search for a file to convert (the model and the Ray +Reconstruction data; the window, the install, `model convert`, `rr-data convert`) reads the well-known lists: + +- `%LOCALAPPDATA%\WrongGPU\ignore-files.txt` (the one an installer writes to; it creates the folder if needed and adds its + lines, keeping the lines already there); +- `ignore-files.txt` in the launcher's own data folder when that is another folder: `WrongGPU-data\ignore-files.txt` next to + a portable launcher, or the `--data=` folder. + +`model convert` and `rr-data convert` add `--ignore-list=` (repeatable; a file that can't be read stops the command, +exit 10, nothing converted) and `--ignore=` (repeatable; one line of the format below, `--ignore=` or +`--ignore=sha256=`; an entry that is not understood stops the command, exit 10). `--library=` searches that +folder's sub-folders as games too (this run only; `--noscan` leaves out the store libraries). + +The format (version 1, stable): a text file, UTF-8 (a BOM is fine) or UTF-16 LE with its BOM, CRLF or LF, one entry per line: + +``` +wronggpu-ignore-files 1 +# placed by Example Tool's installer +C:\Games\Some Game\bin\x64\nvngx_dlssd.dll +C:\Games\Other Game\bin\nvngx_dlssd.dll sha256= +sha256=<64 hex digits> +``` + +- `wronggpu-ignore-files 1`: the format version (optional, first line by convention). A newer version is read with these + rules, with one warning. +- A line whose first non-blank character is `#` is a comment (only whole lines: `#` may be part of a path). Blank lines are fine. +- A path: a full path with a drive letter or `\\server\share` (also written `\\?\C:\...` or `\\?\UNC\server\share\...`; + quotes around it allowed; no wildcards, no environment variables, no relative paths). It matches the candidate that is the + same file: the same fully resolved final path (case, `/` or `\`, `.` and `..`, symbolic links and junctions resolved), or + the same file identity on its volume (a hard link). A path that doesn't exist now is compared as written. UNC was tested + through the PC's own administrative share (`\\localhost\$`): a UNC entry matches the file found by its local path and the + other way round (same file identity); shares on other machines were not tested. +- A path followed by whitespace and `sha256=`: matches by the path or by the hash. +- `sha256=` alone: the file with that SHA-256 is skipped wherever it is and whatever its name. (A hash is computed + only when some list has one.) +- A line that is not understood (not a full path, a hash that is not 64 hex digits, a wildcard) is skipped with a warning + naming the list and the line; the rest of the list is used. + +A file on a list is skipped before its conversion is tried, also when it is the file chosen with `--model=` / `--dll=` +or in the window (no exception); the next file of the search order is tried (for `rr-data convert +--dll=`, which tries only the chosen file, nothing is converted: exit 5). The player is told why: "The file you chose (...) +is not converted: it is on the ignore list , line (path ...)" or "... it is named by --ignore= ...", and to +remove that line if it was listed by mistake (the result's `notes`, the install's warnings, the error when nothing else was +found). Every skipped file is named with the reason (the list and its line, or the `--ignore=` argument, and the path or the +hash) in the log (`model: skipped ...`, `RR data: skipped ...`), on the console and in the JSON result (`skipped`, +`ignore_warnings`). When every file found is on a list, nothing is converted, the message says so and the exit code is 5. An +empty list, or none, never changes which file wins (`tests/run_convert_ignore_tests.py`). + +**A list that can't be read fails closed.** A well-known list that is there but can't be read (another program holds it +open without sharing, no permission, a folder of that name) is tried 5 times, 200 ms apart, when the cause is a sharing +violation (an installer may be writing it); still unreadable, nothing is converted or reused, the message names the file +and the reason, and the exit code is 10 (`model convert`, `rr-data convert`, and an install or repair, whose model check +fails with it). A list that isn't there is simply not read. + +**Conversions on this PC are checked before they are reused.** `model-source.txt` (the model data's, and the RR data's in +`model\F`) records the source file's SHA-256 (`sha256`) besides its path. Wherever the launcher would reuse a +conversion (install, repair, update, the wizard, `model convert`; for the RR data, the install's step), and only while some +list has entries: +- its recorded path or SHA-256 is on a list: it is not reused; the search runs and the first allowed file that converts + replaces it (with or without the output-filter weights). With none, it is not used: the model check fails (exit 5) with a + message that says why, and an RR game's install goes on with RR off and a warning. The file itself is not deleted (other + installed games' runtime reads it; nothing in the runtime changed). +- no SHA-256 is on record (converted by 0.6.0 or earlier): unknown; converted again when an allowed file is found, else kept + with a warning for the player. +With no list entries, a conversion is reused exactly as before. + +**The uninstalls keep the list.** `remove-data` (and the window's Remove data), the launcher's uninstall (`self-uninstall`, +Apps & features) and the last game's uninstall (also through a game's own restorer) remove `%LOCALAPPDATA%\WrongGPU` as +before but put `ignore-files.txt` back in it, and say "kept ignore-files.txt (your/integrators' ignore list): " (the +console and `kept` in `remove-data`'s result; the uninstall's output and log; a game uninstall's result warnings; the +window's toast). The launcher's own data folder's list (portable, `--data=`) was never removed and is now named the same way. +Without a list, the folder goes as before. + +A `WRONGGPU_TESTING` build has `rr-data prepare [--library=]`: the install's RR data step (reuse when the lists allow +it, else convert) without a game, for the tests. + **Update check**: off by default and no URL is built in (`launcher.ini` `update_check`, `update_url`; https only). When both are set, one GET at start (or "Check now") reads a release object (`tag_name`, `html_url`) or text lines `version=` / `page=`; nothing is downloaded. No other network use. @@ -571,6 +651,29 @@ Windows (the Wine run was not repeated for them): `VerifyInstall`, the log-tail parser on synthetic logs, job and result files (relative paths refused), the update reply and the update check staying off without a URL, the game environment of Play, Play's way (Steam app id from an appmanifest fixture, the direct start otherwise; the starts stubbed), `launcher.ini`. +- `tests/convert_ignore_tests.cpp` (ctest `convert-ignore`, every build): the ignore-list format (version line, + comments, a path, a path with its hash, a hash alone, quotes, UTF-8 with a BOM, UTF-16 LE, LF), bad lines (each warned + with its list and line, the rest read), matching (case, `/`, `.` and `..`, a hard link, a junction both ways, a symbolic + link both ways where the account may make one, else it says so and skips those checks; UNC through `\\localhost\$` + where reachable, else it says so), a hash wherever the file is, the well-known list locations, the reason naming the list + and line or the `--ignore=` argument, a list held open without sharing (unreadable after about 0.8 s; read when let go + during the retries), a folder of that name, the recorded-source check, `StartCleanup` keeping the list, and an empty list + keeping every candidate set of `ModelCandidates` as it was. +- `tests/run_convert_ignore_tests.py --launcher --work [--synth-f ]` (CPU only: + `model convert`, `rr-data convert`, `rr-data prepare`, `remove-data`, `self-install` / `self-uninstall` with + `--startmenu=-`, always `--noscan`; no game install, no window): the installer suite's "several DLLs" sets and library-only + sets converted with no list and with an empty list in each place one is read (the same winner, model file and + `model-source.txt`); each list used (`%LOCALAPPDATA%`, the data folder, `--ignore-list=`, `--ignore=`), every candidate + ignored (exit 5, nothing written, the message), a hash-only entry, bad lines, path spellings, a junction, a symbolic link, + a hard link, UNC (`\\localhost\$`, `\\?\UNC\`), the command line's own mistakes (exit 10), a chosen file on a list (the + list and line, or the argument, named); the reuse checks of the model and RR data (recorded SHA-256; listed by hash or + path with and without an allowed file; no SHA-256 on record with and without one; no list); a list held open (exit 10 + after the retries, read when let go, also before a reuse; a folder of that name); `remove-data` and `self-uninstall` + keeping `ignore-files.txt` and saying so. +- `tests/launcher_ignore_cases.py` (run by `run_launcher_tests.py` after its rc6 cases; installs, so it runs with that suite): + the ignore lists on the install path in a work folder of its own: a listed model source not reused by a repair (exit 5, + the list and line), converted again by a second game's install, a list held open (exit 10), and the last game's uninstall + keeping `ignore-files.txt` with the kept line in its result. - `tests/run_launcher_tests.py --launcher --work [--baseline <0.5.1 WrongGPU-Setup.exe>] [--hip ]`: the verbs against generated fixture games (names and synthetic bytes only) with a `WRONGGPU_TESTING` build of the suite's test map: scan statuses, refusals (anti-cheat, DLSS-NR) with nothing changed, diff --git a/clean/installer/src/app/cli.cpp b/clean/installer/src/app/cli.cpp index e81fec60..63df2ff7 100644 --- a/clean/installer/src/app/cli.cpp +++ b/clean/installer/src/app/cli.cpp @@ -20,6 +20,9 @@ // hipcheck the HIP runtime check alone // model status | model convert [--model=] // rr-data status | rr-data convert [--dll=] the Ray Reconstruction data (shared by the RR games) +// both converts: --ignore= | --ignore=sha256=, --ignore-list= (repeatable; added +// to the well-known ignore lists, wg_core/convert_ignore.h), --library= (its sub-folders +// searched as games, this run only) // rr-recheck [--run] what the window's start does: RR's kernels prepared again after a driver / runtime change // self-install [--dir=] [--startmenu=|-] the launcher for this user (one Apps & features entry) // self-uninstall [--keep-games] [--startmenu=] (no window; Apps & features runs "uninstall-launcher", the window's) @@ -60,6 +63,7 @@ const int kRestorerResourceId = 960; // WrongGPU-Restore.exe, embedded by CMake void LauncherInit(const wstring& logOverride, bool worker, bool window) { gl.paths = ResolvePaths(SelfPath(), gl.dataOverride); gl.global = LoadGlobal(gl.paths); + g.ctx.ownDataDir = gl.paths.dataDir; // its ignore-files.txt is a well-known list too (convert_ignore.h) g.ctx.hipOptional = true; g.ctx.hostedLayout = !kDirectRoute; g.ctx.perGameEntry = !LauncherInstalled(); @@ -346,6 +350,8 @@ struct Args { std::map opt; // --name=value (and --name value for the path options) std::set flags; // --name std::vector libraries; // --library= (repeatable) + std::vector ignores; // --ignore=> (repeatable; convert_ignore.h) + std::vector ignoreLists; // --ignore-list= (repeatable) bool has(const wchar_t* f) const { return flags.count(f) > 0; } wstring get(const wchar_t* k) const { auto it = opt.find(k); return it == opt.end() ? wstring() : it->second; } }; @@ -489,6 +495,46 @@ static void RrConfigAfterInstall(const Args& a, const wstring& dir, OpResult& re Print(RrConfigNotice(*rg) + L"\n\nFix it for me: " + set + L"\nDo it yourself: the steps below (rr_config_fix=off stops this question).\n\n" + steps); } +// model convert / rr-data convert: the store libraries (unless --noscan) and the sub-folders of each --library= (one +// game each, as `scan` reads them; this run only). +static std::vector ConvertLibraries(const Args& a, bool noScan) { + std::vector libs = noScan ? std::vector{} : ScanLibraries(); + for (auto& lib : a.libraries) + for (auto& sub : ListDirs(FullPath(lib))) libs.push_back({BaseName(sub), sub, L"", L"Folder"}); + return libs; +} +// --ignore-list= and --ignore= of model convert / rr-data convert (convert_ignore.h), added to the well-known +// lists. A list file that can't be read, or an --ignore= that is not understood, stops the command before anything is +// converted (a file meant to be skipped is never converted because of a typo); a bad line inside a list is skipped with a warning. +static bool ConvertIgnoreArgs(const Args& a, Output& o) { + g.ctx.ignoreListFiles.clear(); + g.ctx.ignoreEntries.clear(); + auto refuse = [&o](const wstring& msg) { o.code = EXIT_USAGE; o.j.S("error", msg); Print(msg); return false; }; + for (auto& f : a.ignoreLists) { + const wstring full = FullPath(f); + string bytes; + if (!IsFile(full) || !ReadAll(full, bytes)) return refuse(L"--ignore-list=" + f + L": the file can't be read (" + full + L"); nothing was converted"); + g.ctx.ignoreListFiles.push_back(full); + } + for (auto& s : a.ignores) { + IgnoreEntry e; + wstring why; + if (!ParseIgnoreLine(s, e, why) || e.text.empty()) + return refuse(L"--ignore=" + s + L": " + (why.empty() ? wstring(L"no path or sha256=") : why) + L"; nothing was converted"); + g.ctx.ignoreEntries.push_back(s); + } + return true; +} +// The files the ignore lists left out, with the reason, and the lists' warnings: in the result and on the console. +static void IgnoreJson(Output& o, const IgnoreReport& r) { + o.j.Key("skipped").Arr(); + for (auto& s : r.skipped) { o.j.Str(s); Print(L" skipped: " + s); } + o.j.End(); + o.j.Key("ignore_warnings").Arr(); + for (auto& w : r.warnings) { o.j.Str(w); Print(L" ignore list warning: " + w); } + o.j.End(); +} + int Verb(const wstring& verb, const Args& a, Output& o) { const bool noScan = a.has(L"noscan"); GlobalSettings& gs = gl.global; @@ -813,15 +859,24 @@ int Verb(const wstring& verb, const Args& a, Output& o) { if (verb == L"model") { const wstring sub = a.pos.empty() ? L"status" : a.pos[0]; if (sub == L"convert") { + if (!ConvertIgnoreArgs(a, o)) return o.code; OpLogBegin(); - const std::vector libs = noScan ? std::vector{} : ScanLibraries(); + const std::vector libs = ConvertLibraries(a, noScan); ModelState m = FindModel(g.ctx, nullptr, a.get(L"model").empty() ? wstring() : FullPath(a.get(L"model")), libs); + IgnoreJson(o, m.ignored); int code = EXIT_OK; + std::vector notes = m.notes; // the player's lines (a chosen file on a list, a model kept unchecked, ...) if (m.ok()) { Ui ui; ui.step = [](int, int, const wstring&) {}; ui.log = [](const wstring& s) { LogLine(g.ctx, s); }; - try { const ModelResult r = WriteModel(g.ctx, m, ui); o.j.S("outcome", r.outcome); Print(L"model: " + r.outcome); } + try { const ModelResult r = WriteModel(g.ctx, m, ui); notes = r.notes; o.j.S("outcome", r.outcome); Print(L"model: " + r.outcome); } catch (const SetupError& e) { o.j.S("error", e.msg); Print(e.msg); code = e.code; } - } else { o.j.S("error", m.error); Print(L"model: " + m.error); code = m.damaged ? EXIT_DAMAGED : EXIT_NO_MODEL; } + } else { + o.j.S("error", m.error); Print(L"model: " + m.error); + code = m.damaged ? EXIT_DAMAGED : m.code ? m.code : EXIT_NO_MODEL; // 10: an ignore list can't be read + } + o.j.Key("notes").Arr(); + for (auto& n : notes) { o.j.Str(n); Print(L" " + n); } + o.j.End(); OpLogEnd(); if (code) return code; } @@ -890,17 +945,38 @@ int Verb(const wstring& verb, const Args& a, Output& o) { if (verb == L"rr-data") { // Ray Reconstruction data (rr.h): status, or "convert" [--dll=] const wstring sub = a.pos.empty() ? L"status" : a.pos[0]; if (sub == L"convert") { + if (!ConvertIgnoreArgs(a, o)) return o.code; OpLogBegin(); - const std::vector libs = noScan ? std::vector{} : ScanLibraries(); + const std::vector libs = ConvertLibraries(a, noScan); wstring why; std::vector notWritten; std::vector warnings; - const bool ok = ConvertRrData(g.ctx, a.get(L"dll").empty() ? wstring() : FullPath(a.get(L"dll")), libs, &why, ¬Written); + IgnoreReport ignored; + const bool ok = ConvertRrData(g.ctx, a.get(L"dll").empty() ? wstring() : FullPath(a.get(L"dll")), libs, &why, ¬Written, &ignored); if (ok) SyncRrSettings(nullptr, L"", warnings, ¬Written); OpLogEnd(); - if (!ok) { o.j.S("error", why); Print(L"Ray Reconstruction data: " + why); return EXIT_NO_MODEL; } + IgnoreJson(o, ignored); + if (!ok) { o.j.S("error", why); Print(L"Ray Reconstruction data: " + why); return ignored.unreadable.empty() ? EXIT_NO_MODEL : EXIT_USAGE; } o.j.Key("warnings").Arr(); for (auto& w : warnings) { o.j.Str(w); Print(L" warning: " + w); } o.j.End(); } +#ifdef WRONGGPU_TESTING + // tests: what an install does with the RR data (FindRrData without `fresh`: the data on this PC reused when the ignore + // lists allow it, else converted again from --library= games), without a game or a device + if (sub == L"prepare") { + if (!ConvertIgnoreArgs(a, o)) return o.code; + const RrDataState st = FindRrData(g.ctx, nullptr, L"", ConvertLibraries(a, noScan), false); + wstring why; + const bool ok = st.ok && WriteRrData(g.ctx, st, &why); + IgnoreJson(o, st.ignored); + o.j.B("reused", st.reused).Key("notes").Arr(); + for (auto& n : st.notes) { o.j.Str(n); Print(L" " + n); } + o.j.End(); + if (!ok) { + o.j.S("error", why.empty() ? st.error : why); Print(L"Ray Reconstruction data: " + (why.empty() ? st.error : why)); + return st.ignored.unreadable.empty() ? EXIT_NO_MODEL : EXIT_USAGE; + } + } +#endif wstring why; const bool valid = RrDataValid(g.ctx, &why); o.j.Key("rr_data").Obj().S("file", RrDataFile()).B("valid", valid).S("source", RrDataSource()).S("why", why).B("payload", RrPayloadAvailable(g.ctx)).End(); @@ -948,10 +1024,11 @@ int Verb(const wstring& verb, const Args& a, Output& o) { } if (verb == L"remove-data") { // a portable copy's Settings > Remove data... without the window (RemoveSharedData) const SharedDataLook look = LookAtSharedData(g.ctx); - wstring why; - if (!RemoveSharedData(g.ctx, &why)) { o.j.S("error", why); Print(why); return EXIT_REFUSED; } - o.j.B("removed", look.there); + wstring why, kept; + if (!RemoveSharedData(g.ctx, &why, &kept)) { o.j.S("error", why); Print(why); return EXIT_REFUSED; } + o.j.B("removed", look.there).S("kept", kept); Print(look.there ? L"WrongGPU's data is removed from this PC." : L"There is no WrongGPU data to remove."); + if (!kept.empty()) Print(kept); // the ignore list stays (convert_ignore.h) return EXIT_OK; } if (verb == L"play") { @@ -1021,12 +1098,17 @@ int RunCli(int argc, wchar_t** argv) { if (s.rfind(L"--", 0) == 0) { const size_t eq = s.find(L'='); const wstring name = Lower(s.substr(2, eq == wstring::npos ? wstring::npos : eq - 2)); + auto put = [&a, &name](const wstring& v) { + if (name == L"library") a.libraries.push_back(v); + else if (name == L"ignore") a.ignores.push_back(v); + else if (name == L"ignore-list") a.ignoreLists.push_back(v); + else a.opt[name] = v; + }; if (eq != wstring::npos) { - const wstring v = s.substr(eq + 1); - if (name == L"library") a.libraries.push_back(v); else a.opt[name] = v; + put(s.substr(eq + 1)); } else if ((name == L"out" || name == L"job" || name == L"result" || name == L"batch" || name == L"data" || name == L"log" || name == L"dir" || name == L"opti" || - name == L"model" || name == L"library" || name == L"startmenu") && i + 1 < argc) { - if (name == L"library") a.libraries.push_back(argv[++i]); else a.opt[name] = argv[++i]; + name == L"model" || name == L"library" || name == L"startmenu" || name == L"ignore" || name == L"ignore-list") && i + 1 < argc) { + put(argv[++i]); } else a.flags.insert(name); } else a.pos.push_back(s); } diff --git a/clean/installer/src/app/ui_words.cpp b/clean/installer/src/app/ui_words.cpp index 1700dc81..1eef5ec8 100644 --- a/clean/installer/src/app/ui_words.cpp +++ b/clean/installer/src/app/ui_words.cpp @@ -757,13 +757,16 @@ void RemoveDataAsked() { if (look->anyGame) { Inform(L"Your games still use WrongGPU's data\n\nUninstall WrongGPU from each game first, then remove the data.", 1); return; } if (!Confirm(RemoveDataText(look->modelData, look->rrData), 0, L"Remove data", L"Cancel", true)) return; auto why = std::make_shared(); + auto kept = std::make_shared(); auto ok = std::make_shared(false); - if (!WaitForRead(L"", L"Removing…", T_NONE, [why, ok] { *ok = RemoveSharedData(g.ctx, why.get()); })) return; + if (!WaitForRead(L"", L"Removing…", T_NONE, [why, kept, ok] { *ok = RemoveSharedData(g.ctx, why.get(), kept.get()); })) return; if (!*ok) { InformDetails(L"WrongGPU's data couldn't be removed.\n\nA file in it may be in use. Close what uses it, then try again. Nothing was removed.", kIconError, *why); return; } - ShowToast(L"WrongGPU's data is removed. You can delete this portable copy once you close it.", 1); + // the ignore list stays (convert_ignore.h): said once in the toast + ShowToast(kept->empty() ? L"WrongGPU's data is removed. You can delete this portable copy once you close it." + : L"WrongGPU's data is removed; kept ignore-files.txt (your/integrators' ignore list). You can delete this portable copy once you close it.", 1); StartHealth(false); // the model and RR data are gone: the status line and Settings follow Invalidate(); } diff --git a/clean/installer/src/wg_core/checks.cpp b/clean/installer/src/wg_core/checks.cpp index bc51a1d4..7c9fcfa2 100644 --- a/clean/installer/src/wg_core/checks.cpp +++ b/clean/installer/src/wg_core/checks.cpp @@ -505,8 +505,10 @@ Checks RunChecks(Context& ctx, const GameTarget& t, SystemInfo& sys, const Model else if (model->ok()) set(L_MODEL, CK_OK, L"Model data: " + (model->reused ? wstring(L"already prepared on this PC") : BaseName(model->dll) + L" " + (model->version.empty() ? wstring() : model->version + L" ") + L"from " + model->origin + L", converted on this PC")); - else set(L_MODEL, CK_FAIL, model->damaged ? model->error : L"No usable DLSS model file (nvngx_dlss.dll 310.5 or newer) - click \"Model file...\"", - model->damaged ? EXIT_DAMAGED : EXIT_NO_MODEL); + // model->code: an ignore-list case (convert_ignore.h) with its own words: a list that can't be read (10), every file found + // listed, the model data on this PC converted from a listed file (5) + else set(L_MODEL, CK_FAIL, model->damaged || model->code ? model->error : L"No usable DLSS model file (nvngx_dlss.dll 310.5 or newer) - click \"Model file...\"", + model->damaged ? EXIT_DAMAGED : model->code ? model->code : EXIT_NO_MODEL); if (!have) set(L_CONFLICT, CK_NA, L"Other upscaler mods and older installs"); else if (const Finding* no = t.coexist.refusal(); no && no->tool != Tool::AntiCheat) // the rules table: no override set(L_CONFLICT, CK_FAIL, no->what + L": " + no->message, no->code ? no->code : EXIT_REFUSED); diff --git a/clean/installer/src/wg_core/context.h b/clean/installer/src/wg_core/context.h index e510ce66..cae0574e 100644 --- a/clean/installer/src/wg_core/context.h +++ b/clean/installer/src/wg_core/context.h @@ -89,6 +89,11 @@ struct Context { // The player's Documents folder (rrconfig.h): the elevated worker gets it from its job (it may run as another account); // empty: the Documents known folder of this process's user. wstring playerDocuments; + // The ignore lists of the converter's file search (convert_ignore.h): the launcher's own data folder (its well-known list + // when that is not %LOCALAPPDATA%\WrongGPU: a portable launcher's WrongGPU-data, a --data= folder), and this run's + // command line (--ignore-list=, --ignore=), added to the well-known lists. + wstring ownDataDir; + std::vector ignoreListFiles, ignoreEntries; Context() = default; Context(const Context&) = delete; Context& operator=(const Context&) = delete; diff --git a/clean/installer/src/wg_core/convert_ignore.cpp b/clean/installer/src/wg_core/convert_ignore.cpp new file mode 100644 index 00000000..6e3486cf --- /dev/null +++ b/clean/installer/src/wg_core/convert_ignore.cpp @@ -0,0 +1,327 @@ +// SPDX-License-Identifier: MIT +// WrongGPU launcher core: the ignore lists of the converter's file search (convert_ignore.h). Written for this project. +#include "convert_ignore.h" + +#include + +namespace wg { + +bool FileIdent::operator==(const FileIdent& o) const { + return valid && o.valid && volume == o.volume && std::memcmp(id, o.id, sizeof(id)) == 0; +} + +bool IgnoreLists::hasHashes() const { + for (auto& e : entries) if (!e.sha256.empty()) return true; + return false; +} + +static wstring TrimW(const wstring& s) { + size_t a = 0, b = s.size(); + while (a < b && (s[a] == L' ' || s[a] == L'\t' || s[a] == L'\r' || s[a] == L'\n' || s[a] == 0xFEFF)) ++a; + while (b > a && (s[b - 1] == L' ' || s[b - 1] == L'\t' || s[b - 1] == L'\r' || s[b - 1] == L'\n')) --b; + return s.substr(a, b - a); +} +static bool SameText(const wstring& a, const wstring& b) { // ordinal, case-insensitive (file names) + return CompareStringOrdinal(a.c_str(), (int)a.size(), b.c_str(), (int)b.size(), TRUE) == CSTR_EQUAL; +} +static bool IsHex64(const wstring& h) { + if (h.size() != 64) return false; + for (wchar_t c : h) + if (!((c >= L'0' && c <= L'9') || (c >= L'a' && c <= L'f') || (c >= L'A' && c <= L'F'))) return false; + return true; +} +// "\\?\C:\x" -> "C:\x", "\\?\UNC\server\share\x" -> "\\server\share\x" +static wstring StripLongPrefix(const wstring& p) { + if (p.rfind(L"\\\\?\\UNC\\", 0) == 0) return L"\\\\" + p.substr(8); + if (p.rfind(L"\\\\?\\", 0) == 0) return p.substr(4); + return p; +} +// A full path: "X:\..." / "X:/..." or "\\server\share..." / "//server/share...", without wildcards or quotes inside. +static bool IsFullPath(const wstring& p) { + if (p.find_first_of(L"*?\"<>|") != wstring::npos) return false; + if (p.size() >= 3 && ((p[0] >= L'A' && p[0] <= L'Z') || (p[0] >= L'a' && p[0] <= L'z')) && p[1] == L':' && (p[2] == L'\\' || p[2] == L'/')) + return p.find(L':', 2) == wstring::npos; + if (p.size() >= 5 && (p[0] == L'\\' || p[0] == L'/') && (p[1] == L'\\' || p[1] == L'/') && p[2] != L'?' && p[2] != L'.') + return p.find(L':') == wstring::npos; + return false; +} + +wstring FinalPath(const wstring& p, FileIdent* ident) { + if (ident) *ident = FileIdent(); + // no access rights needed for the name and the id; links are followed (no FILE_FLAG_OPEN_REPARSE_POINT) + HANDLE h = CreateFileW(p.c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS, nullptr); + if (h == INVALID_HANDLE_VALUE) return L""; + wstring out; + DWORD n = GetFinalPathNameByHandleW(h, nullptr, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); + if (n) { + wstring b(n, L'\0'); + const DWORD got = GetFinalPathNameByHandleW(h, &b[0], n, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); + if (got && got < n) { b.resize(got); out = StripLongPrefix(b); } + } + if (ident) { + FILE_ID_INFO fi{}; + if (GetFileInformationByHandleEx(h, FileIdInfo, &fi, sizeof(fi))) { + ident->volume = fi.VolumeSerialNumber; + std::memcpy(ident->id, fi.FileId.Identifier, sizeof(ident->id)); + ident->valid = true; + } else { + BY_HANDLE_FILE_INFORMATION bi{}; + if (GetFileInformationByHandle(h, &bi)) { + ident->volume = bi.dwVolumeSerialNumber; + const unsigned long long idx = ((unsigned long long)bi.nFileIndexHigh << 32) | bi.nFileIndexLow; + std::memcpy(ident->id, &idx, sizeof(idx)); + ident->valid = true; + } + } + } + CloseHandle(h); + return out; +} + +bool ParseIgnoreLine(const wstring& raw, IgnoreEntry& e, wstring& why, int* version) { + e = IgnoreEntry(); + const wstring line = TrimW(raw); + if (line.empty() || line[0] == L'#') return true; + if (StartsWithI(line, kIgnoreFormatTag)) { + const wstring v = TrimW(line.substr(wcslen(kIgnoreFormatTag))); + if (v.empty() || v.find_first_not_of(L"0123456789") != wstring::npos || v.size() > 6) { + why = L"a format version line needs a number (\"" + wstring(kIgnoreFormatTag) + L" 1\")"; + return false; + } + if (version) *version = _wtoi(v.c_str()); + return true; + } + // "sha256=" at the start (a hash alone), or after whitespace that ends the path + static const wstring kSha = L"sha256="; + wstring path, hash; + bool hasHash = false; + if (StartsWithI(line, kSha)) { + hash = TrimW(line.substr(kSha.size())); + hasHash = true; + } else { + const wstring low = Lower(line); + size_t at = wstring::npos; + for (size_t p = low.find(kSha); p != wstring::npos; p = low.find(kSha, p + 1)) + if (p > 0 && (low[p - 1] == L' ' || low[p - 1] == L'\t')) at = p; + if (at != wstring::npos) { + path = TrimW(line.substr(0, at)); + hash = TrimW(line.substr(at + kSha.size())); + hasHash = true; + } else { + path = line; + } + } + if (hasHash && !IsHex64(hash)) { why = L"sha256= needs the file's SHA-256 as 64 hex digits"; return false; } + if (path.size() >= 2 && path.front() == L'"' && path.back() == L'"') path = TrimW(path.substr(1, path.size() - 2)); + path = StripLongPrefix(path); // "\\?\C:\..." and "\\?\UNC\server\share\..." are full paths too + if (!hasHash || !path.empty()) { + if (path.empty()) { why = L"no path"; return false; } + if (!IsFullPath(path)) { why = L"not a full path (a drive letter or \\\\server\\share, no wildcards): " + path; return false; } + e.path = StripLongPrefix(FullPath(path)); // '/', '.', '..' and repeated separators normalised + e.resolved = FinalPath(e.path, &e.ident); + if (e.resolved.empty()) e.resolved = e.path; + } + e.sha256 = Lower(hash); + e.text = line; + return true; +} + +void ParseIgnoreText(const string& bytes, const wstring& source, IgnoreLists& into) { + wstring text; + if (bytes.size() >= 2 && (unsigned char)bytes[0] == 0xFF && (unsigned char)bytes[1] == 0xFE) { // UTF-16 LE with its BOM + text.resize((bytes.size() - 2) / 2); + if (!text.empty()) std::memcpy(&text[0], bytes.data() + 2, text.size() * 2); + } else { + const size_t skip = bytes.size() >= 3 && (unsigned char)bytes[0] == 0xEF && (unsigned char)bytes[1] == 0xBB && (unsigned char)bytes[2] == 0xBF ? 3 : 0; + text = FromUtf8(bytes.substr(skip)); + } + int lineNo = 0, version = 0; + bool newerSaid = false; + for (size_t a = 0; a <= text.size();) { + size_t e = text.find(L'\n', a); + if (e == wstring::npos) e = text.size(); + const wstring line = text.substr(a, e - a); + a = e + 1; + ++lineNo; + IgnoreEntry entry; + wstring why; + const wstring where = source + L", line " + std::to_wstring(lineNo); + if (!ParseIgnoreLine(line, entry, why, &version)) { + into.warnings.push_back(where + L": " + why + L"; the line is skipped"); + continue; + } + if (version > kIgnoreFormatVersion && !newerSaid) { + newerSaid = true; + into.warnings.push_back(where + L": format version " + std::to_wstring(version) + L" is newer than this launcher reads (" + + std::to_wstring(kIgnoreFormatVersion) + L"); its lines are read as version " + std::to_wstring(kIgnoreFormatVersion)); + } + if (entry.text.empty()) continue; + entry.where = where; + into.entries.push_back(entry); + } +} + +// The list's bytes. A sharing violation (an installer writing the list at this moment) is waited for: 5 tries 200 ms apart. +static bool ReadListBytes(const wstring& file, string& out, wstring& why) { + const DWORD attrs = GetFileAttributesW(file.c_str()); + if (attrs == INVALID_FILE_ATTRIBUTES) { why = L"there is no such file"; return false; } + if (attrs & FILE_ATTRIBUTE_DIRECTORY) { why = L"it is a folder, not a file"; return false; } + HANDLE h = INVALID_HANDLE_VALUE; + DWORD err = 0; + for (int i = 0; i < kIgnoreReadTries; ++i) { + if (i) Sleep(kIgnoreReadWaitMs); + // FILE_SHARE_READ only: while another program has it open for writing, this waits instead of reading half a list + h = CreateFileW(file.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); + if (h != INVALID_HANDLE_VALUE) break; + err = GetLastError(); + if (err != ERROR_SHARING_VIOLATION && err != ERROR_LOCK_VIOLATION) break; + } + if (h == INVALID_HANDLE_VALUE) { + why = (err == ERROR_SHARING_VIOLATION || err == ERROR_LOCK_VIOLATION + ? L"another program holds it open; tried " + std::to_wstring(kIgnoreReadTries) + L" times, " + std::to_wstring(kIgnoreReadWaitMs) + L" ms apart: " + : wstring()) + ErrText(err); + while (!why.empty() && (why.back() == L'\n' || why.back() == L'\r' || why.back() == L' ' || why.back() == L'.')) why.pop_back(); + return false; + } + LARGE_INTEGER sz{}; + bool ok = GetFileSizeEx(h, &sz) && sz.QuadPart < (16ll << 20); + if (!ok) why = L"it is larger than 16 MB"; + if (ok) { + out.resize((size_t)sz.QuadPart); + DWORD got = 0; + ok = out.empty() || (ReadFile(h, &out[0], (DWORD)out.size(), &got, nullptr) && got == out.size()); + if (!ok) why = L"reading it failed: " + ErrText(GetLastError()); + } + CloseHandle(h); + return ok; +} + +bool ReadIgnoreList(const wstring& file, IgnoreLists& into, wstring* why) { + string bytes; + wstring w; + if (!ReadListBytes(file, bytes, w)) { if (why) *why = w; return false; } + ParseIgnoreText(bytes, file, into); + into.lists.push_back(file); + return true; +} + +std::vector WellKnownIgnoreLists(const wstring& ownDataDir) { + std::vector r{Join(AppDataPath(), kIgnoreListName)}; + if (!ownDataDir.empty()) { + const wstring own = Join(ownDataDir, kIgnoreListName); + if (!SameText(StripLongPrefix(FullPath(own)), StripLongPrefix(FullPath(r[0])))) r.push_back(own); + } + return r; +} + +// "on the ignore list , line " or "named by --ignore=": which line or argument matched +static wstring Source(const IgnoreEntry& e) { + return StartsWithI(e.where, L"--ignore") ? L"named by " + e.where : L"on the ignore list " + e.where; +} +// The path entries only (no file is read). +static wstring PathReason(const IgnoreLists& l, const wstring& path) { + FileIdent id; + wstring fin = FinalPath(path, &id); + if (fin.empty()) fin = StripLongPrefix(FullPath(path)); + for (auto& e : l.entries) { + if (e.path.empty()) continue; + if (SameText(fin, e.resolved) || SameText(fin, e.path)) return Source(e) + L" (path " + e.path + L")"; + if (id == e.ident) return Source(e) + L" (path " + e.path + L": the same file)"; + } + return L""; +} +static wstring HashReason(const IgnoreLists& l, const wstring& sha) { + if (sha.empty()) return L""; + for (auto& e : l.entries) + if (!e.sha256.empty() && e.sha256 == Lower(sha)) return Source(e) + L" (SHA-256 " + e.sha256 + L")"; + return L""; +} + +wstring IgnoreReason(const IgnoreLists& l, const wstring& candidate) { + if (l.empty()) return L""; + const wstring byPath = PathReason(l, candidate); + if (!byPath.empty() || !l.hasHashes()) return byPath; + return HashReason(l, Sha256File(candidate)); +} + +wstring RecordedSourceReason(const IgnoreLists& l, const wstring& source, const wstring& sha) { + if (l.empty()) return L""; + const wstring byPath = source.empty() ? wstring() : PathReason(l, source); + return byPath.empty() ? HashReason(l, sha) : byPath; +} + +wstring SourceField(const wstring& file, const wchar_t* key) { + string raw; + if (!ReadAll(file, raw)) return L""; + const wstring t = FromUtf8(raw), k = wstring(key) + L"\t"; + for (size_t a = 0; a < t.size();) { + size_t e = t.find(L'\n', a); + if (e == wstring::npos) e = t.size(); + wstring line = t.substr(a, e - a); + a = e + 1; + while (!line.empty() && (line.back() == L'\r' || line.back() == L' ')) line.pop_back(); + if (line.rfind(k, 0) == 0) return line.substr(k.size()); + } + return L""; +} + +IgnoreLists LoadConvertIgnore(Context& ctx, const wchar_t* what, IgnoreReport* rep) { + IgnoreLists l; + wstring why; + auto unreadable = [&](const wstring& f) { + // fail closed: a list that is there but can't be read might name the very file the search would take + if (l.unreadable.empty()) l.unreadable = L"The ignore list " + f + L" can't be read (" + why + L"), so nothing was converted. Close the program " + L"that holds it, or fix the file, and try again."; + LogLine(ctx, wstring(what) + L": the ignore list " + f + L" can't be read: " + why); + }; + for (auto& f : WellKnownIgnoreLists(ctx.ownDataDir)) + if (GetFileAttributesW(f.c_str()) != INVALID_FILE_ATTRIBUTES && !ReadIgnoreList(f, l, &why)) unreadable(f); + for (auto& f : ctx.ignoreListFiles) + if (!ReadIgnoreList(f, l, &why)) unreadable(f); + for (auto& s : ctx.ignoreEntries) { + IgnoreEntry e; + if (!ParseIgnoreLine(s, e, why)) { l.warnings.push_back(L"--ignore=" + s + L": " + why + L"; skipped"); continue; } + if (e.text.empty()) continue; + e.where = L"--ignore=" + s; + l.entries.push_back(e); + } + for (auto& w : l.warnings) { + LogLine(ctx, wstring(what) + L": warning: " + w); + if (rep) rep->warnings.push_back(w); + } + if (rep) rep->unreadable = l.unreadable; + if (!l.empty()) { + wstring from; + for (auto& f : l.lists) from += (from.empty() ? L"" : L", ") + f; + if (!ctx.ignoreEntries.empty()) from += from.empty() ? L"--ignore" : L", --ignore"; + LogLine(ctx, wstring(what) + L": " + std::to_wstring(l.entries.size()) + L" ignore entr" + (l.entries.size() == 1 ? L"y" : L"ies") + L" (" + from + L")"); + } + return l; +} + +std::vector IgnoredMask(Context& ctx, const IgnoreLists& l, const std::vector& paths, const wchar_t* what, IgnoreReport& rep) { + std::vector skip(paths.size(), false); + if (l.empty()) return skip; + for (size_t i = 0; i < paths.size(); ++i) { + const wstring why = IgnoreReason(l, paths[i]); + if (why.empty()) continue; + skip[i] = true; + rep.skipped.push_back(paths[i] + L": " + why); + LogLine(ctx, wstring(what) + L": skipped " + paths[i] + L": " + why); + } + return skip; +} + +wstring PutIgnoreListBack(const wstring& moved, const wstring& shared) { + const wstring from = Join(moved, kIgnoreListName), to = Join(shared, kIgnoreListName); + const DWORD a = GetFileAttributesW(from.c_str()); + if (a == INVALID_FILE_ATTRIBUTES || (a & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT))) return L""; + CreateDirectoryW(shared.c_str(), nullptr); + if (!MoveFileExW(from.c_str(), to.c_str(), MOVEFILE_WRITE_THROUGH)) { // another volume can't happen (a rename beside it); copy as a fallback + if (!CopyFileW(from.c_str(), to.c_str(), TRUE)) return L""; + } + return wstring(kKeptIgnoreListText) + L": " + to; +} + +} // namespace wg diff --git a/clean/installer/src/wg_core/convert_ignore.h b/clean/installer/src/wg_core/convert_ignore.h new file mode 100644 index 00000000..7885a532 --- /dev/null +++ b/clean/installer/src/wg_core/convert_ignore.h @@ -0,0 +1,101 @@ +// SPDX-License-Identifier: MIT +// WrongGPU launcher core: the ignore lists of the converter's file search (README "Ignore lists"). WrongGPU converts only +// the vendor DLL files that came with the player's own games, never a copy another program's installer placed in a game. +// Such an installer names the files it placed in a list the search always reads (the well-known lists below); the command +// line adds lists and entries of its own (--ignore=, --ignore-list=). A file on a list is never converted: it is skipped +// before the conversion is tried, and named with the reason in the log and the command's output. An empty list (or none) +// leaves the search exactly as it is. Written for this project. +// +// List format (version 1; UTF-8, with or without a BOM, or UTF-16 LE with its BOM; CRLF or LF): +// wronggpu-ignore-files 1 optional format version line (first line by convention) +// # a comment a line whose first non-blank character is '#'; blank lines are fine +// C:\Games\Some Game\bin\nvngx_dlssd.dll a full path (drive or UNC; quotes around it are allowed) +// C:\Games\Other\nvngx_dlssd.dll sha256=<64 hex digits> a path and the file's SHA-256 (whitespace before sha256=) +// sha256=<64 hex digits> a hash alone: the file with this SHA-256 is skipped wherever it is +// A path matches the candidate that is the same file: the same fully resolved final path (case, '/' or '\', '.', '..', +// symbolic links and junctions resolved), or the same file identity on its volume (a hard link, any other way to it). +// A path that does not exist now is compared as written, normalised. A hash matches the candidate whose SHA-256 it is, +// wherever that file is; a line with both matches by either. A line that is not understood is skipped with a warning and +// the rest of the list is used. A newer format version is read with these rules (its lines that are not understood are +// skipped with a warning). "\\?\" and "\\?\UNC\" prefixes are read as the plain path. +// A list that is there but can't be read fails closed: nothing is converted or reused (after kIgnoreReadTries on a sharing +// violation). A conversion on this PC is checked against the lists (the source path and SHA-256 its model-source.txt +// records) before it is reused (model.cpp FindModel, rr.cpp FindRrData). The uninstalls and remove-data keep the list +// (PutIgnoreListBack). +#pragma once +#include "context.h" + +namespace wg { + +const wchar_t* const kIgnoreListName = L"ignore-files.txt"; +const wchar_t* const kIgnoreFormatTag = L"wronggpu-ignore-files"; +const int kIgnoreFormatVersion = 1; +// A list that is there but can't be read fails closed (nothing is converted or reused); a sharing violation is waited for. +const int kIgnoreReadTries = 5, kIgnoreReadWaitMs = 200; +// What the uninstalls and remove-data say when they keep the list (PutIgnoreListBack). +const wchar_t* const kKeptIgnoreListText = L"kept ignore-files.txt (your/integrators' ignore list)"; + +// A file's identity on its volume (FILE_ID_INFO: the volume serial number and the 128-bit file id). +struct FileIdent { + unsigned long long volume = 0; + unsigned char id[16] = {}; + bool valid = false; + bool operator==(const FileIdent& o) const; +}; + +struct IgnoreEntry { + wstring text; // the entry as written (for the messages) + wstring path; // the full path as written, normalised ("" for a hash-only entry) + wstring resolved; // the file's fully resolved final path when it exists now, else `path` + FileIdent ident; // the file's identity when it exists now + wstring sha256; // lower-case hex, "" when none + wstring where; // ", line ", or "--ignore" +}; +struct IgnoreLists { + std::vector entries; + std::vector warnings; // lines skipped (each with its list, line and why) + std::vector lists; // the list files read + wstring unreadable; // a list that is there but could not be read: the player's message (the search fails closed) + bool empty() const { return entries.empty(); } + bool hasHashes() const; +}; +// What a search left out: "path: reason" per candidate skipped, and the warnings of the lists it read. `unreadable`: a list +// could not be read (nothing is converted or reused; the command line's exit code is 10). +struct IgnoreReport { + std::vector skipped, warnings; + wstring unreadable; +}; + +// One list line (trimmed by the caller or not). True: an entry in `e`, or nothing for a blank / comment / version line +// (`e.text` empty). False: a line that is not understood (`why`). +bool ParseIgnoreLine(const wstring& line, IgnoreEntry& e, wstring& why, int* version = nullptr); +// A whole list's bytes (`source`: its name in the messages). Bad lines go to into.warnings and are skipped. +void ParseIgnoreText(const string& bytes, const wstring& source, IgnoreLists& into); +// Reads and parses one list file (a sharing violation is tried kIgnoreReadTries times, kIgnoreReadWaitMs apart). False: it +// could not be read (`why`); nothing is added then. +bool ReadIgnoreList(const wstring& file, IgnoreLists& into, wstring* why = nullptr); +// The well-known lists, in the order they are read: %LOCALAPPDATA%\WrongGPU\ignore-files.txt, then ignore-files.txt in the +// launcher's own data folder when that is another folder (a portable launcher's WrongGPU-data, a --data= folder). +std::vector WellKnownIgnoreLists(const wstring& ownDataDir); +// The fully resolved final path of an existing file or folder (links followed; no \\?\ prefix); "" when it can't be opened. +wstring FinalPath(const wstring& p, FileIdent* ident = nullptr); +// Why `candidate` is on a list ("" : it is not): "on the ignore list , line (path ...)" or "named by --ignore= +// (SHA-256 ...)". An empty list never opens or reads the file. +wstring IgnoreReason(const IgnoreLists& l, const wstring& candidate); +// The same for a conversion on this PC: the source path and SHA-256 its model-source.txt recorded (the file is not read +// again; it may have changed or gone since). "" : not on a list (or nothing recorded to compare). +wstring RecordedSourceReason(const IgnoreLists& l, const wstring& source, const wstring& sha); +// A "keyvalue" line of a model-source.txt ("" when the file or the key is not there). +wstring SourceField(const wstring& file, const wchar_t* key); +// The lists of one search: the well-known lists, then this run's (ctx.ignoreListFiles, ctx.ignoreEntries). Their warnings +// are logged (`what`: the log prefix, "model" or "RR data") and added to rep->warnings; a list that is there but can't be +// read sets l.unreadable and rep->unreadable (the caller converts and reuses nothing then). +IgnoreLists LoadConvertIgnore(Context& ctx, const wchar_t* what, IgnoreReport* rep); +// For each path: true when it is skipped (logged, and "path: reason" added to rep.skipped). An empty list skips nothing. +std::vector IgnoredMask(Context& ctx, const IgnoreLists& l, const std::vector& paths, const wchar_t* what, IgnoreReport& rep); +// The shared folder (%LOCALAPPDATA%\WrongGPU) was moved aside to `moved` to be deleted: its ignore-files.txt goes back into +// `shared` (made again for it), as the uninstalls and remove-data keep it. Returns kKeptIgnoreListText and the path, or "" +// when there was no list. +wstring PutIgnoreListBack(const wstring& moved, const wstring& shared); + +} // namespace wg diff --git a/clean/installer/src/wg_core/install.cpp b/clean/installer/src/wg_core/install.cpp index 8fdc0a70..1036d0fd 100644 --- a/clean/installer/src/wg_core/install.cpp +++ b/clean/installer/src/wg_core/install.cpp @@ -228,6 +228,7 @@ std::vector DoInstall(Context& ctx, const GameTarget& target, const Mod if (rr) { ui.step(0, 1, L"Preparing the Ray Reconstruction data"); const RrDataState rd = FindRrData(ctx, &target, L"", {}); + for (auto& n : rd.notes) { ui.log(n); warnings.push_back(n); } // the ignore lists' lines (an RR data kept unchecked) wstring why; rrData = rd.ok && WriteRrData(ctx, rd, &why); if (!rrData) { // nothing fails silently: the operation's result says it too (the window's note after its success) diff --git a/clean/installer/src/wg_core/launcher.cpp b/clean/installer/src/wg_core/launcher.cpp index 18523de8..0a500633 100644 --- a/clean/installer/src/wg_core/launcher.cpp +++ b/clean/installer/src/wg_core/launcher.cpp @@ -1535,15 +1535,17 @@ SharedDataLook LookAtSharedData(Context& ctx) { return s; } // Log a linked root before RemoveTree unlinks it; never queue recursive cleanup for a remaining link. -static bool RemoveSharedTree(Context& ctx, const wstring& dir) { +// keepFile: a file left in place (the ignore list, when the folder is removed where it is). +static bool RemoveSharedTree(Context& ctx, const wstring& dir, const wstring& keepFile = L"") { const DWORD attrs = GetFileAttributesW(dir.c_str()); const bool link = attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_REPARSE_POINT); if (link) LogLine(ctx, L"cleanup: removing a data folder link; its target will be kept: " + dir); - RemoveTree(dir); + RemoveTree(dir, keepFile); return !link; } -bool RemoveSharedData(Context& ctx, wstring* why) { +bool RemoveSharedData(Context& ctx, wstring* why, wstring* kept) { + if (kept) kept->clear(); try { const SharedDataLook s = LookAtSharedData(ctx); if (!s.there) { LogLine(ctx, L"remove data: no shared data folder: nothing to remove"); return true; } @@ -1577,10 +1579,13 @@ bool RemoveSharedData(Context& ctx, wstring* why) { LogLine(ctx, L"remove data: " + shared + L" could not be moved aside: " + ErrText(err)); return false; } + // the ignore list stays (an integrator's installer registered its files there; convert_ignore.h) + const wstring keptList = PutIgnoreListBack(trash, shared); + if (kept) *kept = keptList; const bool ordinaryTrash = RemoveSharedTree(ctx, trash); if (!ordinaryTrash && IsDir(trash)) { if (why) *why = L"Could not remove the WrongGPU data folder link."; return false; } if (ordinaryTrash && IsDir(trash)) DeleteWhenFree(ctx, trash, true, L""); // what is still held goes once it is free - LogLine(ctx, L"remove data: removed " + shared); + LogLine(ctx, L"remove data: removed " + shared + (keptList.empty() ? wstring() : L"; " + keptList)); return true; } catch (const SetupError& e) { if (why) *why = e.msg; @@ -1708,7 +1713,10 @@ bool SelfUninstall(Context& ctx, const LauncherPaths& p, bool removeGames, const const DWORD dataAttrs = GetFileAttributesW(data.c_str()); const bool dataIsLink = dataAttrs != INVALID_FILE_ATTRIBUTES && (dataAttrs & FILE_ATTRIBUTE_REPARSE_POINT); if (!dataIsLink) RemoveLauncherFiles(data); // never remove named files through a data-folder link - if (!IEq(FullPath(data), FullPath(shared))) RemoveDirectoryW(data.c_str()); // a portable or --data folder: empty now + if (!IEq(FullPath(data), FullPath(shared))) { + RemoveDirectoryW(data.c_str()); // a portable or --data folder: empty now (its ignore-files.txt, never named above, stays) + if (!dataIsLink && IsFile(Join(data, kIgnoreListName))) ui.log(wstring(kKeptIgnoreListText) + L": " + Join(data, kIgnoreListName)); + } // the shared folder (%LOCALAPPDATA%\WrongGPU) once no game uses it: the model data, the games' list and the logs go too. // A game on a drive that is not connected still uses it (it stays on the list, its wronggpu.ini names the model file) if (unreachable) *unreachable = offline; @@ -1734,19 +1742,26 @@ bool SelfUninstall(Context& ctx, const LauncherPaths& p, bool removeGames, const if (MoveFileExW(shared.c_str(), trash.c_str(), MOVEFILE_WRITE_THROUGH)) trashMade = true; else Sleep(200); } + // the ignore list stays (an integrator's installer registered its files there; convert_ignore.h) + const wstring list = Join(shared, kIgnoreListName); + wstring keptList; + if (trashMade) keptList = PutIgnoreListBack(trash, shared); if (!trashMade) { - const bool ordinaryShared = RemoveSharedTree(ctx, shared); + const bool hadList = IsFile(list); + const bool ordinaryShared = RemoveSharedTree(ctx, shared, list); if (!ordinaryShared && IsDir(shared)) { if (why) *why = L"Could not remove the WrongGPU data folder link."; return false; } + if (ordinaryShared && hadList && IsFile(list)) keptList = wstring(kKeptIgnoreListText) + L": " + list; // Only enumerate an ordinary folder; a linked root must never be followed, even after an unlink failure. if (ordinaryShared) { std::vector left, dirs{shared}; for (size_t i = 0; i < dirs.size(); ++i) { - for (auto& f : ListFiles(dirs[i], L"*")) left.push_back(f); + for (auto& f : ListFiles(dirs[i], L"*")) if (!IEq(f, list)) left.push_back(f); for (auto& d : ListDirs(dirs[i])) dirs.push_back(d); } for (auto& f : left) { LogLine(ctx, L"launcher uninstall: deleted once free: " + f); DeleteWhenFree(ctx, f, false, Parent(f)); } } } + if (!keptList.empty()) ui.log(keptList); } // 3. The installed program: moved out of its folder at once (a running program can be renamed, as this one is when // its own window uninstalls it), the folder removed; the hidden cleanup deletes it once it no longer runs. diff --git a/clean/installer/src/wg_core/launcher.h b/clean/installer/src/wg_core/launcher.h index 7612e582..baaf8883 100644 --- a/clean/installer/src/wg_core/launcher.h +++ b/clean/installer/src/wg_core/launcher.h @@ -281,7 +281,8 @@ LauncherUninstallLook LookBeforeLauncherUninstall(Context& ctx); // installed: the installed launcher, or its settings, use the folder). struct SharedDataLook { bool there = false, anyGame = false, installed = false, modelData = false, rrData = false; }; SharedDataLook LookAtSharedData(Context& ctx); -bool RemoveSharedData(Context& ctx, wstring* why); +// ignore-files.txt stays (convert_ignore.h): *kept gets "kept ignore-files.txt (...)" and its path when there was one. +bool RemoveSharedData(Context& ctx, wstring* why, wstring* kept = nullptr); // The hidden cleanups this process started (SelfUninstall's moved exe and folders) handed again for what is still there, // as the program ends: they try for an hour, and a window may stay open longer than that. The window calls it last. void RetryCleanupsAtExit(Context& ctx); diff --git a/clean/installer/src/wg_core/model.cpp b/clean/installer/src/wg_core/model.cpp index 6d8d4c40..26e29e26 100644 --- a/clean/installer/src/wg_core/model.cpp +++ b/clean/installer/src/wg_core/model.cpp @@ -46,6 +46,10 @@ std::vector ModelCandidates(const GameTarget* t, const wstring& ModelState FindModel(Context& ctx, const GameTarget* t, const wstring& userDll, const std::vector& libraries) { ModelState m; if (!LoadEmbeddedMap(ctx)) { m.status = ModelState::FAILED; m.damaged = true; m.error = L"The installer is damaged (" + ctx.mapError + L"). Download it again."; return m; } + // The ignore lists (convert_ignore.h), read first: they decide what the search may convert and whether the file converted + // earlier may be reused. A list that is there but can't be read: nothing is converted or reused (fail closed). + const IgnoreLists ign = LoadConvertIgnore(ctx, L"model", &m.ignored); + if (!ign.unreadable.empty()) { m.status = ModelState::FAILED; m.code = EXIT_USAGE; m.error = ign.unreadable; return m; } wstring why; wgc::ModelFileInfo info; const bool existing = ExistingModelValid(ctx, &why, &info); @@ -59,24 +63,64 @@ ModelState FindModel(Context& ctx, const GameTarget* t, const wstring& userDll, if (existing && optional && !inFile) staleWhy = L"the file has no output-filter weights"; else if (existing && optional && !info.same_map) staleWhy = L"the file was written with other converter data"; const wstring source = existing ? ReadModelSource(ctx) : wstring(); + // A second exception, only while a list has entries: the file it was converted from (path and SHA-256 recorded in + // model-source.txt) is on a list (LISTED), or its SHA-256 is not on record (UNKNOWN: an earlier version converted it). + // Then it is converted again from an allowed file whatever that file has; with none, a LISTED file is not used and an + // UNKNOWN one stays with a note. With no list entries nothing changes here. + wstring distrustWhy; + if (existing && !ign.empty()) { + const wstring listed = RecordedSourceReason(ign, source, SourceField(ModelSourceFile(ctx), L"sha256")); + if (!listed.empty()) { + m.trust = ModelState::LISTED; + distrustWhy = L"it was converted from " + (source.empty() ? wstring(L"a file") : source) + L", which is " + listed; + } else if (SourceField(ModelSourceFile(ctx), L"sha256").empty()) { + m.trust = ModelState::UNKNOWN; + distrustWhy = L"the SHA-256 of the file it was converted from is not on record (an earlier version converted it), so the ignore lists can't check it"; + } + } + const bool trusted = m.trust == ModelState::TRUSTED; auto keep = [&](const wstring& outcome) { m.status = ModelState::READY; m.reused = true; m.dll = source; m.origin = L"converted earlier on this PC"; - m.stale = !staleWhy.empty(); m.outcome = outcome + FilterText(ctx, inFile); + m.stale = !staleWhy.empty() || !trusted; m.outcome = outcome + FilterText(ctx, inFile); }; - if (existing && staleWhy.empty()) { + if (existing && staleWhy.empty() && trusted) { keep(L"reused"); LogLine(ctx, L"model: reusing " + ModelFile(ctx) + L" (matches the converter data; made from " + (m.dll.empty() ? wstring(L"an unknown file") : m.dll) + L")"); return m; } - if (existing) LogLine(ctx, L"model: " + ModelFile(ctx) + L" matches the converter data, but " + staleWhy + L"; searching for a DLSS model file to convert it again"); + const wstring againWhy = trusted ? staleWhy : distrustWhy; // why the file on this PC is converted again + if (existing) LogLine(ctx, L"model: " + ModelFile(ctx) + L" matches the converter data, but " + againWhy + L"; searching for a DLSS model file to convert it again"); else LogLine(ctx, L"model: " + why + L"; searching for a DLSS model file"); + // An UNKNOWN file that stays (nothing allowed was found): the player's line. + auto keepUnknown = [&](const wstring& what) { + keep(L"kept: " + distrustWhy + L"; " + what); + m.notes.push_back(L"The model data on this PC was converted by an earlier version of WrongGPU, which did not record the SHA-256 of the DLSS file " + L"it came from, so the ignore lists can't check it. No other DLSS model file was found to convert it again, so it stays in use."); + LogLine(ctx, L"model: keeping " + ModelFile(ctx) + L" (its source can't be checked against the ignore lists; " + what + L")"); + }; // Several DLLs (spec 08 section 8): the first that converts completely with the output-filter weights is used; if // none has them, the first that converts completely. Every file that converts gives the same other stages, so // the order only decides whether the weights come along. The candidates are the same as before. + // A file on a list is left out before anything is tried; the order of the others stays. The rule is absolute: + // a file the player chose is left out too, and the player is told which list and line (or --ignore=) matched. + const std::vector all = ModelCandidates(t, userDll, libraries); + std::vector cands; + { + std::vector paths; + for (auto& c : all) paths.push_back(c.path); + const std::vector skip = IgnoredMask(ctx, ign, paths, L"model", m.ignored); + for (size_t i = 0; i < all.size(); ++i) if (!skip[i]) cands.push_back(all[i]); + if (!userDll.empty() && !all.empty() && skip[0]) { + const wstring& s = m.ignored.skipped.front(); + m.chosenSkipped = L"The file you chose (" + all[0].path + L") is not converted: it is " + s.substr(std::min(s.size(), all[0].path.size() + 2)) + + L". If it was listed by mistake, remove that line (or that --ignore argument)."; + LogLine(ctx, L"model: " + m.chosenSkipped); + } + } wgc::SearchOptions opt; std::shared_ptr chosen, without; // `without`: the first that converts without the weights ModelCandidate chosenCand, withoutCand; - for (auto& cand : ModelCandidates(t, userDll, libraries)) { + for (auto& cand : cands) { auto conv = std::make_shared(); const wgc::Result r = wgc::convert_file(*ctx.map.map, std::filesystem::path(cand.path), opt, *conv, [&ctx](const string& s) { LogLine(ctx, L"converter: " + FromUtf8(s)); }); @@ -94,15 +138,17 @@ ModelState FindModel(Context& ctx, const GameTarget* t, const wstring& userDll, m.rejections.push_back(cand.path + L": " + FromUtf8(r.message)); LogLine(ctx, L"model: rejected " + cand.path + L": " + FromUtf8(r.message)); if (wgc::is_installer_error(r.code)) { - if (existing) { // the file on this PC is valid and stays in use + if (existing && trusted) { // the file on this PC is valid and stays in use keep(L"kept: " + staleWhy + L"; converting it again failed (" + FromUtf8(r.message) + L")"); LogLine(ctx, L"model: keeping " + ModelFile(ctx)); return m; } + if (existing && m.trust == ModelState::UNKNOWN) { keepUnknown(L"converting it again failed (" + FromUtf8(r.message) + L")"); return m; } m.status = ModelState::FAILED; m.damaged = true; m.error = FromUtf8(r.message); return m; } } + if (!m.chosenSkipped.empty()) m.notes.push_back(m.chosenSkipped); const bool hasGroup = chosen != nullptr; if (!chosen && without) { chosen = without; chosenCand = withoutCand; @@ -112,37 +158,64 @@ ModelState FindModel(Context& ctx, const GameTarget* t, const wstring& userDll, std::vector notes; for (auto& n : chosen->notes) notes.push_back(FromUtf8(n)); const wstring others = m.passedOver.empty() ? wstring() : L" (" + std::to_wstring(m.passedOver.size()) + L" other file(s) convert without output-filter weights)"; - if (existing && !hasGroup) { // nothing to gain: the file on this PC stays + if (existing && !hasGroup && trusted) { // nothing to gain: the file on this PC stays keep(L"kept: " + staleWhy + L"; the DLSS file found (" + chosenCand.path + L") converts without output-filter weights"); - if (!inFile) m.notes = notes; + if (!inFile) m.notes.insert(m.notes.end(), notes.begin(), notes.end()); m.passedOver.clear(); LogLine(ctx, L"model: no DLSS file with output-filter weights was found; keeping " + ModelFile(ctx)); return m; } m.status = ModelState::READY; m.dll = chosenCand.path; m.origin = chosenCand.origin; m.version = FromUtf8(chosen->version); m.converted = chosen; - m.replaces = existing; m.notes = notes; - m.outcome = (existing ? L"replaced: " + staleWhy + L"; converted again from " : wstring(L"converted from ")) + chosenCand.path + FilterText(ctx, hasGroup) + + m.replaces = existing; m.notes.insert(m.notes.end(), notes.begin(), notes.end()); + m.dllSha256 = Lower(Sha256File(chosenCand.path)); + m.outcome = (existing ? L"replaced: " + againWhy + L"; converted again from " : wstring(L"converted from ")) + chosenCand.path + FilterText(ctx, hasGroup) + (optional && !hasGroup ? L" (not known for this DLSS build)" : L""); LogLine(ctx, L"model: using " + chosenCand.path + L" (" + chosenCand.origin + L")" + (optional ? hasGroup ? L", with output-filter weights" : L", without output-filter weights" : L"") + others + (existing ? L"; it will replace " + ModelFile(ctx) : wstring())); return m; } + const size_t skipped = m.ignored.skipped.size(); + if (existing && m.trust == ModelState::LISTED) { // never reused; nothing allowed to convert it again from + m.status = ModelState::FAILED; m.code = EXIT_NO_MODEL; + m.error = L"The model data on this PC (" + ModelFile(ctx) + L") is not used: " + distrustWhy + L". No other DLSS model file that WrongGPU may " + L"convert was found. WrongGPU converts only the DLSS model file (nvngx_dlss.dll) that came with one of your own games: choose " + L"the nvngx_dlss.dll that came with one of your own games (Model file...), or install or update such a game." + (m.chosenSkipped.empty() ? wstring() : L" " + m.chosenSkipped); + LogLine(ctx, L"model: " + m.error); + return m; + } + if (existing && m.trust == ModelState::UNKNOWN) { keepUnknown(L"no other DLSS model file was found"); return m; } if (existing) { - keep(L"kept: " + staleWhy + L"; no usable DLSS file was found to convert it again"); + keep(L"kept: " + staleWhy + L"; no usable DLSS file was found to convert it again" + + (skipped ? L" (" + std::to_wstring(skipped) + L" file(s) on an ignore list were skipped)" : wstring())); LogLine(ctx, L"model: no DLSS model file to convert again; keeping " + ModelFile(ctx) + L" (made from " + (m.dll.empty() ? wstring(L"an unknown file") : m.dll) + L")"); return m; } m.status = ModelState::FAILED; + if (skipped && cands.empty()) { // every file found is on an ignore list: nothing was tried + m.code = EXIT_NO_MODEL; + m.error = !m.chosenSkipped.empty() && skipped == 1 + ? m.chosenSkipped + L" No other DLSS model file was found." + : L"Every DLSS model file found (" + std::to_wstring(skipped) + L") is on an ignore list, so none was converted. " + L"WrongGPU converts only the DLSS model file (nvngx_dlss.dll) that came with one of your own games, never a copy " + L"another program placed. The files skipped, and the list and line that skipped each, are named in the log." + + (m.chosenSkipped.empty() ? wstring() : L" " + m.chosenSkipped); + LogLine(ctx, L"model: every DLSS model file found is on an ignore list; nothing converted"); + return m; + } m.error = L"No DLSS model file that WrongGPU can use was found. WrongGPU needs the DLSS model file (nvngx_dlss.dll) of " L"version 310.5 or newer, which comes with many recent games and apps. Install or update such a game, or choose its " - L"nvngx_dlss.dll yourself. The file stays where it is: only the model data is read, on this PC."; + L"nvngx_dlss.dll yourself. The file stays where it is: only the model data is read, on this PC." + + (skipped ? L" (" + std::to_wstring(skipped) + L" file(s) on an ignore list were skipped; they are named in the log.)" : wstring()) + + (m.chosenSkipped.empty() ? wstring() : L" " + m.chosenSkipped); return m; } // A second conversion replaces a valid model file (ModelState::replaces). The player is never left without a working // model: the new file is written beside the one in use, checked there, and only then moved over it; a copy of the // earlier file is kept until the result has passed its check in place, and is put back otherwise. -// False: the new file was not used (`problem` says why) and the earlier file is where it was. +// False: the new file was not used (`problem` says why) and the earlier file is where it was. A file converted again because +// the ignore lists don't allow (or can't check) the earlier one is used whether it has the output-filter weights or not. bool ReplaceModel(Context& ctx, const ModelState& m, wstring& problem) { + const bool needGroups = m.trust == ModelState::TRUSTED; const wstring side = Join(ModelDir(ctx), L"model-new"), sideFile = Join(side, FromUtf8(wgc::kModelFileName)); const wstring previous = ModelFile(ctx) + L".previous"; auto cleanup = [&] { if (IsDir(side)) RemoveTree(side); DeleteFileW(previous.c_str()); }; @@ -158,7 +231,7 @@ bool ReplaceModel(Context& ctx, const ModelState& m, wstring& problem) { #endif string w; wgc::ModelFileInfo info; - if (!wgc::verify_model_file(*ctx.map.map, std::filesystem::path(sideFile), w, &info) || !info.absent_groups.empty() || !info.same_map) { + if (!wgc::verify_model_file(*ctx.map.map, std::filesystem::path(sideFile), w, &info) || (needGroups && !info.absent_groups.empty()) || !info.same_map) { problem = L"the new file failed its check" + (w.empty() ? wstring() : L" (" + FromUtf8(w) + L")"); cleanup(); return false; } @@ -169,7 +242,7 @@ bool ReplaceModel(Context& ctx, const ModelState& m, wstring& problem) { cleanup(); return false; } wstring why; - if (!ExistingModelValid(ctx, &why, &info) || !info.absent_groups.empty()) { // not expected: the same bytes passed a moment ago + if (!ExistingModelValid(ctx, &why, &info) || (needGroups && !info.absent_groups.empty())) { // not expected: the same bytes passed a moment ago const bool back = MoveFileExW(previous.c_str(), ModelFile(ctx).c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH) != FALSE; problem = L"the new file failed its check after it was moved into place (" + why + L")" + (back ? L"; the earlier file was put back" : L""); cleanup(); return false; @@ -196,6 +269,10 @@ ModelResult WriteModel(Context& ctx, const ModelState& m, Ui& ui) { if (!ReplaceModel(ctx, m, problem)) { // The file that was there has to be valid still; the install then goes on with it. if (!ExistingModelValid(ctx, &why, &info)) Fail(L"The converted model on this PC changed while setup was running (" + why + L"). Run setup again."); + // a file converted from a listed file is never kept in its place + if (m.trust == ModelState::LISTED) + Fail(L"The model data on this PC was converted from a file on an ignore list, and the new conversion from " + m.dll + L" could not replace it (" + + problem + L"). Nothing was changed; try again.", EXIT_NO_MODEL); res.outcome = L"kept: the new conversion from " + m.dll + L" was not used (" + problem + L")" + FilterText(ctx, info.absent_groups.empty()); res.notes.clear(); ui.log(L"model data: the file on this PC stays in use (" + res.outcome + L")"); @@ -212,6 +289,8 @@ ModelResult WriteModel(Context& ctx, const ModelState& m, Ui& ui) { // had the output-filter weights, and the files that convert too but were not used. wstring text = L"source\t" + m.dll + L"\r\nversion\t" + m.version + L"\r\norigin\t" + m.origin + L"\r\nconverted\t" + NowStamp(false) + L"\r\nmap\t" + FromUtf8(wgc::to_hex(ctx.map.map->file_sha256)) + L"\r\n"; + // the source file's SHA-256: a later reuse is checked against the ignore lists with it (FindModel) + if (!m.dllSha256.empty()) text += L"sha256\t" + m.dllSha256 + L"\r\n"; if (MapHasOptionalGroup(*ctx.map.map)) { const bool hasGroup = wgc::has_every_group(*ctx.map.map, *m.converted); text += wstring(L"filter\t") + (hasGroup ? L"from this file" : L"not known for this file; WrongGPU's own filter is used") + L"\r\n"; diff --git a/clean/installer/src/wg_core/model.h b/clean/installer/src/wg_core/model.h index 0f067379..4e9eb1a2 100644 --- a/clean/installer/src/wg_core/model.h +++ b/clean/installer/src/wg_core/model.h @@ -2,6 +2,7 @@ // WrongGPU launcher core: the model data (spec 30 "Model file", spec 08): the model file on this PC, the search for the // player's DLSS model file, the conversion and what replaces what. Written for this project. #pragma once +#include "convert_ignore.h" #include "detect.h" namespace wg { @@ -25,6 +26,15 @@ struct ModelState { wstring outcome; // which case this is, for the log, the install record and the support report std::vector notes; // one line each for the player (spec 08 section 8: no known output filter in this DLSS build) std::vector passedOver; // files that convert, but without the weights, and were not used (a file with them was preferred, or an earlier one without) + IgnoreReport ignored; // the files the ignore lists left out of the search, and the lists' warnings (convert_ignore.h) + // The file converted earlier, checked against the ignore lists before it is reused (only when a list has entries): + // LISTED: its recorded source (path or SHA-256 in model-source.txt) is on a list; UNKNOWN: no SHA-256 on record (an + // earlier version converted it). Either way it is converted again from an allowed file; if there is none, a LISTED + // file is not used (FAILED) and an UNKNOWN one stays with a note. + enum Trust { TRUSTED, LISTED, UNKNOWN } trust = TRUSTED; + wstring chosenSkipped; // the player's line when the file they chose is on a list (which list and line, or --ignore=) + wstring dllSha256; // the SHA-256 of `dll` (written to model-source.txt) + int code = 0; // the exit code of a failure that is not the generic "no model" (10: a list can't be read) bool ok() const { return status == READY; } }; // What became of a model file at install time (WriteModel): the case and the lines for the player. diff --git a/clean/installer/src/wg_core/record.cpp b/clean/installer/src/wg_core/record.cpp index 495c3846..33c63f38 100644 --- a/clean/installer/src/wg_core/record.cpp +++ b/clean/installer/src/wg_core/record.cpp @@ -354,10 +354,10 @@ size_t RemoveRuntimeFiles(const wstring& dir, const wstring& exeDir, const Recor static std::atomic g_cleanupMove{nullptr}; // test seam (record.h SetCleanupMoveForTest) void SetCleanupMoveForTest(CleanupMoveFn fn) { g_cleanupMove = fn; } -void StartCleanup(Context& ctx, const wstring& state, const wstring& self, const wstring& appData) { +wstring StartCleanup(Context& ctx, const wstring& state, const wstring& self, const wstring& appData) { // Detach this generation now. Delayed cleanup must never recursively delete the shared pathname, // which a following install can already have recreated with its new model and runtime selection. - wstring retired; + wstring retired, kept; if (!appData.empty() && IsDir(appData)) { // The name the folder is moved to is free and unique. It was - alone; that clock moves in // ~16 ms steps, so a second cleanup of the same folder within one step (an install's restore, then the last game's @@ -393,8 +393,11 @@ void StartCleanup(Context& ctx, const wstring& state, const wstring& self, const LogLine(ctx, L"Shared data cleanup deferred: cannot detach " + appData + L" (error " + std::to_wstring(err) + L": " + why + L"); it was left in place."); retired.clear(); - } else if (StartsWithI(ctx.logFile, appData + L"\\")) { - ctx.logFile = retired + ctx.logFile.substr(appData.size()); + } else { + if (StartsWithI(ctx.logFile, appData + L"\\")) ctx.logFile = retired + ctx.logFile.substr(appData.size()); + // the ignore list stays (an integrator's installer registered its files there; convert_ignore.h) + kept = PutIgnoreListBack(retired, appData); + if (!kept.empty()) LogLine(ctx, kept); } } wchar_t sys[MAX_PATH]; GetSystemDirectoryW(sys, MAX_PATH); @@ -412,6 +415,7 @@ void StartCleanup(Context& ctx, const wstring& state, const wstring& self, const } else { LogLine(ctx, L"cleanup not started (error " + std::to_wstring(GetLastError()) + L"); remove " + state + L" by hand"); } + return kept; } std::vector DoRestore(Context& ctx, const wstring& dir, Ui& ui, bool failedInstall, bool keepState) { @@ -562,15 +566,20 @@ std::vector DoRestore(Context& ctx, const wstring& dir, Ui& ui, bool fa } wstring state = StateDir(dir); - if (keepBackup) { warnings.push_back(L"The backup folder was kept: " + Join(state, L"backup")); if (!appData.empty()) StartCleanup(ctx, L"", L"", appData); return warnings; } + // the shared folder goes, its ignore-files.txt stays (StartCleanup): said in the result + auto cleanup = [&](const wstring& st, const wstring& sf) { + const wstring kept = StartCleanup(ctx, st, sf, appData); + if (!kept.empty()) { ui.log(kept); warnings.push_back(kept); } + }; + if (keepBackup) { warnings.push_back(L"The backup folder was kept: " + Join(state, L"backup")); if (!appData.empty()) cleanup(L"", L""); return warnings; } if (keepState) { RemoveTree(Join(state, L"backup")); DeleteFileW(RecordPath(dir).c_str()); return warnings; } wstring self = SelfPath(); if (IEq(Parent(self), state)) { RemoveTree(state, self); - StartCleanup(ctx, state, self, appData); + cleanup(state, self); } else { RemoveTree(state); - if (!appData.empty()) StartCleanup(ctx, L"", L"", appData); + if (!appData.empty()) cleanup(L"", L""); } return warnings; } diff --git a/clean/installer/src/wg_core/record.h b/clean/installer/src/wg_core/record.h index e96c75b4..f2c8c315 100644 --- a/clean/installer/src/wg_core/record.h +++ b/clean/installer/src/wg_core/record.h @@ -135,16 +135,18 @@ void NotePortableLauncher(const wstring& exe); void UnregisterUninstall(const Record& m); size_t RemoveRuntimeFiles(const wstring& dir, const wstring& exeDir, const Record& m, bool settings); // The shared folder (appData) is moved aside, to a free name made unique with a process-wide sequence number, and deleted by a -// hidden cmd.exe. The move: a sharing violation (32) or an access denied (5: a scanner holding a file in the folder) is tried -// again, at most kCleanupMoveTries times (waits from kCleanupMoveFirstWaitMs doubling: 50, 100, 200, 400 ms), each wait logged -// with the error; any other error, or the last try, leaves the folder in place ("Shared data cleanup deferred ... (error N: ...)"). +// hidden cmd.exe; its ignore-files.txt is put back first (convert_ignore.h PutIgnoreListBack). Returns that line ("kept +// ignore-files.txt ...") or "". The move: a sharing violation (32) or an access denied (5: a scanner holding a file in the +// folder) is tried again, at most kCleanupMoveTries times (waits from kCleanupMoveFirstWaitMs doubling: 50, 100, 200, 400 ms), +// each wait logged with the error; any other error, or the last try, leaves the folder in place ("Shared data cleanup deferred +// ... (error N: ...)"). constexpr int kCleanupMoveTries = 5; constexpr unsigned long kCleanupMoveFirstWaitMs = 50; // Test seam: the call StartCleanup moves the folder with (MoveFileExW when unset). The tests put in one that fails with // a chosen error for the first tries; nullptr puts MoveFileExW back. using CleanupMoveFn = BOOL (WINAPI*)(LPCWSTR from, LPCWSTR to, DWORD flags); void SetCleanupMoveForTest(CleanupMoveFn fn); -void StartCleanup(Context& ctx, const wstring& state, const wstring& self, const wstring& appData); +wstring StartCleanup(Context& ctx, const wstring& state, const wstring& self, const wstring& appData); std::vector DoRestore(Context& ctx, const wstring& dir, Ui& ui, bool failedInstall, bool keepState); // rc6 L4: the player's things in the state folder that a failed install or update never removes when they were there before // it: profile.ini (the game's settings), previous\ (the version Roll back goes to) and rr-config\ (the Engine.ini fix's backup). diff --git a/clean/installer/src/wg_core/rr.cpp b/clean/installer/src/wg_core/rr.cpp index 6d09963a..cb7774d7 100644 --- a/clean/installer/src/wg_core/rr.cpp +++ b/clean/installer/src/wg_core/rr.cpp @@ -255,6 +255,11 @@ wstring RrDataSource() { return s.substr(p + 7, e == wstring::npos ? wstring::npos : e - p - 7); } +// RR data converted from a listed file and nothing allowed found: what the player does (name the listed source, +// ask for an allowed file from the player's own game) +static const wchar_t* const kRrChooseOwnFile = + L". To use Ray Reconstruction, choose the Ray Reconstruction model file (nvngx_dlssd.dll) that came with one of your own games " + L"(Convert on the game's page, or Settings > Ray Reconstruction data), or install or update such a game"; RrDataState FindRrData(Context& ctx, const GameTarget* t, const wstring& userDll, const std::vector& libraries, bool fresh) { RrDataState m; if (!LoadFMap(ctx)) { @@ -264,22 +269,84 @@ RrDataState FindRrData(Context& ctx, const GameTarget* t, const wstring& userDll return m; } RecoverRrData(ctx); // a swap that did not finish: its data back first (it may be valid) + // The ignore lists (convert_ignore.h), read first: what may be converted, and whether the data on this PC may be reused. + // A list that is there but can't be read: nothing is converted or reused (fail closed). + const IgnoreLists ign = LoadConvertIgnore(ctx, L"RR data", &m.ignored); + if (!ign.unreadable.empty()) { m.error = ign.unreadable; LogLine(ctx, L"RR data: " + m.error); return m; } wstring why; - if (!fresh && userDll.empty() && RrDataValid(ctx, &why)) { + const bool existing = !fresh && userDll.empty() && RrDataValid(ctx, &why); + // As the model data (model.cpp FindModel): while a list has entries, data converted from a listed file (path or SHA-256 in + // its model-source.txt) is not reused, and data without a recorded SHA-256 (an earlier version) is converted again when an + // allowed file is found, else kept with a note. + const wstring source = existing ? RrDataSource() : wstring(); + wstring distrustWhy; + if (existing && !ign.empty()) { + const wstring sha = SourceField(Join(RrDataDir(), L"model-source.txt"), L"sha256"); + const wstring listed = RecordedSourceReason(ign, source, sha); + if (!listed.empty()) { + m.trust = RrDataState::LISTED; + distrustWhy = L"it was converted from " + (source.empty() ? wstring(L"a file") : source) + L", which is " + listed; + } else if (sha.empty()) { + m.trust = RrDataState::UNKNOWN; + distrustWhy = L"the SHA-256 of the file it was converted from is not on record (an earlier version converted it), so the ignore lists can't check it"; + } + } + auto keep = [&] { m.ok = m.reused = true; m.origin = L"converted earlier on this PC"; - m.dll = RrDataSource(); + m.dll = source; + }; + if (existing && m.trust == RrDataState::TRUSTED) { + keep(); LogLine(ctx, L"RR data: reusing " + RrDataFile()); return m; } + if (existing) why = RrDataFile() + L" is valid, but " + distrustWhy; if (why.empty()) why = fresh ? L"converted again" : L"a file was chosen"; LogLine(ctx, L"RR data: " + why + L"; looking for a DLSS Ray Reconstruction model file (nvngx_dlssd.dll)"); + auto keepUnknown = [&] { + keep(); + m.notes.push_back(L"The Ray Reconstruction data on this PC was converted by an earlier version of WrongGPU, which did not record the SHA-256 of " + L"the file it came from, so the ignore lists can't check it. No other Ray Reconstruction model file was found to convert it " + L"again, so it stays in use."); + LogLine(ctx, L"RR data: keeping " + RrDataFile() + L" (its source can't be checked against the ignore lists; nothing else to convert)"); + }; std::vector> cands; std::set seen; auto add = [&](const wstring& p, const wstring& origin) { if (seen.insert(Lower(FullPath(p))).second) cands.push_back({FullPath(p), origin}); }; if (!userDll.empty()) add(userDll, L"the file you chose"); if (t && !t->root.empty()) for (auto& f : FindFilesNamed(t->root, L"nvngx_dlssd.dll")) add(f, L"this game"); // DLSS RR for (auto& g : libraries) for (auto& f : FindFilesNamed(g.root, L"nvngx_dlssd.dll", 30000)) add(f, L"another game (" + g.name + L")"); // DLSS RR + // A file on a list is left out before anything is tried; the order of the others stays. The rule is absolute: a + // file the player chose is left out too, and the player is told which list and line (or --ignore=) matched. + wstring chosenSkipped; + { + std::vector paths; + for (auto& c : cands) paths.push_back(c.first); + const std::vector skip = IgnoredMask(ctx, ign, paths, L"RR data", m.ignored); + std::vector> kept; + for (size_t i = 0; i < cands.size(); ++i) { + if (!skip[i]) kept.push_back(cands[i]); + else if (i == 0 && !userDll.empty()) { + const wstring& s = m.ignored.skipped.front(); + chosenSkipped = L"the file you chose (" + cands[0].first + L") is not converted: it is " + s.substr(std::min(s.size(), cands[0].first.size() + 2)) + + L". If it was listed by mistake, remove that line (or that --ignore argument)"; + } + } + cands.swap(kept); + } + const size_t skipped = m.ignored.skipped.size(); + if (cands.empty() && skipped) { // nothing left to try + if (existing && m.trust == RrDataState::UNKNOWN) { keepUnknown(); return m; } + m.error = !chosenSkipped.empty() && skipped == 1 + ? chosenSkipped + : L"every Ray Reconstruction model file found (" + std::to_wstring(skipped) + L") is on an ignore list, so none was converted " + L"(WrongGPU converts only the files that came with your own games; the files skipped, and the list and line that skipped each, " + L"are named in the log)"; + if (existing) m.error = L"the Ray Reconstruction data on this PC is not used: " + distrustWhy + L"; and " + m.error + kRrChooseOwnFile; + LogLine(ctx, L"RR data: " + m.error); + return m; + } wgc::SearchOptions opt; for (auto& c : cands) { auto conv = std::make_shared(); @@ -287,18 +354,27 @@ RrDataState FindRrData(Context& ctx, const GameTarget* t, const wstring& userDll [&ctx](const string& s) { LogLine(ctx, L"converter (RR): " + FromUtf8(s)); }); if (r.code == wgc::kOk) { m.ok = true; m.dll = c.first; m.origin = c.second; m.version = FromUtf8(conv->version); m.converted = conv; - LogLine(ctx, L"RR data: using " + c.first + L" (" + c.second + L")"); + m.dllSha256 = Lower(Sha256File(c.first)); + LogLine(ctx, L"RR data: using " + c.first + L" (" + c.second + L")" + (existing ? L"; it replaces the data on this PC (" + distrustWhy + L")" : wstring())); return m; } m.rejections.push_back(c.first + L": " + FromUtf8(r.message)); LogLine(ctx, L"RR data: rejected " + c.first + L": " + FromUtf8(r.message)); - if (wgc::is_installer_error(r.code)) { m.error = FromUtf8(r.message); return m; } + if (wgc::is_installer_error(r.code)) { + if (existing && m.trust == RrDataState::UNKNOWN) { keepUnknown(); return m; } + m.error = FromUtf8(r.message); + return m; + } } + if (existing && m.trust == RrDataState::UNKNOWN) { keepUnknown(); return m; } if (!userDll.empty() && cands.size() == 1 && !m.rejections.empty()) // only the chosen file was tried: its own reason m.error = L"the file you chose can't be converted by this version: " + m.rejections[0]; else m.error = cands.empty() ? L"no Ray Reconstruction model file was found in this game or your other games" : L"none of the " + std::to_wstring(cands.size()) + L" Ray Reconstruction model file(s) found can be converted by this version"; + if (skipped) m.error += L" (" + std::to_wstring(skipped) + L" file(s) on an ignore list were skipped)"; + if (!chosenSkipped.empty()) m.error += L"; " + chosenSkipped; + if (existing) m.error = L"the Ray Reconstruction data on this PC is not used: " + distrustWhy + L"; and " + m.error + kRrChooseOwnFile; return m; } @@ -323,7 +399,9 @@ bool WriteRrData(Context& ctx, const RrDataState& m, wstring* why) { return false; } const wstring text = L"source\t" + m.dll + L"\r\nversion\t" + m.version + L"\r\norigin\t" + m.origin + L"\r\nconverted\t" + NowStamp(false) + - L"\r\nmap\t" + FromUtf8(wgc::to_hex(ctx.fMap.map->file_sha256)) + L"\r\n"; + L"\r\nmap\t" + FromUtf8(wgc::to_hex(ctx.fMap.map->file_sha256)) + L"\r\n" + + // the source file's SHA-256: a later reuse is checked against the ignore lists with it (FindRrData) + (m.dllSha256.empty() ? wstring() : L"sha256\t" + m.dllSha256 + L"\r\n"); const string info = ToUtf8(text); WriteFileAtomic(Join(side, L"model-source.txt"), info.data(), info.size()); // the folder in use moves aside first (a running game may hold its file: then nothing changes). A reader that holds it @@ -351,7 +429,8 @@ bool WriteRrData(Context& ctx, const RrDataState& m, wstring* why) { return RrDataValid(ctx, why); } -bool ConvertRrData(Context& ctx, const wstring& dll, const std::vector& libraries, wstring* why, std::vector* notWritten) { +bool ConvertRrData(Context& ctx, const wstring& dll, const std::vector& libraries, wstring* why, std::vector* notWritten, + IgnoreReport* ignored) { // a chosen file is the only one tried: when it does not convert, that is the answer (never another game's file instead) std::vector where; if (dll.empty()) { @@ -362,6 +441,7 @@ bool ConvertRrData(Context& ctx, const wstring& dll, const std::vector failed = RefreshAllRrSettings(ctx); diff --git a/clean/installer/src/wg_core/rr.h b/clean/installer/src/wg_core/rr.h index b9aced70..30145970 100644 --- a/clean/installer/src/wg_core/rr.h +++ b/clean/installer/src/wg_core/rr.h @@ -11,6 +11,7 @@ // model data is (model.h), into %LOCALAPPDATA%\WrongGPU\model\F\model.wgcm: shared by every game (a game whose own file is // too old uses it too). The file is never copied or changed, and never downloaded or shipped. Written for this project. #pragma once +#include "convert_ignore.h" #include "detect.h" #include "profile.h" @@ -85,6 +86,12 @@ struct RrDataState { bool available = true; // false: this build has no RR converter data wstring dll, origin, version, error; std::vector rejections; // "path: reason" for every file tried + IgnoreReport ignored; // the files the ignore lists left out of the search, and the lists' warnings (convert_ignore.h) + // The data on this PC checked against the ignore lists before it is reused (as ModelState::Trust): LISTED is never + // reused; UNKNOWN (no SHA-256 on record) is converted again from an allowed file, else kept with a line in `notes`. + enum Trust { TRUSTED, LISTED, UNKNOWN } trust = TRUSTED; + std::vector notes; // lines for the player (the install's warnings) + wstring dllSha256; // the SHA-256 of `dll` (written to model-source.txt) std::shared_ptr converted; }; bool LoadFMap(Context& ctx); @@ -109,8 +116,10 @@ struct RrSettingsFailure { wstring dir, why; }; // Settings' Convert again / Choose a DLSS file... and a game page's Convert: a fresh conversion (`dll` alone when given: no // other file is tried; else the RR model files of the launcher's games, then of `libraries`), written while the data in // use stays until the new one is checked; then every installed RR game's settings file follows (RR turns on where its -// profile wants it; the ones that could not be written in *notWritten). False with *why. -bool ConvertRrData(Context& ctx, const wstring& dll, const std::vector& libraries, wstring* why, std::vector* notWritten = nullptr); +// profile wants it; the ones that could not be written in *notWritten). False with *why. *ignored: the files the ignore lists +// left out (convert_ignore.h; a chosen file on a list is not converted either) and the lists' warnings. +bool ConvertRrData(Context& ctx, const wstring& dll, const std::vector& libraries, wstring* why, std::vector* notWritten = nullptr, + IgnoreReport* ignored = nullptr); // A managed game that uses the RR data and runs now (its name; empty: none): the data cannot be replaced meanwhile. wstring RunningRrGame(); diff --git a/clean/installer/tests/convert_ignore_tests.cpp b/clean/installer/tests/convert_ignore_tests.cpp new file mode 100644 index 00000000..779f1655 --- /dev/null +++ b/clean/installer/tests/convert_ignore_tests.cpp @@ -0,0 +1,387 @@ +// SPDX-License-Identifier: MIT +// Unit tests of the converter search's ignore lists (wg_core/convert_ignore.h): the list format (version line, comments, +// a path, a path with its SHA-256, a hash alone, quotes, UTF-8 / UTF-16 LE, CRLF), bad lines (warned, skipped, the rest +// read), path matching (case, '/' and '\', '.' and '..', a hard link, a junction, a symbolic link when this account may +// make one), a hash that matches wherever the file is, the well-known list locations, and an empty list that leaves every +// candidate set exactly as it was. Synthetic files only, in the temp folder. Written for this project. Run by ctest +// ("convert-ignore"). +#include +#include +#include +#include +#include + +#include + +#include "wg_core/convert_ignore.h" +#include "wg_core/model.h" +#include "wg_core/record.h" + +using namespace wg; + +namespace { +int checks = 0; +void require(bool ok, const char* what) { + if (!ok) throw std::runtime_error(what); + ++checks; +} +wstring TempFolder() { + wchar_t tmp[MAX_PATH] = {}; + GetTempPathW(MAX_PATH, tmp); + wstring dir = Join(tmp, L"convert-ignore-tests-" + std::to_wstring(GetCurrentProcessId())); + RemoveTree(dir); + EnsureDir(dir); + return FullPath(dir); +} +void File(const wstring& p, const string& text) { EnsureDir(Parent(p)); WriteFileAtomic(p, text.data(), text.size()); } +string U8(const wstring& w) { return ToUtf8(w); } +wstring Slashes(wstring s) { for (auto& c : s) if (c == L'\\') c = L'/'; return s; } +wstring Upper(wstring s) { for (auto& c : s) c = (wchar_t)towupper(c); return s; } +IgnoreLists Parse(const string& text) { IgnoreLists l; ParseIgnoreText(text, L"test.txt", l); return l; } +bool Has(const wstring& s, const wstring& part) { return s.find(part) != wstring::npos; } +// StartCleanup's folder move for the retry cases (record.h SetCleanupMoveForTest): the first g_moveFailFirst calls fail +// with g_moveError, later ones really move. +std::atomic g_moveCalls{0}, g_moveFailFirst{0}; +std::atomic g_moveError{0}; +BOOL WINAPI FakeMove(LPCWSTR from, LPCWSTR to, DWORD flags) { + if (++g_moveCalls <= g_moveFailFirst) { SetLastError(g_moveError); return FALSE; } + return MoveFileExW(from, to, flags); +} +size_t CountIn(const string& s, const string& part) { + size_t n = 0; + for (size_t at = 0; (at = s.find(part, at)) != string::npos; ++at) ++n; + return n; +} +} // namespace + +int main() { + std::vector links; // removed (the link, never its target) before the folder goes + wstring root; + try { + root = TempFolder(); + const wstring lib = Join(root, L"Library"); + const wstring a = Join(lib, L"Game A\\bin\\x64\\nvngx_dlss.dll"), b = Join(lib, L"Game B\\nvngx_dlss.dll"), c = Join(lib, L"Game C\\x\\nvngx_dlss.dll"); + const wstring chosen = Join(root, L"Chosen\\nvngx_dlss.dll"); + File(a, "synthetic file A"); + File(b, "synthetic file B"); + File(c, "synthetic file C"); + File(chosen, "synthetic chosen file"); + const wstring shaB = Lower(Sha256File(b)); + require(shaB.size() == 64, "the test's SHA-256"); + + // ---- the format -------------------------------------------------------------------------------------------- + { + const string text = "wronggpu-ignore-files 1\r\n# placed by an installer\r\n\r\n \r\n" + U8(a) + "\r\n" + U8(b) + " sha256=" + U8(Upper(shaB)) + + "\r\nsha256=" + string(64, 'a') + "\r\n\"" + U8(c) + "\"\r\n"; + IgnoreLists l = Parse(text); + require(l.warnings.empty() && l.entries.size() == 4, "a list with a version line, comments, blank lines, a path, a path with its hash, a hash alone, a quoted path"); + require(l.entries[0].path == a && l.entries[0].sha256.empty() && Has(l.entries[0].where, L"line 5"), "a path entry, where it is written"); + require(l.entries[1].path == b && l.entries[1].sha256 == shaB, "a path with its SHA-256 (hex read in either case)"); + require(l.entries[2].path.empty() && l.entries[2].sha256 == wstring(64, L'a'), "a hash alone"); + require(l.entries[3].path == c, "quotes around a path"); + IgnoreLists lf = Parse("\xEF\xBB\xBF" + U8(a) + "\n" + U8(b) + "\n"); + require(lf.warnings.empty() && lf.entries.size() == 2 && lf.entries[0].path == a, "UTF-8 with a BOM, LF line ends"); + const wstring w16 = L"\xFEFF" + a + L"\r\nsha256=" + shaB + L"\r\n"; + string bytes(w16.size() * 2, '\0'); + std::memcpy(&bytes[0], w16.data(), bytes.size()); + IgnoreLists lw = Parse(string("\xFF\xFE", 2) + bytes.substr(2)); + require(lw.warnings.empty() && lw.entries.size() == 2 && lw.entries[0].path == a && lw.entries[1].sha256 == shaB, "UTF-16 LE with its BOM"); + IgnoreLists ln = Parse("wronggpu-ignore-files 7\n" + U8(a) + "\n"); + require(ln.entries.size() == 1 && ln.warnings.size() == 1 && Has(ln.warnings[0], L"newer"), "a newer format version: said once, its lines still read"); + } + // ---- bad lines: each skipped with a warning naming the list and the line; the rest is read -------------------- + { + const string text = "Game A\\nvngx_dlss.dll\n" + U8(a) + "\nC:nvngx_dlss.dll\nsha256=1234\n" + U8(b) + " sha256=" + string(63, 'f') + "\n" + + "sha256=" + string(63, 'f') + "g\n" + "C:\\Games\\*\\nvngx_dlss.dll\nwronggpu-ignore-files one\n" + U8(c) + "\n"; + IgnoreLists l = Parse(text); + require(l.entries.size() == 2 && l.entries[0].path == a && l.entries[1].path == c, "the good lines between and after the bad ones are read"); + require(l.warnings.size() == 7, "one warning per bad line"); + require(Has(l.warnings[0], L"test.txt, line 1") && Has(l.warnings[0], L"not a full path") && Has(l.warnings[0], L"skipped"), "a relative path: warned with its list and line"); + require(Has(l.warnings[1], L"line 3"), "a drive-relative path"); + require(Has(l.warnings[2], L"line 4") && Has(l.warnings[2], L"64 hex digits"), "a short hash"); + require(Has(l.warnings[3], L"line 5"), "a path with a short hash: the whole line is skipped (not the path alone)"); + require(Has(l.warnings[4], L"line 6"), "a hash with a letter that is not hex"); + require(Has(l.warnings[5], L"line 7"), "a wildcard"); + require(Has(l.warnings[6], L"line 8") && Has(l.warnings[6], L"format version"), "a version line without a number"); + IgnoreEntry e; + wstring why; + require(ParseIgnoreLine(L" # comment", e, why) && e.text.empty() && ParseIgnoreLine(L"", e, why) && e.text.empty(), "comment and blank lines: no entry"); + } + // ---- matching ------------------------------------------------------------------------------------------------- + { + // case, slashes, '.' and '..' + const wstring odd = Slashes(Lower(Join(lib, L"Game A\\.\\bin\\..\\bin\\x64\\NVNGX_DLSS.DLL"))); + IgnoreLists l = Parse(U8(odd) + "\n"); + require(l.entries.size() == 1, "an odd spelling of a path is read"); + const wstring why = IgnoreReason(l, a); + require(!why.empty() && Has(why, L"test.txt, line 1") && Has(why, L"(path "), "case, '/', '.' and '..' normalised: the file is skipped, with the list, the line and the path"); + require(!IgnoreReason(l, Upper(a)).empty(), "the candidate spelled in upper case"); + require(IgnoreReason(l, b).empty() && IgnoreReason(l, c).empty(), "other files are not skipped"); + IgnoreLists gone = Parse(U8(Join(root, L"Gone\\nvngx_dlss.dll")) + "\n"); + require(gone.entries.size() == 1 && IgnoreReason(gone, a).empty(), "a listed path that does not exist matches nothing else"); + // a hash alone: wherever the file is; a path with a hash matches by either + IgnoreLists h = Parse("sha256=" + U8(shaB) + "\n"); + const wstring copy = Join(root, L"Elsewhere\\renamed.dll"); + File(copy, "synthetic file B"); + require(Has(IgnoreReason(h, b), L"SHA-256 " + shaB) && !IgnoreReason(h, copy).empty(), "a hash alone skips the file wherever it is, under any name"); + require(IgnoreReason(h, a).empty(), "a file with other bytes is not skipped by the hash"); + IgnoreLists ph = Parse(U8(c) + " sha256=" + U8(shaB) + "\n"); + require(!IgnoreReason(ph, c).empty() && !IgnoreReason(ph, copy).empty() && IgnoreReason(ph, a).empty(), "a path with a hash: either matches"); + // a hard link to a listed file + const wstring hard = Join(root, L"Hard\\nvngx_dlss.dll"); + EnsureDir(Parent(hard)); + if (CreateHardLinkW(hard.c_str(), a.c_str(), nullptr)) { + require(Has(IgnoreReason(l, hard), L"the same file"), "a hard link to a listed file is the same file"); + } else { + std::printf("convert-ignore: hard link not made here (error %lu): that check is skipped\n", GetLastError()); + } + // a junction over the listed file's folder (no privilege needed) + const wstring junction = Join(root, L"Junction"); + const wstring cmd = L"cmd.exe /d /c mklink /J \"" + junction + L"\" \"" + Parent(a) + L"\" >nul"; + if (_wsystem(cmd.c_str()) == 0 && IsDir(junction)) { + links.push_back(junction); + const wstring via = Join(junction, L"nvngx_dlss.dll"); + require(IsFile(via) && !IgnoreReason(l, via).empty(), "a junction to the listed file's folder: resolved to the listed file and skipped"); + IgnoreLists lj = Parse(U8(via) + "\n"); + require(!IgnoreReason(lj, a).empty(), "a path listed through a junction matches the file itself"); + } else { + std::printf("convert-ignore: junction not made here: those checks are skipped\n"); + } + // a symbolic link (Developer Mode or the privilege: not every account may make one) + const wstring sym = Join(root, L"Symlink\\nvngx_dlss.dll"); + EnsureDir(Parent(sym)); + if (CreateSymbolicLinkW(sym.c_str(), a.c_str(), SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)) { + links.push_back(sym); + require(!IgnoreReason(l, sym).empty(), "a symbolic link to the listed file: skipped"); + IgnoreLists ls = Parse(U8(sym) + "\n"); + require(!IgnoreReason(ls, a).empty(), "a listed symbolic link matches the file it points to"); + } else { + std::printf("convert-ignore: SYMLINK-SKIPPED: this account may not make a symbolic link (error %lu): those checks are skipped\n", GetLastError()); + } + } + // ---- the well-known lists --------------------------------------------------------------------------------------- + { + const wstring lad = Join(root, L"localappdata"); + EnsureDir(lad); + SetEnvironmentVariableW(L"LOCALAPPDATA", lad.c_str()); + const wstring shared = Join(lad, L"WrongGPU"); + std::vector w = WellKnownIgnoreLists(shared); + require(w.size() == 1 && IEq(w[0], Join(shared, L"ignore-files.txt")), "installed launcher: %LOCALAPPDATA%\\WrongGPU\\ignore-files.txt once"); + const wstring portable = Join(root, L"Portable\\WrongGPU-data"); + w = WellKnownIgnoreLists(portable); + require(w.size() == 2 && IEq(w[1], Join(portable, L"ignore-files.txt")), "a portable launcher: its WrongGPU-data\\ignore-files.txt too"); + Context ctx; + ctx.ownDataDir = portable; + IgnoreReport rep; + require(LoadConvertIgnore(ctx, L"model", &rep).empty() && rep.warnings.empty(), "no list anywhere: empty, no warning"); + File(Join(shared, L"ignore-files.txt"), "wronggpu-ignore-files 1\r\n" + U8(a) + "\r\nnot a path\r\n"); + File(Join(portable, L"ignore-files.txt"), "sha256=" + U8(shaB) + "\r\n"); + const wstring cli = Join(root, L"cli-list.txt"); + File(cli, U8(c) + "\n"); + ctx.ignoreListFiles = {cli}; + ctx.ignoreEntries = {chosen, L"bad entry"}; + rep = IgnoreReport(); + IgnoreLists l = LoadConvertIgnore(ctx, L"model", &rep); + require(l.entries.size() == 4 && l.lists.size() == 3, "the well-known lists, then the command line's list and entries"); + require(rep.warnings.size() == 2 && Has(rep.warnings[0], L"line 3") && Has(rep.warnings[1], L"--ignore=bad entry"), "their bad lines are warned about"); + const std::vector skip = IgnoredMask(ctx, l, {chosen, a, b, c, Join(root, L"Elsewhere\\renamed.dll")}, L"model", rep); + require(skip == std::vector({true, true, true, true, true}) && rep.skipped.size() == 5, "every list and entry is honoured"); + require(Has(rep.skipped[0], L"named by --ignore=" + chosen) && Has(rep.skipped[1], L"on the ignore list " + Join(shared, L"ignore-files.txt") + L", line 2") && + Has(rep.skipped[2], L"WrongGPU-data") && Has(rep.skipped[3], L"cli-list.txt, line 1"), + "each skipped file names the list and line, or the --ignore= argument, that skips it"); + + // a well-known list that is there but can't be read fails closed, after a short wait on a sharing violation + ctx.ignoreListFiles.clear(); + ctx.ignoreEntries.clear(); + const wstring wk = Join(shared, L"ignore-files.txt"); + HANDLE held = CreateFileW(wk.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); + require(held != INVALID_HANDLE_VALUE, "the test holds the list open without sharing"); + const ULONGLONG t0 = GetTickCount64(); + rep = IgnoreReport(); + IgnoreLists locked = LoadConvertIgnore(ctx, L"model", &rep); + const ULONGLONG waited = GetTickCount64() - t0; + require(!locked.unreadable.empty() && rep.unreadable == locked.unreadable && Has(locked.unreadable, wk) && Has(locked.unreadable, L"another program holds it open") && + Has(locked.unreadable, L"nothing was converted"), "a list held open by another program: unreadable, the file and the reason named"); + // GetTickCount64 moves in ~16 ms steps: 4 sleeps of 200 ms can read a step short of 800 ms (two steps of slack) + require(waited + 32 >= (ULONGLONG)(kIgnoreReadTries - 1) * kIgnoreReadWaitMs && waited < 5000, "...after the retries (about 5 x 200 ms), not forever"); + std::thread release([held] { Sleep(300); CloseHandle(held); }); // an installer that finishes writing during the wait + rep = IgnoreReport(); + IgnoreLists later = LoadConvertIgnore(ctx, L"model", &rep); + release.join(); + require(later.unreadable.empty() && later.entries.size() == 2, "a list let go during the retries is read"); + DeleteFileW(wk.c_str()); + EnsureDir(wk); // a folder where the list should be + rep = IgnoreReport(); + require(Has(LoadConvertIgnore(ctx, L"model", &rep).unreadable, L"a folder"), "a folder named ignore-files.txt: unreadable (fail closed)"); + RemoveDirectoryW(wk.c_str()); + DeleteFileW(Join(portable, L"ignore-files.txt").c_str()); + rep = IgnoreReport(); + require(LoadConvertIgnore(ctx, L"model", &rep).unreadable.empty(), "no list at all: nothing unreadable"); + } + // ---- a conversion on this PC: its recorded source checked against the lists ------------------------------------ + { + const wstring src = Join(root, L"source.txt"); + File(src, "source\t" + U8(a) + "\r\nversion\t310.5.0.0\r\nsha256\t" + U8(shaB) + "\r\n"); + require(SourceField(src, L"source") == a && SourceField(src, L"sha256") == shaB && SourceField(src, L"none").empty(), "model-source.txt fields read"); + IgnoreLists byPath = Parse(U8(a) + "\n"), byHash = Parse("sha256=" + U8(shaB) + "\n"), other = Parse(U8(c) + "\n"); + require(Has(RecordedSourceReason(byPath, a, L""), L"line 1"), "a recorded source path on a list"); + require(Has(RecordedSourceReason(byHash, Join(root, L"Gone\\nvngx_dlss.dll"), shaB), L"SHA-256"), "a recorded SHA-256 on a list (the source file itself gone)"); + require(RecordedSourceReason(other, a, shaB).empty() && RecordedSourceReason(byHash, a, L"").empty() && RecordedSourceReason(IgnoreLists(), a, shaB).empty(), + "not on a list; no hash recorded (the file is not hashed again); no list"); + } + // ---- the shared folder removed: its ignore list stays (StartCleanup: the last game's uninstall, the restorer) -- + { + const wstring appData = Join(Join(root, L"localappdata"), L"WrongGPU"); + const string listBytes = "wronggpu-ignore-files 1\r\n" + U8(a) + "\r\n"; + File(Join(appData, L"model\\model.wgcm"), "synthetic model data"); + File(Join(appData, L"ignore-files.txt"), listBytes); + Context ctx; + const wstring kept = StartCleanup(ctx, L"", L"", appData); + string back; + require(StartsWithI(kept, kKeptIgnoreListText) && Has(kept, Join(appData, L"ignore-files.txt")), "StartCleanup says it kept ignore-files.txt"); + require(ReadAll(Join(appData, L"ignore-files.txt"), back) && back == listBytes && !IsDir(Join(appData, L"model")), + "the list is back in place byte for byte, the rest of the folder went"); + DeleteFileW(Join(appData, L"ignore-files.txt").c_str()); + File(Join(appData, L"model\\model.wgcm"), "synthetic model data"); + require(StartCleanup(ctx, L"", L"", appData).empty() && !IsDir(appData), "no list: nothing kept, the folder goes as before"); + // 0.6.1 stress (every build, the release configuration too): the two cases above back to back, 200 times on the + // same folder, mostly within one GetTickCount64 step of each other. Before the fix the second move aimed at the + // first one's folder whenever both fell in the same step (ERROR_ALREADY_EXISTS) and the folder stayed. + { + Context sctx; + sctx.logFile = Join(root, L"cleanup-stress.log"); // outside the folder that is moved + const int runs = 200; + int gone = 0; + for (int i = 0; i < runs; ++i) { + File(Join(appData, L"model\\model.wgcm"), "synthetic model data"); + File(Join(appData, L"ignore-files.txt"), listBytes); + const wstring k1 = StartCleanup(sctx, L"", L"", appData); + DeleteFileW(Join(appData, L"ignore-files.txt").c_str()); + File(Join(appData, L"model\\model.wgcm"), "synthetic model data"); + const wstring k2 = StartCleanup(sctx, L"", L"", appData); + if (StartsWithI(k1, kKeptIgnoreListText) && k2.empty() && !IsDir(appData)) ++gone; + } + string slog; + ReadAll(sctx.logFile, slog); + size_t retries = 0, deferred = 0; + for (size_t at = 0; (at = slog.find("); trying again in ", at)) != string::npos; ++at) ++retries; + for (size_t at = 0; (at = slog.find("cleanup deferred", at)) != string::npos; ++at) ++deferred; + std::printf("convert-ignore: cleanup stress %d/%d (retries %zu, deferred %zu)\n", gone, runs, retries, deferred); + require(gone == runs && deferred == 0, "cleanup stress: the list kept, then no list: the folder goes, 200 of 200 times"); + } + // 0.6.1: the move's bounded retry (error 32 sharing violation, error 5 access denied: a scanner's open file), + // through the test seam: fails for the first N tries, then moves; or fails every time and ends deferred + { + Context rctx; + rctx.logFile = Join(root, L"cleanup-retry.log"); + struct Case { DWORD error; int failFirst; bool gone; int calls; size_t retries; const char* what; }; + const Case cases[] = { + {ERROR_ACCESS_DENIED, 3, true, 4, 3, "error 5 for 3 tries, then the move: the folder goes after 3 logged retries"}, + {ERROR_ACCESS_DENIED, 1000, false, kCleanupMoveTries, kCleanupMoveTries - 1, + "error 5 every time: 5 tries (4 logged retries), then deferred with the error, the folder left in place"}, + {ERROR_SHARING_VIOLATION, 2, true, 3, 2, "error 32 for 2 tries, then the move: the folder goes"}, + {ERROR_ALREADY_EXISTS, 1000, false, 1, 0, "another error (183): no retry, deferred at once"}}; + for (const Case& rc : cases) { + DeleteFileW(rctx.logFile.c_str()); + File(Join(appData, L"model\\model.wgcm"), "synthetic model data"); + g_moveCalls = 0; g_moveFailFirst = rc.failFirst; g_moveError = rc.error; + SetCleanupMoveForTest(&FakeMove); + const ULONGLONG t0 = GetTickCount64(); + const wstring rkept = StartCleanup(rctx, L"", L"", appData); + const ULONGLONG took = GetTickCount64() - t0; + SetCleanupMoveForTest(nullptr); + string rlog; + ReadAll(rctx.logFile, rlog); + const string code = "(error " + std::to_string(rc.error); + const size_t retries = CountIn(rlog, code + (rc.error == ERROR_SHARING_VIOLATION ? ", sharing violation); trying again" : ", access denied); trying again")); + const size_t deferred = CountIn(rlog, "cleanup deferred: cannot detach " + U8(appData) + " " + code + ":"); + std::printf("convert-ignore: cleanup retry, error %lu x %d: %d tries, %zu retries logged, deferred %zu, %llu ms\n", + rc.error, rc.failFirst, g_moveCalls.load(), retries, deferred, (unsigned long long)took); + require(rkept.empty() && IsDir(appData) == !rc.gone && g_moveCalls == rc.calls && retries == rc.retries && + deferred == (rc.gone ? 0u : 1u) && took < 5000, rc.what); + } + if (IsDir(appData)) { // the deferred cases left it: the real move now + require(StartCleanup(rctx, L"", L"", appData).empty() && !IsDir(appData), "after the retry cases: the folder goes"); + } + } + for (int i = 0; i < 600; ++i) { // the hidden cleanup deletes the moved-aside folders (ping -n 3 first) + bool left = false; + for (auto& d : ListDirs(Join(root, L"localappdata"))) left = left || Has(d, L".cleanup-"); + if (!left) break; + Sleep(100); + } + } + // ---- UNC paths (the drive's administrative share on this PC, read only; nothing is shared or changed) ------------- + { + const wstring unc = L"\\\\localhost\\" + wstring(1, a[0]) + L"$" + a.substr(2); + if (a.size() > 3 && a[1] == L':' && IsFile(unc)) { + IgnoreLists l = Parse(U8(unc) + "\n"); + require(l.entries.size() == 1 && l.warnings.empty(), "a UNC path is a full path"); + require(!IgnoreReason(l, unc).empty(), "a UNC entry matches the candidate reached by the same UNC path"); + IgnoreLists l2 = Parse(U8(L"\\\\?\\UNC\\localhost\\" + wstring(1, a[0]) + L"$" + a.substr(2)) + "\n"); + require(l2.entries.size() == 1 && !IgnoreReason(l2, unc).empty(), "a \\\\?\\UNC\\ entry is read as the same UNC path"); + IgnoreLists local = Parse(U8(a) + "\n"); + // the share's path and the local path are two names of one file: the file identity matches them + require(Has(IgnoreReason(l, a), L"the same file"), "a UNC entry matches the same file reached by its local path"); + require(Has(IgnoreReason(local, unc), L"the same file"), "a local entry matches the same file reached through the UNC path"); + IgnoreLists lh = Parse("sha256=" + U8(Lower(Sha256File(a))) + "\n"); + require(!IgnoreReason(lh, unc).empty(), "a hash entry matches the file read through its UNC path"); + } else { + std::printf("convert-ignore: UNC-SKIPPED: %ls is not reachable here\n", unc.c_str()); + } + } + // ---- an empty list never changes the candidates (and so never which file wins) ------------------------------ + { + Context ctx; + const wstring shared = Join(Join(root, L"localappdata"), L"WrongGPU"); + GameTarget t; + t.root = Join(lib, L"Game A"); + const std::vector libs{{L"Game B", Join(lib, L"Game B"), L"", L"Folder"}, {L"Game C", Join(lib, L"Game C"), L"", L"Folder"}, + {L"Game A", Join(lib, L"Game A"), L"", L"Folder"}}; + const std::vector rev(libs.rbegin(), libs.rend()); + struct Set { const char* name; const GameTarget* t; wstring user; std::vector libs; }; + const Set sets[] = {{"chosen file, this game, three library games", &t, chosen, libs}, + {"library games only", nullptr, L"", libs}, + {"library games in the other order", nullptr, L"", rev}, + {"chosen file only", nullptr, chosen, {}}, + {"this game only", &t, L"", {}}, + {"nothing found", nullptr, L"", {}}}; + const string emptyLists[] = {"", "\xEF\xBB\xBF", "# nothing\r\n\r\nwronggpu-ignore-files 1\r\n", "\r\n\r\n"}; + for (const string& listText : emptyLists) { + File(Join(shared, L"ignore-files.txt"), listText); + IgnoreReport rep; + const IgnoreLists l = LoadConvertIgnore(ctx, L"model", &rep); + require(l.empty() && rep.warnings.empty(), "an empty list file (blank, a BOM alone, comments and the version line): no entry, no warning"); + for (const Set& s : sets) { + const std::vector cands = ModelCandidates(s.t, s.user, s.libs); + std::vector paths; + for (auto& x : cands) paths.push_back(x.path); + const std::vector skip = IgnoredMask(ctx, l, paths, L"model", rep); + std::vector kept; + for (size_t i = 0; i < paths.size(); ++i) if (!skip[i]) kept.push_back(paths[i]); + if (kept != paths || !rep.skipped.empty()) { std::printf("set: %s\n", s.name); require(false, "an empty list keeps every candidate in its order"); } + ++checks; + } + } + // and a list whose entries match none of them changes nothing either + File(Join(shared, L"ignore-files.txt"), U8(Join(root, L"Gone\\nvngx_dlss.dll")) + "\r\nsha256=" + string(64, '0') + "\r\n"); + IgnoreReport rep; + const IgnoreLists l = LoadConvertIgnore(ctx, L"model", &rep); + const std::vector cands = ModelCandidates(&t, chosen, libs); + std::vector paths; + for (auto& x : cands) paths.push_back(x.path); + require(paths.size() == 4 && l.entries.size() == 2, "the full candidate set: the chosen file and three games' files"); + const std::vector skip = IgnoredMask(ctx, l, paths, L"model", rep); + require(std::find(skip.begin(), skip.end(), true) == skip.end() && rep.skipped.empty(), "a list that names none of the candidates keeps them all"); + DeleteFileW(Join(shared, L"ignore-files.txt").c_str()); + } + } catch (const std::exception& e) { + std::cerr << "FAILED: " << e.what() << " (after " << checks << " checks)\n"; + for (auto& l : links) { if (IsDir(l)) RemoveDirectoryW(l.c_str()); else DeleteFileW(l.c_str()); } + return 1; + } + for (auto& l : links) { if (IsDir(l)) RemoveDirectoryW(l.c_str()); else DeleteFileW(l.c_str()); } + RemoveTree(root); + std::printf("convert-ignore: %d checks passed\n", checks); + return 0; +} diff --git a/clean/installer/tests/launcher_ignore_cases.py b/clean/installer/tests/launcher_ignore_cases.py new file mode 100644 index 00000000..7456c5f5 --- /dev/null +++ b/clean/installer/tests/launcher_ignore_cases.py @@ -0,0 +1,94 @@ +# SPDX-License-Identifier: MIT +"""The ignore lists (src/wg_core/convert_ignore.h) on the install path, for run_launcher_tests.py (which calls run() after +its rc6 cases; this file keeps that suite below the 256 KB source limit). Needs installs, so it runs with the launcher +suite in the GPU window; the CPU-only cases are tests/run_convert_ignore_tests.py and ctest convert-ignore. + +Runs in a work folder of its own (its own %LOCALAPPDATA%, so its own test registry root and managed list): the model data +of the suite's other games is never touched, and its last uninstall really is the last game's. Generated fixture games +and synthetic model files only. Written for this project. + + an install converts from the game's own file; that file listed: a repair does not reuse the model data and, with no + other allowed file, fails (exit 5) with the reason; a second game's install converts again from its own file (the + listed conversion replaced, its SHA-256 recorded); the well-known list held open: a repair exits 10, nothing changed; + the last game's uninstall keeps ignore-files.txt byte for byte and says so in its result +""" +import os +import shutil +import time + +KEPT = "kept ignore-files.txt (your/integrators' ignore list)" + + +def _hold(path): + """The file opened with no sharing (an installer writing it); returns a function that lets it go.""" + import ctypes + from ctypes import wintypes + k = ctypes.WinDLL("kernel32", use_last_error=True) + k.CreateFileW.restype = wintypes.HANDLE + k.CreateFileW.argtypes = [wintypes.LPCWSTR, wintypes.DWORD, wintypes.DWORD, wintypes.LPVOID, wintypes.DWORD, wintypes.DWORD, wintypes.HANDLE] + k.CloseHandle.argtypes = [wintypes.HANDLE] + h = k.CreateFileW(path, 0x80000000 | 0x40000000, 0, None, 3, 0x80, None) + if h in (None, wintypes.HANDLE(-1).value): + raise OSError("could not hold %s (error %d)" % (path, ctypes.get_last_error())) + return lambda: k.CloseHandle(h) + + +def run(T, base, opti, game): + """T: the suite's test class; base: its instance (checks are counted there); game: its fixture-game maker.""" + print("\n[convert-ignore] the ignore lists on the install path: a listed model source, a list held open, the last uninstall") + import hashlib + from run_installer_tests import read, write + work = os.path.join(base.work, "convert-ignore") + shutil.rmtree(work, ignore_errors=True) + t = T(base.exe, work, base.model, base.env.get("WRONGGPU_HIP_RUNTIME")) + t.check = base.check # one count, one list of failures + shared = os.path.join(t.env["LOCALAPPDATA"], "WrongGPU") + lst = os.path.join(shared, "ignore-files.txt") + src_txt = os.path.join(shared, "model", "model-source.txt") + flags = ("--opti=" + opti, "--noscan", "--force", "--experimental") + + def said(): + return " ".join([t.j.get("message", ""), t.j.get("error", "")] + list(t.j.get("warnings", [])) + list(t.j.get("notes", []))) + + root_a, b_a, exe_a = game(t, "Ignore A") + dll_a = os.path.join(b_a, "nvngx_dlss.dll") + rc = t.run("install", exe_a, *flags) + src = read(src_txt).decode("utf-8", "replace") if os.path.isfile(src_txt) else "" + t.check("convert-ignore: an install converts from the game's own file and records its SHA-256", + rc == 0 and ("source\t" + dll_a).lower() in src.lower() and ("sha256\t" + hashlib.sha256(read(dll_a)).hexdigest()) in src, (rc, said(), src)) + if rc != 0: + return + model_before = read(os.path.join(shared, "model", "model.wgcm")) + write(lst, ("wronggpu-ignore-files 1\r\n# placed by another program's installer (test)\r\n%s\r\n" % dll_a).encode()) + rc = t.run("repair", exe_a, *flags) + t.check("convert-ignore: its source listed, no other allowed file: the repair does not reuse the model data, exit 5, the reason names the list and line 3", + rc == 5 and ("is not used: it was converted from " + dll_a).lower() in said().lower() and (lst + ", line 3").lower() in said().lower() and + "choose the nvngx_dlss.dll that came with one of your own games" in said() and + read(os.path.join(shared, "model", "model.wgcm")) == model_before, (rc, said())) + root_b, b_b, exe_b = game(t, "Ignore B", exe="Second.exe", extra={"bin/x64/nvngx_dlss.dll": base.model.file + b" synthetic tail of game B's own copy"}) + dll_b = os.path.join(b_b, "nvngx_dlss.dll") + rc = t.run("install", exe_b, *flags) + src = read(src_txt).decode("utf-8", "replace") if os.path.isfile(src_txt) else "" + t.check("convert-ignore: a second game's install converts again from its own (allowed) file; the listed conversion is replaced, the new SHA-256 recorded", + rc == 0 and ("source\t" + dll_b).lower() in src.lower() and ("sha256\t" + hashlib.sha256(read(dll_b)).hexdigest()) in src, (rc, said(), src)) + let_go = _hold(lst) + try: + t0 = time.time() + rc = t.run("repair", exe_b, *flags) + took = time.time() - t0 + finally: + let_go() + t.check("convert-ignore: the well-known list held open: the repair exits 10 after the retries, the file and the reason named", + rc == 10 and lst.lower() in said().lower() and "another program holds it open" in said() and took >= 0.75, (rc, round(took, 2), said())) + list_bytes = read(lst) + rc_a = t.run("uninstall", exe_a) + rc_b = t.run("uninstall", exe_b) + for _ in range(100): # the hidden cleanup deletes the moved-aside folder + if not [n for n in os.listdir(t.env["LOCALAPPDATA"]) if ".cleanup-" in n]: + break + time.sleep(0.2) + t.check("convert-ignore: the last game's uninstall keeps ignore-files.txt byte for byte (the rest of the shared folder goes) and says so", + rc_a == 0 and rc_b == 0 and os.path.isfile(lst) and read(lst) == list_bytes and sorted(os.listdir(shared)) == ["ignore-files.txt"] and + any(w.startswith(KEPT) for w in t.j.get("warnings", [])), (rc_a, rc_b, sorted(os.listdir(shared)) if os.path.isdir(shared) else None, t.j.get("warnings"))) + os.remove(lst) + shutil.rmtree(work, ignore_errors=True) diff --git a/clean/installer/tests/run_convert_ignore_tests.py b/clean/installer/tests/run_convert_ignore_tests.py new file mode 100644 index 00000000..2f9aa739 --- /dev/null +++ b/clean/installer/tests/run_convert_ignore_tests.py @@ -0,0 +1,726 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: MIT +"""The ignore lists of the converter's file search (src/wg_core/convert_ignore.h) through the launcher's command line: +`model convert` and `rr-data convert` with the well-known lists (%LOCALAPPDATA%\\WrongGPU\\ignore-files.txt and the +launcher's own data folder's), --ignore-list= and --ignore=. + +Written for this project; Python standard library only. Runs WrongGPU.exe (a WRONGGPU_TESTING build made with the +installer suite's test map) against GENERATED library folders only: synthetic model files from the converter's test +generator, the spec team's synthetic preset-F file, names and bytes of our own. Only the two convert verbs run: no +install, no window, no GPU; --noscan always (the tester's own store libraries are never read). + + python run_convert_ignore_tests.py --launcher --work [--synth-f ] + +The candidate sets of the auto search are the installer suite's "several DLLs" sets ([13]: the picked file and a game's, +with and without the output-filter block, in both orders; two without it) and the same with library games only, plus a +file that does not convert first. Each set is converted with no list at all, then with an empty list in every place a +list is read: the winner, the model file's bytes and model-source.txt must be the same each time. +""" +import argparse +import hashlib +import json +import os +import shutil +import subprocess +import sys +import time + +os.environ["WRONGGPU_TESTING"] = "1" +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, HERE) +import run_installer_tests as suite # noqa: E402 (the synthetic model cases, OLD_DLSS) +from run_installer_tests import read, write # noqa: E402 + +DLL = "nvngx_dlss.dll" +RR_DLL = "nvngx_dlssd.dll" # the RR model file's name (a synthetic stand-in here) + + +class T: + def __init__(self, exe, work): + self.exe, self.work = exe, work + self.passed, self.failures, self.skipped = 0, [], [] + self.data = os.path.join(work, "data") # --data: the launcher's own data folder (its ignore-files.txt is read too) + self.log = os.path.join(work, "launcher-test.log") + self.out = os.path.join(work, "out.json") + self.lad = os.path.join(work, "localappdata") + self.env = dict(os.environ, LOCALAPPDATA=self.lad, ProgramData=os.path.join(work, "programdata")) + for k in [k for k in self.env if k.upper().startswith("WG_") or k.upper() in ("HIP_PATH", "ROCM_PATH", "LLVM_PATH", "HIP_DEVICE_LIB_PATH")]: + self.env.pop(k) + os.makedirs(self.lad, exist_ok=True) + self.j, self.stdout = {}, "" + + def check(self, name, cond, detail=""): + if cond: + self.passed += 1 + print(" PASS ", name) + else: + self.failures.append(name + (": " + str(detail)[:1500] if detail else "")) + print(" FAIL ", name, str(detail)[:1500]) + + def skip(self, name, why): + self.skipped.append("%s (%s)" % (name, why)) + print(" SKIP ", name, "-", why) + + def run(self, *args): + """WrongGPU.exe --noscan --data --log --out; the JSON result in self.j; returns the exit code.""" + if os.path.exists(self.out): + os.remove(self.out) + if os.path.exists(self.log): + os.remove(self.log) + cmd = [self.exe] + list(args) + ["--noscan", "--log=" + self.log, "--out=" + self.out, "--data=" + self.data] + p = subprocess.run(cmd, env=self.env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=600) + self.stdout = p.stdout.decode("utf-8", "replace") + try: + with open(self.out, encoding="utf-8") as f: + self.j = json.load(f) + except (OSError, ValueError): + self.j = {} + return p.returncode + + def logtext(self): + return read(self.log).decode("utf-8", "replace") if os.path.isfile(self.log) else "" + + # the shared data: %LOCALAPPDATA%\WrongGPU (the well-known list) and its model folders + def shared(self, *rel): + return os.path.join(self.lad, "WrongGPU", *rel) + + def clear_models(self): + """The converted model and RR data gone (each run converts again); the lists next to them stay.""" + for rel in (("model", "model.wgcm"), ("model", "model-source.txt"), ("model", "F", "model.wgcm"), ("model", "F", "model-source.txt")): + p = self.shared(*rel) + if os.path.isfile(p): + os.remove(p) + + def set_lists(self, shared=None, own=None): + """The two well-known lists: None removes the file, a string is written as it is.""" + for path, text in ((self.shared("ignore-files.txt"), shared), (os.path.join(self.data, "ignore-files.txt"), own)): + if text is None: + if os.path.isfile(path): + os.remove(path) + else: + write(path, text.encode("utf-8") if isinstance(text, str) else text) + + +def library(t, name, files): + """A generated library folder: one sub-folder per game, each with its DLL (name -> bytes, in that order).""" + lib = os.path.join(t.work, "libs", name) + shutil.rmtree(lib, ignore_errors=True) + paths = [] + for game, (rel, data) in files: + p = os.path.join(lib, game, rel) + write(p, data) + paths.append(p) + return lib, paths + + +def model_state(t): + src = t.shared("model", "model-source.txt") + m = t.shared("model", "model.wgcm") + return {"code": None, "model": read(m) if os.path.isfile(m) else b"", + "source": read(src).decode("utf-8", "replace") if os.path.isfile(src) else ""} + + +def source_lines(text): + """model-source.txt without its time stamp (converted\\t...).""" + return [l for l in text.replace("\r", "").split("\n") if l and not l.startswith("converted\t")] + + +def convert_model(t, lib=None, model=None, extra=(), keep=False): + """model convert; keep: the model converted earlier stays (the reuse cases), else it is removed first.""" + if not keep: + t.clear_models() + args = ["model", "convert"] + (["--library=" + lib] if lib else []) + (["--model=" + model] if model else []) + list(extra) + code = t.run(*args) + s = model_state(t) + s["code"] = code + s["skipped"] = t.j.get("skipped", []) + s["warnings"] = t.j.get("ignore_warnings", []) + s["error"] = t.j.get("error", "") + s["notes"] = t.j.get("notes", []) + s["outcome"] = t.j.get("outcome", "") + return s + + +def source_of(s): + for l in source_lines(s["source"]): + if l.startswith("source\t"): + return l.split("\t", 1)[1] + return "" + + +# ---------------------------------------------------------------------------------------------------- the tests + +def test_empty_lists_same_winner(t): + print("\n[1] an empty list never changes which file wins: the existing candidate sets, no list vs an empty list everywhere") + with_f, with_f2, none = suite.cases.f_mini_case(present=0), suite.cases.f_mini_case(present=1), suite.cases.f_mini_case(present=None) + none2 = none.file + b"another synthetic file without the block" + picked = os.path.join(t.work, "elsewhere", "picked", DLL) + # (label, the picked file's bytes or None, [(game, bytes)], the expected winner: "picked" or a game's name) + sets = [ + ("[13] picked file without the block, game's file with it", none.file, [("Game", with_f.file)], "Game"), + ("[13] picked file with the block, game's file without it", with_f.file, [("Game", none.file)], "picked"), + ("[13] two files without the block", none.file, [("Game", none2)], "picked"), + ("library: A without, B with", None, [("A Game", none.file), ("B Game", with_f.file)], "B Game"), + ("library: A with, B without", None, [("A Game", with_f.file), ("B Game", none.file)], "A Game"), + ("library: A with, B with variant 2", None, [("A Game", with_f.file), ("B Game", with_f2.file)], "A Game"), + ("library: two without the block", None, [("A Game", none.file), ("B Game", none2)], "A Game"), + ("library: a file that does not convert first, then one with the block", None, [("A Game", suite.OLD_DLSS), ("B Game", with_f.file)], "B Game"), + ("picked file only", with_f.file, [], "picked"), + ] + empties = [("no list anywhere", None, None, ()), + ("%LOCALAPPDATA%\\WrongGPU\\ignore-files.txt empty", "", None, ()), + ("the launcher's own data folder's ignore-files.txt: comments, version line, blank lines", None, + "# nothing placed\r\nwronggpu-ignore-files 1\r\n\r\n", ()), + ("both well-known lists empty and an empty --ignore-list=", "\ufeff", "", ("--ignore-list=" + os.path.join(t.work, "empty-list.txt"),))] + write(os.path.join(t.work, "empty-list.txt"), b"") + for label, pick, games, winner in sets: + lib, paths = library(t, "same-winner", [(g, (DLL, data)) for g, data in games]) + if pick is not None: + write(picked, pick) + want = picked if winner == "picked" else os.path.join(lib, winner, DLL) + results = [] + for elabel, shared, own, extra in empties: + t.set_lists(shared, own) + s = convert_model(t, lib if games else None, picked if pick is not None else None, extra) + results.append((elabel, s)) + base = results[0][1] + t.check(label + ": converted from the expected file with no list", base["code"] == 0 and source_of(base).lower() == want.lower(), + (base["code"], source_of(base), base["error"], t.logtext()[-1500:])) + for elabel, s in results[1:]: + t.check(" ...%s: the same winner, model file and model-source.txt, nothing skipped, no warning" % elabel, + s["code"] == base["code"] and s["model"] == base["model"] and source_lines(s["source"]) == source_lines(base["source"]) and + s["skipped"] == [] and s["warnings"] == [], (s["code"], source_of(s), s["skipped"], s["warnings"])) + t.set_lists(None, None) + + +def test_model_lists(t): + print("\n[2] model convert: the lists used, every candidate ignored, a hash alone, a bad line, path normalisation, links") + with_f, with_f2 = suite.cases.f_mini_case(present=0), suite.cases.f_mini_case(present=1) + lib, (a, b, c) = library(t, "lists", [("A Game", (DLL, with_f.file)), ("B Game", (DLL, with_f2.file)), ("C Game", (DLL, with_f.file + b" synthetic tail"))]) + shared_list = t.shared("ignore-files.txt") + own_list = os.path.join(t.data, "ignore-files.txt") + + t.set_lists(None, None) + s = convert_model(t, lib) + t.check("no list: the first library game's file wins (A)", s["code"] == 0 and source_of(s).lower() == a.lower(), (s["code"], source_of(s), s["error"])) + + # the well-known list in %LOCALAPPDATA%\WrongGPU: an integrator's installer names the file it placed + t.set_lists("wronggpu-ignore-files 1\r\n# placed by another program's installer\r\n%s\r\n" % a, None) + s = convert_model(t, lib) + log = t.logtext() + t.check("%LOCALAPPDATA%\\WrongGPU\\ignore-files.txt: A skipped, B converted", + s["code"] == 0 and source_of(s).lower() == b.lower(), (s["code"], source_of(s), s["error"], log[-1500:])) + t.check("...the result names the skipped file, the list (with its line) and the path", + len(s["skipped"]) == 1 and s["skipped"][0].lower().startswith(a.lower() + ": on the ignore list " + shared_list.lower() + ", line 3") and + "(path " in s["skipped"][0], s["skipped"]) + t.check("...the log and the console name it too", ("model: skipped " + a).lower() in log.lower() and ("skipped: " + a).lower() in t.stdout.lower(), + (log[-1500:], t.stdout[-800:])) + t.check("...the skipped file is not tried (no converter line for it) and not in model-source.txt", + ("model: rejected " + a).lower() not in log.lower() and a.lower() not in s["source"].lower(), s["source"]) + + # the launcher's own data folder's list (a portable launcher's WrongGPU-data; here the --data folder) + t.set_lists(None, "%s\r\n" % a) + s = convert_model(t, lib) + t.check("the launcher's own data folder's ignore-files.txt: A skipped, B converted, the list named", + s["code"] == 0 and source_of(s).lower() == b.lower() and len(s["skipped"]) == 1 and own_list.lower() in s["skipped"][0].lower(), (s["code"], source_of(s), s["skipped"])) + + # the command line's lists add to the well-known ones + cli_list = os.path.join(t.work, "cli-list.txt") + write(cli_list, ("%s\n" % b).encode()) + t.set_lists("%s\r\n" % a, None) + s = convert_model(t, lib, extra=("--ignore-list=" + cli_list,)) + t.check("--ignore-list= adds to the well-known list: A and B skipped, C converted", + s["code"] == 0 and source_of(s).lower() == c.lower() and len(s["skipped"]) == 2 and cli_list.lower() in s["skipped"][1].lower(), (s["code"], source_of(s), s["skipped"])) + s = convert_model(t, lib, extra=("--ignore=" + b,)) + t.check("--ignore= adds to the well-known list: A and B skipped, C converted, '--ignore' named", + s["code"] == 0 and source_of(s).lower() == c.lower() and len(s["skipped"]) == 2 and "--ignore" in s["skipped"][1], (s["code"], source_of(s), s["skipped"])) + + # every candidate ignored + before = model_state(t) + s = convert_model(t, lib, extra=("--ignore=" + b, "--ignore=" + c)) + t.check("every candidate ignored: exit 5 (no model), nothing converted or written", s["code"] == 5 and s["model"] == b"" and s["source"] == "", + (s["code"], len(s["model"]), s["error"])) + t.check("...a clear message: every file found is on an ignore list, and all three are named", + "Every DLSS model file found (3) is on an ignore list, so none was converted" in s["error"] and len(s["skipped"]) == 3 and + "Every DLSS model file found (3) is on an ignore list" in t.stdout, (s["error"], s["skipped"], t.stdout[-600:])) + del before + + # a hash alone: the file is skipped wherever it is, under any name + sha_a = hashlib.sha256(with_f.file).hexdigest() + t.set_lists("sha256=%s\r\n" % sha_a, None) + s = convert_model(t, lib) + t.check("a hash-only entry: A (that hash) skipped, B converted; the reason names the hash", + s["code"] == 0 and source_of(s).lower() == b.lower() and len(s["skipped"]) == 1 and ("SHA-256 " + sha_a) in s["skipped"][0], (s["code"], source_of(s), s["skipped"])) + copy = os.path.join(t.work, "elsewhere", "renamed", DLL) + write(copy, with_f.file) + s = convert_model(t, lib, model=copy) + t.check("...the same bytes chosen with --model= from another folder: skipped too, B converted", + s["code"] == 0 and source_of(s).lower() == b.lower() and len(s["skipped"]) == 2 and s["skipped"][0].lower().startswith(copy.lower()), (s["code"], source_of(s), s["skipped"])) + + # a bad line: warned (with its list and line), skipped; the rest of the list is used + t.set_lists("this is not a path\r\nsha256=12345\r\n%s\r\n" % a, None) + s = convert_model(t, lib) + t.check("bad lines: each warned with the list and the line, the good line still used (A skipped, B converted)", + s["code"] == 0 and source_of(s).lower() == b.lower() and len(s["warnings"]) == 2 and (shared_list + ", line 1").lower() in s["warnings"][0].lower() and + ", line 2" in s["warnings"][1] and len(s["skipped"]) == 1, (s["code"], source_of(s), s["warnings"], s["skipped"])) + t.check("...the warnings are in the log and on the console", "model: warning: " in t.logtext() and "ignore list warning: " in t.stdout, t.stdout[-800:]) + + # path normalisation: case, slashes, '.' and '..' + odd = a.lower().replace("\\", "/").replace("/a game/", "/a game/./bin/../") + t.set_lists("%s\r\n" % odd, None) + s = convert_model(t, lib) + t.check("a listed path in lower case with '/', '.' and '..': A skipped, B converted (%s)" % odd, + s["code"] == 0 and source_of(s).lower() == b.lower() and len(s["skipped"]) == 1, (s["code"], source_of(s), s["skipped"], s["warnings"])) + t.set_lists('"%s"\r\n' % a.upper(), None) + s = convert_model(t, lib) + t.check("a listed path in upper case, in quotes: A skipped", s["code"] == 0 and source_of(s).lower() == b.lower() and len(s["skipped"]) == 1, + (s["code"], source_of(s), s["skipped"], s["warnings"])) + + # a junction to the listed file's folder: the candidate's fully resolved path is the listed file + junction = os.path.join(t.work, "junction-to-a") + if os.path.lexists(junction): + os.rmdir(junction) + r = subprocess.run(["cmd.exe", "/d", "/c", "mklink", "/J", junction, os.path.dirname(a)], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + if r.returncode == 0 and os.path.isfile(os.path.join(junction, DLL)): + t.set_lists("%s\r\n" % a, None) + s = convert_model(t, lib, model=os.path.join(junction, DLL)) + t.check("--model= through a junction to the listed file's folder: skipped (resolved to the listed file), B converted", + s["code"] == 0 and source_of(s).lower() == b.lower() and len(s["skipped"]) == 2 and s["skipped"][0].lower().startswith(junction.lower()), + (s["code"], source_of(s), s["skipped"])) + t.set_lists("%s\r\n" % os.path.join(junction, DLL), None) + s = convert_model(t, lib) + t.check("a path listed through the junction: the file itself (A) skipped", s["code"] == 0 and source_of(s).lower() == b.lower() and len(s["skipped"]) == 1, + (s["code"], source_of(s), s["skipped"])) + os.rmdir(junction) # the link only + else: + t.skip("junction cases", "mklink /J failed: %s" % r.stderr.decode("utf-8", "replace").strip()) + + # a symbolic link in a library game, named as the DLL, pointing at the listed file kept elsewhere + real = os.path.join(t.work, "elsewhere", "placed", "real-file.dll") + write(real, with_f.file) + lib2 = os.path.join(t.work, "libs", "symlink") + shutil.rmtree(lib2, ignore_errors=True) + link = os.path.join(lib2, "A Game", DLL) + os.makedirs(os.path.dirname(link)) + write(os.path.join(lib2, "B Game", DLL), with_f2.file) + try: + os.symlink(real, link) + made = True + except OSError as e: + made, why = False, str(e) + if made: + t.set_lists("%s\r\n" % real, None) + s = convert_model(t, lib2) + t.check("a symbolic link in a game pointing at the listed file: skipped, B converted", + s["code"] == 0 and source_of(s).lower() == os.path.join(lib2, "B Game", DLL).lower() and len(s["skipped"]) == 1 and s["skipped"][0].lower().startswith(link.lower()), + (s["code"], source_of(s), s["skipped"])) + os.remove(link) # the link only + else: + t.skip("symbolic link case", "this account may not make a symbolic link: " + why) + + # hard link: the same file under another path + hard = os.path.join(t.work, "elsewhere", "hard", DLL) + if os.path.exists(hard): + os.remove(hard) + os.makedirs(os.path.dirname(hard), exist_ok=True) + os.link(a, hard) + t.set_lists("%s\r\n" % a, None) + s = convert_model(t, lib, model=hard) + t.check("--model= a hard link to the listed file: skipped as the same file, B converted", + s["code"] == 0 and source_of(s).lower() == b.lower() and len(s["skipped"]) == 2 and "the same file" in s["skipped"][0], (s["code"], source_of(s), s["skipped"])) + os.remove(hard) + + # UNC: the drive's administrative share on this PC (read only; nothing is shared or changed). Not reachable: said, skipped. + unc = "\\\\localhost\\%s$%s" % (a[0], a[2:]) if len(a) > 2 and a[1] == ":" else "" + if unc and os.path.isfile(unc): + t.set_lists("%s\r\n" % a, None) + s = convert_model(t, lib, model=unc) + t.check("--model= the listed file through \\\\localhost\\%s$ (UNC): skipped as the same file, B converted" % a[0], + s["code"] == 0 and source_of(s).lower() == b.lower() and len(s["skipped"]) == 2 and s["skipped"][0].lower().startswith(unc.lower()), (s["code"], source_of(s), s["skipped"])) + t.set_lists("%s\r\n" % unc, None) + s = convert_model(t, lib) + t.check("a UNC entry for the file: the file found by its local path (A) skipped", s["code"] == 0 and source_of(s).lower() == b.lower() and len(s["skipped"]) == 1, + (s["code"], source_of(s), s["skipped"])) + t.set_lists("\\\\?\\UNC\\localhost\\%s$%s\r\n" % (a[0], a[2:]), None) + s = convert_model(t, lib) + t.check("a \\\\?\\UNC\\ entry for the file: A skipped", s["code"] == 0 and source_of(s).lower() == b.lower() and len(s["skipped"]) == 1, (s["code"], source_of(s), s["skipped"])) + else: + t.skip("UNC cases", "%s is not reachable here" % (unc or "a local drive path")) + + # the command line's own mistakes stop it before anything is converted + t.set_lists(None, None) + s = convert_model(t, lib, extra=("--ignore-list=" + os.path.join(t.work, "no-such-list.txt"),)) + t.check("--ignore-list= of a file that isn't there: exit 10, nothing converted", s["code"] == 10 and s["model"] == b"" and "no-such-list.txt" in s["error"], + (s["code"], s["error"])) + s = convert_model(t, lib, extra=("--ignore=not a path",)) + t.check("--ignore= that is not a path or a hash: exit 10, nothing converted", s["code"] == 10 and s["model"] == b"" and "not a full path" in s["error"], + (s["code"], s["error"])) + s = convert_model(t, lib, extra=("--ignore", b)) + t.check("--ignore (two arguments) works as --ignore=", s["code"] == 0 and source_of(s).lower() == a.lower() and len(s["skipped"]) == 1, + (s["code"], source_of(s), s["skipped"])) + # the chosen file alone, on a list: the message names the argument (or the list and the line) + s = convert_model(t, None, model=a, extra=("--ignore=" + a,)) + t.check("--model= of a file named by --ignore= with nothing else found: exit 5, nothing converted; the message names the file and the --ignore= argument", + s["code"] == 5 and s["model"] == b"" and len(s["skipped"]) == 1 and + ("The file you chose (%s) is not converted: it is named by --ignore=%s" % (a, a)).lower() in s["error"].lower(), (s["code"], s["error"], s["skipped"])) + t.set_lists("# placed by another installer\r\n%s\r\n" % a, None) + s = convert_model(t, lib, model=a) + t.check("--model= of a listed file with other files found: B converted; the player's line names the list and line 2 and how to undo a mistake", + s["code"] == 0 and source_of(s).lower() == b.lower() and + any(("The file you chose (%s) is not converted: it is on the ignore list %s, line 2" % (a, shared_list)).lower() in n.lower() and "remove that line" in n + for n in s["notes"]), (s["code"], source_of(s), s["notes"])) + t.check("...the source file's SHA-256 is recorded in model-source.txt", ("sha256\t" + hashlib.sha256(with_f2.file).hexdigest()) in s["source"], s["source"]) + t.set_lists(None, None) + + +def synth_f_dll(folder): + """The synthetic preset-F source of the test build's F map (as run_launcher_tests.py synth_f_dll): synthetic bytes only.""" + import struct + n = 6386372 // 2 + tags = [struct.pack("