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
use std::collections::{BTreeMap, BTreeSet};

use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};

use crate::{
    cloud::{CloudChange, Rclone},
    prelude::{app_dir, Error},
    report,
    scan::{
        layout::BackupLayout, prepare_backup_target, scan_game_for_backup, BackupId, DuplicateDetector, Launchers,
        OperationStepDecision, ScanKind, SteamShortcuts, TitleFinder, TitleMatch,
    },
};

pub use crate::{
    path::StrictPath,
    prelude::{Finality, SyncDirection},
    report::ApiOutput,
    resource::{config::Config, manifest::Manifest},
    scan::TitleQuery,
};

/// Unlike the CLI, this always uses the config's backup path, never the restore path.
pub struct Ludusavi {
    pub config: Config,
    pub manifest: Manifest,
    layout: BackupLayout,
    title_finder: TitleFinder,
    steam_shortcuts: SteamShortcuts,
}

impl Ludusavi {
    pub fn new(config: Config, manifest: Manifest) -> Self {
        let (layout, title_finder, steam_shortcuts) = Self::make_state(&config, &manifest);

        Self {
            config,
            manifest,
            layout,
            title_finder,
            steam_shortcuts,
        }
    }

    pub fn load() -> Result<Self, Error> {
        let config = Config::load()?;
        let manifest = Manifest::load()?;

        Ok(Self::new(config, manifest))
    }

    fn make_state(config: &Config, manifest: &Manifest) -> (BackupLayout, TitleFinder, SteamShortcuts) {
        let layout = BackupLayout::new(config.backup.path.clone());

        let title_finder = TitleFinder::new(config, manifest, layout.restorable_game_set());

        let steam_shortcuts = SteamShortcuts::scan(&title_finder);

        (layout, title_finder, steam_shortcuts)
    }

    /// Update internal state after a change to config, manifest, or backups.
    pub fn refresh(&mut self) {
        let (layout, title_finder, steam_shortcuts) = Self::make_state(&self.config, &self.manifest);

        self.layout = layout;
        self.title_finder = title_finder;
        self.steam_shortcuts = steam_shortcuts;
    }

    fn target(&self) -> &StrictPath {
        &self.config.backup.path
    }

    fn sync_cloud(&self, sync: SyncDirection, finality: Finality, games: &[String]) -> Result<Vec<CloudChange>, Error> {
        match finality {
            Finality::Preview => log::info!("checking cloud sync"),
            Finality::Final => log::info!("performing cloud sync"),
        }

        let remote = crate::cloud::validate_cloud_config(&self.config, &self.config.cloud.path)?;

        let games = if !games.is_empty() {
            games.iter().filter_map(|x| self.layout.game_folder(x).leaf()).collect()
        } else {
            vec![]
        };

        let rclone = Rclone::new(self.config.apps.rclone.clone(), remote);
        let mut process = match rclone.sync(self.target(), &self.config.cloud.path, sync, finality, &games) {
            Ok(p) => p,
            Err(e) => return Err(Error::UnableToSynchronizeCloud(e)),
        };

        let mut changes = vec![];
        loop {
            let events = process.events();
            for event in events {
                match event {
                    crate::cloud::RcloneProcessEvent::Progress { .. } => {}
                    crate::cloud::RcloneProcessEvent::Change(change) => {
                        changes.push(change);
                    }
                }
            }
            match process.succeeded() {
                Some(Ok(_)) => {
                    return Ok(changes);
                }
                Some(Err(e)) => {
                    return Err(Error::UnableToSynchronizeCloud(e));
                }
                None => (),
            }
        }
    }

