ludusavi 0.31.0

Game save backup tool
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
use std::{
    collections::{BTreeMap, BTreeSet, HashMap},
    sync::LazyLock,
};

use itertools::Itertools;
use regex::Regex;

use crate::{
    resource::{config::Config, manifest::Manifest},
    scan::ScanKind,
};

/// This covers any edition that is clearly separated by punctuation.
static RE_EDITION_PUNCTUATED: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"[™®©:-] .+ edition$"#).unwrap());
/// This covers specific, known editions that are not separated by punctuation.
static RE_EDITION_KNOWN: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#" (game of the year) edition$"#).unwrap());
/// This covers any single-word editions that are not separated by punctuation.
/// We can't assume more than one word because it may be part of the main title.
static RE_EDITION_SHORT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#" [^ ]+ edition$"#).unwrap());
static RE_YEAR_SUFFIX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r" \(\d+\)$").unwrap());
static RE_SYMBOLS_GAP: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"[™®©:-]"#).unwrap());
static RE_SYMBOLS_NO_GAP: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"['"‘’“”]"#).unwrap());
static RE_SPACES: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#" {2,}"#).unwrap());

pub fn normalize_title(title: &str) -> String {
    let normalized = title.to_lowercase();
    let normalized = RE_YEAR_SUFFIX.replace_all(&normalized, "");
    let normalized = RE_EDITION_PUNCTUATED.replace_all(&normalized, "");
    let normalized = RE_EDITION_KNOWN.replace_all(&normalized, "");
    let normalized = RE_EDITION_SHORT.replace_all(&normalized, "");
    let normalized = RE_SYMBOLS_GAP.replace_all(&normalized, " ");
    let normalized = RE_SYMBOLS_NO_GAP.replace_all(&normalized, "");
    let normalized = RE_SPACES.replace_all(&normalized, " ");
    normalized.trim().to_string()
}

#[derive(Clone, Debug, Default)]
struct TitleGameInfo {
    backup: TitleGameOperationInfo,
    restore: TitleGameOperationInfo,
}

#[derive(Clone, Debug, Default)]
struct NormalizedTitleGameInfo {
    canonical: String,
    score: Option<f64>,
}

#[derive(Clone, Debug, Default)]
struct TitleGameOperationInfo {
    known: bool,
    enabled: bool,
    complete: bool,
}

#[derive(Clone, Debug, Default)]
pub struct TitleFinder {
    games: HashMap<String, TitleGameInfo>,
    steam_ids: HashMap<u32, String>,
    gog_ids: HashMap<u64, String>,
    lutris_ids: HashMap<String, String>,
    normalized: HashMap<String, NormalizedTitleGameInfo>,
    aliases: HashMap<String, String>,
}

impl TitleFinder {
    pub fn new(config: &Config, manifest: &Manifest, restorables: BTreeSet<String>) -> Self {
        let mut games: HashMap<String, TitleGameInfo> = HashMap::new();
        for name in manifest.0.keys() {
            let info = games.entry(name.clone()).or_default();
            info.backup = TitleGameOperationInfo {
                known: true,
                enabled: config.is_game_enabled_for_backup(name),
                complete: !config.any_saves_ignored(name, ScanKind::Backup),
            };
        }
        for name in restorables {
            let info = games.entry(name.clone()).or_default();
            info.restore = TitleGameOperationInfo {
                known: true,
                enabled: config.is_game_enabled_for_restore(&name),
                complete: !config.any_saves_ignored(&name, ScanKind::Restore),
            };
        }

        let steam_ids = manifest.map_steam_ids_to_names();
        let gog_ids = manifest.map_gog_ids_to_names();
        let lutris_ids = manifest.map_lutris_ids_to_names();
        let aliases = manifest.aliases();

        let mut normalized: HashMap<String, NormalizedTitleGameInfo> = HashMap::new();
        for title in games.keys() {
            let norm = normalize_title(title);
            let entry = normalized.entry(norm.clone()).or_default();
            let new_score = strsim::jaro_winkler(title, &norm);
            match entry.score {
                Some(old_score) => {
                    if new_score > old_score {
                        entry.canonical = title.to_owned();
                        entry.score = Some(new_score);
                    }
                }
                None => {
                    entry.canonical = title.to_owned();
                    entry.score = Some(new_score);
                }
            }
        }

        Self {
            games,
            steam_ids,
            gog_ids,
            lutris_ids,
            normalized,
            aliases,
        }
    }

