Files
6gbvram---16gb-ram/apps/victory/src/run.rs
T

168 lines
6.3 KiB
Rust
Raw Normal View History

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};
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})"),
}
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());
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
);
let resolver = HleResolver::new(if strict {
HostPolicy::none()
} else {
HostPolicy::default()
});
let wanted = |dll: &str| title_dll(&resolver.host, 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!(
"title {:08X}, storage {} ({} MB)",
title.id,
storage.display(),
title.storage_bytes >> 20
2026-11-23 01:15:26 +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
}
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(());
}
victory_loader::report_crashes();
let late = LateDlls {
host: resolver.host.clone(),
title_dir: title_dir.to_owned(),
cache_dir: images,
policy,
mapped: std::sync::Mutex::new(dlls.clone()),
};
victory_hle::runtime::set_title_dll_loader(Box::new(move |file| {
late.load(file, show).map_err(|e| format!("{e:#}"))
}));
2026-11-23 01:15:26 +01:00
// SAFETY: imports are bound; from here on we run title code.
let main = unsafe { Arc::new(image).spawn_main(dlls)? };
2026-11-23 01:15:26 +01:00
let code = main
.join()
.map_err(|_| anyhow::anyhow!("title main thread panicked"))??;
2026-11-23 01:15:26 +01:00
println!("title exited with {code}");
Ok(())
}
/// DLLs the host or our runtime provide are never taken from the title.
#[cfg(windows)]
fn title_dll(host: &victory_hle::HostPolicy, dll: &str) -> bool {
!host.allows(dll) && !victory_hle::registry().iter().any(|m| m.serves(dll))
}
/// Title DLLs the title loads itself (`LoadLibrary`) that nothing imports,
/// e.g. Grand Theft Auto VI's upscaler behind its FidelityFX loader.
#[cfg(windows)]
struct LateDlls {
host: victory_hle::HostPolicy,
title_dir: std::path::PathBuf,
cache_dir: std::path::PathBuf,
policy: victory_loader::UnimplementedPolicy,
/// Every title DLL mapped so far, to bind new ones against.
mapped: std::sync::Mutex<Vec<(String, victory_loader::LoadedImage)>>,
}
#[cfg(windows)]
impl LateDlls {
/// Maps `file` and the title DLLs it needs, binds them and runs their
/// `DllMain` on the calling thread, as the Windows loader would.
fn load(
&self,
file: &str,
show: impl Fn(&str, victory_loader::BindReport),
) -> Result<Option<usize>> {
use victory_hle::runtime::{image_named, set_image_path};
// shortcut: not reentrant, so a DllMain that loads another new title
// DLL deadlocks; use a reentrant lock if a title does that.
let mut mapped = self.mapped.lock().unwrap_or_else(|e| e.into_inner());
let wanted = |dll: &str| title_dll(&self.host, dll) && image_named(dll).is_none();
let new = victory_loader::load_title_dll(file, &self.title_dir, &self.cache_dir, &wanted)?;
let hle = victory_hle::HleResolver::new(self.host.clone());
let old = victory_loader::WithTitleDlls {
inner: &hle,
dlls: &mapped,
};
let resolver = victory_loader::WithTitleDlls {
inner: &old,
dlls: &new,
};
for (name, dll) in &new {
println!(
"title dll {name} at {:#x} (loaded by the title)",
dll.base()
);
show(name, dll.bind_imports(&resolver, self.policy)?);
set_image_path(dll.base(), &self.title_dir.join(name));
}
for (name, dll) in &new {
// SAFETY: bound above, dependencies attached first.
unsafe { dll.attach() }.with_context(|| format!("attaching {name}"))?;
}
let base = new.last().map(|(_, dll)| dll.base());
mapped.extend(new);
Ok(base)
}
}
2026-11-23 01:15:26 +01:00
#[cfg(not(windows))]
pub fn run(_: &Path, _: Unimplemented, _: bool, _: bool) -> Result<()> {
anyhow::bail!("running titles requires Windows")
}