Skip to content

Commit

Permalink
Refactor TitleFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Apr 14, 2024
1 parent df5a478 commit 049068e
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 150 deletions.
46 changes: 30 additions & 16 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
resource::{cache::Cache, config::Config, manifest::Manifest, ResourceFile, SaveableResourceFile},
scan::{
layout::BackupLayout, prepare_backup_target, scan_game_for_backup, BackupId, DuplicateDetector, Launchers,
OperationStepDecision, SteamShortcuts, TitleFinder,
OperationStepDecision, SteamShortcuts, TitleFinder, TitleQuery,
},
wrap,
};
Expand Down Expand Up @@ -106,7 +106,7 @@ pub fn evaluate_games(
let mut invalid = BTreeSet::new();

for game in requested {
match title_finder.find_one_primary_or_alias(&game) {
match title_finder.find_one_by_name(&game) {
Some(found) => {
valid.insert(found);
}
Expand Down Expand Up @@ -199,7 +199,7 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)
}

let layout = BackupLayout::new(backup_dir.clone(), retention);
let title_finder = TitleFinder::new(&manifest, &layout);
let title_finder = TitleFinder::new(&config, &manifest, layout.restorable_game_set());

let games_specified = !games.is_empty();
let games = match evaluate_games(manifest.primary_titles(), games, &title_finder) {
Expand Down Expand Up @@ -403,7 +403,7 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)
let backup_id = backup.as_ref().map(|x| BackupId::Named(x.clone()));

let manifest = load_manifest(&config, &mut cache, true, false).unwrap_or_default();
let title_finder = TitleFinder::new(&manifest, &layout);
let title_finder = TitleFinder::new(&config, &manifest, layout.restorable_game_set());

let games_specified = !games.is_empty();
let games = match evaluate_games(layout.restorable_game_set(), games, &title_finder) {
Expand Down Expand Up @@ -561,7 +561,7 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)

let layout = BackupLayout::new(restore_dir.clone(), config.backup.retention.clone());
let manifest = load_manifest(&config, &mut cache, true, false).unwrap_or_default();
let title_finder = TitleFinder::new(&manifest, &layout);
let title_finder = TitleFinder::new(&config, &manifest, layout.restorable_game_set());

let games = match evaluate_games(layout.restorable_game_set(), games, &title_finder) {
Ok(games) => games,
Expand Down Expand Up @@ -613,10 +613,17 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)
};
let layout = BackupLayout::new(restore_dir.clone(), config.backup.retention.clone());

let title_finder = TitleFinder::new(&manifest, &layout);
let found = title_finder.find(
&names, &config, &steam_id, &gog_id, normalized, backup, restore, disabled, partial,
);
let title_finder = TitleFinder::new(&config, &manifest, layout.restorable_game_set());
let found = title_finder.find(TitleQuery {
names: names.clone(),
steam_id,
gog_id,
normalized,
backup,
restore,
disabled,
partial,
});
reporter.add_found_titles(&found);

if found.is_empty() {
Expand Down Expand Up @@ -759,7 +766,7 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)

let layout = BackupLayout::new(config.restore.path.clone(), config.backup.retention.clone());
let manifest = load_manifest(&config, &mut cache, true, false).unwrap_or_default();
let title_finder = TitleFinder::new(&manifest, &layout);
let title_finder = TitleFinder::new(&config, &manifest, layout.restorable_game_set());

let games = match evaluate_games(layout.restorable_game_set(), games, &title_finder) {
Ok(games) => games,
Expand Down Expand Up @@ -800,7 +807,7 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)

let layout = BackupLayout::new(config.restore.path.clone(), config.backup.retention.clone());
let manifest = load_manifest(&config, &mut cache, true, false).unwrap_or_default();
let title_finder = TitleFinder::new(&manifest, &layout);
let title_finder = TitleFinder::new(&config, &manifest, layout.restorable_game_set());

let games = match evaluate_games(layout.restorable_game_set(), games, &title_finder) {
Ok(games) => games,
Expand Down Expand Up @@ -832,7 +839,7 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)
} => {
let manifest = load_manifest(&config, &mut cache, no_manifest_update, try_manifest_update)?;
let layout = BackupLayout::new(config.restore.path.clone(), config.backup.retention.clone());
let title_finder = TitleFinder::new(&manifest, &layout);
let title_finder = TitleFinder::new(&config, &manifest, layout.restorable_game_set());

// Determine raw game identifiers
let wrap_game_info = if let Some(name) = name_source.name.as_ref() {
Expand All @@ -843,9 +850,9 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)
} else if let Some(infer) = name_source.infer {
let roots = config.expanded_roots();
match infer {
parse::Launcher::Heroic => wrap::heroic::infer_game_from_heroic(&roots, &commands),
parse::Launcher::Heroic => wrap::heroic::infer_game_from_heroic(&roots),
parse::Launcher::Lutris => wrap::lutris::infer(),
parse::Launcher::Steam => wrap::infer_game_from_steam(&title_finder),
parse::Launcher::Steam => wrap::infer_game_from_steam(),
}
} else {
unreachable!();
Expand All @@ -856,8 +863,15 @@ pub fn run(sub: Subcommand, no_manifest_update: bool, try_manifest_update: bool)
//
// e.g. "Slain: Back From Hell" from legendary to "Slain: Back from
// Hell" as known to ludusavi
let game_name = wrap_game_info.as_ref().and_then(|wrap_game_info| {
title_finder.maybe_find_one(wrap_game_info.name.as_ref(), None, wrap_game_info.gog_id, true)
let game_name = wrap_game_info.clone().and_then(|info| {
let names = info.name.map(|x| vec![x]).unwrap_or_default();
title_finder.find_one(TitleQuery {
names,
steam_id: info.steam_id,
gog_id: info.gog_id,
normalized: true,
..Default::default()
})
});
log::debug!("Title finder result: {:?}", &game_name);

Expand Down
2 changes: 1 addition & 1 deletion src/gui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl App {

let roots = config.expanded_roots();
let layout = BackupLayout::new(config.backup.path.clone(), retention);
let title_finder = TitleFinder::new(&manifest, &layout);
let title_finder = TitleFinder::new(&config, &manifest, layout.restorable_game_set());
let steam = SteamShortcuts::scan();
let launchers = Launchers::scan(&roots, &manifest, &subjects, &title_finder, None);

Expand Down
13 changes: 9 additions & 4 deletions src/scan/launchers/heroic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
resource::{config::RootsConfig, manifest::Os},
scan::{
launchers::{legendary, LauncherGame},
TitleFinder,
TitleFinder, TitleQuery,
},
};

Expand Down Expand Up @@ -103,7 +103,7 @@ fn detect_legendary_games(
game.title,
game.app_name
);
let official_title = title_finder.find_one(&[game.title.to_owned()], &None, &None, true);
let official_title = title_finder.find_one_by_normalized_name(&game.title);
// process game from GamesConfig
let prefix = find_prefix(&root.path, &game.title, &game.platform.to_lowercase(), &game.app_name);
memorize_game(
Expand Down Expand Up @@ -182,7 +182,12 @@ fn detect_gog_games(root: &RootsConfig, title_finder: &TitleFinder) -> HashMap<S
for game in installed_games.installed {
if let Some(game_title) = game_titles.get(&game.app_name) {
let gog_id: Option<u64> = game.app_name.parse().ok();
let official_title = title_finder.find_one(&[game_title.to_owned()], &None, &gog_id, true);
let official_title = title_finder.find_one(TitleQuery {
names: vec![game_title.to_owned()],
gog_id,
normalized: true,
..Default::default()
});
let prefix = find_prefix(&root.path, game_title, &game.platform, &game.app_name);
memorize_game(
&mut games,
Expand Down Expand Up @@ -350,7 +355,7 @@ mod tests {
}

fn title_finder() -> TitleFinder {
TitleFinder::new(&manifest(), &Default::default())
TitleFinder::new(&Default::default(), &manifest(), Default::default())
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/scan/launchers/legendary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn scan(root: &RootsConfig, title_finder: &TitleFinder) -> HashMap<String, L
let mut out = HashMap::new();

for game in get_games(&root.path) {
let Some(official_title) = title_finder.find_one(&[game.title.to_owned()], &None, &None, true) else {
let Some(official_title) = title_finder.find_one_by_normalized_name(&game.title) else {
log::trace!("Ignoring unrecognized game: {}", &game.title);
continue;
};
Expand Down Expand Up @@ -100,7 +100,7 @@ mod tests {
}

fn title_finder() -> TitleFinder {
TitleFinder::new(&manifest(), &Default::default())
TitleFinder::new(&Default::default(), &manifest(), Default::default())
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/scan/launchers/lutris.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn scan_spec(spec: LutrisGame, spec_path: &StrictPath, title_finder: &TitleFinde
return None;
};

let official_title = title_finder.find_one(&[name.clone()], &None, &None, true);
let official_title = title_finder.find_one_by_normalized_name(&name);
let prefix = spec.game.prefix;
let platform = Some(match &prefix {
Some(_) => Os::Windows,
Expand Down Expand Up @@ -173,7 +173,7 @@ mod tests {
}

fn title_finder() -> TitleFinder {
TitleFinder::new(&manifest(), &Default::default())
TitleFinder::new(&Default::default(), &manifest(), Default::default())
}

#[test]
Expand Down
Loading

0 comments on commit 049068e

Please sign in to comment.