[core] Parsers: strict VDF, Steam, Epic, GOG, manual and Domi sources, WinPath, GameId, Launch URIs, merge
This commit is contained in:
@@ -2,13 +2,18 @@
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
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,
|
||||
@@ -19,6 +24,35 @@ pub struct Game {
|
||||
pub cover: Option<CoverHint>,
|
||||
}
|
||||
|
||||
/// 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<WinPath>,
|
||||
launch: Launch,
|
||||
cover: Option<CoverHint>,
|
||||
}
|
||||
|
||||
impl TryFrom<GameRepr> for Game {
|
||||
type Error = ParseError;
|
||||
|
||||
fn try_from(r: GameRepr) -> Result<Self, Self::Error> {
|
||||
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")]
|
||||
@@ -69,6 +103,24 @@ impl Library {
|
||||
/// - 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<Vec<Game>>) -> Library {
|
||||
let _ = scans;
|
||||
Library::default()
|
||||
let mut seen: BTreeSet<GameId> = BTreeSet::new();
|
||||
let mut games: Vec<Game> = 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<String, u8> = 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 }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user