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..8b74d3c5 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,124 @@ 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; read once, by the same bounded reader as the +well-known lists, so a file of 16 MB or more is refused before it is read; a file that can't be read stops the command, +exit 10, nothing converted) and `--ignore=` (repeatable; one line of the format below, `--ignore=`, +`--ignore= sha256=` or `--ignore=sha256=`; an entry that is not understood stops the command, exit 10). For +both the `=` is required: a bare `--ignore` or `--ignore-list` is an error (exit 10) and never takes the next argument as +its value. `--library=` searches that +folder's sub-folders as games too (this run only; `--noscan` leaves out the store libraries). These options are read by +those two verbs only (and, in a `WRONGGPU_TESTING` build, `rr-data prepare`); on any other verb they stop it with exit 10 and +a message, so a file is never believed protected by an option that was ignored. + +**`--ignore=sha256=` matches every file with that SHA-256, wherever it is: every identical copy**, also the copy that +came with one of the player's own games (another program's installer usually places an unmodified vendor DLL, byte for byte +the one the games carry). Use it only to skip a file you know, for one run. An installer lists the file it placed by its path +(and, if it wants, the path with its hash), never by the hash alone. + +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= +``` + +- `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. The tests cover + a UNC entry through the PC's own administrative share (`\\localhost\$`), where it is reachable (otherwise they say + so and skip those checks): a UNC entry matches the file found by its local path and the other way round (same file + identity). Shares on other machines are not covered by a test. +- A path followed by whitespace and `sha256=`: matches **only when both match**: the same file at that path **and** that + SHA-256. The entry names a file as it was placed; an identical copy under another path (the player's own game) is not + skipped, and neither is the same path later holding other bytes (a game update). The file is hashed only when an entry + with a hash applies to its path. +- `sha256=` alone is **not** a list entry: it would skip every identical copy of a vendor DLL, the player's own too. + Such a line is skipped with a warning naming the list and the line (and what to write instead). Only the command line + takes one (`--ignore=sha256=`, above). +- A line that is not understood (not a full path, a hash that is not 64 hex digits, a wildcard, a path that is a folder) 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 and the exit code is 5; the message itself names +the first file skipped with its list and line (or `--ignore=` argument), says how many others were skipped and where the log +is, and to remove that line if it names a file that came with one of the player's own games. A list another program wrote +never switches the conversion off without the player being told which file did it. + +"An empty list, or none, changes nothing" means the **choice of file**: the candidates, their order and so the file that +converts first are the same (`tests/convert_ignore_tests.cpp` runs the real candidate search with no list and with an +empty list in each place one is read; `tests/run_convert_ignore_tests.py` converts with each). Every conversion now also +records its source file's SHA-256 in `model-source.txt` (`sha256`, below), with a list or without one: one line +more in that file; the converted data itself is unchanged. + +**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, a name the system refuses) 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 says what to do (close the program that holds it, or fix the file or delete it), +and the exit code is 10 (`model convert`, `rr-data convert`, and an install or +repair, whose model check fails with it). So does a list that can't be decoded: only UTF-8 and UTF-16 LE with its BOM are +read (UTF-16 big-endian, UTF-16 without a BOM and bytes that are not UTF-8 are not guessed at). A list that isn't there +(file or folder not found) is simply not read. A candidate whose SHA-256 can't be computed (it can't be read) while a hash +entry applies to it (`--ignore=sha256=`, or a path + hash entry for its path) is left out and named, never converted. + +**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: +- it was converted from a listed file: its recorded path is a list's path entry, or its recorded path **and** recorded + SHA-256 are a path + hash entry (both; the same rule as for a candidate), or its recorded SHA-256 is named by + `--ignore=sha256=`. Then 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). Data converted from a legitimate copy that has the same bytes as a + listed file, at another path, is reused. +- The recorded source is compared **as it was recorded**: the recorded path's text (normalised: case, `/` or `\`, `.`, `..`) + with the entry's path, as written and as it resolves now, and the recorded SHA-256 with the entry's. The recorded path is + never opened again: if it was later replaced by a hard link or a symbolic link to a listed file, the data converted from + the file that was there then is still reused. +- no SHA-256 is on record (converted by 0.6.0 or earlier): the path text alone decides (a path + hash entry naming the + recorded path lists it, since its bytes can't be checked); not listed, it is 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 player's list is never deleted**: when another program made +`ignore-files.txt` again between the folder's move and the put-back, the player's list is kept beside it as +`ignore-files.txt.kept-` (the first free n; WrongGPU reads only `ignore-files.txt`, so the result says to copy the lines +still needed into it); when it can't be moved or copied out at all (a file where the folder should be), it stays in the +moved-aside folder, everything else in that folder goes, nothing deletes that folder's tree, and the result names where it +is. A list that is a link is moved as the link (what it points to is never touched). The tests drive each cleanup path +(`StartCleanup`, `RemoveSharedData`, `SelfUninstall`) through both cases. + +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 +691,34 @@ 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, quotes, UTF-8 with a BOM, UTF-16 LE, LF; a hash alone, a folder and a list that + can't be decoded refused), 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 path + hash entry matching + only when both match (two identical stand-ins in two folders, only one listed: the other is still tried; the same path with + other bytes is not skipped), `--ignore=sha256=` wherever the file is, a candidate that can't be hashed while a hash entry + applies (left out), 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, a + list in UTF-16 big-endian and a name the system refuses (unreadable), the recorded-source check (path, path + hash, + hash), `StartCleanup` keeping the list, `PutIgnoreListBack` logging when it can't, and the real candidate search + (`AllowedModelCandidates`, which `FindModel` runs before it converts) with no list and with an empty list in each place + one is read, the same candidates in the same order, plus a list that does change it (so the test can fail). +- `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), `--ignore=sha256=`, a hash alone in a list (warned), a path + hash entry (two identical files, one listed: the other converts), a folder 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 bare `--ignore`, the options on other verbs), 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 path, path + hash or + `--ignore=sha256=`, with and without an allowed file; a path + hash entry with another hash or path: reused; 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..abc09f7d 100644 --- a/clean/installer/src/app/cli.cpp +++ b/clean/installer/src/app/cli.cpp @@ -20,6 +20,10 @@ // 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= (both must match) | +// --ignore=sha256= (every identical copy), --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 +64,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 +351,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,7 +496,74 @@ 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.ignoreListBytes.clear(); + g.ctx.ignoreEntries.clear(); + auto refuse = [&o](const wstring& msg) { o.code = EXIT_USAGE; o.j.S("error", msg); Print(msg); return false; }; + // a bare --ignore / --ignore-list (no "=", or nothing after it): never takes the next argument as its value + if (a.has(L"ignore")) return refuse(L"--ignore needs its entry after an equals sign: --ignore= or --ignore=sha256=; nothing was converted"); + if (a.has(L"ignore-list")) return refuse(L"--ignore-list needs its file: --ignore-list=; nothing was converted"); + for (auto& f : a.ignoreLists) { + // read once, with the lists' own bounded reader (the size is checked before anything is read); the search parses + // these bytes (LoadConvertIgnore), it does not read the file again + const wstring full = FullPath(f); + string bytes; + wstring why; + if (!ReadIgnoreListBytes(full, bytes, why)) return refuse(L"--ignore-list=" + f + L": the file can't be read (" + full + L": " + why + L"); nothing was converted"); + g.ctx.ignoreListFiles.push_back(full); + g.ctx.ignoreListBytes.push_back(std::move(bytes)); + } + for (auto& s : a.ignores) { + IgnoreEntry e; + wstring why; + // a hash alone (--ignore=sha256=) is the player's own choice here; it matches every copy of that file with the same bytes + if (!ParseIgnoreLine(s, e, why, nullptr, true) || 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; +} +// --ignore and --ignore-list are read by model convert and rr-data convert / prepare only: on any other verb they would be +// accepted and do nothing, so the player thinks a file is protected. Exit 10 and a message instead. +static bool IgnoreArgsMisplaced(const wstring& verb, const Args& a, Output& o) { + if (a.ignores.empty() && a.ignoreLists.empty() && !a.has(L"ignore") && !a.has(L"ignore-list")) return false; + const wstring sub = a.pos.empty() ? L"status" : a.pos[0]; + bool applies = (verb == L"model" && sub == L"convert") || (verb == L"rr-data" && sub == L"convert"); +#ifdef WRONGGPU_TESTING + applies = applies || (verb == L"rr-data" && sub == L"prepare"); +#endif + if (applies) return false; + const wstring msg = L"--ignore and --ignore-list apply only to \"model convert\" and \"rr-data convert\" (the well-known ignore lists are read by every conversion, " + L"also an install's); \"" + verb + (a.pos.empty() ? wstring() : L" " + a.pos[0]) + L"\" would ignore them, so it was not run"; + o.code = EXIT_USAGE; + o.j.S("error", msg); + Print(msg); + 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) { + if (IgnoreArgsMisplaced(verb, a, o)) return o.code; const bool noScan = a.has(L"noscan"); GlobalSettings& gs = gl.global; GlobalSettings scan = gs; // --library=: scanned this time only, never saved @@ -813,15 +887,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 +973,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 +1052,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 +1126,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") && i + 1 < argc) { // (--ignore and --ignore-list need their "=") + 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..af257f60 100644 --- a/clean/installer/src/app/ui_words.cpp +++ b/clean/installer/src/app/ui_words.cpp @@ -757,13 +757,20 @@ 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; kept elsewhere than in place (another program's new + // list was there, or it could not be put back): where it is, in full + if (!kept->empty() && !StartsWithI(*kept, kKeptIgnoreListText)) + Inform(L"WrongGPU's data is removed, but your ignore list was not put back as it was\n\n" + *kept, 1); + else + 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..2d34ca40 100644 --- a/clean/installer/src/wg_core/context.h +++ b/clean/installer/src/wg_core/context.h @@ -89,6 +89,14 @@ 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; + // ignoreListBytes[i]: the bytes of ignoreListFiles[i] when the command line read them already (once, with the bounded + // reader: convert_ignore.h ReadIgnoreListBytes); a file without them is read when the search loads the lists. + std::vector ignoreListBytes; 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..fc45b821 --- /dev/null +++ b/clean/installer/src/wg_core/convert_ignore.cpp @@ -0,0 +1,491 @@ +// 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 +#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; +} + +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, bool allowHashAlone) { + 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() && !allowHashAlone) { + // a hash alone would skip every copy of the file with the same bytes, the player's own too (an installer places an unmodified + // vendor DLL): only the command line takes one, as the player's own choice + why = L"a hash alone is not accepted in a list (it would skip every copy of that file with the same bytes, also your own): write the file's " + L"path and its hash, \" sha256=\", or use --ignore=sha256= on the command line"; + return false; + } + 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 + const DWORD attrs = GetFileAttributesW(e.path.c_str()); + if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) { + // a folder would never match a candidate file: say so instead of ignoring nothing + why = L"that is a folder, not a file (name the file's full path): " + e.path; + e = IgnoreEntry(); + return false; + } + e.resolved = FinalPath(e.path, &e.ident); + if (e.resolved.empty()) e.resolved = e.path; + } + e.sha256 = Lower(hash); + e.text = line; + return true; +} + +// The list's text: UTF-16 LE with its BOM, or UTF-8 (BOM or not). Anything else is not guessed at (a list read wrongly would +// skip nothing, silently): UTF-16 big-endian, UTF-16 or UTF-32 without a BOM (they hold NUL bytes), bytes that are not UTF-8. +static bool DecodeListBytes(const string& bytes, wstring& text, wstring& why) { + const auto u = [&](size_t i) { return (unsigned char)bytes[i]; }; + if (bytes.size() >= 2 && u(0) == 0xFF && u(1) == 0xFE) { // UTF-16 LE with its BOM + if ((bytes.size() - 2) % 2 != 0) { why = L"it is UTF-16 with a byte missing"; return false; } + if (bytes.size() >= 4 && u(2) == 0 && u(3) == 0) { why = L"it is UTF-32 (save it as UTF-8, or UTF-16 LE with its BOM)"; return false; } + text.resize((bytes.size() - 2) / 2); + if (!text.empty()) std::memcpy(&text[0], bytes.data() + 2, text.size() * 2); + return true; + } + if (bytes.size() >= 2 && u(0) == 0xFE && u(1) == 0xFF) { why = L"it is UTF-16 big-endian (save it as UTF-8, or UTF-16 LE with its BOM)"; return false; } + if (bytes.find('\0') != string::npos) { why = L"it holds NUL bytes: UTF-16 without a BOM, or UTF-32 (save it as UTF-8, or UTF-16 LE with its BOM)"; return false; } + const size_t skip = bytes.size() >= 3 && u(0) == 0xEF && u(1) == 0xBB && u(2) == 0xBF ? 3 : 0; + if (bytes.size() > skip) { + const int n = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, bytes.data() + skip, (int)(bytes.size() - skip), nullptr, 0); + if (n <= 0) { why = L"it is not UTF-8 text (save it as UTF-8, or UTF-16 LE with its BOM)"; return false; } + text.resize(n); + MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, bytes.data() + skip, (int)(bytes.size() - skip), &text[0], n); + } + return true; +} + +bool ParseIgnoreText(const string& bytes, const wstring& source, IgnoreLists& into, wstring* whyNot) { + wstring text; + { + wstring w; + if (!DecodeListBytes(bytes, text, w)) { if (whyNot) *whyNot = w; return false; } + } + 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); + } + return true; +} + +// The list's bytes. A sharing violation (an installer writing the list at this moment) is waited for: 5 tries 200 ms apart. +// The size is checked on the open handle before anything is read. +bool ReadIgnoreListBytes(const wstring& file, string& out, wstring& why) { + out.clear(); + const DWORD attrs = GetFileAttributesW(file.c_str()); + if (attrs == INVALID_FILE_ATTRIBUTES) { + const DWORD err = GetLastError(); + why = err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND ? wstring(L"there is no such file") : ErrText(err); + 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 < kIgnoreListMaxBytes; + if (!ok) why = L"it is 16 MB or larger, too large for a list"; + if (ok && GetFileType(h) != FILE_TYPE_DISK) { ok = false; why = L"it is not a file on a disk"; } + 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 (!ReadIgnoreListBytes(file, bytes, w)) { if (why) *why = w; return false; } + if (!ParseIgnoreText(bytes, file, into, &w)) { if (why) *why = w; return false; } + 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; +} +// How a path is looked at. A candidate (open): its fully resolved final path and its file identity, as it is now. A recorded +// source (not open): its text as recorded, normalised, and no identity; what is at that path now is not the file converted. +struct PathLook { + wstring fin; + FileIdent id; + explicit PathLook(const wstring& path, bool open = true) { + if (path.empty()) return; + if (open) fin = FinalPath(path, &id); + if (fin.empty()) fin = StripLongPrefix(FullPath(StripLongPrefix(path))); + } +}; +// Whether the entry's path is this file: the same resolved path, or the same file identity (*sameFile). No path entry: no. +static bool PathMatches(const IgnoreEntry& e, const PathLook& p, bool* sameFile) { + *sameFile = false; + if (e.path.empty() || p.fin.empty()) return false; + if (SameText(p.fin, e.resolved) || SameText(p.fin, e.path)) return true; + if (p.id == e.ident) { *sameFile = true; return true; } + return false; +} +// "(path ...)", "(path ...; SHA-256 ...)" or, for a hash alone (--ignore=sha256=), "(SHA-256 ...)" +static wstring EntryText(const IgnoreEntry& e, bool sameFile) { + wstring t = e.path.empty() ? wstring() : L"path " + e.path + (sameFile ? L": the same file" : L""); + if (!e.sha256.empty()) t += (t.empty() ? L"SHA-256 " : L"; SHA-256 ") + e.sha256; + return L" (" + t + L")"; +} + +// One rule for both searches. A path entry matches the same file; a path + hash entry only when the file is the same AND has +// that SHA-256; a hash alone (--ignore=sha256=) when the SHA-256 matches. `hash()` gives the SHA-256 of the thing looked at +// ("" when it can't be had: a file that can't be read); it is asked only when an entry needs it. A hash that can't be had +// while an entry applies is not a "no": `unchecked` names that entry, and the caller leaves the file out. +// pathAloneWithoutHash: a path + hash entry whose path matches decides by the path when there is no hash to compare (an old +// recorded source without a SHA-256); else that is `unchecked`. +template +static wstring EntryReason(const IgnoreLists& l, const PathLook& p, HashFn hash, wstring* unchecked, bool pathAloneWithoutHash = false) { + for (auto& e : l.entries) { + bool same = false; + if (!e.path.empty() && !PathMatches(e, p, &same)) continue; // a path entry that names another file + if (e.sha256.empty()) return Source(e) + EntryText(e, same); + const wstring h = hash(); + if (h.empty()) { + if (pathAloneWithoutHash && !e.path.empty()) return Source(e) + EntryText(e, same) + L"; no SHA-256 on record, so the path alone decides"; + if (unchecked && unchecked->empty()) *unchecked = e.where; + continue; + } + if (h == e.sha256) return Source(e) + EntryText(e, same); + } + return L""; +} + +wstring IgnoreReason(const IgnoreLists& l, const wstring& candidate) { + if (l.empty()) return L""; + const PathLook p(candidate); + wstring sha; + bool hashed = false; + wstring unchecked; + const wstring why = EntryReason(l, p, [&] { + if (!hashed) { hashed = true; sha = Lower(Sha256File(candidate)); } + return sha; + }, &unchecked); + if (!why.empty() || unchecked.empty()) return why; + // fail closed: a file that can't be hashed might be the very file the entry names + return L"unreadable, so it can't be checked against the SHA-256 entry " + unchecked + L"; it is left out"; +} + +wstring RecordedSourceReason(const IgnoreLists& l, const wstring& source, const wstring& sha) { + if (l.empty()) return L""; + // the recorded path as text: never opened (the file may have gone, changed, or the path may lead to another file now) + const PathLook p(source, false); + const wstring recorded = Lower(sha); + return EntryReason(l, p, [&] { return recorded; }, nullptr, true); +} + +wstring IgnoredExample(const Context& ctx, const IgnoreReport& rep) { + if (rep.skipped.empty()) return L""; + const size_t others = rep.skipped.size() - 1; + wstring t = L"Skipped: " + rep.skipped.front() + L"."; + if (others) t += L" " + std::to_wstring(others) + L" other file" + (others == 1 ? L" was" : L"s were") + L" skipped too"; + if (!ctx.logFile.empty()) t += (others ? L"; each, with its list and line, is in the log: " : L" The log has the details: ") + ctx.logFile + L"."; + else if (others) t += L"; each, with its list and line, is in the log."; + t += L" If a line names a file that came with one of your own games, remove that line from the list (a text file)."; + return t; +} + +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 + // the player is told which file stopped WrongGPU, and what to do: a list another program wrote never switches it off unnamed + if (l.unreadable.empty()) + l.unreadable = L"The ignore list " + f + L" can't be read (" + why + L"), so nothing was converted or reused. It is a list of files WrongGPU " + L"must not convert, written by you or by another program's installer. Close the program that holds it, or fix the file (a text " + L"file: UTF-8, one full path per line) or delete it, 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) { + const DWORD err = GetLastError(); + if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) continue; // not there: nothing to read + why = ErrText(err); // there, or may be, but not to be looked at (no permission, a bad name): fail closed + unreadable(f); + continue; + } + if (!ReadIgnoreList(f, l, &why)) unreadable(f); + } + for (size_t i = 0; i < ctx.ignoreListFiles.size(); ++i) { + const wstring& f = ctx.ignoreListFiles[i]; + if (i < ctx.ignoreListBytes.size()) { // read once already, by the command line (bounded): only parsed here + if (ParseIgnoreText(ctx.ignoreListBytes[i], f, l, &why)) l.lists.push_back(f); + else unreadable(f); + } else if (!ReadIgnoreList(f, l, &why)) { + unreadable(f); + } + } + for (auto& s : ctx.ignoreEntries) { + IgnoreEntry e; + if (!ParseIgnoreLine(s, e, why, nullptr, true)) { 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; +} + +static std::atomic g_putBackHook{nullptr}; +void SetPutBackHookForTest(PutBackHookFn fn) { g_putBackHook = fn; } + +static wstring ErrShort(DWORD err) { + wstring t = ErrText(err); + while (!t.empty() && (t.back() == L'\n' || t.back() == L'\r' || t.back() == L' ' || t.back() == L'.')) t.pop_back(); + return t; +} +// The list to `to`, never over a file that is there: moved (a rename beside it), else copied (a link as the link). 0: done. +static DWORD MoveOrCopyList(const wstring& from, const wstring& to, bool link) { + if (MoveFileExW(from.c_str(), to.c_str(), MOVEFILE_WRITE_THROUGH)) return 0; + const DWORD moveErr = GetLastError(); + BOOL cancel = FALSE; + if (CopyFileExW(from.c_str(), to.c_str(), nullptr, nullptr, &cancel, COPY_FILE_FAIL_IF_EXISTS | (link ? COPY_FILE_COPY_SYMLINK : 0))) return 0; + return moveErr; +} + +ListKept PutIgnoreListBack(Context& ctx, const wstring& moved, const wstring& shared) { + ListKept r; + 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)) return r; // no list (a folder of that name is none) + const bool link = (a & FILE_ATTRIBUTE_REPARSE_POINT) != 0; + if (const PutBackHookFn hook = g_putBackHook.load()) hook(shared); +#ifdef WRONGGPU_TESTING + else { + // the command-line suites: another program made the folder again with its own list, or a file is where it should be + const wstring how = EnvVar(L"WRONGGPU_TEST_LIST_PUTBACK"); + if (how == L"exists") { + CreateDirectoryW(shared.c_str(), nullptr); + const string other = "wronggpu-ignore-files 1\r\n# another program's new list (test)\r\n"; + WriteFileAtomic(to, other.data(), other.size()); + } else if (how == L"blocked") { + const string other = "a file where the folder should be (test)"; + WriteFileAtomic(shared, other.data(), other.size()); + } + } +#endif + CreateDirectoryW(shared.c_str(), nullptr); + const DWORD err = MoveOrCopyList(from, to, link); + if (!err) { + r.said = wstring(kKeptIgnoreListText) + L": " + to; + r.inPlace = true; + LogLine(ctx, L"cleanup: " + r.said); + return r; + } + // Another program made the list again meanwhile (or something else is in the way): the player's list is kept beside it, + // under the first free ignore-files.txt.kept-, never over it and never deleted. + for (int n = 1; n <= 99; ++n) { + const wstring keptAs = to + L".kept-" + std::to_wstring(n); + if (GetFileAttributesW(keptAs.c_str()) != INVALID_FILE_ATTRIBUTES) continue; + if (MoveOrCopyList(from, keptAs, link)) break; // the folder can't take it: not one name of this kind will do + const bool other = GetFileAttributesW(to.c_str()) != INVALID_FILE_ATTRIBUTES; + r.said = L"kept your ignore-files.txt as " + keptAs + L": " + (other ? L"another program made " + to + L" again meanwhile, so both are kept" + : L"it could not go back to " + to + L" (" + ErrShort(err) + L")") + + L". WrongGPU reads only ignore-files.txt: copy the lines you still need from " + BaseName(keptAs) + L" into it, then delete " + + BaseName(keptAs) + L"."; + LogLine(ctx, L"cleanup: " + r.said); + return r; + } + // It can't leave the moved folder: it stays there, and the caller deletes everything else of that folder, never the list. + r.leftIn = from; + r.said = L"could not put your ignore-files.txt back into " + shared + L" (" + ErrShort(err) + L"); it is kept at " + from + + L". Move it back to " + to + L" yourself (WrongGPU reads only that one); everything else in that folder is removed."; + LogLine(ctx, L"cleanup: " + r.said); + return r; +} + +} // 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..bf477e68 --- /dev/null +++ b/clean/installer/src/wg_core/convert_ignore.h @@ -0,0 +1,137 @@ +// 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=) +// A path alone 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 path with a hash matches only when BOTH match: the same file +// at that path AND that SHA-256 (the entry names a file as it was placed; the same path later holding other bytes, and a copy +// with the same bytes elsewhere, are not skipped). A hash alone is not a list entry: it would skip every copy of a vendor +// DLL with the same bytes, also the player's own, so a line "sha256=" in a list is skipped with a warning. The command line alone takes one +// (--ignore=sha256=): a deliberate choice of the player, which matches the candidate with that SHA-256 wherever it is. +// A line that is not understood (and a path that is a folder) 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, or can't be decoded (UTF-16 big-endian, UTF-16 without a BOM, bytes that are not +// UTF-8), fails closed: nothing is converted or reused (after kIgnoreReadTries on a sharing violation). So does a candidate +// whose SHA-256 can't be computed while a hash entry applies to it (it is left out). A conversion on this PC is checked +// against the lists (the source path text and SHA-256 its model-source.txt records, as recorded: the path is not opened +// again) before it is reused (model.cpp FindModel, rr.cpp FindRrData). The uninstalls and remove-data keep the list +// (PutIgnoreListBack): a player's list is never deleted. +#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: --ignore=sha256= only) + 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; with a path, it must match too + 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(); } +}; +// 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`). allowHashAlone: "sha256=" without a path is an entry +// (--ignore= only; in a list it is not understood). +bool ParseIgnoreLine(const wstring& line, IgnoreEntry& e, wstring& why, int* version = nullptr, bool allowHashAlone = false); +// A whole list's bytes (`source`: its name in the messages). Bad lines go to into.warnings and are skipped. False: the bytes +// can't be decoded (UTF-16 big-endian or without a BOM, not UTF-8; `why`); nothing is added then. +bool ParseIgnoreText(const string& bytes, const wstring& source, IgnoreLists& into, wstring* why = nullptr); +// One list file's bytes, the one bounded reader of every list (a sharing violation is tried kIgnoreReadTries times, +// kIgnoreReadWaitMs apart; a file of kIgnoreListMaxBytes or more is refused before anything is read). False: `why`. +const long long kIgnoreListMaxBytes = 16ll << 20; +bool ReadIgnoreListBytes(const wstring& file, string& out, wstring& why); +// Reads (ReadIgnoreListBytes) and parses one list file. False: it could not be read or decoded (`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 ...)"; a candidate whose SHA-256 can't be computed while a hash entry applies to it gets "unreadable, ..." (left +// out). An empty list never opens or reads the file; a file is hashed only when an entry that needs its hash applies. +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, compared as recorded. +// The recorded path is never opened again: what is at that path now (the file replaced, or the path made a hard link or a +// symbolic link to another file) says nothing about the file that was converted. The recorded path's text, normalised +// (case, '/' or '\', '.', '..'), is compared with the entry's path as written and as it resolves now. A path entry needs the +// recorded path; a path + hash entry the recorded path and the recorded SHA-256 (an old record without a SHA-256: the path +// text alone, its bytes can't be checked); a hash entry (--ignore=sha256=) the recorded SHA-256. "" : not on a list (or +// nothing recorded to compare). +wstring RecordedSourceReason(const IgnoreLists& l, const wstring& source, const wstring& sha); +// The player's line when files were left out of a search (rep.skipped not empty): the first file with its list and line +// (or --ignore=), the number of the others and where the log is. "" when nothing was left out. +wstring IgnoredExample(const Context& ctx, const IgnoreReport& rep); +// 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, read from ctx.ignoreListBytes when +// the command line read them already, and 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); +// What PutIgnoreListBack did with the list of the folder moved aside. +struct ListKept { + wstring said; // the player's line, also logged: kKeptIgnoreListText + ": " when it is back in place; another line when + // it was kept beside another program's new list (".kept-") or left in the moved folder; "" : no list + wstring leftIn; // not "": the list could not leave the moved folder and is kept there, at this path. The caller deletes + // the rest of the folder but never this file, and never the folder's whole tree (no recursive delete). + bool inPlace = false; // back at \ignore-files.txt as it was +}; +// 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. A player's list is never deleted: when another +// program made \ignore-files.txt again meanwhile, the list is kept beside it as ignore-files.txt.kept- (the first +// free n); when it can't be moved or copied out at all, it stays in the moved folder (ListKept::leftIn). A list that is a +// link (a reparse point) is moved as the link (a copy copies the link, never what it points to). Each case is said in +// ListKept::said for the player's result. +ListKept PutIgnoreListBack(Context& ctx, const wstring& moved, const wstring& shared); +// Test seam: called by PutIgnoreListBack after the folder was moved aside, before the list is put back (`shared`: where it +// goes). The tests make the folder again with another list in it, or put a file where it should be. nullptr: none. +// Test builds (WRONGGPU_TESTING) also read WRONGGPU_TEST_LIST_PUTBACK ("exists" or "blocked") when no seam is set, for the +// command-line suites (tests/run_convert_ignore_tests.py). +using PutBackHookFn = void (*)(const wstring& shared); +void SetPutBackHookForTest(PutBackHookFn fn); + +} // 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..6377073d 100644 --- a/clean/installer/src/wg_core/launcher.cpp +++ b/clean/installer/src/wg_core/launcher.cpp @@ -1535,15 +1535,31 @@ 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) { +// What is still in `dir` after RemoveTree(dir, keep) (held open somewhere) goes file by file once it is free: never `keep` +// (the player's ignore list that could not leave the folder: PutIgnoreListBack), and never the folder's whole tree. +static void DeleteLeftWhenFree(Context& ctx, const wstring& dir, const wstring& keep, const wstring& thenDir = L"", const wstring& upTo = L"") { + std::vector left, dirs{dir}; + for (size_t i = 0; i < dirs.size(); ++i) { + for (auto& f : ListFiles(dirs[i], L"*")) if (!IEq(f, keep)) left.push_back(f); + for (auto& d : ListDirs(dirs[i])) dirs.push_back(d); + } + for (auto& f : left) { + LogLine(ctx, L"cleanup: deleted once free: " + f); + DeleteWhenFree(ctx, f, false, thenDir.empty() ? Parent(f) : thenDir, upTo); + } +} + +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 +1593,17 @@ bool RemoveSharedData(Context& ctx, wstring* why) { LogLine(ctx, L"remove data: " + shared + L" could not be moved aside: " + ErrText(err)); return false; } - const bool ordinaryTrash = RemoveSharedTree(ctx, trash); + // the ignore list stays (an integrator's installer registered its files there; convert_ignore.h): put back, kept beside + // another program's new one, or left in the moved folder; never deleted + const ListKept keptList = PutIgnoreListBack(ctx, trash, shared); + if (kept) *kept = keptList.said; + const bool ordinaryTrash = RemoveSharedTree(ctx, trash, keptList.leftIn); 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); + if (ordinaryTrash && IsDir(trash)) { // what is still held goes once it is free + if (keptList.leftIn.empty()) DeleteWhenFree(ctx, trash, true, L""); + else DeleteLeftWhenFree(ctx, trash, keptList.leftIn); + } + LogLine(ctx, L"remove data: removed " + shared + (keptList.said.empty() ? wstring() : L"; " + keptList.said)); return true; } catch (const SetupError& e) { if (why) *why = e.msg; @@ -1708,7 +1731,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; @@ -1719,6 +1745,7 @@ bool SelfUninstall(Context& ctx, const LauncherPaths& p, bool removeGames, const const bool gamesLeft = AnyGameHasWrongGPU() || !offline.empty() || portableUses; const wstring trash = shared.empty() ? wstring() : shared + L".removed-" + std::to_wstring(GetCurrentProcessId()) + L"-" + std::to_wstring(GetTickCount64()); bool trashMade = false; + wstring listLeftIn; // the player's ignore list when it could not leave the trash (PutIgnoreListBack): kept there const DWORD sharedAttrs = shared.empty() ? INVALID_FILE_ATTRIBUTES : GetFileAttributesW(shared.c_str()); const bool sharedIsLink = sharedAttrs != INVALID_FILE_ATTRIBUTES && (sharedAttrs & FILE_ATTRIBUTE_REPARSE_POINT); if (!shared.empty() && !gamesLeft && sharedIsLink) { @@ -1734,19 +1761,23 @@ 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) { + const ListKept k = PutIgnoreListBack(ctx, trash, shared); + keptList = k.said; + listLeftIn = k.leftIn; // the trash keeps it: never deleted with the trash below + } 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& 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 (ordinaryShared) DeleteLeftWhenFree(ctx, shared, list); } + 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. @@ -1786,9 +1817,12 @@ bool SelfUninstall(Context& ctx, const LauncherPaths& p, bool removeGames, const // the install folder that could not go yet goes after the moved exe (rmdir without /s: only while it is empty), and the // folders above it that the first install made with it (madeAbove), so no empty %LOCALAPPDATA%\Programs stays if (trashMade) { - const bool ordinaryTrash = RemoveSharedTree(ctx, trash); + const bool ordinaryTrash = RemoveSharedTree(ctx, trash, listLeftIn); 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, exeDirLeft, upTo, true); + if (ordinaryTrash && IsDir(trash)) { + if (listLeftIn.empty()) DeleteWhenFree(ctx, trash, true, exeDirLeft, upTo, true); + else DeleteLeftWhenFree(ctx, trash, listLeftIn, exeDirLeft, upTo); // the trash stays with the list in it + } } if (!exeDirLeft.empty() && !IsDir(trash)) RemoveEmptyUpTo(exeDirLeft, upTo); return true; 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..19a56d3a 100644 --- a/clean/installer/src/wg_core/model.cpp +++ b/clean/installer/src/wg_core/model.cpp @@ -43,9 +43,31 @@ std::vector ModelCandidates(const GameTarget* t, const wstring& for (auto& g : libraries) for (auto& f : FindFilesNamed(g.root, L"nvngx_dlss.dll", 30000)) add(f, L"another game (" + g.name + L")"); return c; } +// ModelCandidates minus the files the ignore lists leave out (the order of the others stays); the files left out and why go to +// m.ignored, the player's line for a chosen file that is left out to m.chosenSkipped. With no entries it is ModelCandidates. +std::vector AllowedModelCandidates(Context& ctx, const IgnoreLists& ign, const GameTarget* t, const wstring& userDll, + const std::vector& libraries, ModelState& m) { + 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); + } + return cands; +} 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 +81,51 @@ 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 cands = AllowedModelCandidates(ctx, ign, t, userDll, libraries, m); 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 +143,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 +163,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. " + IgnoredExample(ctx, m.ignored) + + (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 not tried. " + IgnoredExample(ctx, m.ignored) : 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 +236,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 +247,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 +274,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 +294,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..f92be3e7 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. @@ -36,6 +46,10 @@ bool ExistingModelValid(Context& ctx, wstring* why = nullptr, wgc::ModelFileInfo bool MapHasOptionalGroup(const wgc::Map& map); wstring FilterText(Context& ctx, bool present); std::vector ModelCandidates(const GameTarget* t, const wstring& userDll, const std::vector& libraries); +// The candidates FindModel tries: ModelCandidates minus the files the ignore lists leave out (convert_ignore.h). The files left +// out, with the reason, go to m.ignored; the player's line for a chosen file that is left out to m.chosenSkipped. Needs no map. +std::vector AllowedModelCandidates(Context& ctx, const IgnoreLists& ign, const GameTarget* t, const wstring& userDll, + const std::vector& libraries, ModelState& m); ModelState FindModel(Context& ctx, const GameTarget* t, const wstring& userDll, const std::vector& libraries); bool ReplaceModel(Context& ctx, const ModelState& m, wstring& problem); ModelResult WriteModel(Context& ctx, const ModelState& m, Ui& ui); diff --git a/clean/installer/src/wg_core/record.cpp b/clean/installer/src/wg_core/record.cpp index 495c3846..c50d00d1 100644 --- a/clean/installer/src/wg_core/record.cpp +++ b/clean/installer/src/wg_core/record.cpp @@ -354,10 +354,11 @@ 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) { +// Returns the line "kept ignore-files.txt ..." when the shared folder's ignore list was put back (convert_ignore.h), else "". +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 +394,19 @@ 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): put back, kept + // beside another program's new one, or left in the moved folder; never deleted + const ListKept k = PutIgnoreListBack(ctx, retired, appData); + kept = k.said; + if (!k.leftIn.empty()) { + // it could not leave the moved folder: everything else in it goes now; the folder and the list stay, and the + // hidden cleanup below never deletes that folder's tree + RemoveTree(retired, k.leftIn); + LogLine(ctx, L"Shared data cleanup: " + retired + L" stays with the ignore list in it (what was held open in it stays too)"); + retired.clear(); + } } } wchar_t sys[MAX_PATH]; GetSystemDirectoryW(sys, MAX_PATH); @@ -412,6 +424,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 +575,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..827d0813 100644 --- a/clean/installer/src/wg_core/record.h +++ b/clean/installer/src/wg_core/record.h @@ -135,16 +135,20 @@ 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 PutIgnoreListBack's line +// for the player ("kept ignore-files.txt ...", the list kept as ignore-files.txt.kept-, or left in the moved folder) or "". +// A list that could not leave the moved folder is never deleted: the rest of that folder goes at once, the folder stays with +// the list, and the hidden cmd.exe does not delete it. 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..a7d4d550 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,86 @@ 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(); + // the player's line naming the first file skipped, its list and line (the error is a fragment: no full stop at its end) + wstring example = IgnoredExample(ctx, m.ignored); + if (!example.empty() && example.back() == L'.') example.pop_back(); + 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). " + example; + 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 +356,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 not tried). " + example; + 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 +401,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 +431,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 +443,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..8957b327 --- /dev/null +++ b/clean/installer/tests/convert_ignore_tests.cpp @@ -0,0 +1,660 @@ +// 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, quotes, UTF-8 / UTF-16 LE, CRLF; a hash alone, a folder and an undecodable list refused), +// 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 path + hash entry that matches only when both match (an identical copy +// elsewhere is not skipped), --ignore=sha256= that matches wherever the file is, the well-known list locations, and an empty +// list that leaves the real candidate search (AllowedModelCandidates, the one FindModel uses) exactly as it was; a recorded +// source matched as recorded (its path made a hard or symbolic link to a listed file after the conversion is still not +// listed); and a player's list never deleted by the cleanups (StartCleanup, RemoveSharedData, SelfUninstall: another +// program's new list there meanwhile, or no way to put it back; RemoveSharedData and SelfUninstall against a private registry +// hive of the test's own). Synthetic files only, in the temp folder. Written for this project. Run by ctest ("convert-ignore"). +#include +#include +#include +#include + +#include + +#include "wg_core/convert_ignore.h" +#include "wg_core/launcher.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; } +string Bytes(const wstring& p) { string b; return ReadAll(p, b) ? b : string(""); } +// PutIgnoreListBack's test seam (convert_ignore.h SetPutBackHookForTest), run between the folder's move and the put-back: +// another program makes the folder again with its own list, or a file is where the folder should be +const string kOtherList = "wronggpu-ignore-files 1\r\n# another program's new list (test)\r\n"; +void OtherProgramMakesList(const wstring& shared) { File(Join(shared, L"ignore-files.txt"), kOtherList); } +void FileWhereFolderShouldBe(const wstring& shared) { const string t = "a file where the folder should be (test)"; WriteFileAtomic(shared, t.data(), t.size()); } +// A list of --ignore= entries (the command line takes "sha256=" alone; a list does not) +IgnoreLists Cli(const std::vector& entries) { + IgnoreLists l; + for (auto& s : entries) { + IgnoreEntry e; + wstring why; + if (!ParseIgnoreLine(s, e, why, nullptr, true) || e.text.empty()) throw std::runtime_error("a --ignore= entry of the test is not understood: " + U8(s) + ": " + U8(why)); + e.where = L"--ignore=" + s; + l.entries.push_back(e); + } + return l; +} +// The paths of the candidates the search tries (AllowedModelCandidates: what FindModel uses), and what it left out +struct Search { std::vector tried; IgnoreReport ignored; wstring chosenSkipped; }; +Search RunSearch(Context& ctx, const IgnoreLists& ign, const GameTarget* t, const wstring& user, const std::vector& libs) { + ModelState m; + Search s; + for (auto& c : AllowedModelCandidates(ctx, ign, t, user, libs, m)) s.tried.push_back(c.path); + s.ignored = m.ignored; + s.chosenSkipped = m.chosenSkipped; + return s; +} +} // 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.entries.size() == 3 && l.warnings.size() == 1, "a list with a version line, comments, blank lines, a path, a path with its hash, a quoted path; a hash alone is warned about"); + 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 == c, "quotes around a path"); + require(Has(l.warnings[0], L"test.txt, line 7") && Has(l.warnings[0], L"hash alone is not accepted in a list") && Has(l.warnings[0], L"--ignore=sha256=") && + Has(l.warnings[0], L"skipped"), "a hash alone in a list: skipped with a warning (list, line, why, what to write instead)"); + IgnoreEntry ce; + wstring cwhy; + require(ParseIgnoreLine(L"sha256=" + wstring(64, L'a'), ce, cwhy, nullptr, true) && ce.path.empty() && ce.sha256 == wstring(64, L'a') && !ce.text.empty(), + "a hash alone is an entry for --ignore="); + 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\n" + b + L" sha256=" + 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].path == b && 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"); + } + // ---- a list that can't be decoded is not guessed at (fail closed): UTF-16 big-endian, UTF-16 without a BOM, not UTF-8 ---- + { + auto wide = [](const wstring& w, bool bigEndian, bool bom) { + string s; + if (bom) s += bigEndian ? string("\xFE\xFF", 2) : string("\xFF\xFE", 2); + for (wchar_t ch : w) { + const char lo = (char)(ch & 0xFF), hi = (char)(ch >> 8); + s += bigEndian ? string{hi, lo} : string{lo, hi}; + } + return s; + }; + IgnoreLists none; + wstring why; + require(!ParseIgnoreText(wide(a + L"\r\n", true, true), L"be.txt", none, &why) && Has(why, L"big-endian") && none.entries.empty() && none.warnings.empty(), + "UTF-16 big-endian (with its BOM): not decoded, nothing added"); + why.clear(); + require(!ParseIgnoreText(wide(a + L"\r\n", false, false), L"nobom.txt", none, &why) && Has(why, L"NUL") && none.entries.empty(), "UTF-16 LE without a BOM: not decoded"); + why.clear(); + require(!ParseIgnoreText(wide(a + L"\r\n", true, false), L"nobom-be.txt", none, &why) && Has(why, L"NUL"), "UTF-16 BE without a BOM: not decoded"); + why.clear(); + require(!ParseIgnoreText(U8(a) + "\xC3\x28\r\n", L"latin.txt", none, &why) && Has(why, L"not UTF-8") && none.entries.empty(), "bytes that are not UTF-8: not decoded"); + why.clear(); + require(!ParseIgnoreText(string("\xFF\xFE", 2) + string("a\0b", 3), L"odd.txt", none, &why), "UTF-16 with a byte missing: not decoded"); + require(ParseIgnoreText("", L"empty.txt", none) && ParseIgnoreText("\xEF\xBB\xBF", L"bom.txt", none) && ParseIgnoreText(U8(L"cafĂ© ") + "\n", L"u8.txt", none), + "an empty file, a BOM alone, UTF-8 with an accent: decoded"); + IgnoreEntry fe; + wstring fwhy; + require(!ParseIgnoreLine(lib, fe, fwhy) && Has(fwhy, L"folder") && fe.text.empty(), "a folder entry: not understood (it would ignore nothing)"); + IgnoreLists fl = Parse(U8(lib) + "\n" + U8(a) + "\n"); + require(fl.entries.size() == 1 && fl.warnings.size() == 1 && Has(fl.warnings[0], L"test.txt, line 1") && Has(fl.warnings[0], L"folder"), + "a folder in a list: warned with the list and the line like any bad line, the rest 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"); + // --ignore=sha256= (the only hash alone): wherever the file is, under any name + IgnoreLists h = Cli({L"sha256=" + shaB}); + 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(), "--ignore=sha256= 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"); + // a path with a hash matches only when BOTH match: that file, with that SHA-256 + IgnoreLists pb = Parse(U8(b) + " sha256=" + U8(shaB) + "\n"); + require(pb.entries.size() == 1 && pb.warnings.empty(), "a path with its hash is one entry"); + const wstring pbWhy = IgnoreReason(pb, b); + require(!pbWhy.empty() && Has(pbWhy, L"test.txt, line 1") && Has(pbWhy, L"(path " + b) && Has(pbWhy, L"; SHA-256 " + shaB), "path + hash, both match: skipped, the reason names both"); + require(IgnoreReason(pb, copy).empty() && IgnoreReason(pb, a).empty(), "an identical copy under another path is NOT skipped by a path + hash entry (nor another file)"); + IgnoreLists pc = Parse(U8(c) + " sha256=" + U8(shaB) + "\n"); // c's path with b's bytes' hash + require(IgnoreReason(pc, c).empty() && IgnoreReason(pc, copy).empty() && IgnoreReason(pc, b).empty(), + "a path + hash entry whose hash is not the file's: the file is not skipped, and neither is the file with that hash elsewhere"); + // two identical stand-ins in two folders; only one is listed, by path and hash: the other is still a candidate + const wstring twin1 = Join(root, L"Twin\\One\\nvngx_dlss.dll"), twin2 = Join(root, L"Twin\\Two\\nvngx_dlss.dll"); + File(twin1, "synthetic twin bytes"); + File(twin2, "synthetic twin bytes"); + const wstring shaT = Lower(Sha256File(twin1)); + require(shaT == Lower(Sha256File(twin2)), "the stand-ins are identical"); + IgnoreLists pt = Parse(U8(twin1) + " sha256=" + U8(shaT) + "\n"); + require(!IgnoreReason(pt, twin1).empty() && IgnoreReason(pt, twin2).empty(), "two identical files, one listed by path + hash: the listed one is skipped, the other is not"); + { // the real search: the other is the one that is tried + Context sctx; + const std::vector tw{{L"One", Join(root, L"Twin\\One"), L"", L"Folder"}, {L"Two", Join(root, L"Twin\\Two"), L"", L"Folder"}}; + const Search sr = RunSearch(sctx, pt, nullptr, L"", tw); + require(sr.tried == std::vector{twin2} && sr.ignored.skipped.size() == 1 && sr.ignored.skipped[0].rfind(twin1 + L": ", 0) == 0, + "the candidate search tries the unlisted identical copy (twin 2) and names the listed one (twin 1) as skipped"); + const Search none = RunSearch(sctx, IgnoreLists(), nullptr, L"", tw); + require(none.tried == std::vector{twin1, twin2}, "and with no entry both are tried, in order"); + } + // a path + hash entry for a file that now holds other bytes (a game update replaced it): not skipped + const wstring changed = Join(root, L"Changed\\nvngx_dlss.dll"); + File(changed, "the bytes it had when it was listed"); + const wstring shaOld = Lower(Sha256File(changed)); + IgnoreLists pold = Parse(U8(changed) + " sha256=" + U8(shaOld) + "\n"); + require(!IgnoreReason(pold, changed).empty(), "path + hash entry, the file as listed: skipped"); + File(changed, "other bytes after an update"); + require(IgnoreReason(pold, changed).empty(), "...the same path now holding other bytes: not skipped"); + // a hash is computed only when an entry that needs it applies; a file that can't be hashed then is left out (fail closed) + { + const wstring locked = Join(root, L"Locked\\nvngx_dlss.dll"); + File(locked, "synthetic locked file"); + HANDLE held = CreateFileW(locked.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); + require(held != INVALID_HANDLE_VALUE, "the test holds a candidate open without sharing"); + require(Lower(Sha256File(locked)).empty(), "...so it can't be hashed"); + IgnoreLists byHashOnly = Cli({L"sha256=" + shaB}), byPathHash = Parse(U8(locked) + " sha256=" + U8(shaB) + "\n"), elsewhereHash = Parse(U8(b) + " sha256=" + U8(shaB) + "\n"), + byPathOnly = Parse(U8(a) + "\n"); + const wstring r1 = IgnoreReason(byHashOnly, locked), r2 = IgnoreReason(byPathHash, locked); + require(Has(r1, L"unreadable") && Has(r1, L"--ignore=sha256=") && Has(r2, L"unreadable") && Has(r2, L"test.txt, line 1"), + "a candidate that can't be hashed, a hash entry applying to it (--ignore=sha256=, or a path + hash entry for its path): left out, the entry named"); + require(IgnoreReason(elsewhereHash, locked).empty() && IgnoreReason(byPathOnly, locked).empty(), "no hash entry applies (another path, or a path alone): it is not hashed, not skipped"); + CloseHandle(held); + } + // 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"), U8(b) + " 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", L"sha256=" + shaB}; + rep = IgnoreReport(); + IgnoreLists l = LoadConvertIgnore(ctx, L"model", &rep); + require(l.entries.size() == 5 && l.lists.size() == 3, "the well-known lists, then the command line's list and entries (--ignore=sha256= too)"); + 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") && Has(rep.skipped[4], L"named by --ignore=sha256="), + "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()); + // a list that can't be decoded fails closed too (UTF-16 big-endian here), the reason named; a bad hash line only warns + File(wk, string("\xFE\xFF\0a\0b", 6)); + rep = IgnoreReport(); + const IgnoreLists undecoded = LoadConvertIgnore(ctx, L"model", &rep); + require(Has(undecoded.unreadable, wk) && Has(undecoded.unreadable, L"big-endian") && Has(undecoded.unreadable, L"nothing was converted") && rep.unreadable == undecoded.unreadable, + "a well-known list in UTF-16 big-endian: unreadable (fail closed), the file and the reason named"); + DeleteFileW(wk.c_str()); + // GetFileAttributesW failing for a reason other than "not there" (a name the system refuses) is an unreadable list too + const wstring saved = ctx.ownDataDir; + ctx.ownDataDir = Join(root, L"bad?name"); + rep = IgnoreReport(); + const IgnoreLists refused = LoadConvertIgnore(ctx, L"model", &rep); + std::printf("convert-ignore: a name the system refuses: %ls\n", refused.unreadable.c_str()); + require(!refused.unreadable.empty() && Has(refused.unreadable, L"bad?name"), "a well-known list whose attributes can't be read (not \"not found\"): unreadable, fail closed"); + ctx.ownDataDir = saved; + 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 = Cli({L"sha256=" + shaB}), 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 --ignore=sha256= (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"); + // a path + hash entry lists a recorded source only when its recorded path AND recorded SHA-256 are both the entry's + IgnoreLists pathHash = Parse(U8(a) + " sha256=" + U8(shaB) + "\n"); + const wstring otherSha = wstring(64, L'1'); + require(Has(RecordedSourceReason(pathHash, a, shaB), L"line 1") && Has(RecordedSourceReason(pathHash, a, Upper(shaB)), L"SHA-256 " + shaB), + "recorded path and recorded SHA-256 both match the path + hash entry: listed"); + require(RecordedSourceReason(pathHash, a, otherSha).empty() && RecordedSourceReason(pathHash, c, shaB).empty() && + RecordedSourceReason(pathHash, Join(root, L"Gone\\nvngx_dlss.dll"), shaB).empty(), + "only one of them matches (the hash, or the path): not listed, so a model converted from a legitimate identical copy is reused"); + // an old record without a SHA-256: the path text alone decides (its bytes can't be checked) + require(Has(RecordedSourceReason(pathHash, a, L""), L"the path alone decides") && RecordedSourceReason(pathHash, c, L"").empty(), + "no hash on record: a path + hash entry naming the recorded path lists it, one naming another path does not"); + require(Has(RecordedSourceReason(byPath, Slashes(Upper(a)), L""), L"line 1") && Has(RecordedSourceReason(byPath, L"\\\\?\\" + a, shaB), L"line 1"), + "the recorded path text is normalised (case, '/', a \\\\?\\ prefix)"); + } + // ---- a recorded source is matched as it was recorded: its path retargeted after the conversion ----------------- + // The conversion was made from `orig`; later `orig` is made a hard link or a symbolic link to `placed`, a file another + // program placed and listed, with the same bytes. Today a candidate at `orig` is that listed file (skipped), but the + // model data converted earlier came from the file that was at `orig` then: it is not listed, and is reused. + { + const wstring orig = Join(root, L"Retarget\\Game\\nvngx_dlss.dll"), placed = Join(root, L"Retarget\\Placed\\nvngx_dlss.dll"); + const string bytes = "synthetic retarget bytes (the same in both files)"; + File(placed, bytes); + File(orig, bytes); + const wstring shaR = Lower(Sha256File(orig)); + const IgnoreLists byPath = Parse(U8(placed) + "\n"), byPathHash = Parse(U8(placed) + " sha256=" + U8(shaR) + "\n"); + require(RecordedSourceReason(byPath, orig, shaR).empty() && RecordedSourceReason(byPathHash, orig, shaR).empty(), "retarget: before, not listed"); + auto retargeted = [&](const char* kind) { + std::printf("convert-ignore: recorded source retargeted through a %s: checked\n", kind); + require(!IgnoreReason(byPath, orig).empty() && !IgnoreReason(byPathHash, orig).empty(), + "retarget: today a candidate at the source path is the listed file (skipped)"); + require(RecordedSourceReason(byPath, orig, shaR).empty() && RecordedSourceReason(byPathHash, orig, shaR).empty() && + RecordedSourceReason(byPathHash, orig, L"").empty(), + "retarget: the recorded source is matched as recorded (path text and SHA-256): not listed, the data converted from it is reused"); + }; + DeleteFileW(orig.c_str()); + if (CreateHardLinkW(orig.c_str(), placed.c_str(), nullptr)) retargeted("hard link"); + else std::printf("convert-ignore: hard link not made here (error %lu): the retarget check is skipped\n", GetLastError()); + DeleteFileW(orig.c_str()); + if (CreateSymbolicLinkW(orig.c_str(), placed.c_str(), SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)) { links.push_back(orig); retargeted("symbolic link"); } + else std::printf("convert-ignore: SYMLINK-SKIPPED: retarget through a symbolic link not made here (error %lu)\n", GetLastError()); + } + // ---- 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"); + Sleep(100); // the folder's new name carries GetTickCount64 (~16 ms steps): another step than the first cleanup's + require(StartCleanup(ctx, L"", L"", appData).empty() && !IsDir(appData), "no list: nothing kept, the folder goes as before"); + // PutIgnoreListBack: a player's list is never deleted. Back in place; beside another program's new list + // (.kept-1, then .kept-2); left in the moved folder when it can't leave it; a list that is a link moved as the link + { + const wstring moved = Join(root, L"moved-aside"), again = Join(root, L"made-again"), blocker = Join(root, L"not-a-folder"); + const wstring from = Join(moved, L"ignore-files.txt"), to = Join(again, L"ignore-files.txt"); + Context pctx; + pctx.logFile = Join(root, L"put-back.log"); + File(from, listBytes); + File(to, kOtherList); + ListKept k = PutIgnoreListBack(pctx, moved, again); + require(!k.inPlace && k.leftIn.empty() && Has(k.said, L"kept your ignore-files.txt as " + to + L".kept-1") && Has(k.said, L"another program made " + to), + "put back over another program's new list: kept beside it as ignore-files.txt.kept-1, and said"); + require(Bytes(to + L".kept-1") == listBytes && Bytes(to) == kOtherList && !IsFile(from), "...byte for byte; the other list untouched; nothing left behind"); + File(from, listBytes); + k = PutIgnoreListBack(pctx, moved, again); + require(Has(k.said, to + L".kept-2") && Bytes(to + L".kept-2") == listBytes && Bytes(to + L".kept-1") == listBytes, "...a second time: .kept-2, .kept-1 untouched"); + File(blocker, "a file where the shared folder should be"); + File(from, listBytes); + k = PutIgnoreListBack(pctx, moved, blocker); + require(!k.inPlace && IEq(k.leftIn, from) && Has(k.said, L"it is kept at " + from) && Bytes(from) == listBytes, + "it can't be moved or copied out (a file where the folder should be): it stays in the moved folder (leftIn), and said where"); + require(Has(FromUtf8(Bytes(pctx.logFile)), L"could not put your ignore-files.txt back into " + blocker), "...and logged"); + require(PutIgnoreListBack(pctx, Join(root, L"no-such-folder"), blocker).said.empty(), "no list in the moved folder: nothing to put back, nothing said"); + DeleteFileW(from.c_str()); + // a list that is a link (a reparse point): moved as the link; what it points to is never touched + const wstring target = Join(root, L"list-target.txt"), linkDir = Join(root, L"made-again-link"), linkTo = Join(linkDir, L"ignore-files.txt"); + File(target, listBytes); + auto isLink = [](const wstring& p) { const DWORD x = GetFileAttributesW(p.c_str()); return x != INVALID_FILE_ATTRIBUTES && (x & FILE_ATTRIBUTE_REPARSE_POINT); }; + if (CreateSymbolicLinkW(from.c_str(), target.c_str(), SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)) { + k = PutIgnoreListBack(pctx, moved, linkDir); + require(k.inPlace && isLink(linkTo) && Bytes(linkTo) == listBytes && !isLink(from) && !IsFile(from), "a list that is a link: put back as the link"); + CreateSymbolicLinkW(from.c_str(), target.c_str(), SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); + k = PutIgnoreListBack(pctx, moved, linkDir); + require(Has(k.said, linkTo + L".kept-1") && isLink(linkTo + L".kept-1") && Bytes(linkTo + L".kept-1") == listBytes, "...beside another list: .kept-1, still the link"); + CreateSymbolicLinkW(from.c_str(), target.c_str(), SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); + k = PutIgnoreListBack(pctx, moved, blocker); + require(IEq(k.leftIn, from) && isLink(from) && Bytes(target) == listBytes, "...that can't leave the moved folder: the link stays there (leftIn), its target untouched"); + for (auto& l : {from, linkTo, linkTo + L".kept-1"}) DeleteFileW(l.c_str()); + require(Bytes(target) == listBytes, "...removing the links never touched the file they point to"); + } else { + std::printf("convert-ignore: SYMLINK-SKIPPED: a list that is a link not made here (error %lu)\n", GetLastError()); + } + DeleteFileW(blocker.c_str()); + } + // ---- each cleanup path, when the list can't go back as it was (the seam runs between the move and the put-back) + { + const wstring list = Join(appData, L"ignore-files.txt"), lad = Parent(appData); + auto fill = [&] { File(Join(appData, L"model\\model.wgcm"), "synthetic model data"); File(list, listBytes); }; + auto movedWithList = [&](const wchar_t* tag) { // the moved-aside folder that holds the list (others may still be deleted) + for (auto& d : ListDirs(lad)) if (Has(d, tag) && IsFile(Join(d, L"ignore-files.txt"))) return d; + return wstring(); + }; + auto cleanAfterExists = [&](const char* what) { + require(Bytes(list + L".kept-1") == listBytes && Bytes(list) == kOtherList && !IsDir(Join(appData, L"model")), what); + DeleteFileW((list + L".kept-1").c_str()); + DeleteFileW(list.c_str()); + RemoveDirectoryW(appData.c_str()); + }; + auto checkBlocked = [&](const wstring& said, const wchar_t* tag, const char* what) { + const wstring d = movedWithList(tag); + require(!d.empty() && Has(said, L"it is kept at " + Join(d, L"ignore-files.txt")) && Bytes(Join(d, L"ignore-files.txt")) == listBytes && + !IsDir(Join(d, L"model")) && ListFiles(d, L"*").size() == 1, what); + return d; + }; + auto cleanAfterBlocked = [&](const wstring& d) { + DeleteFileW(Join(d, L"ignore-files.txt").c_str()); + RemoveDirectoryW(d.c_str()); + DeleteFileW(appData.c_str()); // the test's file where the folder should be + }; + // StartCleanup (the last game's uninstall, the restorer: record.cpp) + Sleep(100); + fill(); + SetPutBackHookForTest(&OtherProgramMakesList); + wstring said = StartCleanup(ctx, L"", L"", appData); + SetPutBackHookForTest(nullptr); + require(Has(said, L"kept your ignore-files.txt as " + list + L".kept-1"), "StartCleanup, the list made again meanwhile: kept as .kept-1, said in its result"); + cleanAfterExists("StartCleanup: .kept-1 holds the player's list, the other program's list untouched, the rest went"); + Sleep(100); + fill(); + SetPutBackHookForTest(&FileWhereFolderShouldBe); + said = StartCleanup(ctx, L"", L"", appData); + SetPutBackHookForTest(nullptr); + const wstring sd = checkBlocked(said, L".cleanup-", "StartCleanup, the list can't be put back: it stays in the moved folder (the rest went), said where"); + Sleep(4500); // its hidden cmd.exe waits ~2 s (ping -n 3) before it deletes: never this folder's tree now + require(Bytes(Join(sd, L"ignore-files.txt")) == listBytes, "StartCleanup: the hidden cleanup does not delete the list afterwards"); + cleanAfterBlocked(sd); + // RemoveSharedData (a portable copy's Remove data...) and SelfUninstall (the launcher's uninstall): launcher.cpp. A + // private registry hive of the test's own (RegLoadAppKey: a file in the temp folder, nothing in the user's registry) + // stands in for Apps & features, so no WrongGPU installed on this PC is seen or changed. + HKEY hive = nullptr; + const LONG hr = RegLoadAppKeyW(Join(root, L"test-hive.dat").c_str(), &hive, KEY_ALL_ACCESS, 0, 0); + if (hr == ERROR_SUCCESS) { + SetUninstallHive(hive); + wstring why, kept; + fill(); + SetPutBackHookForTest(&OtherProgramMakesList); + const bool ok1 = RemoveSharedData(ctx, &why, &kept); + SetPutBackHookForTest(nullptr); + require(ok1 && Has(kept, L"kept your ignore-files.txt as " + list + L".kept-1"), "RemoveSharedData, the list made again meanwhile: done, kept as .kept-1, said"); + cleanAfterExists("RemoveSharedData: .kept-1 holds the player's list, the other program's list untouched, the rest went"); + fill(); + SetPutBackHookForTest(&FileWhereFolderShouldBe); + const bool ok2 = RemoveSharedData(ctx, &why, &kept); + SetPutBackHookForTest(nullptr); + require(ok2, "RemoveSharedData, the list can't be put back: done"); + cleanAfterBlocked(checkBlocked(kept, L".removed-", "RemoveSharedData: the list stays in the moved folder (the rest went), said where")); + LauncherPaths lp; + lp.dataDir = Join(root, L"own-data"); + lp.custom = true; + std::vector uiLines; + Ui ui{[](int, int, const wstring&) {}, [&uiLines](const wstring& s) { uiLines.push_back(s); }}; + auto uiSaid = [&uiLines] { wstring all; for (auto& s : uiLines) all += s + L"\n"; return all; }; + fill(); + SetPutBackHookForTest(&OtherProgramMakesList); + const bool ok3 = SelfUninstall(ctx, lp, false, L"-", ui, &why); + SetPutBackHookForTest(nullptr); + require(ok3 && Has(uiSaid(), L"kept your ignore-files.txt as " + list + L".kept-1"), "SelfUninstall, the list made again meanwhile: done, kept as .kept-1, said"); + cleanAfterExists("SelfUninstall: .kept-1 holds the player's list, the other program's list untouched, the rest went"); + uiLines.clear(); + fill(); + SetPutBackHookForTest(&FileWhereFolderShouldBe); + const bool ok4 = SelfUninstall(ctx, lp, false, L"-", ui, &why); + SetPutBackHookForTest(nullptr); + require(ok4, "SelfUninstall, the list can't be put back: done"); + const wstring ud = checkBlocked(uiSaid(), L".removed-", "SelfUninstall: the list stays in the moved folder (the rest went), said where"); + Sleep(2500); + require(Bytes(Join(ud, L"ignore-files.txt")) == listBytes, "SelfUninstall: nothing deletes the list afterwards"); + cleanAfterBlocked(ud); + SetUninstallHive(nullptr); + RegCloseKey(hive); + } else { + std::printf("convert-ignore: HIVE-SKIPPED: no private registry hive here (error %ld): RemoveSharedData / SelfUninstall cases skipped\n", hr); + } + } + 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 = Cli({L"sha256=" + Lower(Sha256File(a))}); + require(!IgnoreReason(lh, unc).empty(), "an --ignore=sha256= 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 candidate search (and so never which file wins) ----------------------------- + // The search is AllowedModelCandidates, the code FindModel runs before it converts (the conversion needs the embedded + // map, which the CI build has not: this test compares what the search hands to the converter, in its order, whose first + // entry is the file that converts first). No list anywhere is the baseline; an empty list in each place one is read. + { + Context ctx; + const wstring shared = Join(Join(root, L"localappdata"), L"WrongGPU"), portable = Join(root, L"Portable\\WrongGPU-data"); + const wstring sharedList = Join(shared, L"ignore-files.txt"), portableList = Join(portable, L"ignore-files.txt"); + DeleteFileW(sharedList.c_str()); + DeleteFileW(portableList.c_str()); + 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; size_t count; }; + const Set sets[] = {{"chosen file, this game, three library games", &t, chosen, libs, 4}, + {"library games only", nullptr, L"", libs, 3}, + {"library games in the other order", nullptr, L"", rev, 3}, + {"chosen file only", nullptr, chosen, {}, 1}, + {"this game only", &t, L"", {}, 1}, + {"nothing found", nullptr, L"", {}, 0}}; + // the baseline: no list at all (nothing in either well-known place, nothing on the command line) + ctx.ownDataDir = portable; + IgnoreReport baseRep; + const IgnoreLists none = LoadConvertIgnore(ctx, L"model", &baseRep); + require(none.empty() && baseRep.warnings.empty() && none.unreadable.empty(), "no list anywhere: no entry, no warning"); + std::vector base; + for (const Set& s : sets) { + base.push_back(RunSearch(ctx, none, s.t, s.user, s.libs)); + std::vector plain; + for (auto& x : ModelCandidates(s.t, s.user, s.libs)) plain.push_back(x.path); + if (base.back().tried != plain || base.back().tried.size() != s.count || !base.back().ignored.skipped.empty()) { + std::printf("set: %s\n", s.name); + require(false, "with no list the search hands over ModelCandidates unchanged"); + } + ++checks; + } + // an empty list in each place one is read: %LOCALAPPDATA%\WrongGPU, the launcher's own data folder, both, and + // --ignore-list=; each text that is empty (blank, a BOM alone, comments and the version line, blank lines) + const wstring cliEmpty = Join(root, L"cli-empty.txt"); + const string emptyLists[] = {"", "\xEF\xBB\xBF", "# nothing\r\n\r\nwronggpu-ignore-files 1\r\n", "\r\n\r\n"}; + struct Place { const char* name; bool shared, own, cli; }; + const Place places[] = {{"%LOCALAPPDATA%\\WrongGPU\\ignore-files.txt", true, false, false}, {"the launcher's own data folder", false, true, false}, + {"both well-known lists", true, true, false}, {"--ignore-list=", false, false, true}}; + for (const string& listText : emptyLists) { + for (const Place& pl : places) { + DeleteFileW(sharedList.c_str()); + DeleteFileW(portableList.c_str()); + ctx.ignoreListFiles.clear(); + if (pl.shared) File(sharedList, listText); + if (pl.own) File(portableList, listText); + if (pl.cli) { File(cliEmpty, listText); ctx.ignoreListFiles = {cliEmpty}; } + IgnoreReport rep; + const IgnoreLists l = LoadConvertIgnore(ctx, L"model", &rep); + if (!l.empty() || !rep.warnings.empty() || !l.unreadable.empty()) { std::printf("place: %s\n", pl.name); require(false, "an empty list file: no entry, no warning, readable"); } + for (size_t i = 0; i < std::size(sets); ++i) { + const Search got = RunSearch(ctx, l, sets[i].t, sets[i].user, sets[i].libs); + const bool sameWinner = got.tried.empty() == base[i].tried.empty() && (got.tried.empty() || got.tried.front() == base[i].tried.front()); + if (got.tried != base[i].tried || !sameWinner || !got.ignored.skipped.empty() || !got.chosenSkipped.empty()) { + std::printf("set: %s, empty list in %s\n", sets[i].name, pl.name); + require(false, "an empty list leaves the candidates, their order and the winner as they were"); + } + ++checks; + } + } + } + ctx.ignoreListFiles.clear(); + DeleteFileW(sharedList.c_str()); + DeleteFileW(portableList.c_str()); + // the search does change with an entry (the test can fail): the first candidate listed, the next one is the winner + File(sharedList, U8(chosen) + "\r\n"); + IgnoreReport rep1; + const IgnoreLists one = LoadConvertIgnore(ctx, L"model", &rep1); + const Search s1 = RunSearch(ctx, one, &t, chosen, libs); + require(one.entries.size() == 1 && s1.tried.size() == 3 && s1.tried.front() == base[0].tried[1] && s1.ignored.skipped.size() == 1 && !s1.chosenSkipped.empty(), + "a list that names the chosen file: it is left out (and said), the next candidate is the first tried"); + // and a list whose entries match none of them changes nothing either: another path, a path whose hash is not the file's + File(sharedList, U8(Join(root, L"Gone\\nvngx_dlss.dll")) + "\r\n" + U8(a) + " sha256=" + string(64, '0') + "\r\n"); + IgnoreReport rep2; + const IgnoreLists nomatch = LoadConvertIgnore(ctx, L"model", &rep2); + const Search s2 = RunSearch(ctx, nomatch, &t, chosen, libs); + require(nomatch.entries.size() == 2 && rep2.warnings.empty() && s2.tried == base[0].tried && s2.ignored.skipped.empty(), "a list that names none of the candidates keeps them all"); + DeleteFileW(sharedList.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..d3613e5a --- /dev/null +++ b/clean/installer/tests/run_convert_ignore_tests.py @@ -0,0 +1,941 @@ +#!/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 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 named_log_has(message, prefix, count): + """The log a player's message names ("... is in the log: .") exists and holds at least `count` lines with `prefix`.""" + at = message.find("is in the log: ") + if at < 0: + return False + rest = message[at + len("is in the log: "):] + end = rest.lower().find(".log") + path = rest[:end + 4] if end >= 0 else "" + return bool(path) and os.path.isfile(path) and read(path).decode("utf-8", "replace").count(prefix) >= count + + +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:])) + t.check("...the message itself names the first file skipped with its list and line, and points to the log for the other two", + ("Skipped: " + s["skipped"][0] + ".") in s["error"] and (shared_list + ", line 1").lower() in s["error"].lower() and + "2 other files were skipped too" in s["error"] and named_log_has(s["error"], "model: skipped ", len(s["skipped"])) and + "remove that line" in s["error"], (s["error"], s["skipped"])) + del before + + # a hash alone is not a list entry (it would skip every identical copy, the player's own too): warned, nothing skipped + 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 alone in a list: warned (list, line, why), skipped; nothing is left out, A converts", + s["code"] == 0 and source_of(s).lower() == a.lower() and s["skipped"] == [] and len(s["warnings"]) == 1 and + (shared_list + ", line 1").lower() in s["warnings"][0].lower() and "hash alone is not accepted in a list" in s["warnings"][0], (s["code"], source_of(s), s["skipped"], s["warnings"])) + # --ignore=sha256= (the command line only): the file is skipped wherever it is, under any name + t.set_lists(None, None) + s = convert_model(t, lib, extra=("--ignore=sha256=" + sha_a,)) + t.check("--ignore=sha256=: A (that hash) skipped, B converted; the reason names the argument and 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] and "--ignore=sha256=" 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, extra=("--ignore=sha256=" + sha_a,)) + 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 path + hash entry matches only when BOTH match; an identical copy under another path is still converted + t.set_lists("%s sha256=%s\r\n" % (a, sha_a), None) + s = convert_model(t, lib) + t.check("a path + hash entry (both match): A skipped, B converted; the reason names the path and the hash", + s["code"] == 0 and source_of(s).lower() == b.lower() and len(s["skipped"]) == 1 and "(path %s; SHA-256 %s)" % (a, sha_a) in s["skipped"][0], (s["code"], source_of(s), s["skipped"])) + s = convert_model(t, lib, model=copy) + t.check("...the same bytes at another path (--model=): NOT skipped by the path + hash entry, converted (it is the first candidate)", + s["code"] == 0 and source_of(s).lower() == copy.lower() and len(s["skipped"]) == 1 and s["skipped"][0].lower().startswith(a.lower()), (s["code"], source_of(s), s["skipped"])) + t.set_lists("%s sha256=%s\r\n" % (a, hashlib.sha256(with_f2.file).hexdigest()), None) + s = convert_model(t, lib) + t.check("a path + hash entry whose hash is not the file's: nothing skipped, A converts", s["code"] == 0 and source_of(s).lower() == a.lower() and s["skipped"] == [], + (s["code"], source_of(s), s["skipped"])) + # two identical stand-ins in two folders, only one listed by path + hash: the other is still converted + twin_lib, (t1, t2) = library(t, "twins", [("Twin One", (DLL, with_f.file)), ("Twin Two", (DLL, with_f.file))]) + t.set_lists("%s sha256=%s\r\n" % (t1, sha_a), None) + s = convert_model(t, twin_lib) + t.check("two identical files, one listed by path + hash: the listed one is skipped, the identical other one is converted", + s["code"] == 0 and source_of(s).lower() == t2.lower() and len(s["skipped"]) == 1 and s["skipped"][0].lower().startswith(t1.lower()), (s["code"], source_of(s), s["skipped"])) + t.set_lists("%s\r\n" % t1, None) + s = convert_model(t, twin_lib, extra=("--ignore=" + t2,)) + t.check("...and both listed (by path, one from the command line): exit 5, nothing converted", s["code"] == 5 and s["model"] == b"" and len(s["skipped"]) == 2, (s["code"], s["error"], s["skipped"])) + + # a folder entry is warned about like any bad line (it would ignore nothing) + t.set_lists("%s\r\n%s\r\n" % (os.path.dirname(a), a), None) + s = convert_model(t, lib) + t.check("a folder in a list: warned (list, line, folder), the file line after it still used (A skipped, B converted)", + s["code"] == 0 and source_of(s).lower() == b.lower() and len(s["warnings"]) == 1 and "folder" in s["warnings"][0] and (shared_list + ", line 1").lower() in s["warnings"][0].lower(), + (s["code"], source_of(s), s["warnings"])) + + # 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("a bare --ignore (no '='): exit 10, nothing converted; it does not take the next argument as its value", + s["code"] == 10 and s["model"] == b"" and "--ignore needs its entry after an equals sign" in s["error"], (s["code"], s["error"])) + s = convert_model(t, lib, extra=("--ignore-list",)) + t.check("a bare --ignore-list followed by another option: exit 10, nothing converted; it does not take that option as its file", + s["code"] == 10 and s["model"] == b"" and "--ignore-list needs its file" in s["error"], (s["code"], s["error"])) + s = convert_model(t, lib, extra=("--ignore-list", cli_list)) + t.check("a bare --ignore-list followed by a file (no '='): exit 10, nothing converted; the file is not taken as its value", + s["code"] == 10 and s["model"] == b"" and "--ignore-list needs its file" in s["error"], (s["code"], s["error"])) + # --ignore / --ignore-list are applied by model convert and rr-data convert only: elsewhere exit 10 and a message + for verb_args in (["scan"], ["model", "status"], ["rr-data", "status"], ["hipcheck"]): + for arg in ("--ignore=" + b, "--ignore-list=" + cli_list): + t.clear_models() + code = t.run(*(verb_args + [arg])) + t.check("%s %s: exit 10, not run, the message says where they apply" % (" ".join(verb_args), arg.split("=")[0]), + code == 10 and "apply only to" in t.j.get("error", "") and "apply only to" in t.stdout, (code, t.j.get("error"), t.stdout[-300:])) + # 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("