Launcher: catch up versions an older launcher installed without preparing them

Launcher 0.1.3 auto-installed v2026.10.13.1 unprepared; after updating to 0.1.4 nothing installs
it again, so install's reuse never ran for it. backend::reuse_prepared_everywhere() applies the
same reuse (same compatibility checks, copied then renamed) to every installed version that is
not prepared, oldest first. It runs when the window starts (on a thread; the page refreshes when
it is done), in the CLI's `versions` and `update`, and in the MCP list_versions and check_updates
tools. The partial folder is now named per copy, and a version prepared meanwhile is left as it is,
since the window's start and an install can copy at the same time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-13 21:11:43 +02:00
parent 8fa3eef809
commit 1404dd5a0a
4 changed files with 67 additions and 8 deletions
+48 -8
View File
@@ -486,20 +486,43 @@ fn reuse_prepared(versions: &Path, tag: &str) -> Option<String> {
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<PathBuf> {
let home = dirs::home_dir()?;
@@ -1080,6 +1103,8 @@ pub enum Msg {
Updates(Result<Updates, String>),
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);
}
+7
View File
@@ -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();
+9
View File
@@ -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));
+3
View File
@@ -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<String, String> {
if matches!(name, "list_versions" | "check_updates") {
backend::reuse_prepared_everywhere();
}
match name {
"list_versions" => {
let installed = backend::installed();