Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ Run the title:

```powershell
cargo build --release -p xtp
target\release\xtp.exe run <game dir>\<the title's exe> --execute
target\release\xtp.exe run "<game dir>" --execute
```

With a folder, `xtp run` selects an existing executable named in
`MicrosoftGame.config`. If none matches, it looks for a single `.exe` in
the folder and reports why it skipped any named entries. When several match,
it lists them so you can pass one explicitly.

The window caption shows the framerate. Saves, settings and `crashes.log`
are kept under the data folder (default `%LOCALAPPDATA%\xtp\data`), derived
data under the cache folder.
Expand Down Expand Up @@ -109,7 +114,7 @@ xtp census [paths...] [--missing] [--strict]
xtp lib-exports <import.lib> [--list]
xtp shader-info [paths...] [--verbose] [--disasm]
xtp translate [paths...] [--validate] [--pipelines] [--list] [--jobs N]
xtp run <exe> [--execute] [--unimplemented trap|zero] [--strict]
xtp run <exe|folder> [--execute] [--unimplemented trap|zero] [--strict]
[--console series-s|series-x] [-- <title arguments>]
xtp config
```
Expand Down
3 changes: 3 additions & 0 deletions apps/xtp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ memchr.workspace = true
clap.workspace = true
walkdir.workspace = true

[dev-dependencies]
tempfile.workspace = true

[features]
# Debug-level logs and the debugging switches (frame capture, stack dumps,
# the host debug layer, scripted keys). Off in a plain release build.
Expand Down
8 changes: 5 additions & 3 deletions apps/xtp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ enum Command {
},
/// Prepare, map and bind a title executable; with --execute, run it.
Run {
exe: PathBuf,
/// A title executable or a package folder with a manifest or a single .exe.
#[arg(value_name = "EXE|FOLDER")]
target: PathBuf,
/// What unimplemented imports do when called.
#[arg(long, value_enum, default_value_t = Unimplemented::Trap)]
unimplemented: Unimplemented,
Expand Down Expand Up @@ -223,7 +225,7 @@ fn main() -> Result<()> {
host_lanes,
),
Command::Run {
exe,
target,
unimplemented,
strict,
execute,
Expand All @@ -233,7 +235,7 @@ fn main() -> Result<()> {
} => {
let env = std::env::var("XTP_CONSOLE").ok();
let console = Console::pick(console, env.as_deref())?;
run::run(&exe, unimplemented, strict, execute, console, autopilot)
run::run(&target, unimplemented, strict, execute, console, autopilot)
}
Command::Config => show_config(),
}
Expand Down
232 changes: 224 additions & 8 deletions apps/xtp/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,126 @@ use anyhow::{Context, Result};

use crate::{Console, Unimplemented};

#[cfg(windows)]
struct RunTarget<'a> {
exe: PathBuf,
package_dir: &'a Path,
title: xtp_hle::runtime::Title,
}

#[cfg(windows)]
fn resolve_target(path: &Path) -> Result<RunTarget<'_>> {
let folder = path.is_dir();
let package_dir = if folder {
path
} else {
path.parent().unwrap_or(Path::new("."))
};
let manifest_path = package_dir.join("MicrosoftGame.config");
let manifest = match std::fs::read_to_string(&manifest_path) {
Ok(text) => text,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => String::new(),
Err(e) => {
eprintln!("warning: could not read {}: {e}", manifest_path.display());
String::new()
}
};
let title = xtp_hle::runtime::Title::from_manifest(&manifest);
let exe = if folder {
pick_executable(package_dir, &title.executables)?
} else {
path.to_owned()
};
Ok(RunTarget {
exe,
package_dir,
title,
})
}

#[cfg(windows)]
fn configured_executable(root: &Path, name: &str) -> Result<PathBuf> {
use std::path::Component;

let path = Path::new(name);
anyhow::ensure!(is_exe(path), "expected an .exe extension");
anyhow::ensure!(
path.components()
.all(|c| matches!(c, Component::Normal(_) | Component::CurDir)),
"expected a path inside the package"
);
let file = root.join(path).canonicalize()?;
let relative = file
.strip_prefix(root)
.context("resolves outside the package")?;
anyhow::ensure!(file.is_file(), "not a regular file");
Ok(relative.to_owned())
}

#[cfg(windows)]
fn is_exe(path: &Path) -> bool {
path.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("exe"))
}

#[cfg(windows)]
fn pick_executable(folder: &Path, configured: &[String]) -> Result<PathBuf> {
let root = folder
.canonicalize()
.with_context(|| format!("opening {}", folder.display()))?;
let mut candidates = Vec::new();
let mut skipped = Vec::new();
for name in configured {
match configured_executable(&root, name) {
Ok(path) => candidates.push(path),
Err(e) => skipped.push(format!("{name:?}: {e}")),
}
}
if candidates.is_empty() {
for entry in std::fs::read_dir(folder)
.with_context(|| format!("listing executables in {}", folder.display()))?
{
let entry = entry?;
if is_exe(&entry.path()) && entry.file_type()?.is_file() {
candidates.push(PathBuf::from(entry.file_name()));
}
}
if !skipped.is_empty() {
let details = skipped.join("\n ");
if candidates.is_empty() {
anyhow::bail!(
"no executable found in {}. MicrosoftGame.config entries skipped:\n {details}",
folder.display()
);
}
eprintln!(
"warning: using the top-level .exe search in {}. MicrosoftGame.config entries skipped:\n {details}",
folder.display()
);
}
}
candidates.sort();
candidates.dedup();
match candidates.as_slice() {
[exe] => Ok(folder.join(exe)),
[] => anyhow::bail!("no executable found in {}", folder.display()),
_ => {
let names = candidates
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>();
anyhow::bail!(
"multiple executables in {}. Pass one to xtp run:\n {}",
folder.display(),
names.join("\n ")
)
}
}
}

#[cfg(windows)]
pub fn run(
exe: &Path,
target: &Path,
unimplemented: Unimplemented,
strict: bool,
execute: bool,
Expand All @@ -30,7 +147,12 @@ pub fn run(
bindings: input.bindings.clone(),
})
.map_err(|e| anyhow::anyhow!("[input] in the config: {e}"))?;
let original = std::fs::read(exe).with_context(|| format!("reading {}", exe.display()))?;
let RunTarget {
exe,
package_dir,
title,
} = resolve_target(target)?;
let original = std::fs::read(&exe).with_context(|| format!("reading {}", exe.display()))?;
let prepared = xtp_loader::prepare(&original)?;
let hash = prepared.original_hash.to_hex();
match xtp_game::identify(&hash) {
Expand All @@ -44,7 +166,7 @@ pub fn run(
// SAFETY: just mapped from the executable `hash` names; no title code has run.
unsafe { xtp_game::watchdog::apply_if_asked(&hash, image.base(), image.info().size_of_image) };
xtp_hle::runtime::set_main_image(image.base());
xtp_hle::runtime::set_image_path(image.base(), exe);
xtp_hle::runtime::set_image_path(image.base(), &exe);
println!(
"mapped at {:#x} (preferred {:#x})",
image.base(),
Expand All @@ -57,10 +179,6 @@ pub fn run(
});
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 = xtp_hle::runtime::Title::from_manifest(&manifest);
let storage = cfg.data_subdir(&format!("{:08X}/storage", title.id))?;
println!(
"title {:08X}, storage {} ({} MB)",
Expand All @@ -69,7 +187,7 @@ pub fn run(
title.storage_bytes >> 20
);
// The console's fixed drives: the package as `G:`, temporary files `T:`.
xtp_hle::modules::files::mount_at('G', title_dir);
xtp_hle::modules::files::mount_at('G', package_dir);
let temp = cfg.data_subdir(&format!("{:08X}/temp", title.id))?;
xtp_hle::modules::files::mount_at('T', &temp);
xtp_hle::runtime::set_title(xtp_hle::runtime::Title { storage, ..title });
Expand Down Expand Up @@ -327,8 +445,106 @@ impl LoaderLock {

#[cfg(all(test, windows))]
mod tests {
use std::fs;
use std::sync::{Arc, Mutex};

use super::resolve_target;

#[test]
fn folder_prefers_the_configured_executable() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
fs::create_dir(root.join("bin")).unwrap();
let selected = root.join("bin").join("Title & Tools.exe");
fs::write(&selected, []).unwrap();
fs::write(root.join("Helper.exe"), []).unwrap();
fs::write(
root.join("MicrosoftGame.config"),
r#"<Game><TitleId>2A</TitleId>
<!-- <ExecutableList><Executable Name="Helper.exe"/></ExecutableList> -->
<Other><Executable Name="Helper.exe"/></Other>
<ExecutableList><Executable DisplayName="A > B" Name = 'bin/Title &amp; Tools.exe'/>
</ExecutableList></Game>"#,
)
.unwrap();
let target = resolve_target(root).unwrap();
assert_eq!(target.exe, selected);
assert_eq!(target.package_dir, root);
assert_eq!(target.title.id, 42);
}

#[test]
fn folder_falls_back_to_its_only_executable() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
let exe = root.join("Only.EXE");
fs::write(&exe, []).unwrap();
fs::create_dir(root.join("not-a-file.exe")).unwrap();
assert_eq!(resolve_target(root).unwrap().exe, exe);
fs::write(
root.join("MicrosoftGame.config"),
r#"<Game><ExecutableList><Executable Name="missing.exe"/></ExecutableList></Game>"#,
)
.unwrap();
assert_eq!(resolve_target(root).unwrap().exe, exe);
}

#[test]
fn ambiguous_folder_lists_candidates_and_accepts_an_explicit_path() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
for name in ["B.exe", "A.exe"] {
fs::write(root.join(name), []).unwrap();
}
let error = resolve_target(root).err().unwrap().to_string();
assert!(error.ends_with("\n A.exe\n B.exe"), "{error}");
let exe = root.join("B.exe");
assert_eq!(resolve_target(&exe).unwrap().exe, exe);
}

#[test]
fn several_configured_executables_list_only_the_matches() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
for name in ["Tool.exe", "A.exe", "B.exe"] {
fs::write(root.join(name), []).unwrap();
}
fs::write(
root.join("MicrosoftGame.config"),
r#"<Game><ExecutableList><Executable Name="B.exe"/><Executable Name="A.exe"/>
<Executable Name="B.exe"/></ExecutableList></Game>"#,
)
.unwrap();
let error = resolve_target(root).err().unwrap().to_string();
assert!(error.ends_with("\n A.exe\n B.exe"), "{error}");
}

#[test]
fn empty_folder_does_not_follow_a_manifest_outside_the_package() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path().join("package");
fs::create_dir(&root).unwrap();
fs::write(dir.path().join("Outside.exe"), []).unwrap();
fs::write(
root.join("MicrosoftGame.config"),
r#"<Game><ExecutableList><Executable Name="../Outside.exe"/><Executable Name="readme.txt"/>
<Executable Name="missing.exe"/></ExecutableList></Game>"#,
)
.unwrap();
let error = resolve_target(&root).err().unwrap().to_string();
assert!(error.contains("no executable found"), "{error}");
for reason in [
"Outside.exe",
"inside the package",
"readme.txt",
".exe extension",
"missing.exe",
] {
assert!(error.contains(reason), "{error}");
}
assert!(error.contains(root.to_str().unwrap()), "{error}");
}

#[test]
fn loader_lock_is_reentrant_and_exclusive() {
let lock = Arc::new(super::LoaderLock::default());
Expand Down
27 changes: 27 additions & 0 deletions apps/xtp/tests/run_folder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![cfg(windows)]

#[test]
fn warns_before_falling_back_from_unusable_manifest_entries() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("Fallback.exe"), b"synthetic non-PE").unwrap();
std::fs::write(
dir.path().join("MicrosoftGame.config"),
r#"<Game><ExecutableList><Executable Name="missing.exe"/>
<Executable Name="notes.txt"/></ExecutableList></Game>"#,
)
.unwrap();
let output = std::process::Command::new(env!("CARGO_BIN_EXE_xtp"))
.current_dir(dir.path())
.args(["run", "."])
.output()
.unwrap();
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("warning:") && stderr.contains("MicrosoftGame.config"),
"{stderr}"
);
assert!(
stderr.contains("missing.exe") && stderr.contains("notes.txt"),
"{stderr}"
);
}
Loading