Skip to content

Commit

Permalink
Organize Heroic deserializations into modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed May 18, 2024
1 parent 55a21f3 commit d79e2ca
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 45 deletions.
64 changes: 34 additions & 30 deletions src/scan/launchers/heroic/gog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,39 @@ use crate::{
},
};

/// `gog_store/installed.json`
#[derive(serde::Deserialize)]
struct Installed {
installed: Vec<InstalledGame>,
}
pub mod installed {
pub const PATH: &str = "gog_store/installed.json";

#[derive(serde::Deserialize)]
struct InstalledGame {
/// This is an opaque ID, not the human-readable title.
#[serde(rename = "appName")]
app_name: String,
platform: String,
install_path: String,
}
#[derive(serde::Deserialize)]
pub struct Data {
pub installed: Vec<Game>,
}

/// `gog_store/library.json` or `store_cache/gog_library.json`
#[derive(serde::Deserialize)]
struct Library {
games: Vec<LibraryGame>,
#[derive(serde::Deserialize)]
pub struct Game {
/// This is an opaque ID, not the human-readable title.
#[serde(rename = "appName")]
pub app_name: String,
pub platform: String,
pub install_path: String,
}
}

#[derive(serde::Deserialize)]
pub struct LibraryGame {
/// This is an opaque ID, not the human-readable title.
pub app_name: String,
pub title: String,
pub mod library {
pub const PATH: &str = "store_cache/gog_library.json";
pub const PATH_LEGACY: &str = "gog_store/library.json";

#[derive(serde::Deserialize)]
pub struct Data {
pub games: Vec<Game>,
}

#[derive(serde::Deserialize)]
pub struct Game {
/// This is an opaque ID, not the human-readable title.
pub app_name: String,
pub title: String,
}
}

pub fn scan(root: &RootsConfig, title_finder: &TitleFinder) -> HashMap<String, LauncherGame> {
Expand All @@ -52,10 +59,10 @@ pub fn scan(root: &RootsConfig, title_finder: &TitleFinder) -> HashMap<String, L
}

// 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 installed_path = root.path.joined(installed::PATH);
let content = installed_path.read();

match serde_json::from_str::<Installed>(&content.unwrap_or_default()) {
match serde_json::from_str::<installed::Data>(&content.unwrap_or_default()) {
Ok(installed_games) => {
for game in installed_games.installed {
let Some(game_title) = game_titles.get(&game.app_name) else {
Expand Down Expand Up @@ -106,11 +113,8 @@ pub fn scan(root: &RootsConfig, title_finder: &TitleFinder) -> HashMap<String, L
games
}

pub fn get_library(root: &RootsConfig) -> Vec<LibraryGame> {
let libraries = [
root.path.joined("store_cache").joined("gog_library.json"),
root.path.joined("gog_store").joined("library.json"),
];
pub fn get_library(root: &RootsConfig) -> Vec<library::Game> {
let libraries = [root.path.joined(library::PATH), root.path.joined(library::PATH_LEGACY)];

let library_path = 'outer: {
for library in libraries {
Expand All @@ -122,7 +126,7 @@ pub fn get_library(root: &RootsConfig) -> Vec<LibraryGame> {
return vec![];
};

match serde_json::from_str::<Library>(&library_path.read().unwrap_or_default()) {
match serde_json::from_str::<library::Data>(&library_path.read().unwrap_or_default()) {
Ok(gog_library) => {
log::trace!("Found {} games in {:?}", gog_library.games.len(), &library_path);

Expand Down
2 changes: 1 addition & 1 deletion src/scan/launchers/heroic/legendary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn scan(
games
}

pub fn get_installed(root: &RootsConfig, legendary: Option<&StrictPath>) -> Vec<legendary_standalone::Game> {
pub fn get_installed(root: &RootsConfig, legendary: Option<&StrictPath>) -> Vec<legendary_standalone::installed::Game> {
let mut out = vec![];

let legendary_paths = match legendary {
Expand Down
33 changes: 19 additions & 14 deletions src/scan/launchers/legendary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@ use crate::{
scan::{launchers::LauncherGame, TitleFinder},
};

#[derive(Clone, serde::Deserialize)]
pub struct Game {
/// This is an opaque ID, not the human-readable title.
pub app_name: String,
pub title: String,
pub platform: String,
pub install_path: String,
}
pub mod installed {
use std::collections::HashMap;

pub const PATH: &str = "installed.json";

#[derive(serde::Deserialize)]
pub struct Data(pub HashMap<String, Game>);

/// `installed.json`
#[derive(serde::Deserialize)]
struct Library(HashMap<String, Game>);
#[derive(Clone, serde::Deserialize)]
pub struct Game {
/// This is an opaque ID, not the human-readable title.
pub app_name: String,
pub title: String,
pub platform: String,
pub install_path: String,
}
}

pub fn scan(root: &RootsConfig, title_finder: &TitleFinder) -> HashMap<String, LauncherGame> {
let mut out = HashMap::new();
Expand Down Expand Up @@ -53,10 +58,10 @@ pub fn scan(root: &RootsConfig, title_finder: &TitleFinder) -> HashMap<String, L
out
}

pub fn get_games(source: &StrictPath) -> Vec<Game> {
pub fn get_games(source: &StrictPath) -> Vec<installed::Game> {
let mut out = vec![];

let library = source.joined("installed.json");
let library = source.joined(installed::PATH);

let content = match library.try_read() {
Ok(content) => content,
Expand All @@ -70,7 +75,7 @@ pub fn get_games(source: &StrictPath) -> Vec<Game> {
}
};

if let Ok(installed_games) = serde_json::from_str::<Library>(&content) {
if let Ok(installed_games) = serde_json::from_str::<installed::Data>(&content) {
out.extend(installed_games.0.into_values());
}

Expand Down

0 comments on commit d79e2ca

Please sign in to comment.