diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index 67fe93665..f83716966 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -486,20 +486,43 @@ fn reuse_prepared(versions: &Path, tag: &str) -> Option { continue; } // Copied beside it and renamed when complete: an interrupted copy is never "prepared". - let partial = to.with_file_name(format!(".{version}.partial")); + // The name is this call's own, as the window's start and an install can both copy. + static COPIES: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + let n = COPIES.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let partial = to.with_file_name(format!(".{version}.partial-{}-{n}", std::process::id())); let _ = fs::remove_dir_all(&partial); let copied = copy_dir(&from, &partial).and_then(|_| { + if to.join(RESOURCES_MANIFEST).exists() { + return Ok(false); // Prepared meanwhile, by another copy or the player. + } let _ = fs::remove_dir_all(&to); - fs::rename(&partial, &to) + fs::rename(&partial, &to).map(|_| true) }); - if copied.is_ok() { - return Some(other); - } let _ = fs::remove_dir_all(&partial); + match copied { + Ok(true) => return Some(other), + Ok(false) => return None, + Err(_) => {} + } } None } +/// `reuse_prepared` for every installed version that is not prepared, oldest first, so a +/// version prepared this way can serve the next. Launchers before 0.1.4 installed new versions +/// without it; this catches those up. Returns each (version, where its resources came from). +pub fn reuse_prepared_everywhere() -> Vec<(String, String)> { + reuse_prepared_in(&versions_dir()) +} + +fn reuse_prepared_in(versions: &Path) -> Vec<(String, String)> { + let mut tags = installed_in(versions); + tags.reverse(); + tags.into_iter() + .filter_map(|tag| reuse_prepared(versions, &tag).map(|from| (tag, from))) + .collect() +} + /// The official launcher's game directory, where prepare_native_client.py looks by default. pub fn default_minecraft_dir() -> Option { let home = dirs::home_dir()?; @@ -1080,6 +1103,8 @@ pub enum Msg { Updates(Result), LauncherProgress(f32), LauncherUpdated(Result<(), String>), + /// `reuse_prepared_everywhere` prepared these versions. + Reused(Vec<(String, String)>), } /// A running process; its output arrives as `Msg::Line`, its end as `Msg::Exit`. @@ -1398,9 +1423,12 @@ mod tests { assert!(resources_at(&versions.join("v2026.10.13.1")) .join("worldgen/biomes.json") .exists()); - assert!(!versions - .join("v2026.10.13.1/.local/horizon-client/.26.3.partial") - .exists()); + let left: Vec<_> = fs::read_dir(versions.join("v2026.10.13.1/.local/horizon-client")) + .unwrap() + .flatten() + .map(|e| e.file_name()) + .collect(); + assert_eq!(left, ["26.3"]); // Already prepared: nothing to do. assert_eq!(reuse_prepared(&versions, "v2026.10.13.1"), None); @@ -1414,6 +1442,18 @@ mod tests { write("v2026.10.16", "tools/prepare_native_client.py", "schema 8"); assert_eq!(reuse_prepared(&versions, "v2026.10.16"), None); assert!(!is_prepared("v2026.10.15") && !is_prepared("v2026.10.16")); + + // A version an older launcher installed without preparing it is caught up by the sweep + // that starting the window, `versions` and `update` run; the incompatible ones stay as + // they are, and a second sweep has nothing to do. + release("v2026.10.17", "26.3"); + assert_eq!( + reuse_prepared_in(&versions), + [("v2026.10.17".to_string(), "v2026.10.13.1".to_string())] + ); + assert!(is_prepared("v2026.10.17")); + assert!(!is_prepared("v2026.10.15") && !is_prepared("v2026.10.16")); + assert!(reuse_prepared_in(&versions).is_empty()); let _ = fs::remove_dir_all(&versions); } diff --git a/launcher/src/cli.rs b/launcher/src/cli.rs index 2d4d0dddc..67ace9a82 100644 --- a/launcher/src/cli.rs +++ b/launcher/src/cli.rs @@ -137,6 +137,13 @@ fn release_json(r: &Release) -> serde_json::Value { } fn execute(command: Cmd, as_json: bool) -> Result<(), String> { + if matches!(command, Cmd::Versions { .. } | Cmd::Update { .. }) { + for (tag, from) in backend::reuse_prepared_everywhere() { + if !as_json { + eprintln!("Prepared {tag} with the resources of {from}."); + } + } + } match command { Cmd::Versions { installed: true } => { let installed = backend::installed(); diff --git a/launcher/src/main.rs b/launcher/src/main.rs index f1445be6a..3a46184a5 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -227,6 +227,13 @@ impl Launcher { rx, _appearance: appearance, }; + let tx = this.tx.clone(); + std::thread::spawn(move || { + let reused = backend::reuse_prepared_everywhere(); + if !reused.is_empty() { + let _ = tx.send(Msg::Reused(reused)); + } + }); this.fetch_releases(); this.fetch_roadmap(); this.fetch_links(); @@ -321,6 +328,8 @@ impl Launcher { Msg::Releases(r) => self.releases = r.map_or_else(Load::Failed, Load::Ready), Msg::Roadmap(r) => self.roadmap = r.map_or_else(Load::Failed, Load::Ready), Msg::Links(links) => self.links = links, + // The page reads is_prepared when drawn; `changed` redraws it. + Msg::Reused(_) => self.installed = backend::installed(), Msg::Progress(tag, p) => { self.bar = (self.bar.1, p); self.installing = Some((tag, p)); diff --git a/launcher/src/mcp.rs b/launcher/src/mcp.rs index c434dee16..af9f901bc 100644 --- a/launcher/src/mcp.rs +++ b/launcher/src/mcp.rs @@ -53,6 +53,9 @@ fn arg_str<'a>(args: &'a Value, key: &str) -> Option<&'a str> { impl Session { fn call(&mut self, name: &str, args: &Value) -> Result { + if matches!(name, "list_versions" | "check_updates") { + backend::reuse_prepared_everywhere(); + } match name { "list_versions" => { let installed = backend::installed();