diff --git a/crates/core/src/hosted_catalog.rs b/crates/core/src/hosted_catalog.rs index f293809a..de9e9912 100644 --- a/crates/core/src/hosted_catalog.rs +++ b/crates/core/src/hosted_catalog.rs @@ -34,6 +34,7 @@ struct Snapshot { details: Vec, metadata: HashMap, icons: HashMap>, + nightly_icons: HashMap>, } /// Only desktop metadata consumed by existing integration. Package identities stay pinned. @@ -41,6 +42,7 @@ pub struct Metadata { pub desktop_categories: String, pub startup_wm_class: String, pub icon_sha256: String, + pub nightly_icon_sha256: Option, } #[derive(Deserialize)] @@ -72,6 +74,8 @@ struct Record { mac_bundle_name: Option, icon_url: String, icon_sha256: String, + nightly_icon_url: Option, + nightly_icon_sha256: Option, desktop_categories: Vec, startup_wm_class: String, details: Option, @@ -152,26 +156,47 @@ impl VerifiedCatalog { self.records.iter().map(|r| (r.id.as_str(), r.icon_url.as_str(), r.icon_sha256.as_str())) } - /// Publishes once per process; omitted bundled apps stay manageable after a remote removal. - pub fn activate(self, icons: HashMap>) { - ACTIVE.get_or_init(|| self.into_snapshot(icons)); + /// Validated optional Developer Build (nightly) icon URLs and digests. + pub fn nightly_icons(&self) -> impl Iterator { + self.records.iter().filter_map(|r| { + let url = r.nightly_icon_url.as_deref()?; + let digest = r.nightly_icon_sha256.as_deref()?; + Some((r.id.as_str(), url, digest)) + }) } - fn into_snapshot(self, icons: HashMap>) -> Snapshot { + /// Publishes once per process; omitted bundled apps stay manageable after a remote removal. + pub fn activate(self, icons: HashMap>) { + self.activate_with_nightly_icons(icons, HashMap::new()); + } + + /// Publishes once per process with optional hosted Developer Build icons. + pub fn activate_with_nightly_icons(self, icons: HashMap>, nightly_icons: HashMap>) { + ACTIVE.get_or_init(|| self.into_snapshot(icons, nightly_icons)); + } + + fn into_snapshot(self, icons: HashMap>, nightly_icons: HashMap>) -> Snapshot { let mut apps = Vec::new(); let mut details = Vec::new(); let mut metadata = HashMap::new(); let mut kept_icons = HashMap::new(); + let mut kept_nightly_icons = HashMap::new(); for record in self.records { if let Some(icon) = icons.get(&record.id).filter(|b| valid_icon(b, &record.icon_sha256)) { kept_icons.insert(record.id.clone(), icon.clone()); } + if let Some(digest) = record.nightly_icon_sha256.as_deref() + && let Some(icon) = nightly_icons.get(&record.id).filter(|b| valid_icon(b, digest)) + { + kept_nightly_icons.insert(record.id.clone(), icon.clone()); + } metadata.insert( record.id.clone(), Metadata { desktop_categories: record.desktop_categories.join(";") + ";", startup_wm_class: record.startup_wm_class.clone(), icon_sha256: record.icon_sha256.clone(), + nightly_icon_sha256: record.nightly_icon_sha256.clone(), }, ); if let Some(page) = record.details { @@ -224,7 +249,7 @@ impl VerifiedCatalog { apps.push(*app); } } - Snapshot { apps, details, metadata, icons: kept_icons } + Snapshot { apps, details, metadata, icons: kept_icons, nightly_icons: kept_nightly_icons } } } @@ -241,6 +266,9 @@ pub fn metadata(id: &str) -> Option<&'static Metadata> { pub fn icon(id: &str) -> Option<&'static [u8]> { ACTIVE.get()?.icons.get(id).map(Vec::as_slice) } +pub fn nightly_icon(id: &str) -> Option<&'static [u8]> { + ACTIVE.get()?.nightly_icons.get(id).map(Vec::as_slice) +} /// Bound decoded allocation before handing an authenticated PNG to an image loader. pub fn valid_icon(bytes: &[u8], digest: &str) -> bool { @@ -334,6 +362,14 @@ fn valid_record(r: &Record) -> bool { { return false; } + match (r.nightly_icon_url.as_deref(), r.nightly_icon_sha256.as_deref()) { + (None, None) => {} + (Some(url), Some(sha)) + if sha.len() == 64 + && sha.bytes().all(|c| matches!(c, b'0'..=b'9' | b'a'..=b'f')) + && url == format!("https://getartcraft.com/launcher-nightly-icons/{}-{sha}.png", r.id) => {} + _ => return false, + } r.details.as_ref().is_none_or(|p| { p.features.len() <= 16 && p.shots.len() <= 16 @@ -453,6 +489,8 @@ mod tests { r.mac_bundle_name = Some(r.name.clone()); r.startup_wm_class = "ai.storyteller.examplecraft".into(); r.icon_url = format!("https://getartcraft.com/launcher-app-icons/{}-{}.png", r.id, r.icon_sha256); + r.nightly_icon_url = None; + r.nightly_icon_sha256 = None; r.details = None; r } @@ -472,6 +510,16 @@ mod tests { r.icon_url = "https://other.example/icon.png".into(); assert!(!valid_record(&r)); r = future_app(); + let sha = "a".repeat(64); + r.nightly_icon_url = Some(format!("https://getartcraft.com/launcher-nightly-icons/examplecraft-{sha}.png")); + assert!(!valid_record(&r)); + r.nightly_icon_sha256 = Some(sha.to_ascii_uppercase()); + assert!(!valid_record(&r)); + r.nightly_icon_sha256 = Some(sha.clone()); + assert!(valid_record(&r)); + r.nightly_icon_url = Some(format!("https://getartcraft.com/launcher-nightly-icons/other-{sha}.png")); + assert!(!valid_record(&r)); + r = future_app(); r.startup_wm_class = "example\nExec=other".into(); assert!(!valid_record(&r)); r = future_app(); @@ -504,14 +552,24 @@ mod tests { shots: vec![], }); let icon = crate::catalog::app_icon_png("photocraft").unwrap().to_vec(); + let nightly_digest = format!("{:x}", Sha256::digest(&icon)); + let expected_url = format!("https://getartcraft.com/launcher-nightly-icons/examplecraft-{nightly_digest}.png"); + record.nightly_icon_url = Some(expected_url.clone()); + record.nightly_icon_sha256 = Some(nightly_digest.clone()); + assert!(valid_record(&record)); + let verified = VerifiedCatalog { records: vec![record], rejected_entries: 0 }; + let nightly_entries: Vec<_> = verified.nightly_icons().collect(); + assert_eq!(nightly_entries, vec![("examplecraft", expected_url.as_str(), nightly_digest.as_str())]); let snapshot = - VerifiedCatalog { records: vec![record], rejected_entries: 0 }.into_snapshot(HashMap::from([("examplecraft".into(), icon.clone())])); + verified.into_snapshot(HashMap::from([("examplecraft".into(), icon.clone())]), HashMap::from([("examplecraft".into(), icon.clone())])); assert_eq!(snapshot.apps.len(), SUITE.len() + 1); assert_eq!(snapshot.apps[0].id, "examplecraft"); assert_eq!(snapshot.apps[0].repo, "storytold/examplecraft"); assert_eq!(snapshot.details[0].headline, "New app"); assert_eq!(snapshot.metadata["examplecraft"].startup_wm_class, "ai.storyteller.examplecraft"); + assert_eq!(snapshot.metadata["examplecraft"].nightly_icon_sha256.as_deref(), Some(nightly_digest.as_str())); assert_eq!(snapshot.icons["examplecraft"], icon); + assert_eq!(snapshot.nightly_icons["examplecraft"], icon); assert!(SUITE.iter().all(|app| snapshot.apps.iter().any(|a| a.id == app.id))); } } diff --git a/crates/native/src/hosted_catalog.rs b/crates/native/src/hosted_catalog.rs index 3de10158..1e7d3673 100644 --- a/crates/native/src/hosted_catalog.rs +++ b/crates/native/src/hosted_catalog.rs @@ -57,35 +57,44 @@ pub fn load_once(http: Option<&Http>, paths: &Paths) { if artcraft_launcher_core::catalog::app_icon_png(id).is_some_and(|b| valid_icon(b, digest)) { continue; } - let saved = cache.with_file_name(format!("catalog-icon-{digest}.png")); - let icon = read_capped(&saved, MAX_ICON_BYTES).filter(|b| valid_icon(b, digest)).or_else(|| { - if !network_available { - return None; - } - let bytes = match http?.get_catalog_bytes(url, MAX_ICON_BYTES) { - Ok(bytes) => bytes?, - Err(_) => { - network_available = false; - return None; - } - }; - if !valid_icon(&bytes, digest) { - return None; - } - if let Err(why) = write_atomic(&saved, &bytes) { - log::warn!("could not cache catalog icon: {why}"); - } - Some(bytes) - }); - if let Some(icon) = icon { + if let Some(icon) = fetch_cached_icon(http, &cache, "catalog-icon", url, digest, &mut network_available) { icons.insert(id.to_string(), icon); } } - catalog.activate(icons); + let mut nightly_icons = HashMap::new(); + for (id, url, digest) in catalog.nightly_icons() { + if let Some(icon) = fetch_cached_icon(http, &cache, "catalog-nightly-icon", url, digest, &mut network_available) { + nightly_icons.insert(id.to_string(), icon); + } + } + catalog.activate_with_nightly_icons(icons, nightly_icons); } }); } +fn fetch_cached_icon(http: Option<&Http>, cache: &Path, prefix: &str, url: &str, digest: &str, network_available: &mut bool) -> Option> { + let saved = cache.with_file_name(format!("{prefix}-{digest}.png")); + read_capped(&saved, MAX_ICON_BYTES).filter(|b| valid_icon(b, digest)).or_else(|| { + if !*network_available { + return None; + } + let bytes = match http?.get_catalog_bytes(url, MAX_ICON_BYTES) { + Ok(bytes) => bytes?, + Err(_) => { + *network_available = false; + return None; + } + }; + if !valid_icon(&bytes, digest) { + return None; + } + if let Err(why) = write_atomic(&saved, &bytes) { + log::warn!("could not cache {prefix}: {why}"); + } + Some(bytes) + }) +} + fn saved_catalog(path: &Path) -> Option { // JSON escaping can expand a signed TOML byte to six bytes. let bytes = read_capped(path, MAX_CATALOG_BYTES * 6 + MAX_SIGNATURE_BYTES + 128)?; @@ -107,6 +116,31 @@ fn read_capped(path: &Path, max: usize) -> Option> { mod tests { use super::*; + #[test] + fn fetch_cached_icon_reads_valid_disk_cache_and_rejects_tampered_bytes() { + let dir = tempfile::tempdir().unwrap(); + let cache = dir.path().join("launcher-catalog.json"); + let png = artcraft_launcher_core::catalog::app_icon_png("photocraft").unwrap(); + let digest = { + use sha2::{Digest, Sha256}; + format!("{:x}", Sha256::digest(png)) + }; + let saved = cache.with_file_name(format!("catalog-nightly-icon-{digest}.png")); + std::fs::write(&saved, png).unwrap(); + + let mut net = true; + let hit = fetch_cached_icon(None, &cache, "catalog-nightly-icon", "https://getartcraft.com/unused.png", &digest, &mut net); + assert_eq!(hit.as_deref(), Some(png)); + + std::fs::write(&saved, b"not a png").unwrap(); + let miss = fetch_cached_icon(None, &cache, "catalog-nightly-icon", "https://getartcraft.com/unused.png", &digest, &mut net); + assert!(miss.is_none()); + + net = false; + let offline = fetch_cached_icon(None, &cache, "catalog-nightly-icon", "https://getartcraft.com/unused.png", &digest, &mut net); + assert!(offline.is_none()); + } + #[test] #[ignore = "requires the live signed catalog at getartcraft.com"] fn live_catalog_is_verified_cached_and_activated() { diff --git a/crates/ui-egui/src/icons.rs b/crates/ui-egui/src/icons.rs index 3df86b06..73e44e8a 100644 --- a/crates/ui-egui/src/icons.rs +++ b/crates/ui-egui/src/icons.rs @@ -92,9 +92,18 @@ pub fn artcraft_mark() -> ImageSource<'static> { egui::include_image!("../../../docs/brand/artcraft-mark.svg") } -/// Deep-background nightly artwork for every bundled official catalog app. +fn nightly_icon_revision(id: &str) -> &'static str { + artcraft_launcher_core::hosted_catalog::metadata(id).and_then(|m| m.nightly_icon_sha256.as_deref()).unwrap_or("bundled") +} + +/// Deep-background nightly artwork: prefers the signed hosted catalog's nightly icon from +/// `getartcraft.com/launcher-nightly-icons/`, falling back to bundled artwork for offline use. /// Community or future unknown brands retain their supplied artwork, never a runtime tint. fn nightly_app(id: &str) -> Option> { + if let Some(png) = artcraft_launcher_core::hosted_catalog::nightly_icon(id) { + let revision = nightly_icon_revision(id); + return Some(ImageSource::Bytes { uri: format!("bytes://nightly-icons/{id}/{revision}.png").into(), bytes: png.into() }); + } Some(match id { "photocraft" => egui::include_image!("../../../assets/nightly-icons/photocraft.png"), "vectorcraft" => egui::include_image!("../../../assets/nightly-icons/vectorcraft.png"), @@ -128,7 +137,14 @@ pub(crate) fn nightly_icon(ui: &mut egui::Ui, id: &str, name: &str, size: f32) { #[cfg(test)] mod tests { - use super::desaturate; + use super::{desaturate, nightly_app, nightly_icon_revision}; + + #[test] + fn nightly_icon_falls_back_to_bundled_or_app_icon_when_unactivated() { + assert_eq!(nightly_icon_revision("photocraft"), "bundled"); + assert!(nightly_app("photocraft").is_some()); + assert!(nightly_app("nonexistent-app-id").is_none()); + } #[test] fn grey_pixels_have_no_colour_and_keep_their_alpha() {