Skip to content
Open
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
91 changes: 63 additions & 28 deletions src-tauri/src/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Project {
self.archive_exe
.as_deref()
.is_some_and(|exe| !build_zip::is_victory(exe))
|| rust_install(&self.repo).is_some()
|| rust_install(&self.repo).is_some_and(|install| !install.victory)
}

fn web_url(&self) -> String {
Expand Down Expand Up @@ -1095,12 +1095,24 @@ fn slug(repo: &str) -> String {
#[derive(Serialize, Deserialize)]
struct RustInstall {
executable: Option<PathBuf>,
/// The program is a VIctory build under its own name (`valhalla`): played as one.
#[serde(default)]
victory: bool,
}

fn rust_install_file(repo: &str) -> PathBuf {
target_dir(repo).join("mandyhub-rust.json")
}

fn record_rust_install(repo: &str, install: &RustInstall) -> Result<(), String> {
let marker = rust_install_file(repo);
let temporary = marker.with_extension("tmp");
let record = serde_json::to_vec(install).map_err(|error| error.to_string())?;
std::fs::write(&temporary, record)
.and_then(|_| std::fs::rename(&temporary, &marker))
.map_err(|error| format!("Could not record the installed Rust project: {error}"))
}

fn rust_install(repo: &str) -> Option<RustInstall> {
serde_json::from_slice(&std::fs::read(rust_install_file(repo)).ok()?).ok()
}
Expand Down Expand Up @@ -2460,7 +2472,11 @@ fn install(
}
// The runtime loads Microsoft's D3D12 Agility files from D3D12\ next to its exe. Put them
// there now and check them, so the new build is playable as soon as it finishes.
if let Ok(exe) = project_exe(p) {
// Other Rust programs need none of that.
let runtime = project_exe(p)
.ok()
.filter(|exe| !p.runs_standalone() || diagnose::exe_names_agility(exe));
if let Some(exe) = runtime {
let data = project_data(&p.repo);
let cache = project_cache(&p.repo);
let check = diagnose::Check {
Expand Down Expand Up @@ -2531,14 +2547,12 @@ fn build_project(
) -> Result<(), String> {
let workspace = cargo_workspace(dir)?;
let metadata = rust_project::metadata(&workspace)?;
// A listed project is built by name only when its workspace has the `victory` program;
// otherwise the whole workspace is built and its program found from Cargo's output.
let victory = if p.custom {
rust_project::is_victory(&workspace, &metadata)
} else {
rust_project::has_victory_program(&metadata)
};
let profile = if victory {
// A listed project is built by name only when its workspace has the `victory` program
// (under any name); otherwise the whole workspace is built and its program found from
// Cargo's output.
let victory = rust_project::victory_program(&metadata)
.filter(|_| !p.custom || rust_project::is_victory(&workspace, &metadata));
let profile = if victory.is_some() {
detect_build_profile(&workspace)
} else {
"release"
Expand All @@ -2551,18 +2565,33 @@ fn build_project(
cargo.arg("--locked");
}
cargo.current_dir(&workspace);
if victory {
if let Some(program) = victory {
let renamed = program != "victory";
cargo.args(["-p", "victory"]);
if documented_scarlet_script(&workspace).is_some() {
cargo.args(["--bin", "victory"]);
if renamed || documented_scarlet_script(&workspace).is_some() {
cargo.args(["--bin", program]);
}
run(job, ctx, &mut cargo)?;
let executable = victory_program(&target.join(profile));
let dir = target.join(profile);
let executable = if renamed {
dir.join(crate::platform::exe_name(program))
Comment on lines +2575 to +2577

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Resolve renamed executables from Cargo's artifact output

On a fresh install of a renamed workspace with build.target set, even to the host triple, this lookup rejects a successful build. Cargo's documentation notes that target-mode artifacts are “placed in a separate directory” (cargo build). In a dependency-free Linux fixture with build.target = "x86_64-unknown-linux-gnu", the command produces <target>/x86_64-unknown-linux-gnu/release/valhalla, but this branch checks <target>/release/valhalla, reports the build made no ..., and never records the install. These workspaces previously used the artifact-driven branch, which captured the correct path. Use Cargo's emitted executable path for both the existence check and the saved install record.

Useful? React with 👍 / 👎.

} else {
victory_program(&dir)
};
if !executable.is_file() {
return Err(format!("the build made no {}", executable.display()));
}
let marker = rust_install_file(&p.repo);
if marker.exists() {
if renamed {
let executable = Path::new(profile).join(crate::platform::exe_name(program));
record_rust_install(
&p.repo,
&RustInstall {
executable: Some(executable),
victory: true,
},
)?;
} else if marker.exists() {
std::fs::remove_file(marker).map_err(|error| error.to_string())?;
}
} else {
Expand Down Expand Up @@ -2590,13 +2619,13 @@ fn build_project(
.map(|exe| Path::new(exe).strip_prefix(target).map(Path::to_owned))
.transpose()
.map_err(|_| "Cargo reported an executable outside the install folder")?;
let marker = rust_install_file(&p.repo);
let temporary = marker.with_extension("tmp");
let record =
serde_json::to_vec(&RustInstall { executable }).map_err(|error| error.to_string())?;
std::fs::write(&temporary, record)
.and_then(|_| std::fs::rename(&temporary, &marker))
.map_err(|error| format!("Could not record the installed Rust project: {error}"))?;
record_rust_install(
&p.repo,
&RustInstall {
executable,
victory: false,
},
)?;
}
Ok(())
}
Expand Down Expand Up @@ -2901,12 +2930,13 @@ fn runnable(path: PathBuf) -> Result<PathBuf, String> {
Ok(path)
}

/// The victory program `p` runs with: its build, or a build already on this PC.
/// The program `p` runs with: its build (under whatever name the project gives it), or a
/// build already on this PC.
fn project_exe(p: &Project) -> Result<PathBuf, String> {
let exe = p
.archive_program()
.or_else(|| p.local_exe.as_ref().map(PathBuf::from))
.unwrap_or_else(|| built_exe(&p.repo));
.unwrap_or_else(|| installed_exe(&p.repo));
if crate::platform::is_program(&exe) {
Ok(exe)
} else {
Expand Down Expand Up @@ -3821,8 +3851,8 @@ fn fix_build(
stage(job, ctx, msg_str!("Rebuilding the missing program"));
install(job, ctx, p, project_installed(&p.repo), l)?;
// The build just ran, so a missing program is not "not installed": the file is gone
// (antivirus often removes a new victory.exe) or the build left it elsewhere.
let expected = built_exe(&p.repo);
// (antivirus often removes a new program) or the build left it elsewhere.
let expected = installed_exe(&p.repo);
let found = project_exe(p).map_err(|_| {
format!(
"The build finished, but its program is not at {}. Check whether antivirus removed it, then press Fix and verify again.",
Expand Down Expand Up @@ -4236,8 +4266,13 @@ fn run_fixes(
));
continue;
}
// A standalone program can run without the game folder; the others cannot.
if p.runs_standalone() && finding.fix == Some(diagnose::FIX_GAME_FILES) {
// A standalone program can run without the game folder, and without the Agility
// SDK when it does not name it; the others cannot.
if p.runs_standalone()
&& (finding.fix == Some(diagnose::FIX_GAME_FILES)
|| (finding.fix == Some(diagnose::FIX_AGILITY)
&& !diagnose::exe_names_agility(&exe)))
{
continue;
}
tried += 1;
Expand Down
64 changes: 64 additions & 0 deletions src-tauri/src/launcher/parallel_install_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ fn two_installs_and_an_update_overlap() {
first.lock().unwrap().running = false;
let prior = repo_state(update).built.unwrap();
assert!(installed_exe(&update.repo).is_file());
// The program is not called `victory`: Play and Fix and verify must still find it, and a
// program that does not load the Agility SDK gets no D3D12 folder (#69, #88).
let program = project_exe(update).unwrap();
assert_eq!(program, installed_exe(&update.repo));
assert_eq!(
program.file_name().unwrap(),
std::ffi::OsStr::new(&crate::platform::exe_name("fixture_update"))
);
assert!(!program.with_file_name("D3D12").exists());
assert!(update.runs_standalone());
renamed_victory_is_installed(&root, &ctx);

for name in names {
let source = root.join(name);
Expand Down Expand Up @@ -121,6 +132,59 @@ fn two_installs_and_an_update_overlap() {
);
}

/// A fork that renamed the `victory` package's program, beside a helper program named as its
/// package (SlaktarnVI's `valhalla` and `xtask`): the renamed one is recorded and played as a
/// VIctory build.
fn renamed_victory_is_installed(root: &Path, ctx: &Ctx) {
let dir = root.join("renamed_victory");
let write = |path: &str, text: &str| {
let path = dir.join(path);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, text).unwrap();
};
let package =
|name: &str| format!("[package]\nname = '{name}'\nversion = '0.1.0'\nedition = '2021'\n");
write(
"Cargo.toml",
"[workspace]\nresolver = '2'\nmembers = ['apps/victory', 'victory-hle', 'xtask']\n",
);
write(
"apps/victory/Cargo.toml",
&format!(
"{}[[bin]]\nname = 'valhalla'\npath = 'src/main.rs'\n",
package("victory")
),
);
write("apps/victory/src/main.rs", "fn main() {}\n");
write("victory-hle/Cargo.toml", &package("victory-hle"));
write("victory-hle/src/lib.rs", "");
write("xtask/Cargo.toml", &package("xtask"));
write("xtask/src/main.rs", "fn main() {}\n");
git(&dir, &["init", "-q"]);
git(&dir, &["add", "."]);
git(&dir, &["commit", "-qm", "Initial fixture"]);
for custom in [true, false] {
let project = Project {
name: "renamed".into(),
custom,
repo: url::Url::from_directory_path(&dir).unwrap().to_string(),
..test_project()
};
let job = Arc::new(Mutex::new(Job::default()));
let target = target_dir(&project.repo);
build_project(&job, ctx, &project, &dir, &target)
.unwrap_or_else(|error| panic!("{error}\n{}", job.lock().unwrap().log.join("\n")));
assert_eq!(
project_exe(&project).unwrap(),
target
.join("release")
.join(crate::platform::exe_name("valhalla"))
);
assert!(!project.runs_standalone());
assert!(project_installed(&project.repo));
}
}

fn make_project(root: &Path, name: &str) -> Project {
let dir = root.join(name);
fs::create_dir_all(dir.join("src")).unwrap();
Expand Down
66 changes: 52 additions & 14 deletions src-tauri/src/rust_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,26 @@ fn binaries(package: &Value) -> impl Iterator<Item = &str> {
})
}

/// Whether the workspace has the `victory` package with its `victory` program: the one
/// the launcher builds by name (`cargo build -p victory`).
pub fn has_victory_program(metadata: &Value) -> bool {
metadata["packages"].as_array().is_some_and(|packages| {
packages.iter().any(|package| {
package["name"] == "victory" && binaries(package).any(|name| name == "victory")
})
})
/// The program of the workspace's `victory` package: the one the launcher builds by name
/// (`cargo build -p victory`). It is `victory`, or the package's only program when a fork
/// renamed it (Valhalla's `valhalla`) and kept the layer's own crates.
pub fn victory_program(metadata: &Value) -> Option<&str> {
let packages = metadata["packages"].as_array()?;
let package = packages
.iter()
.find(|package| package["name"] == "victory")?;
if binaries(package).any(|name| name == "victory") {
return Some("victory");
}
let mut programs = binaries(package);
let only = programs.next()?;
(programs.next().is_none() && has_layer_crates(packages)).then_some(only)
}

fn has_layer_crates(packages: &[Value]) -> bool {
packages
.iter()
.any(|package| package["name"] == "victory-hle" || package["name"] == "victory-loader")
}

/// Whether a custom project is a VIctory build: the program, plus the layer's own folders
Expand All @@ -49,13 +61,11 @@ pub fn is_victory(workspace: &Path, metadata: &Value) -> bool {
let Some(packages) = metadata["packages"].as_array() else {
return false;
};
has_victory_program(metadata)
victory_program(metadata).is_some()
&& (["apps/victory", "crates/victory", "victory"]
.iter()
.any(|path| workspace.join(path).join("Cargo.toml").is_file())
|| packages.iter().any(|package| {
package["name"] == "victory-hle" || package["name"] == "victory-loader"
}))
|| has_layer_crates(packages))
}

pub fn select_executable<'a>(metadata: &Value, artifacts: &'a [Value]) -> Option<&'a str> {
Expand Down Expand Up @@ -96,14 +106,42 @@ fn a_listed_project_without_the_victory_package_is_not_built_by_name() {
"targets": [{ "kind": ["bin"], "name": "anyxboxx" }]
}]
});
assert!(!has_victory_program(&without));
assert_eq!(victory_program(&without), None);
let with = serde_json::json!({
"packages": [{
"name": "victory",
"targets": [{ "kind": ["bin"], "name": "victory" }]
}]
});
assert!(has_victory_program(&with));
assert_eq!(victory_program(&with), Some("victory"));
}

#[cfg(test)]
#[test]
fn a_renamed_victory_program_is_still_the_victory_package() {
// SlaktarnVI (#69, #88): the `victory` package's only program is `valhalla`.
let hle = serde_json::json!({
"name": "victory-hle", "targets": [{ "kind": ["lib"], "name": "victory_hle" }]
});
let app = |bins: &[&str]| {
let targets: Vec<_> = bins
.iter()
.map(|name| serde_json::json!({ "kind": ["bin"], "name": name }))
.collect();
serde_json::json!({ "name": "victory", "targets": targets })
};
let workspace = |packages: &[&Value]| serde_json::json!({ "packages": packages });
let renamed = workspace(&[&app(&["valhalla"]), &hle]);
assert_eq!(victory_program(&renamed), Some("valhalla"));
// Its own name wins; another package called victory, or one of several programs, is no guess.
let both = workspace(&[&app(&["other", "victory"]), &hle]);
assert_eq!(victory_program(&both), Some("victory"));
assert_eq!(victory_program(&workspace(&[&app(&["valhalla"])])), None);
assert_eq!(
victory_program(&workspace(&[&app(&["a", "b"]), &hle])),
None
);
assert_eq!(victory_program(&workspace(&[&app(&[]), &hle])), None);
}

#[test]
Expand Down