2026-11-23 01:15:26 +01:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
|
|
use anyhow::{Context, Result};
|
|
|
|
|
|
|
|
|
|
use crate::Unimplemented;
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
pub fn run(exe: &Path, unimplemented: Unimplemented, strict: bool, execute: bool) -> Result<()> {
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use victory_hle::{HleResolver, HostPolicy};
|
2026-11-23 14:42:21 +01:00
|
|
|
use victory_loader::{BindReport, UnimplementedPolicy, WithTitleDlls};
|
2026-11-23 01:15:26 +01:00
|
|
|
|
|
|
|
|
let cfg = victory_core::Config::load()?;
|
|
|
|
|
let original = std::fs::read(exe).with_context(|| format!("reading {}", exe.display()))?;
|
|
|
|
|
let prepared = victory_loader::prepare(&original)?;
|
|
|
|
|
let hash = prepared.original_hash.to_hex();
|
|
|
|
|
match victory_game::identify(&hash) {
|
|
|
|
|
Some(build) => println!("build {} ({hash})", build.label),
|
|
|
|
|
None => println!("build unknown ({hash})"),
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 14:42:21 +01:00
|
|
|
let images = cfg.cache_subdir("images")?;
|
|
|
|
|
let image = victory_loader::load(&prepared, &images)?;
|
2026-11-23 01:15:26 +01:00
|
|
|
victory_hle::runtime::set_main_image(image.base());
|
2026-11-23 14:42:21 +01:00
|
|
|
victory_hle::runtime::set_image_path(image.base(), exe);
|
2026-11-23 01:15:26 +01:00
|
|
|
println!(
|
|
|
|
|
"mapped at {:#x} (preferred {:#x})",
|
|
|
|
|
image.base(),
|
|
|
|
|
image.info().image_base
|
|
|
|
|
);
|
2026-11-23 14:42:21 +01:00
|
|
|
let resolver = HleResolver::new(if strict {
|
|
|
|
|
HostPolicy::none()
|
|
|
|
|
} else {
|
|
|
|
|
HostPolicy::default()
|
|
|
|
|
});
|
|
|
|
|
// DLLs the host or our runtime provide are never taken from the title.
|
|
|
|
|
let wanted = |dll: &str| {
|
|
|
|
|
!resolver.host.allows(dll) && !victory_hle::registry().iter().any(|m| m.serves(dll))
|
|
|
|
|
};
|
|
|
|
|
let title_dir = exe.parent().unwrap_or(Path::new("."));
|
|
|
|
|
// A missing manifest leaves id 0 and no storage quota.
|
|
|
|
|
let manifest =
|
|
|
|
|
std::fs::read_to_string(title_dir.join("MicrosoftGame.config")).unwrap_or_default();
|
|
|
|
|
let title = victory_hle::runtime::Title::from_manifest(&manifest);
|
|
|
|
|
let storage = cfg.data_subdir(&format!("{:08X}/storage", title.id))?;
|
2026-11-23 01:15:26 +01:00
|
|
|
println!(
|
2026-11-23 14:42:21 +01:00
|
|
|
"title {:08X}, storage {} ({} MB)",
|
|
|
|
|
title.id,
|
|
|
|
|
storage.display(),
|
|
|
|
|
title.storage_bytes >> 20
|
2026-11-23 01:15:26 +01:00
|
|
|
);
|
2026-11-23 14:42:21 +01:00
|
|
|
victory_hle::runtime::set_title(victory_hle::runtime::Title { storage, ..title });
|
|
|
|
|
let dlls = victory_loader::load_title_dlls(image.info(), title_dir, &images, &wanted)?;
|
|
|
|
|
for (name, dll) in &dlls {
|
|
|
|
|
println!("title dll {name} at {:#x}", dll.base());
|
|
|
|
|
victory_hle::runtime::set_image_path(dll.base(), &title_dir.join(name));
|
2026-11-23 01:15:26 +01:00
|
|
|
}
|
2026-11-23 14:42:21 +01:00
|
|
|
|
|
|
|
|
let policy = match unimplemented {
|
|
|
|
|
Unimplemented::Trap => UnimplementedPolicy::Trap,
|
|
|
|
|
Unimplemented::Zero => UnimplementedPolicy::ReturnZero,
|
|
|
|
|
};
|
|
|
|
|
let with_dlls = WithTitleDlls {
|
|
|
|
|
inner: &resolver,
|
|
|
|
|
dlls: &dlls,
|
|
|
|
|
};
|
|
|
|
|
let show = |name: &str, report: BindReport| {
|
|
|
|
|
println!(
|
|
|
|
|
"bound {name}: {} hle, {} host, {} title, {} missing",
|
|
|
|
|
report.hle,
|
|
|
|
|
report.host,
|
|
|
|
|
report.title,
|
|
|
|
|
report.missing.len()
|
|
|
|
|
);
|
|
|
|
|
for m in &report.missing {
|
|
|
|
|
println!(" missing {}!{}", m.dll, m.symbol);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
for (name, dll) in &dlls {
|
|
|
|
|
show(name, dll.bind_imports(&with_dlls, policy)?);
|
|
|
|
|
}
|
|
|
|
|
let exe_name = exe.file_name().unwrap_or_default().to_string_lossy();
|
|
|
|
|
show(&exe_name, image.bind_imports(&with_dlls, policy)?);
|
2026-11-23 01:15:26 +01:00
|
|
|
if !execute {
|
|
|
|
|
println!("not executing (pass --execute to start the title)");
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
2026-11-23 14:42:21 +01:00
|
|
|
victory_loader::report_crashes();
|
2026-11-23 01:15:26 +01:00
|
|
|
// SAFETY: imports are bound; from here on we run title code.
|
2026-11-23 14:42:21 +01:00
|
|
|
let main = unsafe { Arc::new(image).spawn_main(dlls)? };
|
2026-11-23 01:15:26 +01:00
|
|
|
let code = main
|
|
|
|
|
.join()
|
2026-11-23 14:42:21 +01:00
|
|
|
.map_err(|_| anyhow::anyhow!("title main thread panicked"))??;
|
2026-11-23 01:15:26 +01:00
|
|
|
println!("title exited with {code}");
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
|
pub fn run(_: &Path, _: Unimplemented, _: bool, _: bool) -> Result<()> {
|
|
|
|
|
anyhow::bail!("running titles requires Windows")
|
|
|
|
|
}
|