171 lines
5.2 KiB
Rust
171 lines
5.2 KiB
Rust
|
|
|
|
use std::collections::{BTreeMap, BTreeSet};
|
|
use std::io::{self, Read};
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use anyhow::{Context, Result};
|
|
use crate::hle::{HleResolver, HostPolicy};
|
|
use eonacat_xsbox_gpu::loader::Provenance;
|
|
use eonacat_xsbox_gpu::pe::{ImageInfo, ImportSymbol};
|
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
|
enum FunctionKey {
|
|
Name(String),
|
|
Ordinal(u16),
|
|
}
|
|
|
|
impl From<&ImportSymbol> for FunctionKey {
|
|
fn from(symbol: &ImportSymbol) -> Self {
|
|
match symbol {
|
|
ImportSymbol::Name { name, .. } => Self::Name(name.clone()),
|
|
ImportSymbol::Ordinal(ordinal) => Self::Ordinal(*ordinal),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Default)]
|
|
struct DllStats {
|
|
functions: BTreeSet<FunctionKey>,
|
|
hle: usize,
|
|
host: usize,
|
|
missing: usize,
|
|
images: BTreeSet<PathBuf>,
|
|
}
|
|
|
|
pub fn run(paths: Vec<PathBuf>, strict: bool, list_missing: bool) -> Result<()> {
|
|
let paths = crate::default_paths(paths)?;
|
|
let resolver = HleResolver::new(if strict {
|
|
HostPolicy::none()
|
|
} else {
|
|
HostPolicy::default()
|
|
});
|
|
|
|
let mut images = 0usize;
|
|
let mut seen = BTreeSet::new();
|
|
let mut failed = Vec::new();
|
|
let mut dlls: BTreeMap<String, DllStats> = BTreeMap::new();
|
|
let mut missing: BTreeMap<(String, String), BTreeSet<PathBuf>> = BTreeMap::new();
|
|
|
|
for root in &paths {
|
|
for entry in walkdir::WalkDir::new(root) {
|
|
let entry = match entry {
|
|
Ok(entry) => entry,
|
|
Err(error) if error.depth() == 0 => {
|
|
return Err(error).context("cannot scan census input");
|
|
}
|
|
Err(error) => {
|
|
failed.push(error.to_string());
|
|
continue;
|
|
}
|
|
};
|
|
let path = entry.path();
|
|
if !entry.file_type().is_file() {
|
|
continue;
|
|
}
|
|
match looks_like_pe(path) {
|
|
Ok(true) => {}
|
|
Ok(false) => continue,
|
|
Err(error) => {
|
|
failed.push(format!("{}: {error}", path.display()));
|
|
continue;
|
|
}
|
|
}
|
|
|
|
let identity = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
|
|
if !seen.insert(identity) {
|
|
continue;
|
|
}
|
|
let info = match std::fs::read(path)
|
|
.map_err(anyhow::Error::from)
|
|
.and_then(|d| ImageInfo::parse(&d).map_err(anyhow::Error::from))
|
|
{
|
|
Ok(info) => info,
|
|
Err(e) => {
|
|
failed.push(format!("{}: {e}", path.display()));
|
|
continue;
|
|
}
|
|
};
|
|
images += 1;
|
|
tally_image(path, &info, &resolver, &mut dlls, &mut missing);
|
|
}
|
|
}
|
|
|
|
println!("{images} images scanned, {} unreadable", failed.len());
|
|
for error in &failed {
|
|
println!(" skipped {error}");
|
|
}
|
|
println!(
|
|
"\n{:<56} {:>6} {:>6} {:>6} {:>8} {:>7}",
|
|
"dll", "funcs", "hle", "host", "missing", "images"
|
|
);
|
|
let (mut f, mut h, mut o, mut m) = (0, 0, 0, 0);
|
|
for (dll, s) in &dlls {
|
|
println!(
|
|
"{dll:<56} {:>6} {:>6} {:>6} {:>8} {:>7}",
|
|
s.functions.len(),
|
|
s.hle,
|
|
s.host,
|
|
s.missing,
|
|
s.images.len()
|
|
);
|
|
f += s.functions.len();
|
|
h += s.hle;
|
|
o += s.host;
|
|
m += s.missing;
|
|
}
|
|
println!("{:<56} {f:>6} {h:>6} {o:>6} {m:>8}", "TOTAL");
|
|
|
|
if list_missing {
|
|
let mut rows: Vec<_> = missing.into_iter().collect();
|
|
rows.sort_by(|a, b| b.1.len().cmp(&a.1.len()).then_with(|| a.0.cmp(&b.0)));
|
|
println!("\nmissing (by number of images using it)");
|
|
for ((dll, sym), users) in rows {
|
|
println!(" {:>4} {dll}!{sym}", users.len());
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn looks_like_pe(path: &Path) -> io::Result<bool> {
|
|
let mut magic = [0u8; 2];
|
|
match std::fs::File::open(path)?.read_exact(&mut magic) {
|
|
Ok(()) => Ok(&magic == b"MZ"),
|
|
Err(error) if error.kind() == io::ErrorKind::UnexpectedEof => Ok(false),
|
|
Err(error) => Err(error),
|
|
}
|
|
}
|
|
|
|
fn tally_image(
|
|
path: &Path,
|
|
info: &ImageInfo,
|
|
resolver: &HleResolver,
|
|
dlls: &mut BTreeMap<String, DllStats>,
|
|
missing: &mut BTreeMap<(String, String), BTreeSet<PathBuf>>,
|
|
) {
|
|
for module in info.imports.iter().chain(&info.delay_imports) {
|
|
let key = module.dll.to_ascii_lowercase();
|
|
let stats = dlls.entry(key.clone()).or_default();
|
|
stats.images.insert(path.to_path_buf());
|
|
for e in &module.entries {
|
|
let class = resolver.classify(&module.dll, &e.symbol);
|
|
if class.is_none() {
|
|
missing
|
|
.entry((key.clone(), e.symbol.to_string()))
|
|
.or_default()
|
|
.insert(path.to_path_buf());
|
|
}
|
|
|
|
if stats.functions.insert(FunctionKey::from(&e.symbol)) {
|
|
match class {
|
|
Some(Provenance::Hle) => stats.hle += 1,
|
|
Some(Provenance::Host) => stats.host += 1,
|
|
Some(Provenance::Title) => unreachable!("census maps no title DLLs"),
|
|
None => stats.missing += 1,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|