    /// Back up games.
    pub fn back_up(
        &mut self,
        parameters::BackUp {
            games,
            finality,
            resolve_cloud_conflict,
            wine_prefix,
            include_disabled,
            skip_downgrade,
        }: parameters::BackUp,
    ) -> Result<ApiOutput, Error> {
        let mut reporter = report::Reporter::json();

        let roots = self.config.expanded_roots();
        let backup_dir = self.target().clone();

        if !finality.preview() {
            prepare_backup_target(&backup_dir)?;
        }

        let retention = self.config.backup.retention;

        let games_specified = !games.is_empty();
        let games = evaluate_games(self.manifest.primary_titles(), games, &self.title_finder)?;

        let mut duplicate_detector = DuplicateDetector::default();
        let launchers = Launchers::scan(&roots, &self.manifest, &games, &self.title_finder, None);

        let cloud_sync = self.config.cloud.synchronize
            && !finality.preview()
            && crate::cloud::validate_cloud_config(&self.config, &self.config.cloud.path).is_ok();
        let mut should_sync_cloud_after = cloud_sync && !finality.preview();
        let mut should_sync_cloud_after_even_if_unchanged = false;
        if cloud_sync {
            let changes = self.sync_cloud(
                SyncDirection::Upload,
                Finality::Preview,
                if games_specified { &games } else { &[] },
            );
            match changes {
                Ok(changes) => {
                    if !changes.is_empty() {
                        match resolve_cloud_conflict {
                            Some(direction @ SyncDirection::Download) => {
                                // We need to download before the new backup
                                // to keep mapping.yaml in a coherent state.
                                if let Err(e) = self.sync_cloud(
                                    direction,
                                    Finality::Final,
                                    if games_specified { &games } else { &[] },
                                ) {
                                    log::error!("Failed to resolve save conflict pre-backup with direction {direction:?}: {e:?}");
                                    should_sync_cloud_after = false;
                                    reporter.trip_cloud_sync_failed();
                                }
                            }
                            Some(SyncDirection::Upload) => {
                                // We'll make the new backup first and then sync after.
                                should_sync_cloud_after_even_if_unchanged = true;
                            }
                            None => {
                                should_sync_cloud_after = false;
                                reporter.trip_cloud_conflict();
                            }
                        }
                    }
                }
                Err(_) => {
                    should_sync_cloud_after = false;
                    reporter.trip_cloud_sync_failed();
                }
            }
        }

        let step = |i, name| {
            log::trace!("step {i} / {}: {name}", games.len());
            let game = &self.manifest.0[name];

            let previous = self.layout.latest_backup(
                name,
                ScanKind::Backup,
                &self.config.redirects,
                self.config.restore.reverse_redirects,
                &self.config.restore.toggled_paths,
                self.config.backup.only_constructive,
            );

            if self
                .config
                .backup
                .filter
                .excludes(games_specified, previous.is_some(), &game.cloud)
            {
                log::trace!("[{name}] excluded by backup filter");
                return None;
            }

            let scan_info = scan_game_for_backup(
                game,
                name,
                &roots,
                &app_dir(),
                &launchers,
                &self.config.backup.filter,
                wine_prefix.as_ref(),
                &self.config.backup.toggled_paths,
                &self.config.backup.toggled_registry,
                previous.as_ref(),
                &self.config.redirects,
                self.config.restore.reverse_redirects,
                &self.steam_shortcuts,
                self.config.backup.only_constructive,
            );
            let ignored = !&self.config.is_game_enabled_for_backup(name) && !games_specified && !include_disabled;
            let decision = if ignored {
                OperationStepDecision::Ignored
            } else {
                OperationStepDecision::Processed
            };
            let backup_info = if finality.preview()
                || ignored
                || (skip_downgrade && previous.is_some_and(|x| scan_info.is_downgraded_backup(x.when)))
            {
                None
            } else {
                self.layout.game_layout(name).back_up(
                    &scan_info,
                    &chrono::Utc::now(),
                    &self.config.backup.format,
                    retention,
                    self.config.backup.only_constructive,
                )
            };
            log::trace!("step {i} completed");
            if !scan_info.can_report_game() {
                None
            } else {
                let display_title = self.config.display_name(name);
                Some((display_title, scan_info, backup_info, decision))
            }
        };

        log::info!("beginning backup with {} steps", games.len());

        let info: Vec<_> = games
            .par_iter()
            .enumerate()
            .filter_map(|(i, name)| step(i, name))
            .collect();
        log::info!("completed backup");

        if should_sync_cloud_after {
            let changed_games: Vec<_> = info
                .iter()
                .filter(|(_, scan_info, backup_info, _)| scan_info.needs_cloud_sync() && backup_info.is_some())
                .map(|(_, scan_info, _, _)| scan_info.game_name.clone())
                .collect();
            if !changed_games.is_empty() || should_sync_cloud_after_even_if_unchanged {
                let sync_result = self.sync_cloud(SyncDirection::Upload, Finality::Final, &changed_games);
                if sync_result.is_err() {
                    reporter.trip_cloud_sync_failed();
                }
            }
        }

        for (_, scan_info, _, _) in info.iter() {
            duplicate_detector.add_game(
                scan_info,
                self.config
                    .is_game_enabled_for_operation(&scan_info.game_name, ScanKind::Backup),
            );
        }

        for (name, scan_info, backup_info, decision) in info {
            reporter.add_game(
                name,
                &scan_info,
                backup_info.as_ref(),
                &decision,
                &duplicate_detector,
                false,
            );
        }

        self.refresh();
        reporter.json_output().ok_or(Error::SomeEntriesFailed)
    }

