game_scanner/epicgames/
item.rs

1use std::{
2    fs,
3    path::{Path, PathBuf},
4};
5
6use serde::{Deserialize, Serialize};
7
8use crate::{
9    error::{Error, ErrorKind, Result},
10    prelude::{Game, GameCommands, GameState, GameType},
11};
12
13#[derive(Serialize, Deserialize, Debug)]
14struct Manifest {
15    #[serde(rename(deserialize = "bIsIncompleteInstall"))]
16    is_incomplete_install: bool,
17    #[serde(rename(deserialize = "AppName"))]
18    app_name: String,
19    #[serde(rename(deserialize = "DisplayName"))]
20    display_name: String,
21    #[serde(rename(deserialize = "InstallLocation"))]
22    install_location: String,
23    #[serde(rename(deserialize = "LaunchExecutable"))]
24    launch_executable: String,
25}
26
27pub fn read(file: &Path, launcher_executable: &Path) -> Result<Game> {
28    let manifest_data = fs::read_to_string(&file).map_err(|error| {
29        Error::new(
30            ErrorKind::InvalidManifest,
31            format!(
32                "Invalid Epic Games manifest: {} {}",
33                file.display().to_string(),
34                error.to_string()
35            ),
36        )
37    })?;
38
39    let manifest = serde_json::from_str::<Manifest>(manifest_data.as_str()).map_err(|error| {
40        Error::new(
41            ErrorKind::InvalidManifest,
42            format!(
43                "Error on read the Epic Games manifest: {} {}",
44                file.display().to_string(),
45                error.to_string()
46            ),
47        )
48    })?;
49
50    if manifest.app_name.contains("UE") || manifest.app_name.contains("QuixelBridge") {
51        return Err(Error::new(
52            ErrorKind::IgnoredApp,
53            format!(
54                "({}) {} is an invalid game",
55                &manifest.app_name, &manifest.display_name
56            ),
57        ));
58    }
59
60    return Ok(Game {
61        _type: GameType::EpicGames.to_string(),
62        id: manifest.app_name.clone(),
63        name: manifest.display_name.clone(),
64        path: Some(PathBuf::from(manifest.install_location)),
65        commands: GameCommands {
66            install: None,
67            launch: Some(vec![
68                launcher_executable.display().to_string(),
69                format!(
70                    "com.epicgames.launcher://apps/{}?action=launch&silent=true",
71                    &manifest.app_name
72                ),
73            ]),
74            uninstall: None,
75        },
76        state: GameState {
77            installed: !manifest.is_incomplete_install,
78            needs_update: false,
79            downloading: false,
80            total_bytes: None,
81            received_bytes: None,
82        },
83    });
84}