    fn eligible(&self, game: &str, backup: bool, restore: bool) -> bool {
        let (can_backup, can_restore) = self
            .games
            .get(game)
            .map(|x| (x.backup.known, x.restore.known))
            .unwrap_or_default();

        if backup && restore {
            can_backup && can_restore
        } else if backup {
            can_backup
        } else if restore {
            can_restore
        } else {
            true
        }
    }

    pub fn find_one(&self, query: TitleQuery) -> Option<String> {
        self.find(query)
            .into_iter()
            .sorted_by(compare_ranked_titles)
            .map(|(name, _info)| name)
            .next()
    }

    pub fn find_one_by_name(&self, name: &str) -> Option<String> {
        self.find_one(TitleQuery {
            names: vec![name.to_string()],
            ..Default::default()
        })
    }

    pub fn find_one_by_normalized_name(&self, name: &str) -> Option<String> {
        self.find_one(TitleQuery {
            names: vec![name.to_string()],
            normalized: true,
            ..Default::default()
        })
    }

    /// Look up games based on certain criteria.
    /// Returns a set of matching game names.
    ///
    /// Only returns one result when querying for exact titles or store IDs.
    /// Precedence: Steam ID -> GOG ID -> exact title -> normalized title.
    ///
    /// Otherwise, returns all results that match the query.
    pub fn find(&self, query: TitleQuery) -> BTreeMap<String, TitleMatch> {
        let TitleQuery {
            multiple,
            names,
            steam_id,
            gog_id,
            lutris_id,
            normalized,
            fuzzy,
            backup,
            restore,
            disabled,
            partial,
        } = query;

        let mut output: BTreeMap<String, TitleMatch> = BTreeMap::new();
        let mut update = |title: String, info: TitleMatch| {
            output
                .entry(title)
                .and_modify(|entry| {
                    if entry.score.is_none() || entry.score.is_some_and(|old| info.score.is_some_and(|new| new > old)) {
                        entry.score = info.score;
                    }
                })
                .or_insert(info);
        };

        let singular = !names.is_empty() || steam_id.is_some() || gog_id.is_some() || lutris_id.is_some();

        'outer: {
            if singular {
                if let Some(steam_id) = steam_id {
                    if let Some(found) = self.steam_ids.get(&steam_id) {
                        if self.eligible(found, backup, restore) {
                            update(found.to_owned(), TitleMatch::perfect());
                            if !multiple {
                                break 'outer;
                            }
                        }
                    }
                }

                if let Some(gog_id) = gog_id {
                    if let Some(found) = self.gog_ids.get(&gog_id) {
                        if self.eligible(found, backup, restore) {
                            update(found.to_owned(), TitleMatch::perfect());
                            if !multiple {
                                break 'outer;
                            }
                        }
                    }
                }

                if let Some(lutris_id) = lutris_id {
                    if let Some(found) = self.lutris_ids.get(&lutris_id) {
                        if self.eligible(found, backup, restore) {
                            update(found.to_owned(), TitleMatch::perfect());
                            if !multiple {
                                break 'outer;
                            }
                        }
                    }
                }

                for name in &names {
                    if self.games.contains_key(name) && self.eligible(name, backup, restore) {
                        update(name.to_owned(), TitleMatch::perfect());
                        if !multiple {
                            break 'outer;
                        }
                    }
                }

                if normalized {
                    for name in &names {
                        if let Some(found) = self.normalized.get(&normalize_title(name)) {
                            if self.eligible(&found.canonical, backup, restore) {
                                update(found.canonical.to_owned(), TitleMatch { score: found.score });
                                if !multiple {
                                    break 'outer;
                                }
                            }
                        }
                    }
                }

                if fuzzy {
                    let mut matches = BTreeMap::new();

                    for name in &names {
                        for known in self.games.keys() {
                            let score = if normalized {
                                strsim::jaro_winkler(&normalize_title(known), &normalize_title(name))
                            } else {
                                strsim::jaro_winkler(known, name)
                            };

                            if score < 0.75 {
                                continue;
                            }

                            if self.eligible(known, backup, restore) {
                                matches.insert(known.to_string(), score);
                            }
                        }
                    }

                    let sorted: Vec<_> = matches
                        .into_iter()
                        .map(|(name, score)| (name, TitleMatch { score: Some(score) }))
                        .sorted_by(compare_ranked_titles)
                        .collect();

                    if multiple {
                        for (name, info) in sorted {
                            update(name, info);
                        }
                    } else if let Some((name, info)) = sorted.first() {
                        update(name.clone(), info.clone());
                        break 'outer;
                    }
                }
            } else {
                for (game, info) in &self.games {
                    if (backup && !info.backup.known) || (restore && !info.restore.known) {
                        continue;
                    }

                    if disabled {
                        let skip = match (backup, restore) {
                            (true, true) => info.backup.enabled || info.restore.enabled,
                            (true, false) => info.backup.enabled,
                            (false, true) => info.restore.enabled,
                            (false, false) => info.backup.enabled && info.restore.enabled,
                        };
                        if skip {
                            continue;
                        }
                    }

                    if partial {
                        let skip = match (backup, restore) {
                            (true, true) => info.backup.complete || info.restore.complete,
                            (true, false) => info.backup.complete,
                            (false, true) => info.restore.complete,
                            (false, false) => info.backup.complete && info.restore.complete,
                        };
                        if skip {
                            continue;
                        }
                    }

                    output.insert(game.to_owned(), TitleMatch::default());
                }
            }
        }