    /// Restore backups.
    pub fn restore(
        &mut self,
        parameters::Restore {
            games,
            finality,
            backup,
            resolve_cloud_conflict,
            include_disabled,
            skip_downgrade,
        }: parameters::Restore,
    ) -> Result<ApiOutput, Error> {
        let mut reporter = report::Reporter::json();

        if backup.is_some() && games.len() != 1 {
            return Err(Error::CliBackupIdWithMultipleGames);
        }
        let backup_id = backup.as_ref().map(|x| BackupId::Named(x.clone()));

        let games_specified = !games.is_empty();
        let games = evaluate_games(self.manifest.primary_titles(), games, &self.title_finder)?;

        let mut duplicate_detector = DuplicateDetector::default();

        let cloud_sync = self.config.cloud.synchronize
            && !finality.preview()
            && crate::cloud::validate_cloud_config(&self.config, &self.config.cloud.path).is_ok();
        if cloud_sync {
            let changes = self.sync_cloud(
                SyncDirection::Upload,
                Finality::Preview,
                if games_specified { &games } else { &[] },
            );
            match changes {
                Ok(changes) => {
                    if !changes.is_empty() {
                        match resolve_cloud_conflict {
                            Some(direction) => {
                                if let Err(e) = self.sync_cloud(
                                    direction,
                                    Finality::Final,
                                    if games_specified { &games } else { &[] },
                                ) {
                                    log::error!("Failed to resolve save conflict pre-restore with direction {direction:?}: {e:?}");
                                    reporter.trip_cloud_sync_failed();
                                }
                            }
                            None => {
                                reporter.trip_cloud_conflict();
                            }
                        }
                    }
                }
                Err(_) => {
                    reporter.trip_cloud_sync_failed();
                }
            }
        }

        let step = |i, name| {
            log::trace!("step {i} / {}: {name}", games.len());
            let mut layout = self.layout.game_layout(name);
            let scan_info = layout.scan_for_restoration(
                name,
                backup_id.as_ref().unwrap_or(&BackupId::Latest),
                &self.config.redirects,
                self.config.restore.reverse_redirects,
                &self.config.restore.toggled_paths,
                &self.config.restore.toggled_registry,
            );
            let ignored = !&self.config.is_game_enabled_for_restore(name) && !games_specified && !include_disabled;
            let decision = if ignored {
                OperationStepDecision::Ignored
            } else {
                OperationStepDecision::Processed
            };

            if let Some(backup) = &backup {
                if let Some(BackupId::Named(scanned_backup)) = scan_info.backup.as_ref().map(|x| x.id()) {
                    if backup != &scanned_backup {
                        log::trace!("step {i} completed (backup mismatch)");
                        let display_title = self.config.display_name(name);
                        return Some((
                            display_title,
                            scan_info,
                            Default::default(),
                            decision,
                            Some(Error::CliInvalidBackupId),
                        ));
                    }
                }
            }

            let restore_info = if scan_info.backup.is_none()
                || finality.preview()
                || ignored
                || (skip_downgrade && scan_info.is_downgraded_restore())
            {
                None
            } else {
                Some(layout.restore(&scan_info, &self.config.restore.toggled_registry))
            };
            log::trace!("step {i} completed");
            if !scan_info.can_report_game() {
                None
            } else {
                let display_title = self.config.display_name(name);
                Some((display_title, scan_info, restore_info, decision, None))
            }
        };

        log::info!("beginning restore with {} steps", games.len());

        let info: Vec<_> = games
            .par_iter()
            .enumerate()
            .filter_map(|(i, name)| step(i, name))
            .collect();
        log::info!("completed restore");

        for (_, scan_info, _, _, failure) in info.iter() {
            if let Some(failure) = failure {
                return Err(failure.clone());
            }
            duplicate_detector.add_game(
                scan_info,
                self.config
                    .is_game_enabled_for_operation(&scan_info.game_name, ScanKind::Restore),
            );
        }

        for (name, scan_info, backup_info, decision, _) in info {
            reporter.add_game(
                name,
                &scan_info,
                backup_info.as_ref(),
                &decision,
                &duplicate_detector,
                false,
            );
        }

        reporter.json_output().ok_or(Error::SomeEntriesFailed)
    }

    /// List backups.
    pub fn list_backups(&self, parameters::ListBackups { games }: parameters::ListBackups) -> Result<ApiOutput, Error> {
        let mut reporter = report::Reporter::json();
        reporter.suppress_overall();

        let games = evaluate_games(self.layout.restorable_game_set(), games, &self.title_finder)?;

        let info: Vec<_> = games
            .par_iter()
            .map(|name| {
                let mut layout = self.layout.game_layout(name);
                let backups = layout.get_backups();
                let display_title = self.config.display_name(name);
                let backup_dir = layout.path;
                (name, display_title, backup_dir, backups)
            })
            .collect();

        for (name, display_title, backup_dir, backups) in info {
            reporter.add_backups(name, display_title, backup_dir, &backups);
        }

        reporter.json_output().ok_or(Error::SomeEntriesFailed)
    }

