// SPDX-License-Identifier: CPAL-1.0 // Copyright (c) 2026 Marek Novák. Built on Domi, https://aula.marek-novak.cz use crate::error::ParseError; use crate::id::{GameId, Source}; use crate::launch::Launch; use crate::limits::MAX_GAMES; use crate::path::WinPath; use crate::text; use serde::{Deserialize, Serialize}; use std::collections::{BTreeMap, BTreeSet}; /// One game in the library. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(try_from = "GameRepr")] pub struct Game { pub id: GameId, pub source: Source, /// Display title: trimmed, 1 to [`crate::limits::MAX_TITLE`] characters, no control characters. pub title: String, pub install_dir: Option, pub launch: Launch, pub cover: Option, } /// The wire shape of [`Game`]; deserialising goes through it so the title is checked and the id matches the source. #[derive(Deserialize)] struct GameRepr { id: GameId, source: Source, title: String, install_dir: Option, launch: Launch, cover: Option, } impl TryFrom for Game { type Error = ParseError; fn try_from(r: GameRepr) -> Result { if r.id.source() != r.source { return Err(ParseError::BadId); } Ok(Game { title: text::title(&r.title, "title")?, id: r.id, source: r.source, install_dir: r.install_dir, launch: r.launch, cover: r.cover, }) } } /// Where the tile's picture comes from. No online lookups, ever. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum CoverHint { /// Steam's own local library cache for this app id (the app finds the file). SteamLibraryCache { app_id: u32 }, /// A picture file on disk. LocalFile(WinPath), /// A lettered tile: the title's first letter or digit on a colour of this hue (0 to 359). Lettered { letter: char, hue: u16 }, } impl CoverHint { /// The lettered tile for a game: the first letter or digit of `title` (uppercased; `#` when there is none) and a /// hue derived from `id`, so a game keeps its colour across scans. pub fn lettered(title: &str, id: &GameId) -> CoverHint { crate::text::lettered(title, id) } } /// The merged library, ordered by title. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct Library { pub games: Vec, } impl Library { /// The game with this id. pub fn get(&self, id: &GameId) -> Option<&Game> { self.games.iter().find(|g| &g.id == id) } pub fn len(&self) -> usize { self.games.len() } pub fn is_empty(&self) -> bool { self.games.is_empty() } } /// Merges per-source scans into one library. /// /// - The same [`GameId`] is kept once (its first occurrence). /// - Games of different sources with the same install folder (compared ignoring case): only the highest /// [`Source::priority`] stays (Steam > Epic > GOG > Domi > Manual). Games of the same source are never dropped /// for sharing a folder. /// - The result is sorted by title (ignoring case), then by id, so the order does not depend on scan order. /// - At most [`crate::limits::MAX_GAMES`] games are considered; the rest are dropped. pub fn merge(scans: Vec>) -> Library { let mut seen: BTreeSet = BTreeSet::new(); let mut games: Vec = Vec::new(); for g in scans.into_iter().flatten().take(MAX_GAMES) { if seen.insert(g.id.clone()) { games.push(g); } } let mut best: BTreeMap = BTreeMap::new(); for g in &games { if let Some(dir) = &g.install_dir { let e = best.entry(dir.key()).or_insert(0); *e = (*e).max(g.source.priority()); } } games.retain(|g| match &g.install_dir { Some(dir) => best.get(&dir.key()).is_none_or(|&b| g.source.priority() >= b), None => true, }); games.sort_by_cached_key(|g| (g.title.to_lowercase(), g.id.clone())); Library { games } }