ludusavi 0.21.0

Game save backup tool
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
use std::collections::HashMap;

use crate::prelude::{StrictPath, ENV_DEBUG};

use crate::{
    resource::{config::RootsConfig, manifest::Os},
    scan::{launchers::LauncherGame, TitleFinder},
};

/// Deserialization of Heroic gog_store/installed.json
#[derive(serde::Deserialize)]
struct HeroicInstalledGame {
    /// This is an opaque ID, not the human-readable title.
    #[serde(rename = "appName")]
    app_name: String,
    platform: String,
    install_path: String,
}
#[derive(serde::Deserialize)]
struct HeroicInstalled {
    installed: Vec<HeroicInstalledGame>,
}

/// Deserialization of Heroic gog_store/library.json
#[derive(serde::Deserialize)]
struct GogLibraryGame {
    /// This is an opaque ID, not the human-readable title.
    app_name: String,
    title: String,
}
#[derive(serde::Deserialize)]
struct GogLibrary {
    games: Vec<GogLibraryGame>,
}

/// Deserialization of Legendary legendary/installed.json
#[derive(serde::Deserialize)]
struct LegendaryInstalledGame {
    /// This is an opaque ID, not the human-readable title.
    #[serde(rename = "app_name")]
    app_name: String,
    title: String,
    platform: String,
    install_path: String,
}
#[derive(serde::Deserialize)]
struct LegendaryInstalled(HashMap<String, LegendaryInstalledGame>);

/// Deserialization of Heroic GamesConfig/*.json
#[derive(serde::Deserialize, Debug)]
struct GamesConfigWrapper(HashMap<String, GamesConfig>);

#[derive(serde::Deserialize, Debug)]
#[serde(untagged)]
enum GamesConfig {
    Config {
        #[serde(rename = "winePrefix")]
        wine_prefix: String,
        #[serde(rename = "wineVersion")]
        wine_version: GamesConfigWine,
    },
    IgnoreOther(serde::de::IgnoredAny),
}
#[derive(serde::Deserialize, Debug)]
struct GamesConfigWine {
    #[serde(rename = "type")]
    wine_type: String,
}

pub fn scan(
    root: &RootsConfig,
    title_finder: &TitleFinder,
    legendary: Option<&StrictPath>,
) -> HashMap<String, LauncherGame> {
    let mut games = HashMap::new();

    games.extend(detect_legendary_games(root, title_finder, legendary));
    games.extend(detect_gog_games(root, title_finder));

    games
}

fn detect_legendary_games(
    root: &RootsConfig,
    title_finder: &TitleFinder,
    legendary: Option<&StrictPath>,
) -> HashMap<String, LauncherGame> {
    let mut games = HashMap::new();

    log::trace!("detect_legendary_games searching for legendary config...");

    let legendary_paths = match legendary {
        None => vec![
            StrictPath::relative("../legendary".to_string(), Some(root.path.interpret())),
            StrictPath::relative("legendaryConfig/legendary".to_string(), Some(root.path.interpret())),
            StrictPath::new("~/.config/legendary".to_string()),
        ],
        Some(x) => vec![x.clone()],
    };

    for legendary_path in legendary_paths {
        if legendary_path.is_dir() {
            log::trace!(
                "detect_legendary_games checking for legendary configuration in {}",
                legendary_path.interpret()
            );

            let legendary_installed = legendary_path.joined("installed.json");
            if legendary_installed.is_file() {
                // read list of installed games and call find_prefix for result
                if let Ok(installed_games) =
                    serde_json::from_str::<LegendaryInstalled>(&legendary_installed.read().unwrap_or_default())
                {
                    for game in installed_games.0.values() {
                        log::trace!(
                            "detect_legendary_games found legendary game {} ({})",
                            game.title,
                            game.app_name
                        );
                        let official_title =
                            title_finder.find_one(&[game.title.to_owned()], &None, &None, true, true, false);
                        // process game from GamesConfig
                        let prefix =
                            find_prefix(&root.path, &game.title, &game.platform.to_lowercase(), &game.app_name);
                        memorize_game(
                            &mut games,
                            &game.title,
                            official_title,
                            StrictPath::new(game.install_path.clone()),
                            prefix,
                            &game.platform,
                        );
                    }
                }
            } else {
                log::trace!(
                    "detect_legendary_games no such file '{:?}', legendary probably not used yet... skipping",
                    legendary_installed
                );
            }
        }
    }

    games
}

