census.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //! Which imports do titles need, and which do we cover?
  2. use std::collections::{BTreeMap, BTreeSet};
  3. use std::io::Read;
  4. use std::path::{Path, PathBuf};
  5. use anyhow::Result;
  6. use victory_hle::{HleResolver, HostPolicy};
  7. use victory_loader::Provenance;
  8. use victory_pe::{ImageInfo, ImportSymbol};
  9. #[derive(Default)]
  10. struct DllStats {
  11. functions: BTreeSet<ImportSymbol>,
  12. hle: usize,
  13. host: usize,
  14. missing: usize,
  15. images: BTreeSet<PathBuf>,
  16. }
  17. pub fn run(paths: Vec<PathBuf>, strict: bool, list_missing: bool) -> Result<()> {
  18. let paths = crate::default_paths(paths)?;
  19. let resolver = HleResolver::new(if strict {
  20. HostPolicy::none()
  21. } else {
  22. HostPolicy::default()
  23. });
  24. let mut images = 0usize;
  25. let mut failed = Vec::new();
  26. let mut dlls: BTreeMap<String, DllStats> = BTreeMap::new();
  27. let mut missing: BTreeMap<(String, String), BTreeSet<PathBuf>> = BTreeMap::new();
  28. for root in &paths {
  29. for entry in walkdir::WalkDir::new(root)
  30. .into_iter()
  31. .filter_map(Result::ok)
  32. {
  33. let path = entry.path();
  34. if !entry.file_type().is_file() || !looks_like_pe(path) {
  35. continue;
  36. }
  37. let info = match std::fs::read(path)
  38. .map_err(anyhow::Error::from)
  39. .and_then(|d| ImageInfo::parse(&d).map_err(anyhow::Error::from))
  40. {
  41. Ok(info) => info,
  42. Err(e) => {
  43. failed.push((path.to_path_buf(), e.to_string()));
  44. continue;
  45. }
  46. };
  47. images += 1;
  48. for module in info.imports.iter().chain(&info.delay_imports) {
  49. let key = module.dll.to_ascii_lowercase();
  50. let stats = dlls.entry(key.clone()).or_default();
  51. stats.images.insert(path.to_path_buf());
  52. for e in &module.entries {
  53. let class = resolver.classify(&module.dll, &e.symbol);
  54. if class.is_none() {
  55. missing
  56. .entry((key.clone(), e.symbol.to_string()))
  57. .or_default()
  58. .insert(path.to_path_buf());
  59. }
  60. // Count each dll!function once across all images.
  61. if stats.functions.insert(e.symbol.clone()) {
  62. match class {
  63. Some(Provenance::Hle) => stats.hle += 1,
  64. Some(Provenance::Host) => stats.host += 1,
  65. Some(Provenance::Title) => unreachable!("census maps no title DLLs"),
  66. None => stats.missing += 1,
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. println!("{images} images scanned, {} unreadable", failed.len());
  74. for (p, e) in &failed {
  75. println!(" skipped {}: {e}", p.display());
  76. }
  77. println!(
  78. "\n{:<56} {:>6} {:>6} {:>6} {:>8} {:>7}",
  79. "dll", "funcs", "hle", "host", "missing", "images"
  80. );
  81. let (mut f, mut h, mut o, mut m) = (0, 0, 0, 0);
  82. for (dll, s) in &dlls {
  83. println!(
  84. "{dll:<56} {:>6} {:>6} {:>6} {:>8} {:>7}",
  85. s.functions.len(),
  86. s.hle,
  87. s.host,
  88. s.missing,
  89. s.images.len()
  90. );
  91. f += s.functions.len();
  92. h += s.hle;
  93. o += s.host;
  94. m += s.missing;
  95. }
  96. println!("{:<56} {f:>6} {h:>6} {o:>6} {m:>8}", "TOTAL");
  97. if list_missing {
  98. let mut rows: Vec<_> = missing.into_iter().collect();
  99. rows.sort_by(|a, b| b.1.len().cmp(&a.1.len()).then_with(|| a.0.cmp(&b.0)));
  100. println!("\nmissing (by number of images using it)");
  101. for ((dll, sym), users) in rows {
  102. println!(" {:>4} {dll}!{sym}", users.len());
  103. }
  104. }
  105. Ok(())
  106. }
  107. /// Cheap pre-filter: an `MZ` header, regardless of extension (console
  108. /// packages do not always use .exe/.dll).
  109. fn looks_like_pe(path: &Path) -> bool {
  110. let mut magic = [0u8; 2];
  111. std::fs::File::open(path)
  112. .and_then(|mut f| f.read_exact(&mut magic))
  113. .is_ok()
  114. && &magic == b"MZ"
  115. }