        // Resolve aliases to primary name.
        output = output
            .into_iter()
            .map(|(name, info)| match self.aliases.get(&name) {
                Some(aliased) => (aliased.to_string(), info),
                None => (name, info),
            })
            .collect();

        output
    }
}

#[derive(Clone, Debug, Default)]
pub struct TitleQuery {
    /// Keep looking for all potential matches,
    /// instead of stopping at the first match.
    pub multiple: bool,
    /// Search for exact titles or aliases.
    /// This will cause only one result to be returned.
    pub names: Vec<String>,
    /// Search for a Steam ID.
    /// This will cause only one result to be returned.
    pub steam_id: Option<u32>,
    /// Search for a GOG ID.
    /// This will cause only one result to be returned.
    pub gog_id: Option<u64>,
    /// Search for a Lutris slug.
    /// This will cause only one result to be returned.
    pub lutris_id: Option<String>,
    /// Search by normalizing the `names`.
    pub normalized: bool,
    /// Search with fuzzy matching.
    pub fuzzy: bool,
    /// Only return games that are possible to back up.
    pub backup: bool,
    /// Only return games that are possible to restore.
    pub restore: bool,
    /// Only return games that are disabled for processing.
    pub disabled: bool,
    /// Only return games that have some saves deselected.
    pub partial: bool,
}

#[derive(Debug, Default, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
pub struct TitleMatch {
    /// How well the title matches the query.
    /// Range: 0.0 to 1.0 (higher is better).
    pub score: Option<f64>,
}

impl TitleMatch {
    pub fn perfect() -> Self {
        Self { score: Some(1.0) }
    }
}

pub fn compare_ranked_titles(x: &(String, TitleMatch), y: &(String, TitleMatch)) -> std::cmp::Ordering {
    compare_ranked_titles_ref(&(&x.0, &x.1), &(&y.0, &y.1))
}

