From e5e27c24d4f4cbd24ac22cb361c2ae8dc24c2949 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Tue, 13 Oct 2026 13:11:00 +0200 Subject: [PATCH] Launcher: backend as a shared library; game and launcher tags kept apart, numeric version order, HORIZON_LAUNCHER_HOME Co-Authored-By: Claude Opus 5.5 --- launcher/src/backend.rs | 91 ++++++++++++++++++++++++++++++++++++----- launcher/src/lib.rs | 2 + launcher/src/main.rs | 4 +- 3 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 launcher/src/lib.rs diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index b1a715337..b589775e2 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -1,5 +1,6 @@ -//! Everything that is not UI: GitHub, installs, resource preparation and the game's processes. -//! Functions here block; the UI runs them on threads and reads their messages. +//! Everything that is not UI: GitHub, installs, resource preparation, the game's processes and +//! updates. The GUI, the CLI and the MCP server all go through this module. Functions here +//! block; the GUI runs them on threads and reads their messages. use std::fs; use std::io::{BufRead, BufReader, Read, Write}; use std::path::{Path, PathBuf}; @@ -9,9 +10,13 @@ use std::sync::{Arc, Mutex}; pub const REPO: &str = "ludovic111/horizon"; pub const SPONSOR_URL: &str = "https://github.com/sponsors/ludovic111"; +pub const README_URL: &str = "https://github.com/ludovic111/horizon/blob/launcher/launcher/README.md"; +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); +/// Launcher releases are tagged `launcher-vX.Y.Z`; Horizon's own are `vYYYY.MM.DD`. +pub const LAUNCHER_TAG_PREFIX: &str = "launcher-v"; const AGENT: &str = concat!("horizon-launcher/", env!("CARGO_PKG_VERSION")); -#[derive(Clone, Debug)] +#[derive(Clone, Debug, serde::Serialize)] pub struct Release { pub tag: String, pub name: String, @@ -33,6 +38,21 @@ pub const ASSET: Option<&str> = Some("horizon-windows-x64.zip"); )))] pub const ASSET: Option<&str> = None; +#[cfg(all(target_os = "linux", target_arch = "x86_64"))] +pub const LAUNCHER_ASSET: Option<&str> = Some("horizon-launcher-linux-x86_64.tar.gz"); +#[cfg(all(target_os = "macos", target_arch = "aarch64"))] +pub const LAUNCHER_ASSET: Option<&str> = Some("horizon-launcher-macos-arm64.zip"); +#[cfg(all(target_os = "windows", target_arch = "x86_64"))] +pub const LAUNCHER_ASSET: Option<&str> = Some("horizon-launcher-windows-x64.zip"); +#[cfg(not(any( + all(target_os = "linux", target_arch = "x86_64"), + all(target_os = "macos", target_arch = "aarch64"), + all(target_os = "windows", target_arch = "x86_64") +)))] +pub const LAUNCHER_ASSET: Option<&str> = None; + +const LAUNCHER_BINARY: &str = if cfg!(windows) { "horizon-launcher.exe" } else { "horizon-launcher" }; + const CLIENT: &str = if cfg!(windows) { "horizon-client.exe" } else { "horizon-client" }; fn agent() -> ureq::Agent { @@ -47,17 +67,43 @@ fn get(url: &str) -> Result { }) } -/// Published releases, newest first. Drafts never reach the anonymous API. -pub fn releases() -> Result, String> { - let url = format!("https://api.github.com/repos/{REPO}/releases?per_page=50"); +/// A Horizon game tag: `v` then a digit (`v2026.10.13`). Launcher tags never match. +pub fn is_game_tag(tag: &str) -> bool { + tag.strip_prefix('v').is_some_and(|rest| rest.starts_with(|c: char| c.is_ascii_digit())) +} + +/// The launcher version in a `launcher-vX.Y.Z` tag, or None for any other tag. +pub fn launcher_version(tag: &str) -> Option<&str> { + tag.strip_prefix(LAUNCHER_TAG_PREFIX).filter(|v| v.starts_with(|c: char| c.is_ascii_digit())) +} + +/// The numeric parts of a version or tag: `v2026.10.13` and `2026.10.13` give [2026, 10, 13]. +/// Anything after a `-` (a pre-release) is ignored. +fn version_parts(v: &str) -> Vec { + let v = v.trim_start_matches(LAUNCHER_TAG_PREFIX).trim_start_matches('v'); + v.split('-').next().unwrap_or("").split('.').map(|p| p.parse().unwrap_or(0)).collect() +} + +/// Compares two versions part by part, missing parts counting as zero (1.2 == 1.2.0). +pub fn compare_versions(a: &str, b: &str) -> std::cmp::Ordering { + let (a, b) = (version_parts(a), version_parts(b)); + (0..a.len().max(b.len())) + .map(|i| a.get(i).unwrap_or(&0).cmp(b.get(i).unwrap_or(&0))) + .find(|o| o.is_ne()) + .unwrap_or(std::cmp::Ordering::Equal) +} + +/// Every published release of the repository, both kinds, with the asset named `asset`. +fn all_releases(asset: Option<&str>) -> Result, String> { + let url = format!("https://api.github.com/repos/{REPO}/releases?per_page=100"); let list: serde_json::Value = get(&url)?.into_json().map_err(|e| e.to_string())?; let mut out = Vec::new(); for r in list.as_array().into_iter().flatten() { if r["draft"].as_bool().unwrap_or(false) { continue; } - let asset = r["assets"].as_array().into_iter().flatten().find_map(|a| { - (Some(a["name"].as_str()?) == ASSET) + let found = r["assets"].as_array().into_iter().flatten().find_map(|a| { + (Some(a["name"].as_str()?) == asset) .then(|| Some((a["browser_download_url"].as_str()?.to_string(), a["size"].as_u64()?)))? }); out.push(Release { @@ -65,12 +111,33 @@ pub fn releases() -> Result, String> { name: r["name"].as_str().unwrap_or_default().into(), body: r["body"].as_str().unwrap_or_default().replace("\r\n", "\n"), date: r["published_at"].as_str().unwrap_or_default().chars().take(10).collect(), - asset, + asset: found, }); } Ok(out) } +/// Published Horizon releases, newest first. Drafts never reach the anonymous API, and launcher +/// releases are left out. +pub fn releases() -> Result, String> { + let mut out: Vec = all_releases(ASSET)?.into_iter().filter(|r| is_game_tag(&r.tag)).collect(); + out.sort_by(|a, b| compare_versions(&b.tag, &a.tag)); + Ok(out) +} + +/// Published launcher releases (`launcher-v…`), newest first, with this platform's archive. +pub fn launcher_releases() -> Result, String> { + let mut out: Vec = + all_releases(LAUNCHER_ASSET)?.into_iter().filter(|r| launcher_version(&r.tag).is_some()).collect(); + out.sort_by(|a, b| compare_versions(&b.tag, &a.tag)); + Ok(out) +} + +/// The newest Horizon release that has a build for this platform. +pub fn latest(releases: &[Release]) -> Option<&Release> { + releases.iter().find(|r| r.asset.is_some()) +} + /// The README's roadmap section from main (the project status section if it has none). pub fn roadmap() -> Result { let url = format!("https://raw.githubusercontent.com/{REPO}/main/README.md"); @@ -91,7 +158,11 @@ pub fn roadmap() -> Result { Err("The README has no roadmap section yet.".into()) } +/// The launcher's data folder; `HORIZON_LAUNCHER_HOME` overrides it (for tests and scripts). pub fn data_dir() -> PathBuf { + if let Some(home) = std::env::var_os("HORIZON_LAUNCHER_HOME").filter(|h| !h.is_empty()) { + return PathBuf::from(home); + } dirs::data_dir().unwrap_or_else(std::env::temp_dir).join("horizon-launcher") } @@ -114,7 +185,7 @@ pub fn installed() -> Vec { .filter(|e| e.path().join(COMPLETE).exists()) .filter_map(|e| e.file_name().into_string().ok()) .collect(); - tags.sort_by(|a, b| b.cmp(a)); + tags.sort_by(|a, b| compare_versions(b, a)); tags } diff --git a/launcher/src/lib.rs b/launcher/src/lib.rs new file mode 100644 index 000000000..64766a00c --- /dev/null +++ b/launcher/src/lib.rs @@ -0,0 +1,2 @@ +//! The launcher's core, shared by the GUI, the CLI and the MCP server. +pub mod backend; diff --git a/launcher/src/main.rs b/launcher/src/main.rs index 0f451cdb1..e1d85e4f8 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -1,10 +1,10 @@ //! Horizon launcher: installs Horizon builds, prepares them from the player's own Minecraft, //! and starts the client or a dedicated server. -mod backend; + mod markdown; mod theme; -use backend::{Job, Msg, Release, Running, Settings}; +use horizon_launcher::backend::{self, Job, Msg, Release, Running, Settings}; use gpui::{ div, prelude::*, px, size, App, Application, Bounds, ClickEvent, Context, Div, FocusHandle, FontWeight, KeyDownEvent, PathPromptOptions, ScrollHandle, SharedString, Stateful, Subscription, TitlebarOptions, Window,