fn detect_gog_games(root: &RootsConfig, title_finder: &TitleFinder) -> HashMap<String, LauncherGame> {
    let mut games = HashMap::new();

    log::trace!(
        "detect_gog_games searching for GOG information in {}",
        root.path.interpret()
    );

    // use library.json to build map .app_name -> .title
    let libraries = [
        root.path.joined("store_cache").joined("gog_library.json"),
        root.path.joined("gog_store").joined("library.json"),
    ];
    let library_path: StrictPath;
    'library: {
        for library in libraries {
            if library.is_file() {
                library_path = library;
                break 'library;
            }
        }
        log::info!("detect_gog_games aborting since it could not find GOG library");
        return games;
    }

    let game_titles: HashMap<String, String> =
        match serde_json::from_str::<GogLibrary>(&library_path.read().unwrap_or_default()) {
            Ok(gog_library) => gog_library
                .games
                .iter()
                .map(|game| (game.app_name.clone(), game.title.clone()))
                .collect(),
            Err(e) => {
                log::warn!(
                    "detect_gog_games aborting since it could not read {}: {}",
                    library_path.interpret(),
                    e
                );
                return games;
            }
        };
    log::trace!(
        "detect_gog_games found {} games in {}",
        game_titles.len(),
        library_path.interpret()
    );

    // iterate over all games found in HEROCONFIGDIR/gog_store/installed.json and call find_prefix
    let installed_path = root.path.joined("gog_store").joined("installed.json");
    let content = installed_path.read();
    if let Ok(installed_games) = serde_json::from_str::<HeroicInstalled>(&content.unwrap_or_default()) {
        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, true, false);
                let prefix = find_prefix(&root.path, game_title, &game.platform, &game.app_name);
                memorize_game(
                    &mut games,
                    game_title,
                    official_title,
                    StrictPath::new(game.install_path),
                    prefix,
                    &game.platform,
                );
            }
        }
    }

    games
}

fn memorize_game(
    games: &mut HashMap<String, LauncherGame>,
    heroic_title: &str,
    official_title: Option<String>,
    install_dir: StrictPath,
    prefix: Option<StrictPath>,
    platform: &str,
) {
    let platform = Some(Os::from(platform));

    log::trace!(
        "memorize_game memorizing info for '{:?}' (from: '{}'): install_dir={:?}, prefix={:?}",
        &official_title,
        heroic_title,
        &install_dir,
        &prefix
    );

    if let Some(official) = official_title {
        games.insert(
            official,
            LauncherGame {
                install_dir,
                prefix,
                platform,
            },
        );
    } else {
        // Handling game name mismatches, e.g. GRIP vs. GRIP: Combat Racing
        let log_message = format!("Ignoring unrecognized Heroic game: '{}'", heroic_title);
        if std::env::var(ENV_DEBUG).is_ok() {
            eprintln!("{}", &log_message);
        }
        log::info!("{}", &log_message);

        games.insert(
            heroic_title.to_string(),
            LauncherGame {
                install_dir,
                prefix,
                platform,
            },
        );
    }
}

fn find_prefix(heroic_path: &StrictPath, game_name: &str, platform: &str, app_name: &str) -> Option<StrictPath> {
    match platform {
        "windows" => {
            log::trace!(
                "find_prefix found Heroic Windows game {}, looking closer ...",
                game_name
            );

            let games_config_path = heroic_path.joined("GamesConfig").joined(&format!("{app_name}.json"));
            match serde_json::from_str::<GamesConfigWrapper>(&games_config_path.read().unwrap_or_default()) {
                Ok(games_config_wrapper) => {
                    if let Some(game_config) = games_config_wrapper.0.get(app_name) {
                        match game_config {
                            GamesConfig::Config {
                                wine_version,
                                wine_prefix,
                            } => match wine_version.wine_type.as_str() {
                                "wine" => {
                                    log::trace!(
                                        "find_prefix found Heroic Wine prefix for {} ({}) -> adding {}",
                                        game_name,
                                        app_name,
                                        wine_prefix
                                    );
                                    Some(StrictPath::new(wine_prefix.clone()))
                                }

                                "proton" => {
                                    log::trace!(
                                        "find_prefix found Heroic Proton prefix for {} ({}), adding... -> {}",
                                        game_name,
                                        app_name,
                                        format!("{}/pfx", wine_prefix)
                                    );
                                    Some(StrictPath::new(format!("{}/pfx", wine_prefix)))
                                }

                                _ => {
                                    log::info!(
                                            "find_prefix found Heroic Windows game {} ({}), checking... unknown wine_type: {:#?} -> ignored",
                                            game_name,
                                            app_name,
                                            wine_version.wine_type
                                        );
                                    None
                                }
                            },
                            GamesConfig::IgnoreOther(_) => None,
                        }
                    } else {
                        None
                    }
                }
                Err(e) => {
                    log::trace!("find_prefix error: '{}', ignoring", e);
                    None
                }
            }
        }

        "linux" => {
            log::trace!("find_prefix found Heroic Linux game {}, ignoring", game_name);
            None
        }

        _ => {
            log::trace!(
                "find_prefix found Heroic game {} with unhandled platform {}, ignoring.",
                game_name,
                platform,
            );
            None
        }
    }
}

