//! `victory play`, and `victory` started with no command at all (a double //! click): the title with the settings this fork plays with, so that the //! program alone, put beside the title's `game_scarlett_master.exe`, is all //! there is to start. What `scripts\play-foxhound.ps1` does, built in. //! //! Nothing is written beside the program or into the title's folder: the //! settings the F1 menu saves (`victory-settings.txt`), the logs and NVIDIA's //! files are kept under `%LOCALAPPDATA%\victory`, where the caches and the //! saves already are. //! //! NVIDIA's DLSS and frame generation files are NVIDIA's and are not part of //! this program. Where they are missing, the first start asks once whether to //! fetch them from NVIDIA's own GitHub (`victory get-dlss` does it later); //! without them the title runs with its own upscaling. //! //! AMD's FSR 3 frame generation files are AMD's, likewise: on a card without //! DLSS Frame Generation (or with FSR 3 chosen in F1) the first start asks //! once whether to fetch them from AMD's own GitHub (`victory get-fsr`). //! //! The one thing that goes beside the program: on a PC whose Direct3D 12 runs //! no shader model 6.6 (Windows 10's own stops at 6.5), Microsoft's newer //! runtime, `D3D12\D3D12Core.dll` from Microsoft's NuGet package, after asking //! (`victory get-d3d12`). Windows loads it only from there (main.rs). //! //! MFG Unlocker, someone else's mod that unlocks DLSS Frame Generation on RTX //! 20 and 30 cards, is an option (F1, MFG UNLOCKER; off by default, asked once //! on such a card): on, it is fetched from its author's GitHub release into //! `%LOCALAPPDATA%\victory\mfg-unlocker` and loaded by this program at the //! start (`victory get-mfg` fetches it by hand). It writes its own logs and //! settings beside the program. use std::collections::BTreeMap; use std::io::{IsTerminal, Write}; use std::path::{Path, PathBuf}; use anyhow::{Context, Result}; /// The title's executable, looked for beside this program. const GAME: &str = "game_scarlett_master.exe"; /// `scripts\get-nvidia-sdks.ps1`, run by Windows PowerShell. const FETCH: &str = include_str!("../../../scripts/get-nvidia-sdks.ps1"); /// `scripts\get-amd-fsr.ps1`, run by Windows PowerShell. const FETCH_FSR: &str = include_str!("../../../scripts/get-amd-fsr.ps1"); /// `scripts\get-d3d12.ps1`, run by Windows PowerShell. const FETCH_D3D12: &str = include_str!("../../../scripts/get-d3d12.ps1"); /// The two AMD files FSR 3 frame generation loads. const FSR_FILES: [&str; 2] = ["amd_fidelityfx_loader_dx12.dll", "amd_fidelityfx_framegeneration_dx12.dll"]; /// The settings the title is started with (`scripts\play-foxhound.ps1` has /// the same, with what each is for). const SETTINGS: &[(&str, &str)] = &[ // Submission and copy machinery ("VICTORY_THIN", "1"), ("VICTORY_JOIN_PARTS", "8"), ("VICTORY_RELAY", "2"), ("VICTORY_RELAY_COPY", "1"), ("VICTORY_WIDE_BACK", "1"), ("VICTORY_CARD_REHASH_RUNS", "1"), ("VICTORY_JOIN_COPIES", "1"), ("VICTORY_CARD_STEPS", "2"), ("VICTORY_CARD_AWAKE", "1"), ("VICTORY_CARD_BUFFERS_MIN_KB", "64"), ("VICTORY_INDIRECT_WINDOW", "1"), ("VICTORY_INDIRECT_BATCH", "2"), ("VICTORY_UPLOAD_AHEAD", "1"), ("VICTORY_SHOW_AFTER", "1"), ("VICTORY_MAP_THREADS", "1"), // Shader writes into big write-combined copies stay on the card ("VICTORY_WC_NO_BACK", "1"), ("VICTORY_WC_NO_BACK_MIN_KB", "1024"), // The title's page-mapping copies kept without Windows views ("VICTORY_SOFT_VIEWS", "copies"), // Textures ("VICTORY_TEXTURE_POOL", "1"), ("VICTORY_RIPE_BLOCKS", "0"), ("VICTORY_TEXTURE_NO_WAIT", "1"), ("VICTORY_TEXTURE_CATCHUP", "0"), ("VICTORY_TEXTURE_STAGE_MS", "16"), ("VICTORY_LEVELS_ON_DEMAND", "0"), ("VICTORY_PRELUDE_UPLOADS", "1"), ("VICTORY_SHOW_NEW_LATER", "1"), ("VICTORY_POOL_SMALL", "0"), ("VICTORY_TABLED_REREAD", "1"), ("VICTORY_QUEUE_HOST_TEXTURES", "0"), // Descriptor heaps' records ("VICTORY_HEAP_SYNC_FIRST", "1"), ("VICTORY_SYNC_IN_PLACE", "1"), ("VICTORY_SPANS_QUICK", "1"), ("VICTORY_SYNC_FINE", "1"), ("VICTORY_HEAP_SYNC_AT_SUBMIT", "1"), // Light without the title's ray tracing ("VICTORY_AMBIENT", "0.8"), ("VICTORY_AMBIENT_AIR", "1.5"), ("VICTORY_AMBIENT_SPECULAR", "1"), // Pixel programs keep the console's early depth test ("VICTORY_EARLY_DEPTH", "1"), // Left out: the decal heap's defragmentation and the heat-haze pass ("VICTORY_LEAVE_OUT", "02384a60fda6f2d0,731a39171b568298"), // Pipelines the title makes are written down, for `victory warm` ("VICTORY_PIPELINE_BOOK", "1"), ]; /// Path settings a start keeps from its surroundings; every other /// `VICTORY_...` variable is cleared before the settings are put in (a /// restart from the menu inherits the last run's). const KEPT: &[&str] = &[ "VICTORY_CONFIG", "VICTORY_GAME_DIR", "VICTORY_CACHE_DIR", "VICTORY_DATA_DIR", "VICTORY_STREAMLINE_DIR", "VICTORY_FIDELITYFX_DIR", "VICTORY_LOG", ]; const FG_TYPES: [&str; 3] = ["auto", "dlss", "fsr"]; const RESOLUTIONS: [&str; 3] = ["1440", "1080to1440", "4k"]; const DLSS_MODES: [&str; 5] = ["off", "dlaa", "quality", "balanced", "performance"]; #[derive(clap::Args, Clone, Debug)] pub(crate) struct Play { /// The title's executable. Without it: `game_scarlett_master.exe` beside /// this program, else in the current folder. exe: Option, /// The Series S profile (1280x720 inside: faster, softer). #[arg(long)] series_s: bool, /// 1440: output and scene 2560x1440; 1080to1440: scene 1920x1080 /// upscaled by the title; 4k: the stock 3840x2160 output. #[arg(long, default_value = "1440", value_parser = RESOLUTIONS)] res: String, /// DLSS in place of the title's upscaling (needs NVIDIA's files). #[arg(long, default_value = "quality", value_parser = DLSS_MODES)] dlss: String, /// DLSS frame generation on from the start: frames shown for each one /// rendered, 2 to 6. Without it, it is ready and the F1 menu switches it. #[arg(long, value_parser = clap::value_parser!(u32).range(2..=6))] framegen: Option, /// Frame generation not made ready at the start. #[arg(long)] no_framegen_ready: bool, /// Which frame generation: auto (DLSS frame generation where the card has /// it, RTX 40 and later, else AMD FSR 3), dlss, or fsr (2X at most). FSR 3 /// needs AMD's FidelityFX DLLs (`VICTORY_FIDELITYFX_DIR`, else a /// `fidelityfx` folder beside this program) and, on Series X, DLSS. #[arg(long, default_value = "auto", value_parser = FG_TYPES)] fg_type: String, /// Frames a second at most (30: the title's own rate; 1000: no cap). #[arg(long, default_value_t = 1000, value_parser = clap::value_parser!(u32).range(1..=1000))] fps: u32, /// Add or override a switch, as often as needed: `--set VICTORY_X=1`. #[arg(long = "set", value_name = "NAME=VALUE")] more: Vec, /// Make every shader before the title starts (`victory preload`, as the /// F1 menu's "PRELOAD SHADERS" does): a while, maybe long. #[arg(long)] pub(crate) preload: bool, } /// What a preload is, said before it begins. pub(crate) fn explain_preload() { println!(); println!("Preloading shaders: every shader of the title is translated, then the pipelines the game"); println!("made in earlier runs are built, and those it needs while loading are stored where it loads"); println!("them from at the start, so it does not stop to make them while you play."); println!("This takes a while, and possibly a long time, depending on your hardware (CPU above all)."); println!("What is done is kept: stopped (close this window), the next preload goes on from there."); println!("A later update of this build, or a new graphics driver, may need a new preload."); println!(); } /// Runs `victory preload` over the title's folder with this start's settings /// (the translations depend on them), then lets the start go on, whatever came of it. pub(crate) fn preload(exe: &Path) { let folder = exe.parent().map(Path::to_path_buf).unwrap_or_default(); let began = std::time::Instant::now(); let done = std::env::current_exe().and_then(|program| { std::process::Command::new(program) .arg("preload") .arg(&folder) // Its own lines in this window; the start's log is the title's. .env_remove("VICTORY_LOG_FILE") .status() }); let minutes = began.elapsed().as_secs_f64() / 60.0; match done { Ok(s) if s.success() => println!("Shaders preloaded in {minutes:.1} min. Starting the game."), Ok(s) => println!("The preload stopped ({s}) after {minutes:.1} min; what it did is kept. Starting the game."), Err(e) => println!("The preload did not start ({e}). Starting the game."), } } /// `%LOCALAPPDATA%\victory`, or the folder `VICTORY_HOME` names. fn home() -> Result { if let Some(home) = victory_core::config::home_override() { return Ok(home); } let base = std::env::var_os("LOCALAPPDATA").context("no %LOCALAPPDATA% to keep settings and logs in")?; Ok(PathBuf::from(base).join("victory")) } /// Where `victory get-dlss` puts NVIDIA's files. fn nvidia_home() -> Result { Ok(home()?.join("nvidia-sdk")) } fn has_streamline(dir: &Path) -> bool { dir.join("sl.interposer.dll").is_file() } /// The folder with NVIDIA's files: the one `VICTORY_STREAMLINE_DIR` names, /// where `get-dlss` puts them, or beside this program (a repository's /// `nvidia-sdk`, or the files themselves). fn streamline_dir(here: &Path) -> Option { let named = std::env::var_os("VICTORY_STREAMLINE_DIR").filter(|v| !v.is_empty()).map(PathBuf::from); let ours = nvidia_home().ok().map(|d| d.join("Streamline").join("bin").join("x64")); let beside = [here.join("nvidia-sdk").join("Streamline").join("bin").join("x64"), here.to_path_buf()]; named.into_iter().chain(ours).chain(beside).find(|d| has_streamline(d)) } /// Where `victory get-fsr` puts AMD's files. fn amd_home() -> Result { Ok(home()?.join("amd-fidelityfx")) } fn has_fsr(dir: &Path) -> bool { FSR_FILES.iter().all(|f| dir.join(f).is_file()) } /// The folder with AMD's FSR 3 files: the one `VICTORY_FIDELITYFX_DIR` names, /// where `get-fsr` puts them, or beside this program (a `fidelityfx` folder, /// a repository's `amd-sdk`, or the files themselves). fn fidelityfx_dir(here: &Path) -> Option { let named = std::env::var_os("VICTORY_FIDELITYFX_DIR").filter(|v| !v.is_empty()).map(PathBuf::from); let ours = amd_home().ok(); let beside = [here.join("fidelityfx"), here.join("amd-sdk").join("FidelityFX").join("bin"), here.to_path_buf()]; named.into_iter().chain(ours).chain(beside).find(|d| has_fsr(d)) } /// The lines `NAME=value` of the menu's file. fn parse_menu(text: &str) -> Vec<(String, String)> { text.lines() .filter_map(|l| l.split_once('=')) .map(|(k, v)| (k.trim().to_owned(), v.trim().to_owned())) .filter(|(k, _)| { k.starts_with("VICTORY_") && k.bytes().all(|b| b.is_ascii_uppercase() || b.is_ascii_digit() || b == b'_') }) .collect() } /// The settings of a start and whether it is the Series S profile: the /// options, what the menu saved over them (`menu`), `--set` over both. /// `streamline`: the folder with NVIDIA's files, if there is one. fn settings(o: &Play, menu: &[(String, String)], streamline: Option<&Path>) -> (BTreeMap, bool) { let saved = |k: &str| menu.iter().rev().find(|(n, _)| n == k).map(|(_, v)| v.as_str()); let series_s = saved("VICTORY_MENU_CONSOLE").map_or(o.series_s, |c| c == "series-s"); let res = saved("VICTORY_MENU_RES").filter(|r| RESOLUTIONS.contains(r)).unwrap_or(&o.res); let dlss = saved("VICTORY_MENU_DLSS").filter(|d| DLSS_MODES.contains(d)).unwrap_or(&o.dlss); // 0: off; 2 to 6: that many frames shown for each one rendered (1, from an older file: 2). let (mut framegen, mut times) = (o.framegen.is_some(), o.framegen.unwrap_or(2)); if let Some(saved) = saved("VICTORY_MENU_FRAMEGEN") { let n = saved.parse::().ok().filter(|n| (1..=6).contains(n)); framegen = n.is_some(); if let Some(n) = n.filter(|n| *n >= 2) { times = n; } } let fg_type = saved("VICTORY_MENU_FG_TYPE").filter(|t| FG_TYPES.contains(t)).unwrap_or(&o.fg_type); let fps = saved("VICTORY_MENU_FPS") .and_then(|f| f.parse::().ok()) .filter(|f| (1..=1000).contains(f)) .unwrap_or(o.fps); let mut s: BTreeMap = SETTINGS.iter().map(|(k, v)| ((*k).to_owned(), (*v).to_owned())).collect(); let mut set = |k: &str, v: &str| drop(s.insert(k.to_owned(), v.to_owned())); if !series_s { match res { "1440" => set("VICTORY_NATIVE_1440P", "1"), "1080to1440" => set("VICTORY_RENDER_1080P", "1"), _ => {} } } let (mut dlss_on, mut framegen_x) = (false, None); if dlss != "off" && let Some(dir) = streamline { dlss_on = true; set("VICTORY_DLSS", "1"); set("VICTORY_DLSS_MODE", dlss); set("VICTORY_STREAMLINE_DIR", &dir.to_string_lossy()); if framegen { framegen_x = Some(times); } else if !o.no_framegen_ready { framegen_x = Some(0); // ready, off } if let Some(x) = framegen_x { set("VICTORY_FRAMEGEN", "1"); set("VICTORY_FRAMEGEN_X", &x.to_string()); // Frame generation takes 1.5 to 2 GB of the card's memory, which the layer // otherwise fills with textures: on from the start it is left a fifth of the // budget; ready but off, a little less is held back. set("VICTORY_TEXTURE_ROOM_PERCENT", if framegen { "80" } else { "86" }); } } // AMD FSR 3 frame generation, made ready as DLSS frame generation is: auto, the layer uses // it only where DLSS frame generation does not run; fsr, DLSS frame generation is not asked. // The layer checks the files and the card and says at the start what runs and why not. if fg_type != "dlss" && (framegen || !o.no_framegen_ready) { set("VICTORY_FSR_FRAMEGEN", "1"); if fg_type == "fsr" { s.remove("VICTORY_FRAMEGEN"); } let mut set = |k: &str, v: &str| drop(s.insert(k.to_owned(), v.to_owned())); let x = *framegen_x.get_or_insert(if framegen { times } else { 0 }); set("VICTORY_FRAMEGEN_X", &x.to_string()); if !s.contains_key("VICTORY_TEXTURE_ROOM_PERCENT") { s.insert("VICTORY_TEXTURE_ROOM_PERCENT".to_owned(), if framegen { "80" } else { "86" }.to_owned()); } } let mut set = |k: &str, v: &str| drop(s.insert(k.to_owned(), v.to_owned())); set("VICTORY_FRAME_INTERVAL_US", &(1_000_000.0 / f64::from(fps)).round().to_string()); for (k, v) in menu.iter().filter(|(k, _)| !k.starts_with("VICTORY_MENU_")) { if v.is_empty() { s.remove(k); } else { s.insert(k.clone(), v.clone()); } } for (k, v) in o.more.iter().filter_map(|m| m.split_once('=')) { s.insert(k.trim().to_owned(), v.trim().to_owned()); } // For the menu: what it shows as the current choices. let mut set = |k: &str, v: &str| drop(s.insert(k.to_owned(), v.to_owned())); set("VICTORY_MENU_CONSOLE", if series_s { "series-s" } else { "series-x" }); set("VICTORY_MENU_RES", res); set("VICTORY_MENU_DLSS", if dlss_on { dlss } else { "off" }); set("VICTORY_MENU_FRAMEGEN", &framegen_x.unwrap_or(0).to_string()); set("VICTORY_MENU_FG_TYPE", fg_type); set("VICTORY_MENU_FPS", &fps.to_string()); (s, series_s) } /// `year-month-day-hour-minute-second` (UTC) of `seconds` since 1970. fn stamp(seconds: u64) -> String { let (days, rest) = (seconds / 86_400, seconds % 86_400); // (Howard Hinnant's civil-from-days.) let z = days as i64 + 719_468; let era = z.div_euclid(146_097); let doe = z.rem_euclid(146_097); let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); let mp = (5 * doy + 2) / 153; let (day, month) = (doy - (153 * mp + 2) / 5 + 1, if mp < 10 { mp + 3 } else { mp - 9 }); let year = yoe + era * 400 + i64::from(month <= 2); format!("{year:04}{month:02}{day:02}-{:02}{:02}{:02}", rest / 3600, rest % 3600 / 60, rest % 60) } /// A new log file's name under `logs`, the ten newest of ours kept. The /// process id is part of it: two starts in one second (a test beside a /// player's own start, 2026-11-26) had the same name, and one of them was /// left without a log. fn log_file(logs: &Path) -> Result { std::fs::create_dir_all(logs).with_context(|| format!("creating {}", logs.display()))?; let mut old: Vec = std::fs::read_dir(logs) .into_iter() .flatten() .flatten() .map(|e| e.path()) // ("play-": the name before 2026-11-27; it sorts first, so goes first.) .filter(|p| p.file_name().is_some_and(|n| n.to_string_lossy().starts_with(LOG_PREFIX) || n.to_string_lossy().starts_with("play-"))) .collect(); old.sort(); for gone in old.iter().rev().skip(9) { let _ = std::fs::remove_file(gone); } let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_secs()); Ok(logs.join(format!("{LOG_PREFIX}{}-{}.log", stamp(now), std::process::id()))) } /// The start of the built-in launcher's log names (scripts\play-foxhound.ps1's /// are `victory-nvidia--