pub fn compare_ranked_titles_ref(x: &(&String, &TitleMatch), y: &(&String, &TitleMatch)) -> std::cmp::Ordering {
    y.1.score
        .partial_cmp(&x.1.score)
        .unwrap_or(std::cmp::Ordering::Equal)
        .then_with(|| x.0.to_lowercase().cmp(&y.0.to_lowercase()))
        .then_with(|| x.0.cmp(y.0))
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;
    use velcro::{btree_map, btree_set};

    use crate::resource::ResourceFile;

    use super::*;

    #[test]
    fn can_normalize_title() {
        // capitalization
        assert_eq!("foo bar", normalize_title("foo bar"));
        assert_eq!("foo bar", normalize_title("Foo Bar"));

        // punctuated editions
        assert_eq!("foo bar", normalize_title("Foo Bar: Any Arbitrary Edition"));
        assert_eq!("foo bar", normalize_title("Foo Bar - Any Arbitrary Edition"));
        assert_eq!("foo bar", normalize_title("Foo Bar™ Any Arbitrary Edition"));
        assert_eq!("foo bar", normalize_title("Foo Bar® - Any Arbitrary Edition"));

        // special cased editions
        assert_eq!("foo bar", normalize_title("Foo Bar Game of the Year Edition"));

        // short editions
        assert_eq!("foo bar", normalize_title("Foo Bar Special Edition"));

        // year suffixes
        assert_eq!("foo bar", normalize_title("Foo Bar (2000)"));

        // symbols
        assert_eq!("foo bar", normalize_title("Foo:Bar"));
        assert_eq!("foo bar", normalize_title("Foo: Bar"));
        assert_eq!("foo bar", normalize_title("Fo'o Bar"));
        assert_eq!("foo bar", normalize_title("Foo \"Bar\""));

        // spaces
        assert_eq!("foo bar", normalize_title("  Foo  Bar  "));
    }

    #[test]
    fn can_find_one_title() {
        let manifest = Manifest::load_from_string(
            r#"
            by-name: {}
            by-name-alias:
                alias: by-name
            by-steam:
                steam:
                    id: 1
            by-gog:
                gog:
                    id: 2
            by-steam-extra:
                id:
                    steamExtra: [3]
            by-gog-extra:
                id:
                    gogExtra: [4]
            by-lutris:
                id:
                    lutris: slug
            "#,
        )
        .unwrap();

        let finder = TitleFinder::new(&Default::default(), &manifest, Default::default());

        assert_eq!(
            btree_map! { "by-name".to_string(): TitleMatch::perfect() },
            finder.find(TitleQuery {
                names: vec!["by-name".to_string()],
                ..Default::default()
            }),
        );
        assert_eq!(
            btree_map! {"by-name".to_string(): TitleMatch::perfect() },
            finder.find(TitleQuery {
                names: vec!["by-name-alias".to_string()],
                ..Default::default()
            }),
        );
        assert_eq!(
            btree_map! {"by-name".to_string(): TitleMatch { score: Some(0.9428571428571428) } },
            finder.find(TitleQuery {
                names: vec!["By Na".to_string()],
                normalized: true,
                fuzzy: true,
                ..Default::default()
            }),
        );
        assert_eq!(
            btree_map! {"by-steam".to_string(): TitleMatch::perfect() },
            finder.find(TitleQuery {
                steam_id: Some(1),
                ..Default::default()
            }),
        );
        assert_eq!(
            btree_map! {"by-gog".to_string(): TitleMatch::perfect() },
            finder.find(TitleQuery {
                gog_id: Some(2),
                ..Default::default()
            }),
        );
        assert_eq!(
            btree_map! {"by-steam-extra".to_string(): TitleMatch::perfect() },
            finder.find(TitleQuery {
                steam_id: Some(3),
                ..Default::default()
            }),
        );
        assert_eq!(
            btree_map! {"by-gog-extra".to_string(): TitleMatch::perfect() },
            finder.find(TitleQuery {
                gog_id: Some(4),
                ..Default::default()
            }),
        );
        assert_eq!(
            btree_map! {"by-lutris".to_string(): TitleMatch::perfect() },
            finder.find(TitleQuery {
                lutris_id: Some("slug".to_string()),
                ..Default::default()
            }),
        );
    }

    #[test]
    fn can_find_multiple_titles() {
        let config = Config::load_from_string(
            r#"
            manifest:
                url: foo
            roots: []
            backup:
                path: /backup
                ignoredGames:
                    - backup-disabled
                toggledPaths:
                    backup-partial:
                        /foo: false
            restore:
                path: /backup
                ignoredGames:
                    - restore-disabled
                toggledPaths:
                    restore-partial:
                        /foo: false
            "#,
        )
        .unwrap();

        let manifest = Manifest::load_from_string(
            r#"
            both: {}
            backup: {}
            backup-disabled: {}
            backup-partial: {}
            "#,
        )
        .unwrap();

        let restorables = btree_set![
            "both".to_string(),
            "restore".to_string(),
            "restore-disabled".to_string(),
            "restore-partial".to_string()
        ];

        let finder = TitleFinder::new(&config, &manifest, restorables);

        assert_eq!(
            btree_map! {
                "both".to_string(): TitleMatch::default(),
                "backup".to_string(): TitleMatch::default(),
                "backup-disabled".to_string(): TitleMatch::default(),
                "backup-partial".to_string(): TitleMatch::default(),
                "restore".to_string(): TitleMatch::default(),
                "restore-disabled".to_string(): TitleMatch::default(),
                "restore-partial".to_string(): TitleMatch::default(),
            },
            finder.find(TitleQuery::default()),
        );

        assert_eq!(
            btree_map! {
                "both".to_string(): TitleMatch::default(),
                "backup".to_string(): TitleMatch::default(),
                "backup-disabled".to_string(): TitleMatch::default(),
                "backup-partial".to_string(): TitleMatch::default(),
            },
            finder.find(TitleQuery {
                backup: true,
                ..Default::default()
            }),
        );
        assert_eq!(
            btree_map! {
                "both".to_string(): TitleMatch::default(),
                "restore".to_string(): TitleMatch::default(),
                "restore-disabled".to_string(): TitleMatch::default(),
                "restore-partial".to_string(): TitleMatch::default(),
            },
            finder.find(TitleQuery {
                restore: true,
                ..Default::default()
            }),
        );
        assert_eq!(
            btree_map! {"both".to_string(): TitleMatch::default() },
            finder.find(TitleQuery {
                backup: true,
                restore: true,
                ..Default::default()
            }),
        );

        assert_eq!(
            btree_map! {
                "backup".to_string(): TitleMatch::default(),
                "backup-disabled".to_string(): TitleMatch::default(),
                "backup-partial".to_string(): TitleMatch::default(),
                "restore".to_string(): TitleMatch::default(),
                "restore-disabled".to_string(): TitleMatch::default(),
                "restore-partial".to_string(): TitleMatch::default(),
            },
            finder.find(TitleQuery {
                disabled: true,
                ..Default::default()
            }),
        );
        assert_eq!(
            btree_map! {"backup-disabled".to_string(): TitleMatch::default() },
            finder.find(TitleQuery {
                backup: true,
                disabled: true,
                ..Default::default()
            }),
        );

        assert_eq!(
            btree_map! {
                "backup".to_string(): TitleMatch::default(),
                "backup-disabled".to_string(): TitleMatch::default(),
                "backup-partial".to_string(): TitleMatch::default(),
                "restore".to_string(): TitleMatch::default(),
                "restore-disabled".to_string(): TitleMatch::default(),
                "restore-partial".to_string(): TitleMatch::default(),
            },
            finder.find(TitleQuery {
                partial: true,
                ..Default::default()
            }),
        );
        assert_eq!(
            btree_map! {"restore-partial".to_string(): TitleMatch::default() },
            finder.find(TitleQuery {
                restore: true,
                partial: true,
                ..Default::default()
            }),
        );

        assert_eq!(
            btree_map! {
                "backup".to_string(): TitleMatch { score: Some(0.8888888888888888) },
                "backup-disabled".to_string(): TitleMatch { score: Some(0.7555555555555555) },
                "backup-partial".to_string(): TitleMatch { score: Some(0.7619047619047619) },
            },
            finder.find(TitleQuery {
                names: vec!["acku".to_string()],
                multiple: true,
                normalized: true,
                fuzzy: true,
                ..Default::default()
            }),
        );
    }
}