Merge feat/launcher-horizon-mods (main merged) into feat/launcher-horizon-tab

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-25 14:57:31 +01:00
266 changed files with 23284 additions and 3130 deletions
+31 -25
View File
@@ -282,11 +282,6 @@ pub fn releases() -> Result<Vec<Release>, String> {
Ok(parse_releases(&release_list()?, ASSET, is_game_tag))
}
/// Published launcher releases (`launcher-v…`), newest first, with this platform's archive.
pub fn launcher_releases() -> Result<Vec<Release>, String> {
Ok(launcher_releases_in(&release_list()?))
}
/// The launcher releases in `list` (the newest 100 releases). Game releases come out several times
/// a day, so the newest launcher release can be older than all of those: then the newest
/// `launcher@` and `launcher-v` tags are looked up one by one (a draft's tag is skipped: it has no
@@ -563,7 +558,19 @@ pub fn install(release: &Release, progress: impl Fn(f32)) -> Result<(), String>
}
pub fn remove(tag: &str) -> Result<(), String> {
fs::remove_dir_all(version_dir(tag)).map_err(|e| e.to_string())
let dir = version_dir(tag);
// Windows holds a file for a moment after the game, a scan or the launcher's own reader lets
// go of it, and remove_dir_all stops at the first one. Retry for a second; a folder that is
// already gone is removed.
for attempt in 1.. {
match fs::remove_dir_all(&dir) {
Ok(()) => break,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => break,
Err(e) if attempt == 5 => return Err(e.to_string()),
Err(_) => std::thread::sleep(std::time::Duration::from_millis(250)),
}
}
Ok(())
}
/// The Minecraft version a Horizon build targets, from its research-lock.json.
@@ -1027,21 +1034,18 @@ fn download_for_owner(
}
}
/// A JDK of at least `major` for the preparation. `None` when horizon-prepare finds one by
/// itself (JAVA_HOME, the PATH, an installed JDK): the preparation then runs as before, in an
/// unchanged environment (a JDK found on the PATH can be a stub such as macOS's
/// /usr/bin/javac, whose folder is no Java home: `JAVA_HOME=/usr` makes it call itself
/// forever). Else the home of the Java the official launcher downloaded into one of the
/// `games` folders, else of Minecraft's own Java downloaded from Mojang into Horizon's game
/// folder, the runtime the version JSON at `manifest` names (Mojang's runtimes are whole JDKs).
/// The home of a JDK whose compiler and runtime support `major`. Forward even an installed
/// JDK to external preparers: older versions may otherwise rediscover incompatible PATH tools.
/// Else try the official launcher's Java in `games`, then download the runtime named by
/// `manifest` into Horizon's game folder (Mojang's runtimes are whole JDKs).
fn java(
major: i64,
games: &[PathBuf],
manifest: &Path,
log: &dyn Fn(String),
) -> Result<Option<PathBuf>, String> {
if horizon_prepare::java::find(major, &[]).is_ok() {
return Ok(None);
) -> Result<PathBuf, String> {
if let Ok(jdk) = horizon_prepare::java::find(major, &[]) {
return Ok(jdk.home());
}
let mut runtimes: Vec<PathBuf> = Vec::new();
for home in games
@@ -1053,7 +1057,7 @@ fn java(
}
}
let missing = match horizon_prepare::java::find(major, &runtimes) {
Ok(jdk) => return Ok(Some(jdk.home())),
Ok(jdk) => return Ok(jdk.home()),
Err(e) => e,
};
let component = read_json(manifest)
@@ -1078,7 +1082,7 @@ fn java(
let home = mojang::java_runtime(&component, &runtime, 8, &fetch, log).map_err(|e| {
format!("{missing}\n • Horizon also tried to download Minecraft's own Java from Mojang, but: {e}")
})?;
horizon_prepare::java::find(major, &[home]).map(|jdk| Some(jdk.home()))
horizon_prepare::java::find(major, &[home]).map(|jdk| jdk.home())
}
/// The dynamic loader glibc programs on x86-64 Linux start through.
@@ -1201,14 +1205,12 @@ pub fn preparer(
import: !reused,
minecraft_dir: Some(minecraft.clone()),
server_jar: (!reused).then_some(jar),
java_home: java_home.clone(),
java_home: Some(java_home.clone()),
};
return Ok((Preparer::InProcess(options), minecraft));
}
let mut cmd = Command::new(binary);
if let Some(home) = &java_home {
with_java(&mut cmd, home);
}
with_java(&mut cmd, &java_home);
cmd.current_dir(&dir).arg("--root").arg(&dir);
if !reused {
cmd.args(["--prepare", "--server-jar"]).arg(&jar);
@@ -1217,9 +1219,7 @@ pub fn preparer(
return Ok((Preparer::Process(cmd), minecraft));
}
let mut cmd = Command::new(python.unwrap());
if let Some(home) = &java_home {
with_java(&mut cmd, home);
}
with_java(&mut cmd, &java_home);
cmd.current_dir(&dir)
.args(["-u", "tools/prepare_native_client.py"]);
if !reused {
@@ -2176,6 +2176,8 @@ pub enum Msg {
Links(Links),
Progress(String, f32),
Installed(String, Result<(), String>),
/// A version's files are gone (or could not be deleted).
Removed(String, Result<(), String>),
Line(Job, String),
Exit(Job, Result<(), String>),
Updates(Result<Updates, String>),
@@ -2342,6 +2344,10 @@ impl Settings {
#[path = "backend/release_loading_tests.rs"]
mod release_loading_tests;
#[cfg(test)]
#[path = "backend/java_tests.rs"]
mod java_tests;
#[cfg(test)]
mod tests {
use super::*;
+85
View File
@@ -0,0 +1,85 @@
use super::*;
#[test]
fn selected_jdk_reaches_external_preparers() {
const CHILD: &str = "HORIZON_LAUNCHER_JAVA_TEST";
if let Some(root) = std::env::var_os(CHILD) {
let root = PathBuf::from(root);
let home = java(25, &[], &root.join("missing.json"), &|_| {}).unwrap();
assert_eq!(
fs::canonicalize(&home).unwrap(),
fs::canonicalize(root.join("jdk25")).unwrap()
);
// An older preparer invokes Java tools by name; both must now resolve to this JDK.
for name in ["java", "javac"] {
let mut cmd = crate::process::command(name);
with_java(&mut cmd, &home);
let output = cmd.arg("-version").output().unwrap();
assert!(output.status.success());
let text = String::from_utf8_lossy(&output.stdout).into_owned()
+ &String::from_utf8_lossy(&output.stderr);
assert!(text.contains("25.0.1"), "{name}: {text}");
assert_eq!(
cmd.get_envs()
.find(|(key, _)| *key == "JAVA_HOME")
.unwrap()
.1,
Some(home.as_os_str())
);
}
return;
}
let root = std::env::temp_dir().join(format!("horizon-launcher-java-{}", std::process::id()));
fs::create_dir_all(&root).unwrap();
let executable =
|bin: &Path, name: &str| bin.join(format!("{name}{}", std::env::consts::EXE_SUFFIX));
let probe = executable(&root, "probe");
let source = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../crates/horizon-prepare/tests/support/java_probe.rs");
let output = crate::process::command("rustc")
.arg(source)
.arg("-o")
.arg(&probe)
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
for (home, compiler, runtime) in [
("jre21", None, "21.0.1"),
("jdk25", Some("25.0.1"), "25.0.1"),
] {
let bin = root.join(home).join("bin");
fs::create_dir_all(&bin).unwrap();
for (name, version) in [("javac", compiler), ("java", Some(runtime))] {
if let Some(version) = version {
fs::copy(&probe, executable(&bin, name)).unwrap();
fs::write(bin.join(format!("{name}.version")), version).unwrap();
}
}
}
let output = crate::process::command(std::env::current_exe().unwrap())
.args([
"--exact",
"backend::java_tests::selected_jdk_reaches_external_preparers",
"--nocapture",
])
.env(CHILD, &root)
.env_remove("JAVA_HOME")
.env(
"PATH",
std::env::join_paths([root.join("jre21/bin"), root.join("jdk25/bin")]).unwrap(),
)
.output()
.unwrap();
assert!(
output.status.success(),
"{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
fs::remove_dir_all(root).unwrap();
}
+67 -9
View File
@@ -144,6 +144,9 @@ struct Launcher {
releases: Load<Vec<Release>>,
installed: Vec<String>,
installing: Option<(String, f32)>,
/// The version being deleted on a background thread. It is already out of `installed`, so
/// nothing can launch, select or prepare it, and its row can't install or remove it again.
removing: Option<String>,
install_error: Option<(String, String)>,
confirm_remove: Option<String>,
picker_open: bool,
@@ -286,6 +289,7 @@ impl Launcher {
releases: Load::Loading,
installed,
installing: None,
removing: None,
install_error: None,
confirm_remove: None,
picker_open: false,
@@ -415,7 +419,7 @@ impl Launcher {
let Some(release) = self.updates.horizon.clone() else {
return;
};
if self.installing.is_some() {
if self.installing.is_some() || self.removing.as_ref() == Some(&release.tag) {
return;
}
let select =
@@ -542,6 +546,17 @@ impl Launcher {
Err(e) => self.install_error = Some((tag, e)),
}
}
Msg::Removed(tag, result) => {
if self.removing.as_ref() == Some(&tag) {
self.removing = None;
}
self.installed = backend::installed();
self.settings.selected =
selection_after_removal(&self.installed, Settings::load().selected);
if let Err(e) = result {
self.install_error = Some((tag, e));
}
}
Msg::Updates(Ok(updates)) => {
self.update_error = None;
self.updates = updates;
@@ -706,7 +721,7 @@ impl Launcher {
}
fn install(&mut self, release: &Release) {
if release.asset.is_none() {
if release.asset.is_none() || self.removing.as_ref() == Some(&release.tag) {
return;
}
let (release, tx) = (release.clone(), self.tx.clone());
@@ -724,15 +739,26 @@ impl Launcher {
});
}
/// Deletes an installed version on a background thread: a version is thousands of files and
/// the window must keep drawing meanwhile. `Msg::Removed` ends it.
fn remove(&mut self, tag: &str) {
if let Err(e) = backend::remove_version(tag) {
self.install_error = Some((tag.to_string(), e));
}
self.confirm_remove = None;
self.installed = backend::installed();
self.settings.selected = Settings::load()
.selected
.or_else(|| self.installed.first().cloned());
if self.removing.is_some()
|| self.installing.as_ref().is_some_and(|(t, _)| t == tag)
|| !self.installed.iter().any(|t| t == tag)
{
return;
}
self.install_error = None;
self.removing = Some(tag.to_string());
self.installed.retain(|t| t != tag);
self.settings.selected =
selection_after_removal(&self.installed, self.settings.selected.take());
let (tag, tx) = (tag.to_string(), self.tx.clone());
std::thread::spawn(move || {
let result = backend::remove_version(&tag);
let _ = tx.send(Msg::Removed(tag, result));
});
}
fn prepare(&mut self) {
@@ -1158,6 +1184,14 @@ fn segments<T: Copy + PartialEq + 'static>(
}))
}
/// The selected version once one has been removed: the saved choice if it is still installed,
/// else the newest left.
fn selection_after_removal(installed: &[String], saved: Option<String>) -> Option<String> {
saved
.filter(|s| installed.contains(s))
.or_else(|| installed.first().cloned())
}
fn disabled(b: Stateful<Div>) -> Stateful<Div> {
b.opacity(theme::DISABLED).cursor_default().tab_stop(false)
}
@@ -2923,6 +2957,7 @@ impl Launcher {
self.installing.as_ref().is_some_and(|(t, _)| t == tag)
|| self.install_error.as_ref().is_some_and(|(t, _)| t == tag)
|| self.confirm_remove.as_ref() == Some(tag)
|| self.removing.as_ref() == Some(tag)
};
let list = self.long_list.clone().column(
&self.content_scroll.handle,
@@ -2959,6 +2994,15 @@ impl Launcher {
.child(format!("{:.0}%", p * 100.)),
)
.into_any_element()
} else if self.removing.as_ref() == Some(&tag) {
div()
.flex()
.items_center()
.gap(px(10.))
.w(px(180.))
.child(progress_bar(None, &t))
.child(text(theme::MICRO).text_color(t.muted).child("Removing…"))
.into_any_element()
} else if installed {
let confirming = self.confirm_remove.as_ref() == Some(&tag);
let (t1, t2) = (tag.clone(), tag.clone());
@@ -3587,6 +3631,20 @@ mod tests {
assert!(hit.iter().any(|b| b.action().partial_eq(&CloseWindow)));
}
#[test]
fn the_selection_survives_a_removal_or_moves_to_the_newest_left() {
let left = ["v0.2.49".to_string(), "v0.2.48".to_string()];
let pick =
|saved: Option<&str>| super::selection_after_removal(&left, saved.map(String::from));
assert_eq!(pick(Some("v0.2.48")).as_deref(), Some("v0.2.48"));
assert_eq!(pick(Some("v0.2.47")).as_deref(), Some("v0.2.49"));
assert_eq!(pick(None).as_deref(), Some("v0.2.49"));
assert_eq!(
super::selection_after_removal(&[], Some("v0.2.47".into())),
None
);
}
#[test]
fn the_app_menu_has_quit() {
let menus = super::menus();
+4
View File
@@ -181,12 +181,16 @@ pub fn java_runtime(
Ok(home)
}
// Used by the unix link step below; the path-traversal check itself is tested on every
// platform, so these stay compiled and are allowed to look unused elsewhere.
#[cfg_attr(not(unix), allow(dead_code))]
fn name_in<'a>(home: &Path, path: &'a Path) -> &'a Path {
path.strip_prefix(home).unwrap_or(path)
}
/// Whether a link at `link` (relative to the runtime's home) pointing at `target` stays in the
/// home.
#[cfg_attr(not(unix), allow(dead_code))]
fn link_stays_inside(link: &Path, target: &str) -> bool {
let mut depth = link.components().count().saturating_sub(1);
for c in Path::new(target).components() {