main.rs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //! `victory`: the command-line front end.
  2. //!
  3. //! Every command reads the user's own files and writes only to stdout or to
  4. //! the user's cache directory (outside the repository).
  5. mod census;
  6. mod inspect;
  7. #[cfg(all(windows, feature = "diagnostics"))]
  8. mod replay;
  9. mod run;
  10. mod shader;
  11. mod translate;
  12. use std::path::PathBuf;
  13. use anyhow::Result;
  14. use clap::{Parser, Subcommand, ValueEnum};
  15. #[derive(Parser)]
  16. #[command(
  17. name = "victory",
  18. version,
  19. about = "Run an Xbox Series title natively on Windows"
  20. )]
  21. struct Cli {
  22. #[command(subcommand)]
  23. command: Command,
  24. }
  25. #[derive(Subcommand)]
  26. enum Command {
  27. /// Summarise one PE image: headers, sections, imports, TLS, build id.
  28. Inspect {
  29. file: PathBuf,
  30. /// List every imported function.
  31. #[arg(long)]
  32. imports: bool,
  33. /// List every exported function.
  34. #[arg(long)]
  35. exports: bool,
  36. },
  37. /// Aggregate the imports of every PE image under the given paths and
  38. /// show which ones our runtime covers. Defaults to the configured
  39. /// game and test-title directories.
  40. Census {
  41. paths: Vec<PathBuf>,
  42. /// Do not count host pass-through as covered.
  43. #[arg(long)]
  44. strict: bool,
  45. /// List every missing `dll!function`, most used first.
  46. #[arg(long)]
  47. missing: bool,
  48. },
  49. /// List the `dll!export` pairs an import library describes.
  50. LibExports {
  51. lib: PathBuf,
  52. /// List every export, not just per-DLL counts.
  53. #[arg(long)]
  54. list: bool,
  55. },
  56. /// Find shader containers in files or directories (also inside engine
  57. /// archives, if uncompressed) and report their layout.
  58. ShaderInfo {
  59. paths: Vec<PathBuf>,
  60. /// Print one line per container found.
  61. #[arg(long)]
  62. verbose: bool,
  63. /// Also list the decoded native instructions (implies --verbose).
  64. #[arg(long)]
  65. disasm: bool,
  66. },
  67. /// Run every shader found (containers, SGD5 files) through the
  68. /// translator and report what it cannot handle yet.
  69. Translate {
  70. paths: Vec<PathBuf>,
  71. /// Check emitted DXIL with dxil.dll's validator (`VICTORY_DXIL`,
  72. /// else the newest Windows SDK's).
  73. #[arg(long)]
  74. validate: bool,
  75. /// Also create a host D3D12 pipeline for each emitted shader.
  76. #[arg(long)]
  77. pipelines: bool,
  78. /// Print one line per program and outcome.
  79. #[arg(long)]
  80. list: bool,
  81. /// Worker threads (default: every logical core).
  82. #[arg(long)]
  83. jobs: Option<usize>,
  84. /// Warm the run's shader cache: a file of shader hashes (the 16
  85. /// hex digits a run's log names a shader by, anywhere in a line);
  86. /// the containers with those hashes are translated into the
  87. /// cache and nothing else is done. Set `VICTORY_HOST_WAVE` and
  88. /// `VICTORY_VIEW_LIMIT` to what the run's log says of the host.
  89. #[arg(long)]
  90. warm: Option<PathBuf>,
  91. },
  92. /// Translate every shader found (as `translate` finds them) into the
  93. /// shader cache, so that no run has to: a few minutes, once per change
  94. /// of the translator.
  95. Shaders {
  96. paths: Vec<PathBuf>,
  97. /// Worker threads (default: every logical core).
  98. #[arg(long)]
  99. jobs: Option<usize>,
  100. },
  101. /// Prepare, map and bind a title executable; with --execute, run it.
  102. Run {
  103. exe: PathBuf,
  104. /// What unimplemented imports do when called.
  105. #[arg(long, value_enum, default_value_t = Unimplemented::Trap)]
  106. unimplemented: Unimplemented,
  107. /// Disable host pass-through (everything must be implemented by us).
  108. #[arg(long)]
  109. strict: bool,
  110. /// Actually start the title. Without it, stop after binding.
  111. #[arg(long)]
  112. execute: bool,
  113. /// The console model the title sees (device type, memory).
  114. #[arg(long, value_enum, default_value_t = Console::SeriesX)]
  115. console: Console,
  116. /// Arguments for the title, after `--` (it reads the whole command
  117. /// line; a title that relaunches itself passes some).
  118. #[arg(last = true)]
  119. title_args: Vec<String>,
  120. },
  121. /// Run a captured frame's GPU work again (diagnostics builds; see
  122. /// docs/replay.md).
  123. #[cfg(all(windows, feature = "diagnostics"))]
  124. Replay(replay::Replay),
  125. /// Show the resolved configuration and check it.
  126. Config,
  127. }
  128. #[derive(Clone, Copy, ValueEnum)]
  129. enum Unimplemented {
  130. Trap,
  131. Zero,
  132. }
  133. #[derive(Clone, Copy, ValueEnum)]
  134. enum Console {
  135. SeriesX,
  136. SeriesS,
  137. }
  138. fn main() -> Result<()> {
  139. victory_core::logging::init("info");
  140. // The audio recorder `run` starts for `VICTORY_AUDIO_CAPTURE`.
  141. #[cfg(all(windows, feature = "diagnostics"))]
  142. if victory_hle::modules::audio::capture::child() {
  143. return Ok(());
  144. }
  145. let cli = Cli::parse();
  146. match cli.command {
  147. Command::Inspect {
  148. file,
  149. imports,
  150. exports,
  151. } => inspect::run(&file, imports, exports),
  152. Command::Census {
  153. paths,
  154. strict,
  155. missing,
  156. } => census::run(paths, strict, missing),
  157. Command::LibExports { lib, list } => inspect::lib_exports(&lib, list),
  158. Command::ShaderInfo {
  159. paths,
  160. verbose,
  161. disasm,
  162. } => shader::run(paths, verbose || disasm, disasm),
  163. Command::Translate {
  164. paths,
  165. validate,
  166. pipelines,
  167. list,
  168. jobs,
  169. warm,
  170. } => {
  171. // `VICTORY_VIEW_LIMIT=<null slot>`: translate as on a host whose view heap is capped
  172. if let Some(n) = std::env::var("VICTORY_VIEW_LIMIT").ok().and_then(|v| v.parse().ok()) {
  173. victory_shader::emit::set_view_heap_limit(n);
  174. }
  175. // `VICTORY_HOST_WAVE=32`: translate as for a GPU whose waves are at most 32 lanes
  176. if let Some(n) = std::env::var("VICTORY_HOST_WAVE").ok().and_then(|v| v.parse().ok()) {
  177. victory_shader::emit::set_host_max_wave(n);
  178. }
  179. if let Some(file) = warm {
  180. return translate::warm(paths, &file, jobs);
  181. }
  182. translate::run(paths, validate, pipelines, list, jobs)
  183. }
  184. Command::Shaders { paths, jobs } => translate::precompile(paths, jobs),
  185. Command::Run {
  186. exe,
  187. unimplemented,
  188. strict,
  189. execute,
  190. console,
  191. title_args,
  192. } => {
  193. victory_hle::runtime::set_title_args(title_args);
  194. run::run(&exe, unimplemented, strict, execute, console)
  195. }
  196. #[cfg(all(windows, feature = "diagnostics"))]
  197. Command::Replay(options) => replay::run(options),
  198. Command::Config => show_config(),
  199. }
  200. }
  201. fn show_config() -> Result<()> {
  202. let cfg = victory_core::Config::load()?;
  203. let status = |p: &Option<PathBuf>| match p {
  204. Some(p) if p.exists() => format!("{}", p.display()),
  205. Some(p) => format!("{} (does not exist)", p.display()),
  206. None => "(not set)".to_owned(),
  207. };
  208. println!(
  209. "config file : {}",
  210. cfg.source
  211. .as_ref()
  212. .map_or("(none)".into(), |p| p.display().to_string())
  213. );
  214. println!("game_dir : {}", status(&cfg.game_dir));
  215. println!("cache_dir : {}", cfg.cache_dir.display());
  216. println!("data_dir : {}", cfg.data_dir.display());
  217. for t in &cfg.test_titles {
  218. println!("test_title : {}", status(&Some(t.clone())));
  219. }
  220. for p in cfg
  221. .game_dir
  222. .iter()
  223. .chain(&cfg.test_titles)
  224. .chain([&cfg.cache_dir, &cfg.data_dir])
  225. {
  226. if let Err(e) = victory_core::ensure_outside_repo(p) {
  227. println!("POLICY VIOLATION: {e}");
  228. }
  229. }
  230. Ok(())
  231. }
  232. /// Paths given on the command line, or the configured game + test titles.
  233. pub(crate) fn default_paths(paths: Vec<PathBuf>) -> Result<Vec<PathBuf>> {
  234. if !paths.is_empty() {
  235. return Ok(paths);
  236. }
  237. let cfg = victory_core::Config::load()?;
  238. let all: Vec<PathBuf> = cfg.game_dir.into_iter().chain(cfg.test_titles).collect();
  239. anyhow::ensure!(
  240. !all.is_empty(),
  241. "no paths given and none configured (see victory.example.toml)"
  242. );
  243. Ok(all)
  244. }