#[cfg(test)]
mod tests {
    use maplit::hashmap;
    use pretty_assertions::assert_eq;

    use super::*;
    use crate::{
        resource::{
            manifest::{Manifest, Store},
            ResourceFile,
        },
        testing::repo,
    };

    fn manifest() -> Manifest {
        Manifest::load_from_string(
            r#"
            windows-game:
              files:
                <base>/file1.txt: {}
            proton-game:
              files:
                <base>/file1.txt: {}
            "#,
        )
        .unwrap()
    }

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

    #[test]
    fn scan_finds_nothing_when_folder_does_not_exist() {
        let root = RootsConfig {
            path: StrictPath::new(format!("{}/tests/nonexistent", repo())),
            store: Store::Heroic,
        };
        let legendary = Some(StrictPath::new(format!("{}/tests/nonexistent", repo())));
        let games = scan(&root, &title_finder(), legendary.as_ref());
        assert_eq!(HashMap::new(), games);
    }

    #[test]
    fn scan_finds_all_games_without_store_cache() {
        let root = RootsConfig {
            path: StrictPath::new(format!("{}/tests/launchers/heroic-without-store-cache", repo())),
            store: Store::Heroic,
        };
        let legendary = Some(StrictPath::new(format!("{}/tests/launchers/legendary", repo())));
        let games = scan(&root, &title_finder(), legendary.as_ref());
        assert_eq!(
            hashmap! {
                "windows-game".to_string() => LauncherGame {
                    install_dir: StrictPath::new("C:\\Users\\me\\Games\\Heroic\\windows-game".to_string()),
                    prefix: Some(StrictPath::new("/home/root/Games/Heroic/Prefixes/windows-game".to_string())),
                    platform: Some(Os::Windows),
                },
                "proton-game".to_string() => LauncherGame {
                    install_dir: StrictPath::new("/home/root/Games/proton-game".to_string()),
                    prefix: Some(StrictPath::new("/home/root/Games/Heroic/Prefixes/proton-game/pfx".to_string())),
                    platform: Some(Os::Windows),
                },
            },
            games,
        );
    }

    #[test]
    fn scan_finds_all_games_with_store_cache() {
        let root = RootsConfig {
            path: StrictPath::new(format!("{}/tests/launchers/heroic-with-store-cache", repo())),
            store: Store::Heroic,
        };
        let legendary = Some(StrictPath::new(format!("{}/tests/launchers/legendary", repo())));
        let games = scan(&root, &title_finder(), legendary.as_ref());
        assert_eq!(
            hashmap! {
                "windows-game".to_string() => LauncherGame {
                    install_dir: StrictPath::new("C:\\Users\\me\\Games\\Heroic\\windows-game".to_string()),
                    prefix: Some(StrictPath::new("/home/root/Games/Heroic/Prefixes/windows-game".to_string())),
                    platform: Some(Os::Windows),
                },
                "proton-game".to_string() => LauncherGame {
                    install_dir: StrictPath::new("/home/root/Games/proton-game".to_string()),
                    prefix: Some(StrictPath::new("/home/root/Games/Heroic/Prefixes/proton-game/pfx".to_string())),
                    platform: Some(Os::Windows),
                },
            },
            games,
        );
    }
}