main.rs 6.2 KB

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