lib_game_detector 0.0.33

A Rust library for detecting and parsing data about games installed on the system
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
// PATHS:
// - ~/.local/share/bottles/
// - Flatpak: ~/.var/app/com.usebottles.bottles/data/bottles/
use std::{
    fs::{read_dir, read_to_string},
    io,
    path::{Path, PathBuf},
    sync::Arc,
};

use nom::{
    IResult, Parser,
    bytes::complete::{is_not, tag, take_till},
    character::complete::multispace1,
    sequence::preceded,
};
use tracing::{error, trace, warn};

use crate::{
    data::{Game, GamesResult, Launcher, SupportedLaunchers},
    macros::logs::{debug_fallback_flatpak, debug_path, warn_no_games},
    parsers::{
        parse_not_alphanumeric, parse_till_end_of_line, parse_until_key_yml, parse_value_yml,
    },
    utils::{
        clean_game_title, get_launch_command, get_launch_command_flatpak, some_if_dir, some_if_file,
    },
};

#[derive(Debug, Clone)]
pub struct ParsableLibraryData {
    id: String,
    title: String,
    icon: Option<PathBuf>,
    box_art: Option<String>,
    bottle_name: String,
    bottle_subdir: String,
}

#[derive(Debug, Clone)]
pub struct ParsableBottleYmlData {
    id: String,
    game_dir: String,
}

#[derive(Debug)]
pub struct ParsableDataCombined {
    title: String,
    path_icon: Option<PathBuf>,
    box_art: Option<String>,
    bottle_name: String,
    bottle_subdir: String,
    game_dir: String,
}

impl ParsableDataCombined {
    fn combine(library_data: ParsableLibraryData, bottle_data: ParsableBottleYmlData) -> Self {
        ParsableDataCombined {
            title: library_data.title,
            path_icon: library_data.icon,
            box_art: library_data.box_art,
            bottle_subdir: library_data.bottle_subdir,
            bottle_name: library_data.bottle_name,
            game_dir: bottle_data.game_dir,
        }
    }
}

const LAUNCHER: SupportedLaunchers = SupportedLaunchers::Bottles;

// UTILS --------------------------------------------------------------------------------
/// Used for parsing a single game's relevant data from the given bottle `.yml` file's contents
#[tracing::instrument(level = "trace", skip(file_content))]
fn parse_game_from_bottle_yml(file_content: &str) -> IResult<&str, ParsableBottleYmlData> {
    // GAME DIR
    let key_game_dir = "folder";
    let (file_content, _) = parse_until_key_yml(file_content, key_game_dir)?;
    let (mut file_content, first_path_fragment) =
        preceded(take_till(|c| c == '/'), parse_till_end_of_line).parse(file_content)?;

    // Path can be split into multiple lines unfortunately
    let mut game_dir_fragments: Vec<&str> = vec![&first_path_fragment];
    loop {
        let (new_file_content, line) =
            preceded(tag("\n"), parse_till_end_of_line).parse(file_content)?;

        if line.contains(':') {
            break;
        };

        file_content = new_file_content;

        let (path_fragment, _) = multispace1.parse(line)?;

        game_dir_fragments.push(path_fragment);
    }

    let game_dir = game_dir_fragments.join(" ");

    // ID
    let key_id = "id";
    let (file_content, _) = parse_until_key_yml(file_content, key_id)?;
    let (file_content, id) = parse_value_yml(file_content, key_id)?;

    Ok((file_content, ParsableBottleYmlData { id, game_dir }))
}

