diff --git a/d3d12/README.md b/d3d12/README.md index 7f149eae5e..d730b36e59 100644 --- a/d3d12/README.md +++ b/d3d12/README.md @@ -330,7 +330,14 @@ still reaches. cap, ray tracing, Smooth Motion, console profile, graphics card, and the speed switches. Lines marked LIVE change at once; the others are saved to `leonida-settings.txt` and apply at the next start, which "SAVE AND RESTART - NOW" does for you. F3 shows the numbers panel. + NOW" does for you. F3 shows the numbers panel. The last choice wins, one + setting at a time: a switch you type (`-Resolution`, `-SeriesS`, + `-Fullscreen` ...) wins over what the menu saved for the same setting, the + menu file drops that value, and the menu shows the command line's; every + setting you did not type keeps the menu's. "SAVE AND RESTART NOW" keeps the + menu's values, since they were chosen after the switches it starts with + again. Each start says which values came from the menu and which the command + line replaced. - **DLSS frame generation** (NVIDIA Streamline), 2x to 6x, from the menu. The amount changes while playing. Frames are only generated while the game's window has the focus. diff --git a/d3d12/crates/leonida-gpu/src/host/menu.rs b/d3d12/crates/leonida-gpu/src/host/menu.rs index b8f63af88f..b60bae46db 100644 --- a/d3d12/crates/leonida-gpu/src/host/menu.rs +++ b/d3d12/crates/leonida-gpu/src/host/menu.rs @@ -57,7 +57,12 @@ const ITEMS: &[Item] = &[ Item { var: "LEONIDA_UPSCALE", label: "UPSCALER", values: &[("dlss", "DLSS"), ("stretch", "STRETCH"), ("", "GAME OWN (S: FSR SLOW)")], live: false }, // DLSS's mode is its render height: of the output's lines 2/3 (quality), 0.6 (balanced), 1/2 (performance), 1/3 // (ultra performance). Unset: Series S renders its own 2/3; the launcher gives Series X 2/3 with DLSS. - Item { var: "LEONIDA_RENDER_SCALE", label: "RENDER SCALE (DLSS MODE)", values: &[("", "GAME"), ("0.6667", "0.667 QUALITY"), ("0.6", "0.6 BALANCED"), ("0.5", "0.5 PERFORMANCE"), ("0.3333", "0.333 ULTRA PERF."), ("0.75", "0.75")], live: false }, + // 1 is what -Resolution makes at the output's own height (1440 on Series X). On Series S it ended the title at + // its own breakpoint on the loading screen (2026-12-01, RTX 3060), hence the note. + Item { var: "LEONIDA_RENDER_SCALE", label: "RENDER SCALE (DLSS MODE)", values: &[("", "GAME"), ("0.6667", "0.667 QUALITY"), ("0.6", "0.6 BALANCED"), ("0.5", "0.5 PERFORMANCE"), ("0.3333", "0.333 ULTRA PERF."), ("0.75", "0.75"), ("1", "1 NATIVE (SERIES X)")], live: false }, + // The lowest scale of the title's own dynamic resolution (-Lowest; the loader's hooks.rs): it picks the scale + // above it by itself. The render scale above wins over it when both are set. + Item { var: "LEONIDA_RENDER_SCALE_MIN", label: "LOWEST SCALE (DYNAMIC RES)", values: &[("", "GAME"), ("0.75", "0.75 (1080 OF 1440)"), ("0.6667", "0.667"), ("0.5", "0.5")], live: false }, // On Series X the launcher turns DLSS on with it: the generator's depth and motion vectors come from that upscale. Item { var: "LEONIDA_FRAME_GEN", label: "FRAME GEN (NOT WITH RAY TR.)", values: &[("", "OFF"), ("2", "2X"), ("3", "3X"), ("4", "4X"), ("5", "5X"), ("6", "6X (MOST)")], live: false }, // Leonida: AMD's FSR 3 frame generation (crate::fsr), 2X, for cards without DLSS frame generation (RTX 20 and 30). diff --git a/d3d12/scripts/play-nvidia.ps1 b/d3d12/scripts/play-nvidia.ps1 index 298e9f6b98..dfa371e08c 100644 --- a/d3d12/scripts/play-nvidia.ps1 +++ b/d3d12/scripts/play-nvidia.ps1 @@ -6,7 +6,8 @@ # nvidia-sdk\. Without them the game runs with its picture stretched to the screen instead. # The layer is built here if it is not built yet, and built again with DLSS once those files are there. # In the game: F1 = settings (arrows pick and change, Enter saves; kept in leonida-settings.txt next to this -# folder), F3 = the numbers panel. +# folder), F3 = the numbers panel. The last choice wins, one setting at a time: a switch typed for a start wins over +# the menu's saved value of what it sets (and the menu file drops that value); every other setting keeps the menu's. # -NoSmoothMotion run target\\leonida.exe itself. By default a copy named leonida_sm.exe is started instead, # so a Smooth Motion profile set for that exe in the NVIDIA App applies every time. # -NoDlss no DLSS: the picture is stretched (fast, softer) @@ -89,7 +90,12 @@ param( [hashtable]$More = @{}, [string]$PcGlyphs, [uint32]$WindowsBarMB, - [switch]$DryRun + [switch]$DryRun, + # The switches the user typed, by this script's names, when another script starts this one with switches of its + # own (scripts\play-d3d12.ps1's Series X default): they win over the F1 menu's saved values. Unset: this start's. + [string[]]$Typed, + # Set by the F1 menu's SAVE AND RESTART: the switches are the last start's again, so the menu's values win. + [switch]$MenuRestart ) $root = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path if (-not (Test-Path $Exe)) { Write-Host "No $Exe."; return } @@ -507,16 +513,98 @@ if ($PSBoundParameters.ContainsKey('WindowsBarMB')) { } } -# What the F1 menu saved (leonida-settings.txt, git-ignored), over the settings above. An empty value unsets one. +# What the F1 menu saved (leonida-settings.txt, git-ignored) and the command line: the user's last choice wins, one +# setting at a time. A setting a switch typed for this start makes (-Resolution, -SeriesS, -Fullscreen ...: $Typed, +# which scripts\play-d3d12.ps1 passes, else this start's own switches) was chosen now: it wins over the menu's, and +# the menu file drops that value, so F1 shows and saves the command line's. Every other setting keeps the menu's, which +# was chosen after the defaults. A start that the menu's SAVE AND RESTART made (-MenuRestart) replays the switches of +# the start before, so there the menu's values win: they were chosen after them. An empty value unsets a setting. +# -More wins over the menu like a typed switch. +function Read-SettingsFile([string]$path) { + $read = [ordered]@{} + if (Test-Path $path) { + foreach ($line in Get-Content $path) { + if ($line -match '^\s*(LEONIDA_[A-Z0-9_]+)\s*=(.*)$') { $read[$Matches[1]] = $Matches[2].Trim() } + } + } + $read +} +# The settings each switch makes; a switch not listed makes none the menu holds. +$switchSettings = @{ + SeriesX = 'LEONIDA_MENU_CONSOLE' + NoSmoothMotion = 'LEONIDA_MENU_SMOOTH_MOTION' + Resolution = 'LEONIDA_RENDER_SCALE', 'LEONIDA_RENDER_SCALE_MIN', 'LEONIDA_OWN_UPSCALER', 'LEONIDA_UPSCALE', 'LEONIDA_FSR_UPSCALER', 'LEONIDA_OUTPUT' + Lowest = 'LEONIDA_RENDER_SCALE', 'LEONIDA_RENDER_SCALE_MIN', 'LEONIDA_OWN_UPSCALER', 'LEONIDA_UPSCALE', 'LEONIDA_FSR_UPSCALER', 'LEONIDA_OUTPUT' + Dlss = 'LEONIDA_RENDER_SCALE', 'LEONIDA_RENDER_SCALE_MIN', 'LEONIDA_UPSCALE', 'LEONIDA_OWN_UPSCALER', 'LEONIDA_FSR_UPSCALER', 'LEONIDA_OUTPUT' + NoDlss = 'LEONIDA_UPSCALE' + # DLSS's and FSR 3's frame generation are one choice (never both, below): a typed one replaces the menu's other. + FrameGen = 'LEONIDA_FRAME_GEN', 'LEONIDA_FSR_FRAMEGEN' + NoFrameGen = 'LEONIDA_FRAME_GEN' + FsrFrameGen = 'LEONIDA_FSR_FRAMEGEN', 'LEONIDA_FRAME_GEN' + Fullscreen = 'LEONIDA_FULLSCREEN' + Aspect = 'LEONIDA_ASPECT' + PresentScale = 'LEONIDA_PRESENT_SCALE' + Adapter = 'LEONIDA_ADAPTER' + RayTracing = 'LEONIDA_BVH_BUILDER', 'LEONIDA_AMBIENT' + Quality = 'LEONIDA_RELAY', 'LEONIDA_CARD_STEPS' +} +$typedNames = if ($PSBoundParameters.ContainsKey('Typed')) { @($Typed) } else { @($PSBoundParameters.Keys) } +$chosenNow = @{} +if (-not $MenuRestart) { + foreach ($name in $typedNames) { + # -SeriesS reaches this script as the console's switch, SeriesX. + # play-d3d12.ps1's -SeriesS and -SmoothMotion reach this script as SeriesX and NoSmoothMotion. + $said = switch ($name) { + 'SeriesX' { '-SeriesS/-SeriesX' } + 'NoSmoothMotion' { '-SmoothMotion/-NoSmoothMotion' } + default { "-$name" } + } + foreach ($k in @($switchSettings[$name])) { if ($k) { $chosenNow[$k] = $said } } + } + foreach ($k in $More.Keys) { $chosenNow[$k] = '-More' } +} $menuFile = Join-Path $root 'leonida-settings.txt' $smooth = -not $NoSmoothMotion -if (Test-Path $menuFile) { - foreach ($line in Get-Content $menuFile) { - if ($line -notmatch '^\s*(LEONIDA_[A-Z0-9_]+)\s*=(.*)$') { continue } - $k = $Matches[1]; $v = $Matches[2].Trim() - if ($k -eq 'LEONIDA_MENU_SMOOTH_MOTION') { $smooth = $v -ne '0'; continue } - if ($k -eq 'LEONIDA_MENU_CONSOLE') { if ($v) { $console = $v }; continue } - if ($v) { $settings[$k] = $v } else { $settings.Remove($k) } +# The menu's console and Smooth Motion lines stand for $console and $smooth here. +$fromLine = @{} + $settings +$fromLine.LEONIDA_MENU_CONSOLE = $console +$fromLine.LEONIDA_MENU_SMOOTH_MOTION = if ($smooth) { '1' } else { '0' } +# -More is applied over the settings below; its values are what replaces the menu's. +foreach ($k in $More.Keys) { $fromLine[$k] = "$($More[$k])" } +$menu = Read-SettingsFile $menuFile +$menuRead = $menu.Count +$fromMenu = @(); $replaced = @() +foreach ($k in @($menu.Keys)) { + $v = $menu[$k] + if ($chosenNow.ContainsKey($k)) { + $now = "$($fromLine[$k])" + if ($now -ne $v) { $replaced += "$k $(if ($v) { $v } else { '(unset)' }) -> $(if ($now) { $now } else { '(unset)' }) ($($chosenNow[$k]))" } + $menu.Remove($k) + continue + } + $fromMenu += "$k=$(if ($v) { $v } else { '(unset)' })" + if ($k -eq 'LEONIDA_MENU_SMOOTH_MOTION') { $smooth = $v -ne '0'; continue } + if ($k -eq 'LEONIDA_MENU_CONSOLE') { if ($v) { $console = $v }; continue } + if ($v) { $settings[$k] = $v } else { $settings.Remove($k) } +} +if ($fromMenu) { Write-Host "F1 menu settings (leonida-settings.txt, for what the command line did not set): $($fromMenu -join ', ')" } +if ($replaced) { Write-Host "Command line over the F1 menu's saved values: $($replaced -join ', ')" } +# The menu file keeps the rest as it was; a value the command line set leaves it. If the file cannot be written, this +# start still runs with the command line's values and the file stays whole, so nothing saved is lost. +if (-not $DryRun -and $menu.Count -ne $menuRead) { + $tmp = "$menuFile.tmp" + $wrote = $false + try { + # CRLF, as the menu writes the file (host/menu.rs). + $text = "# Saved by the F1 menu; play.ps1 applies these after its own settings (an empty value: unset).`r`n" + + (($menu.Keys | ForEach-Object { "$_=$($menu[$_])`r`n" }) -join '') + [IO.File]::WriteAllText($tmp, $text, [Text.UTF8Encoding]::new($false)) + $wrote = $true + Move-Item -LiteralPath $tmp -Destination $menuFile -Force -ErrorAction Stop + } catch { + # Only the file this start wrote goes: whatever stood at that name before is left alone. + if ($wrote) { Remove-Item -LiteralPath $tmp -ErrorAction SilentlyContinue } + Write-Host "The F1 menu file was not updated ($($_.Exception.Message)): this start runs with the command line's values, and the file keeps the menu's." } } # Leonida: the runs write down the pipelines the title creates (pipeline/known.rs), for the precompile below. @@ -591,13 +679,17 @@ $settings.LEONIDA_MENU_CONSOLE = $console $settings.LEONIDA_MENU_FILE = $menuFile $settings.LEONIDA_MENU_PLAY = $MyInvocation.MyCommand.Path $again = @("-Exe '$($Exe -replace "'", "''")'", "-Build $Build") -foreach ($s in 'NoSmoothMotion', 'MotionBlur', 'NoDlss', 'SeriesX', 'Quality', 'RayTracing', 'FsrFrameGen', 'NoPrecompile', 'Log', 'Capture', 'Fullscreen') { +foreach ($s in 'NoSmoothMotion', 'MotionBlur', 'NoDlss', 'NoFrameGen', 'SeriesX', 'Quality', 'RayTracing', 'FsrFrameGen', 'NoPrecompile', 'Log', 'Capture', 'Fullscreen') { if ($PSBoundParameters[$s]) { $again += "-$s" } } foreach ($s in 'Resolution', 'Lowest', 'Dlss', 'FrameGen', 'Aspect', 'PresentScale', 'Adapter', 'DlssDir', 'StreamlineDir', 'FidelityFxDir') { if ($PSBoundParameters[$s]) { $again += "-$s '$($PSBoundParameters[$s] -replace "'", "''")'" } } if ($PSBoundParameters.ContainsKey('WindowsBarMB')) { $again += "-WindowsBarMB $WindowsBarMB" } +# The menu's SAVE AND RESTART starts this script again with these switches, the ones this start was given (not the +# settings they made), and -MenuRestart: the menu's values, saved after these switches were typed, win over them there +# (see "What the F1 menu saved" above). Without it a typed -Resolution would undo the scale the menu had just saved. +$again += '-MenuRestart' $settings.LEONIDA_MENU_ARGS = $again -join ' ' $scaled = if ($settings.LEONIDA_RENDER_SCALE) { " from render scale $($settings.LEONIDA_RENDER_SCALE)" } else { '' } diff --git a/docs/mod-menu.md b/docs/mod-menu.md index 9ac70e56f0..da9f4dfbdf 100644 --- a/docs/mod-menu.md +++ b/docs/mod-menu.md @@ -119,7 +119,10 @@ The cap, the sharpness, the panel and the recorder are the renderer's own settin (`knobs::Owned`). The first three are in the F1 menu too: the two menus change the same thing, and each shows what the other set (the F1 menu reads its live settings again when it opens; the trainer looks twice a second while open). The F1 menu saves to `leonida-settings.txt`; what the trainer changes lasts for the run unless the F1 menu -is opened afterwards and saves it. +is opened afterwards and saves it. At the next start the launcher applies the saved settings, except those a switch +typed for that start sets (`-Resolution`, `-SeriesS` ...): those take the command line's value and leave the menu +file, so the last choice wins either way. The menu's SAVE AND RESTART passes `-MenuRestart` with the switches it +replays, and there the menu's values win. Game speed is experimental and not verified in game: the runtime serves the title's `QueryPerformanceCounter` import (from `api-ms-win-core-profile-l1-1-0.dll`) and scales it continuously, so the title's time never jumps. diff --git a/scripts/play-d3d12.ps1 b/scripts/play-d3d12.ps1 index 70cb328ecf..caaa53736a 100644 --- a/scripts/play-d3d12.ps1 +++ b/scripts/play-d3d12.ps1 @@ -112,4 +112,9 @@ if ($Resolution) { $launch.Resolution = $Resolution } if ($Lowest) { $launch.Lowest = $Lowest } if ($Adapter) { $launch.Adapter = $Adapter } if ($PcGlyphs) { $launch.PcGlyphs = (Resolve-Path $PcGlyphs).Path } +# The switches typed here, by play-nvidia.ps1's names: those win over the F1 menu's saved values, while the Series X +# default and its -Lowest 1080 above, which nobody typed, leave the menu's choice standing. +$launch.Typed = @($PSBoundParameters.Keys | ForEach-Object { + switch ($_) { 'SeriesS' { 'SeriesX' } 'SmoothMotion' { 'NoSmoothMotion' } default { $_ } } +}) & (Join-Path $runtime "scripts\play-nvidia.ps1") @launch diff --git a/tests/test_play_nvidia_menu_choice.py b/tests/test_play_nvidia_menu_choice.py new file mode 100644 index 0000000000..128608acec --- /dev/null +++ b/tests/test_play_nvidia_menu_choice.py @@ -0,0 +1,205 @@ +"""d3d12/scripts/play-nvidia.ps1: the F1 menu's saved settings and the command line, the user's last choice winning +one setting at a time (D3D12-MENU-LAST-CHOICE-WINS): a typed switch wins over the menu's value of what it sets, the +menu file drops that value, every other setting keeps the menu's, and the menu's SAVE AND RESTART (-MenuRestart) +keeps the menu's. The launcher's block is run alone in Windows PowerShell with the settings a command line made, +against a menu file in a temporary folder.""" + +import json +import os +import subprocess +import tempfile +import unittest +from pathlib import Path + +SCRIPT = Path(__file__).resolve().parents[1] / "d3d12" / "scripts" / "play-nvidia.ps1" +BEGIN = "# What the F1 menu saved (leonida-settings.txt" +END = "# Leonida: the runs write down the pipelines" + + +def block() -> str: + text = SCRIPT.read_text(encoding="utf-8-sig") + return text[text.index(BEGIN):text.index(END)] + + +@unittest.skipUnless(os.name == "nt", "Windows PowerShell launcher") +class MenuChoiceTests(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.root = Path(self.tmp.name) + self.menu = self.root / "leonida-settings.txt" + + def tearDown(self): + self.tmp.cleanup() + + def start(self, made: dict, typed=None, console="series-s", more=None, restart=False, dry=False, + direct=None) -> dict: + """One start: `made` is what the command line made, `typed` the switches typed (play-d3d12.ps1's -Typed), + `direct` the switches given when play-nvidia.ps1 is started by hand (no -Typed).""" + pairs = "; ".join(f"'{k}' = '{v}'" for k, v in made.items()) + more_pairs = "; ".join(f"'{k}' = '{v}'" for k, v in (more or {}).items()) + driver = self.root / "driver.ps1" + driver.write_text( + "param([string[]]$Typed, [switch]$MenuRestart, [string]$Resolution, [switch]$Fullscreen)\n" + "$ErrorActionPreference = 'Stop'\n" + f"$root = '{self.root}'\n" + f"$settings = @{{ {pairs} }}\n" + f"$More = @{{ {more_pairs} }}\n" + f"$console = '{console}'\n" + "$NoSmoothMotion = $true\n" + f"$DryRun = ${'true' if dry else 'false'}\n" + + block() + + "\n[pscustomobject]@{ settings = $settings; console = $console; smooth = $smooth } | ConvertTo-Json -Compress\n", + encoding="utf-8-sig", + ) + args = [] + if typed is not None: + args += ["-Typed", ",".join(typed) if typed else "''"] + if restart: + args.append("-MenuRestart") + args += direct or [] + command = f"& '{driver}' {' '.join(args)}" + res = subprocess.run( + ["powershell.exe", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", command], + capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=60, + ) + self.assertEqual(res.returncode, 0, res.stderr) + out = res.stdout.strip().splitlines() + ran = json.loads(out[-1]) + ran["said"] = "\n".join(out[:-1]) + return ran + + def saved(self) -> dict: + lines = self.menu.read_text(encoding="utf-8").splitlines() if self.menu.exists() else [] + return dict(l.split("=", 1) for l in lines if l.startswith("LEONIDA_")) + + def test_a_typed_switch_wins_over_the_menu_and_the_menu_file_follows(self): + self.menu.write_text("LEONIDA_RENDER_SCALE=0.6\r\nLEONIDA_FPS_LINE=0\r\n", encoding="utf-8") + ran = self.start({"LEONIDA_RENDER_SCALE": "1", "LEONIDA_OWN_UPSCALER": "1"}, typed=["Resolution"]) + self.assertEqual(ran["settings"]["LEONIDA_RENDER_SCALE"], "1") + self.assertEqual(ran["settings"]["LEONIDA_FPS_LINE"], "0", "a setting no switch made keeps F1's") + self.assertEqual(self.saved(), {"LEONIDA_FPS_LINE": "0"}) + self.assertIn("LEONIDA_RENDER_SCALE 0.6 -> 1 (-Resolution)", ran["said"]) + + def test_the_same_switch_typed_again_after_a_menu_save_wins_again(self): + self.start({"LEONIDA_RENDER_SCALE": "1"}, typed=["Resolution"]) + self.menu.write_text("LEONIDA_RENDER_SCALE=0.6\r\n", encoding="utf-8") + ran = self.start({"LEONIDA_RENDER_SCALE": "1"}, typed=["Resolution"]) + self.assertEqual(ran["settings"]["LEONIDA_RENDER_SCALE"], "1") + self.assertEqual(self.saved(), {}) + + def test_a_default_nobody_typed_keeps_the_menu_value(self): + # play-d3d12.ps1's Series X default with its -Lowest 1080: nothing typed. + self.menu.write_text("LEONIDA_MENU_CONSOLE=series-s\r\nLEONIDA_RENDER_SCALE_MIN=0.6667\r\n", encoding="utf-8") + ran = self.start({"LEONIDA_RENDER_SCALE_MIN": "0.75"}, typed=[], console="series-x") + self.assertEqual(ran["console"], "series-s") + self.assertEqual(ran["settings"]["LEONIDA_RENDER_SCALE_MIN"], "0.6667") + self.assertIn("F1 menu settings", ran["said"]) + # -SeriesS typed (passed on as SeriesX, the console's switch) wins over a menu's Series X. + self.menu.write_text("LEONIDA_MENU_CONSOLE=series-x\r\n", encoding="utf-8") + self.assertEqual(self.start({}, typed=["SeriesX"], console="series-s")["console"], "series-s") + self.assertEqual(self.saved(), {}) + + def test_a_setting_no_switch_makes_is_never_taken_from_the_menu(self): + self.menu.write_text("LEONIDA_CARD_AWAKE=1\r\n", encoding="utf-8") + ran = self.start({"LEONIDA_CARD_AWAKE": "0", "LEONIDA_RENDER_SCALE": "1"}, typed=["Resolution", "Log"]) + self.assertEqual(ran["settings"]["LEONIDA_CARD_AWAKE"], "1") + self.assertEqual(self.saved(), {"LEONIDA_CARD_AWAKE": "1"}) + self.assertNotIn("Command line over", ran["said"]) + + def test_the_menus_restart_keeps_the_values_it_saved(self): + self.menu.write_text("LEONIDA_RENDER_SCALE=0.6\r\n", encoding="utf-8") + before = self.menu.read_bytes() + ran = self.start({"LEONIDA_RENDER_SCALE": "1"}, restart=True, direct=["-Resolution", "1080"]) + self.assertEqual(ran["settings"]["LEONIDA_RENDER_SCALE"], "0.6") + self.assertEqual(self.menu.read_bytes(), before) + + def test_a_start_by_hand_counts_its_own_switches(self): + self.menu.write_text("LEONIDA_RENDER_SCALE=0.6\r\nLEONIDA_FULLSCREEN=\r\n", encoding="utf-8") + ran = self.start({"LEONIDA_RENDER_SCALE": "1", "LEONIDA_FULLSCREEN": "1"}, + direct=["-Resolution", "1080", "-Fullscreen"]) + self.assertEqual(ran["settings"]["LEONIDA_RENDER_SCALE"], "1") + self.assertEqual(ran["settings"]["LEONIDA_FULLSCREEN"], "1") + self.assertEqual(self.saved(), {}) + + def test_more_wins_over_the_menu_like_a_typed_switch(self): + self.menu.write_text("LEONIDA_PROMPTS=playstation\r\nLEONIDA_PANEL=1\r\n", encoding="utf-8") + ran = self.start({}, typed=["More"], more={"LEONIDA_PROMPTS": "xbox"}) + self.assertEqual(self.saved(), {"LEONIDA_PANEL": "1"}) + self.assertIn("LEONIDA_PROMPTS playstation -> xbox (-More)", ran["said"]) + # The same value from -More replaces nothing worth reporting; an empty menu value set by -More is reported. + self.menu.write_text("LEONIDA_PANEL=1\r\nLEONIDA_FPS_LINE=\r\n", encoding="utf-8") + ran = self.start({}, typed=["More"], more={"LEONIDA_PANEL": "1", "LEONIDA_FPS_LINE": "0"}) + self.assertNotIn("LEONIDA_PANEL", ran["said"]) + self.assertIn("LEONIDA_FPS_LINE (unset) -> 0 (-More)", ran["said"]) + self.assertEqual(self.saved(), {}) + + def test_a_typed_dlss_mode_replaces_the_menus_lowest_scale(self): + self.menu.write_text("LEONIDA_RENDER_SCALE_MIN=0.5\r\n", encoding="utf-8") + ran = self.start({"LEONIDA_UPSCALE": "dlss"}, typed=["Dlss"]) + self.assertNotIn("LEONIDA_RENDER_SCALE_MIN", ran["settings"]) + self.assertEqual(self.saved(), {}) + + def test_smooth_motion_typed_in_play_d3d12_is_named_as_typed(self): + self.menu.write_text("LEONIDA_MENU_SMOOTH_MOTION=1\r\n", encoding="utf-8") + ran = self.start({}, typed=["NoSmoothMotion"]) + self.assertIn("(-SmoothMotion/-NoSmoothMotion)", ran["said"]) + + def test_an_empty_menu_value_still_unsets(self): + self.menu.write_text("LEONIDA_PANEL=\r\n", encoding="utf-8") + self.assertNotIn("LEONIDA_PANEL", self.start({"LEONIDA_PANEL": "1"}, typed=[])["settings"]) + + def test_a_dry_run_writes_nothing(self): + self.menu.write_text("LEONIDA_RENDER_SCALE=0.6\r\n", encoding="utf-8") + before = self.menu.read_bytes() + ran = self.start({"LEONIDA_RENDER_SCALE": "1"}, typed=["Resolution"], dry=True) + self.assertEqual(ran["settings"]["LEONIDA_RENDER_SCALE"], "1") + self.assertEqual(self.menu.read_bytes(), before) + + def test_a_menu_file_that_cannot_be_written_keeps_every_saved_value(self): + self.menu.write_text("LEONIDA_RENDER_SCALE_MIN=0.6667\r\nLEONIDA_PANEL=1\r\n", encoding="utf-8") + before = self.menu.read_bytes() + blocker = self.root / "leonida-settings.txt.tmp" + blocker.mkdir() # the new file cannot be written beside it + ran = self.start({"LEONIDA_RENDER_SCALE_MIN": "0.5"}, typed=["Lowest"]) + self.assertEqual(ran["settings"]["LEONIDA_RENDER_SCALE_MIN"], "0.5", "the start still runs with the switch") + self.assertEqual(self.menu.read_bytes(), before) + self.assertIn("was not updated", ran["said"]) + self.assertTrue(blocker.is_dir(), "what stood at the temporary name is not the launcher's to remove") + + + def test_a_typed_frame_generation_replaces_the_menus_other_one(self): + # DLSS's and FSR 3's are one choice: the launcher keeps FSR 3's when both are set, so a saved FSR 3 would + # undo a typed -FrameGen. + self.menu.write_text("LEONIDA_FSR_FRAMEGEN=1\r\nLEONIDA_PANEL=1\r\n", encoding="utf-8") + ran = self.start({"LEONIDA_FRAME_GEN": "2"}, typed=["FrameGen"]) + self.assertEqual(ran["settings"]["LEONIDA_FRAME_GEN"], "2") + self.assertNotIn("LEONIDA_FSR_FRAMEGEN", ran["settings"]) + self.assertEqual(self.saved(), {"LEONIDA_PANEL": "1"}) + self.menu.write_text("LEONIDA_FRAME_GEN=3\r\n", encoding="utf-8") + ran = self.start({"LEONIDA_FSR_FRAMEGEN": "1"}, typed=["FsrFrameGen"]) + self.assertEqual(ran["settings"]["LEONIDA_FSR_FRAMEGEN"], "1") + self.assertNotIn("LEONIDA_FRAME_GEN", ran["settings"]) + self.assertEqual(self.saved(), {}) + + +@unittest.skipUnless(os.name == "nt", "Windows PowerShell launcher") +class MenuRestartArgsTests(unittest.TestCase): + def test_the_restart_replays_every_frame_generation_switch(self): + """The menu's SAVE AND RESTART starts the launcher again with LEONIDA_MENU_ARGS: a switch left out of it is + lost on that start (-NoFrameGen was, and the restart turned frame generation on).""" + with tempfile.TemporaryDirectory() as tmp: + exe = Path(tmp) / "title.exe" + exe.write_bytes(b"") + res = subprocess.run( + ["powershell.exe", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", str(SCRIPT), + "-Exe", str(exe), "-SeriesX", "-Lowest", "1080", "-NoFrameGen", "-FsrFrameGen", "-DryRun"], + capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=120, + ) + self.assertEqual(res.returncode, 0, res.stderr) + args = next(l for l in res.stdout.splitlines() if l.startswith("LEONIDA_MENU_ARGS=")) + for switch in ("-NoFrameGen", "-FsrFrameGen", "-SeriesX", "-Lowest '1080'", "-MenuRestart"): + self.assertIn(switch, args) + + +if __name__ == "__main__": + unittest.main()