    /// Edit a backup.
    ///
    /// These changes are not automatically synced with the cloud.
    pub fn edit_backup(
        &mut self,
        parameters::EditBackup {
            game,
            backup,
            locked,
            comment,
        }: parameters::EditBackup,
    ) -> Result<(), Error> {
        let backup = backup.map(BackupId::Named).unwrap_or(BackupId::Latest);

        let Some(game) = self.title_finder.find_one_by_name(&game) else {
            return Err(Error::GameIsUnrecognized);
        };

        let mut layout = self.layout.game_layout(&game);
        layout.validate_id(&backup)?;

        if let Some(locked) = locked {
            layout.set_backup_locked(&backup, locked);
        }
        if let Some(comment) = comment {
            layout.set_backup_comment(&backup, &comment);
        }
        layout.save();

        self.refresh();
        Ok(())
    }

    /// Look up games based on certain criteria.
    ///
    /// 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_title(&self, query: TitleQuery) -> BTreeMap<String, TitleMatch> {
        self.title_finder.find(query)
    }
}

pub mod parameters {
    use super::*;

    #[derive(Clone, Debug, PartialEq, Eq)]
    pub struct BackUp {
        /// Which game to process. Defaults to all games.
        pub games: Vec<String>,
        /// Whether to actually perform the operation or just preview the results.
        pub finality: Finality,
        /// Automatically resolve cloud conflicts by performing an upload or download.
        pub resolve_cloud_conflict: Option<SyncDirection>,
        /// Extra Wine/Proton prefix to check for saves.
        /// This should be a folder with an immediate child folder named "drive_c" (or another letter).
        pub wine_prefix: Option<StrictPath>,
        /// Process disabled games.
        pub include_disabled: bool,
        /// Skip a game when its backup is newer than the live data.
        /// Currently, this only considers file-based saves, not the Windows registry.
        ///
        /// You might want to use this if you force a backup on game exit,
        /// but you sometimes restore an older save temporarily to check something,
        /// and you don't want to accidentally back up that old save again.
        /// (If the save file gets updated during play, it will be considered newer.)
        pub skip_downgrade: bool,
    }

    #[derive(Clone, Debug, PartialEq, Eq)]
    pub struct Restore {
        /// Which game to process. Defaults to all games.
        pub games: Vec<String>,
        /// Whether to actually perform the operation or just preview the results.
        pub finality: Finality,
        /// Restore a specific backup, using an ID returned by the `backups` command.
        /// This is only valid when restoring a single game.
        pub backup: Option<String>,
        /// Automatically resolve cloud conflicts by performing an upload or download.
        pub resolve_cloud_conflict: Option<SyncDirection>,
        /// Process disabled games.
        pub include_disabled: bool,
        /// Skip a game when its backup is newer than the live data.
        /// Currently, this only considers file-based saves, not the Windows registry.
        ///
        /// You might want to use this if you force a backup on game exit,
        /// but you sometimes restore an older save temporarily to check something,
        /// and you don't want to accidentally back up that old save again.
        /// (If the save file gets updated during play, it will be considered newer.)
        pub skip_downgrade: bool,
    }

    #[derive(Clone, Debug, PartialEq, Eq)]
    pub struct ListBackups {
        /// Which game to list. Defaults to all games.
        pub games: Vec<String>,
    }

    #[derive(Clone, Debug, PartialEq, Eq)]
    pub struct EditBackup {
        /// Which game to edit.
        pub game: String,
        /// Edit a specific backup, using an ID returned by the `backups` command.
        /// When not specified, this defaults to the latest backup.
        pub backup: Option<String>,
        pub locked: Option<bool>,
        pub comment: Option<String>,
    }
}

fn evaluate_games(
    default: BTreeSet<String>,
    requested: Vec<String>,
    title_finder: &TitleFinder,
) -> Result<Vec<String>, Error> {
    if requested.is_empty() {
        return Ok(default.into_iter().collect());
    }

    let mut valid = BTreeSet::new();
    let mut invalid = BTreeSet::new();

    for game in requested {
        match title_finder.find_one_by_name(&game) {
            Some(found) => {
                valid.insert(found);
            }
            None => {
                invalid.insert(game);
            }
        }
    }

    if !invalid.is_empty() {
        return Err(Error::CliUnrecognizedGames {
            games: invalid.into_iter().collect(),
        });
    }

    Ok(valid.into_iter().collect())
}