/// Used for parsing relevant games' data from the given bottle library file's contents
#[tracing::instrument(level = "trace", skip(file_content))]
fn parse_game_from_library<'a>(file_content: &'a str) -> IResult<&'a str, ParsableLibraryData> {
    // BOTTLE NAME
    let key_bottle_name = "name";
    let (file_content, _) = parse_until_key_yml(file_content, key_bottle_name)?;
    let (file_content, bottle_name) = parse_value_yml(file_content, key_bottle_name)?;

    // BOTTLE SUBDIR
    let key_bottle_subdir = "path";
    let (file_content, _) = parse_until_key_yml(file_content, key_bottle_subdir)?;
    let (file_content, bottle_subdir) = parse_value_yml(file_content, key_bottle_subdir)?;

    // ICON
    let key_icon = "icon";
    let (file_content, _) = parse_until_key_yml(file_content, key_icon)?;
    let (file_content, _) = tag(format!("{key_icon}: ").as_str()).parse(file_content)?;
    let (mut file_content, line1) = parse_till_end_of_line(file_content)?;
    file_content = file_content.trim_start();

    let (next_file_content, line2) = parse_till_end_of_line(file_content)?;
    let (file_content, icon_str) = if line2.contains("id:") {
        (file_content, line1.to_owned())
    } else {
        (next_file_content, [line1, line2].join(" "))
    };
    let icon_path = PathBuf::from(icon_str);
    let icon = icon_path.is_file().then_some(icon_path);

    // ID
    let key_id = "id";
    let (file_content, _) = parse_until_key_yml(file_content, key_id)?;
    let (file_content, id) = parse_value_yml(file_content, key_id)?;

    // TITLE
    let key_title = "name";
    let (file_content, _) = parse_until_key_yml(file_content, key_title)?;
    let (file_content, title) = parse_value_yml(file_content, key_title)?;

    // BOX ART
    let key_box_art = "thumbnail";
    let (file_content, _) = parse_until_key_yml(file_content, key_box_art)?;
    let (file_content, _) = preceded(parse_not_alphanumeric, is_not(":")).parse(file_content)?;

    let box_art = if let Ok((_, box_art)) =
        preceded(tag(": grid:"), parse_till_end_of_line).parse(file_content)
    {
        Some(box_art.to_owned())
    } else {
        None
    };

    Ok((
        file_content,
        ParsableLibraryData {
            id,
            title,
            bottle_subdir,
            bottle_name,
            box_art,
            icon,
        },
    ))
}

// BOTTLES LAUNCHER ----------------------------------------------------------------------
#[derive(Debug)]
pub struct Bottles {
    path_bottles_dir: PathBuf,
    path_bottles_library: PathBuf,
    is_using_flatpak: bool,
}

impl Bottles {
    pub fn new(path_home: &Path, path_data: &Path) -> Self {
        let mut path_bottles_data = path_data.join("bottles");
        let mut is_using_flatpak = false;

        if !path_bottles_data.is_dir() {
            debug_fallback_flatpak!();

            is_using_flatpak = true;
            path_bottles_data = path_home.join(".var/app/com.usebottles.bottles/data/bottles");
        }

        let path_bottles_dir = path_bottles_data.join("bottles");
        let path_bottles_library = path_bottles_data.join("library.yml");

        debug_path!("data directory", path_bottles_data);
        debug_path!("bottles directory", path_bottles_dir);
        debug_path!("library yaml file", path_bottles_library);

        Bottles {
            path_bottles_dir,
            path_bottles_library,
            is_using_flatpak,
        }
    }

    /// Parse data from a given `bottle.yml` file
    #[tracing::instrument(level = "trace")]
    fn get_parsable_bottle_yml_data(
        &self,
        path_bottle_yml: PathBuf,
    ) -> Option<Vec<ParsableBottleYmlData>> {
        let file_content = read_to_string(&path_bottle_yml)
            .map_err(|e| {
                error!(
                    "Error with reading bottle yaml file at {:?}:\n{e}",
                    path_bottle_yml
                );
            })
            .ok()?;

        let mut parsed_games_data: Vec<ParsableBottleYmlData> = Vec::new();
        let mut file_content_str: &str = &file_content;

        loop {
            let Ok((new_file_content, parsed_data)) = parse_game_from_bottle_yml(file_content_str)
            else {
                break;
            };
            file_content_str = new_file_content;

            parsed_games_data.push(parsed_data)
        }

        Some(parsed_games_data)
    }

    /// Parse data from all `bottle.yml` files
    #[tracing::instrument(level = "trace")]
    fn parse_all_bottles(&self) -> Result<Arc<[ParsableBottleYmlData]>, io::Error> {
        Ok(read_dir(&self.path_bottles_dir)
            .map_err(|e| {
                error!("Error with reading the 'bottles' directory: {e:?}");
                e
            })?
            .flatten()
            .filter_map(|d| self.get_parsable_bottle_yml_data(d.path().join("bottle.yml")))
            .flatten()
            .collect())
    }

    /// Parse data from the Bottle's `library.yml` file
    #[tracing::instrument(level = "trace")]
    fn parse_bottles_library(&self) -> Result<Vec<ParsableLibraryData>, io::Error> {
        let library_file_content = read_to_string(&self.path_bottles_library).map_err(|e| {
            error!(
                "Error with reading Bottles library file at {:?}:\n{e}",
                &self.path_bottles_library
            );
            e
        })?;

        let mut parsed_data: Vec<ParsableLibraryData> = Vec::new();
        let mut library_file_content_str: &str = &library_file_content;

        loop {
            let Ok((new_library_file_content, parsed_library_data)) =
                parse_game_from_library(library_file_content_str)
            else {
                break;
            };

            library_file_content_str = new_library_file_content;
            parsed_data.push(parsed_library_data);
        }

        Ok(parsed_data)
    }

