diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index 89849c12f..9612bda2b 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -1524,7 +1524,7 @@ pub fn append_game_args(cmd: &mut Command) { pub fn client_command(tag: &str) -> Command { let home = game_home(); let _ = fs::create_dir_all(&home); - let mut cmd = Command::new(version_dir(tag).join(CLIENT)); + let mut cmd = Command::new(client_program(tag)); cmd.current_dir(home).arg("--resources").arg(resources(tag)); // PipeWire remembers volume and mute per application name. The game's own name is // "PipeWire ALSA [horizon-client]", shared with development and test runs, where @@ -1538,6 +1538,18 @@ pub fn client_command(tag: &str) -> Command { cmd } +/// The game's program: on macOS the one inside the version's Horizon.app, so the Dock and Cmd+Tab +/// show Horizon's name and icon (game_app.rs); the bare binary elsewhere, or if that fails. +fn client_program(tag: &str) -> PathBuf { + let dir = version_dir(tag); + #[cfg(target_os = "macos")] + match crate::game_app::ensure(&dir) { + Ok(program) => return program, + Err(e) => eprintln!("Starting the game outside {}: {e}", crate::game_app::APP), + } + dir.join(CLIENT) +} + pub fn server_command(tag: &str, folder: &Path) -> Command { let mut cmd = Command::new(version_dir(tag).join(CLIENT)); cmd.current_dir(folder) diff --git a/launcher/src/game_app.rs b/launcher/src/game_app.rs new file mode 100644 index 000000000..d4fc9972d --- /dev/null +++ b/launcher/src/game_app.rs @@ -0,0 +1,146 @@ +//! The game's app bundle on macOS. Started as a bare executable, the game shows in the Dock and +//! in Cmd+Tab as "horizon-client" with the generic executable icon (winit's window icon does +//! nothing there). So the launcher starts it from a small Horizon.app inside its version folder: +//! the Info.plist names it and the icon is the launcher's own. The programs inside are hard links +//! to the version's, so the bundle takes no disk space and the release's signature is untouched. +use std::fs; +use std::io; +use std::os::unix::fs::MetadataExt; +use std::path::{Path, PathBuf}; + +/// The bundle's folder in a version folder. +pub const APP: &str = "Horizon.app"; + +/// The game, then horizon-ctl, which the game looks for beside itself (Copy Connection Details). +const PROGRAMS: [&str; 2] = ["horizon-client", "horizon-ctl"]; + +const ICON: &[u8] = include_bytes!("../../packaging/icons/horizon.icns"); + +/// Its own identifier, so macOS keeps it apart from the launcher (dev.horizon.launcher); the +/// games category lets macOS turn Game Mode on when it plays full screen. +const INFO_PLIST: &str = r#" + + + + CFBundleDisplayName + Horizon + CFBundleExecutable + horizon-client + CFBundleIconFile + horizon + CFBundleIdentifier + dev.horizon.game + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + Horizon + CFBundlePackageType + APPL + LSApplicationCategoryType + public.app-category.games + NSHighResolutionCapable + + + +"#; + +/// Brings `version`'s Horizon.app up to date and returns the game's program inside it. A program +/// replaced in the version folder (a reinstall, a build copied over) is linked again. +pub fn ensure(version: &Path) -> io::Result { + let contents = version.join(APP).join("Contents"); + let programs = contents.join("MacOS"); + fs::create_dir_all(&programs)?; + fs::create_dir_all(contents.join("Resources"))?; + write_if_changed(&contents.join("Info.plist"), INFO_PLIST.as_bytes())?; + write_if_changed(&contents.join("Resources/horizon.icns"), ICON)?; + for (i, name) in PROGRAMS.into_iter().enumerate() { + let (from, to) = (version.join(name), programs.join(name)); + // Releases before horizon-ctl shipped have only the game. + if i > 0 && !from.exists() { + continue; + } + if !same_file(&from, &to) { + let _ = fs::remove_file(&to); + fs::hard_link(&from, &to)?; + } + } + Ok(programs.join(PROGRAMS[0])) +} + +fn write_if_changed(path: &Path, bytes: &[u8]) -> io::Result<()> { + if fs::read(path).ok().as_deref() != Some(bytes) { + fs::write(path, bytes)?; + } + Ok(()) +} + +fn same_file(a: &Path, b: &Path) -> bool { + match (fs::metadata(a), fs::metadata(b)) { + (Ok(a), Ok(b)) => a.dev() == b.dev() && a.ino() == b.ino(), + _ => false, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_bundle_links_the_version_programs_and_follows_a_replaced_one() { + let version = std::env::temp_dir().join(format!("horizon-game-app-{}", std::process::id())); + let _ = fs::remove_dir_all(&version); + fs::create_dir_all(&version).unwrap(); + fs::write(version.join("horizon-client"), "game 1").unwrap(); + fs::write(version.join("horizon-ctl"), "ctl").unwrap(); + + let program = ensure(&version).unwrap(); + assert_eq!( + program, + version.join("Horizon.app/Contents/MacOS/horizon-client") + ); + assert!(same_file(&program, &version.join("horizon-client"))); + assert!(same_file( + &version.join("Horizon.app/Contents/MacOS/horizon-ctl"), + &version.join("horizon-ctl") + )); + let plist = fs::read_to_string(version.join("Horizon.app/Contents/Info.plist")).unwrap(); + assert!(plist.contains("Horizon")); + assert_eq!( + fs::read(version.join("Horizon.app/Contents/Resources/horizon.icns")).unwrap(), + ICON + ); + + // A new build copied over the version's game: the bundle runs it, not the old one. + fs::remove_file(version.join("horizon-client")).unwrap(); + fs::write(version.join("horizon-client"), "game 2").unwrap(); + assert_eq!( + fs::read_to_string(ensure(&version).unwrap()).unwrap(), + "game 2" + ); + fs::remove_dir_all(&version).unwrap(); + } + + #[test] + fn a_version_without_horizon_ctl_still_gets_its_bundle() { + let version = + std::env::temp_dir().join(format!("horizon-game-app-old-{}", std::process::id())); + let _ = fs::remove_dir_all(&version); + fs::create_dir_all(&version).unwrap(); + fs::write(version.join("horizon-client"), "game").unwrap(); + assert!(ensure(&version).unwrap().exists()); + assert!(!version + .join("Horizon.app/Contents/MacOS/horizon-ctl") + .exists()); + fs::remove_dir_all(&version).unwrap(); + } + + #[test] + fn a_version_without_the_game_has_no_bundle_program() { + let version = + std::env::temp_dir().join(format!("horizon-game-app-none-{}", std::process::id())); + let _ = fs::remove_dir_all(&version); + fs::create_dir_all(&version).unwrap(); + assert!(ensure(&version).is_err()); + fs::remove_dir_all(&version).unwrap(); + } +} diff --git a/launcher/src/lib.rs b/launcher/src/lib.rs index 61a9713ff..aa8aec7f2 100644 --- a/launcher/src/lib.rs +++ b/launcher/src/lib.rs @@ -4,6 +4,8 @@ pub mod agents; pub mod backend; pub mod browser; pub mod desktop; +#[cfg(unix)] +pub mod game_app; pub mod heads; pub mod modpacks; pub mod mojang; diff --git a/launcher/src/servers.rs b/launcher/src/servers.rs index 87dccbf35..49ef05d66 100644 --- a/launcher/src/servers.rs +++ b/launcher/src/servers.rs @@ -197,13 +197,14 @@ pub fn set_up_in(root: &Path, version: &Path, tag: &str, game_version: &str) -> Ok(()) } -/// A release folder without the launcher's own files (its prepared copy and install marker). +/// A release folder without the launcher's own files (its prepared copy, install marker and the +/// game's macOS app bundle). fn copy_release(from: &Path, to: &Path) -> std::io::Result<()> { fs::create_dir_all(to)?; for entry in fs::read_dir(from)? { let entry = entry?; let name = entry.file_name(); - if name == ".local" || name == ".installed" { + if name == ".local" || name == ".installed" || name == "Horizon.app" { continue; } let target = to.join(&name); diff --git a/launcher/src/servers/tests.rs b/launcher/src/servers/tests.rs index f096baf3a..890deb64e 100644 --- a/launcher/src/servers/tests.rs +++ b/launcher/src/servers/tests.rs @@ -58,6 +58,7 @@ fn programs_are_laid_out_as_install_sh_does() { fs::write(version.join(PROGRAM), "server").unwrap(); fs::write(version.join("horizon-client"), "game").unwrap(); fs::write(version.join(".installed"), "").unwrap(); + crate::game_app::ensure(&version).unwrap(); fs::write( version.join(".local/horizon-client/26.3/horizon-resources.json"), "{}", @@ -70,6 +71,8 @@ fn programs_are_laid_out_as_install_sh_does() { "server" ); assert!(!rt.join("releases/v1/.installed").exists()); + // The game's macOS bundle is the launcher's, not the server's. + assert!(!rt.join("releases/v1/Horizon.app").exists()); // The release finds the game files where horizon-server looks: .local beside it. assert!(rt .join("current/.local/horizon-client/26.3/horizon-resources.json")