    /// Get all relevant game data by combining data from the `library.yml` file and
    /// each bottle's `.yml` file. Data is matched using game ID.
    #[tracing::instrument]
    pub fn parse_game_data(&self) -> Result<Vec<ParsableDataCombined>, io::Error> {
        let parsed_library_data = self.parse_bottles_library()?;
        let parsed_bottles_data = self.parse_all_bottles()?;

        Ok(parsed_library_data
            .into_iter()
            .filter_map(|library_data| {
                parsed_bottles_data
                    .iter()
                    .find(|b| b.id == library_data.id)
                    .map(|bottle_data| {
                        ParsableDataCombined::combine(library_data, bottle_data.clone())
                    })
            })
            .collect())
    }
}

impl Launcher for Bottles {
    fn is_detected(&self) -> bool {
        self.path_bottles_library.exists()
    }

    fn get_launcher_type(&self) -> SupportedLaunchers {
        LAUNCHER
    }

    #[tracing::instrument(level = "trace")]
    fn get_detected_games(&self) -> GamesResult {
        let parsed_data = self.parse_game_data()?;

        if parsed_data.is_empty() {
            warn_no_games!();
        }

        Ok(parsed_data
            .into_iter()
            .map(
                |ParsableDataCombined {
                     title,
                     path_icon,
                     box_art,
                     bottle_name,
                     bottle_subdir,
                     game_dir,
                 }| {
                    let launch_command = {
                        let base_args = ["run", "-p", &title, "-b", &bottle_name];
                        if self.is_using_flatpak {
                            get_launch_command_flatpak(
                                "com.usebottles.bottles",
                                ["--command=bottles-cli"],
                                base_args,
                                [],
                            )
                        } else {
                            get_launch_command("bottles-cli", base_args, [])
                        }
                    };
                    trace!("{LAUNCHER} - launch command for '{title}': {launch_command:?}");

                    let path_box_art = box_art.clone().and_then(|s| {
                        let path = self
                            .path_bottles_dir
                            .join(format!("{bottle_subdir}/grids/{s}"));
                        some_if_file(path)
                    });

                    let path_game_dir = some_if_dir(PathBuf::from(game_dir));

                    trace!("{LAUNCHER} - Game directory for '{title}': {path_game_dir:?}");
                    trace!("{LAUNCHER} - Box art for '{title}': {path_box_art:?}");
                    trace!("{LAUNCHER} - Icon for '{title}': {path_icon:?}");

                    Game {
                        title: clean_game_title(title),
                        path_icon,
                        launch_command,
                        path_box_art,
                        path_game_dir,
                        source: LAUNCHER.clone(),
                    }
                },
            )
            .collect())
    }
}

#[cfg(test)]
mod tests {
    use test_case::test_case;

    use super::*;
    use crate::{error::GamesParsingError, linux::test_utils::get_mock_file_system_path};

    #[test_case(false, ".local/share"; "standard")]
    #[test_case(true, "invalid/data/path"; "flatpak")]
    fn test_bottles_launcher(
        is_testing_flatpak: bool,
        path_data: &str,
    ) -> Result<(), GamesParsingError> {
        let path_file_system_mock = get_mock_file_system_path();
        let launcher = Bottles::new(
            &path_file_system_mock,
            &path_file_system_mock.join(path_data),
        );

        assert!(launcher.is_detected());
        assert!(launcher.is_using_flatpak == is_testing_flatpak);

        let games = launcher.get_detected_games()?;
        assert_eq!(games.len(), 4);

        assert_eq!(games[0].title, "Warcraft III");
        assert_eq!(games[1].title, "GOG Galaxy");
        assert_eq!(games[2].title, "EA Client");
        assert_eq!(games[3].title, "Estlcam");

        assert!(games[0].path_game_dir.is_some());
        assert!(games[1].path_game_dir.is_none());
        assert!(games[2].path_game_dir.is_none());
        assert!(games[3].path_game_dir.is_none());

        assert!(games[0].path_box_art.is_some());
        assert!(games[1].path_box_art.is_some());
        assert!(games[2].path_box_art.is_some());
        assert!(games[3].path_box_art.is_none());

        // TODO: test icons - need some way to write correct paths in test `library.yml` file
        for g in games {
            assert!(g.path_icon.is_none());
        }

        Ok(())
    }
}