diff --git a/Cargo.lock b/Cargo.lock index c5a6c94..3b7f071 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1045,9 +1045,9 @@ dependencies = [ [[package]] name = "steam_shortcuts_util" -version = "1.1.1" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffa391cd87de757cdc91153362307680c6ec4ad50fe7a582509678f1ede863eb" +checksum = "51eecf84626e7ba46667be999ca124d9cd16c9feb09918dd991eccbb4ac298e7" dependencies = [ "ascii", "crc32fast", diff --git a/Cargo.toml b/Cargo.toml index c5bca86..2a4dbee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -steam_shortcuts_util="1.1.1" +steam_shortcuts_util="1.1.2" steamgriddb_api="0.2,0" serde={version="*", features=["derive"]} serde_json="*" diff --git a/src/egs/epic_platform.rs b/src/egs/epic_platform.rs new file mode 100644 index 0000000..5ecb1eb --- /dev/null +++ b/src/egs/epic_platform.rs @@ -0,0 +1,30 @@ +use crate::platform::Platform; + +use super::{ + get_egs_manifests, get_manifests::EpicGamesManifestsError, EpicGamesLauncherSettings, + ManifestItem, +}; + +pub struct EpicPlatform { + settings: EpicGamesLauncherSettings, +} + +impl EpicPlatform { + pub fn new(settings: EpicGamesLauncherSettings) -> Self { + EpicPlatform { settings } + } +} + +impl Platform for EpicPlatform { + fn enabled(&self) -> bool { + self.settings.enabled + } + + fn name(&self) -> &str { + "EGS" + } + + fn get_shortcuts(&self) -> Result, EpicGamesManifestsError> { + get_egs_manifests(&self.settings) + } +} diff --git a/src/egs/manifest_item.rs b/src/egs/manifest_item.rs index 28ec2cc..62cef95 100644 --- a/src/egs/manifest_item.rs +++ b/src/egs/manifest_item.rs @@ -19,8 +19,8 @@ pub struct ManifestItem { pub app_name: String, } -impl From<&ManifestItem> for ShortcutOwned { - fn from(manifest: &ManifestItem) -> Self { +impl From for ShortcutOwned { + fn from(manifest: ManifestItem) -> Self { let exe = format!( "\"{}\\{}\"", manifest.install_location, manifest.launch_executable diff --git a/src/egs/mod.rs b/src/egs/mod.rs index 1a33a3a..48e9d67 100644 --- a/src/egs/mod.rs +++ b/src/egs/mod.rs @@ -1,6 +1,9 @@ mod get_manifests; mod manifest_item; mod settings; +mod epic_platform; + pub use manifest_item::*; -pub use get_manifests::get_egs_manifests; -pub use settings::EpicGamesLauncherSettings; \ No newline at end of file +use get_manifests::get_egs_manifests; +pub use settings::EpicGamesLauncherSettings; +pub use epic_platform::*; \ No newline at end of file diff --git a/src/egs/settings.rs b/src/egs/settings.rs index 0e762cb..22f5c0a 100644 --- a/src/egs/settings.rs +++ b/src/egs/settings.rs @@ -1,6 +1,6 @@ use serde::Deserialize; -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize,Clone)] pub struct EpicGamesLauncherSettings { pub enabled: bool, pub location: Option, diff --git a/src/legendary/get_legendary_games.rs b/src/legendary/get_legendary_games.rs deleted file mode 100644 index ea4d18d..0000000 --- a/src/legendary/get_legendary_games.rs +++ /dev/null @@ -1,14 +0,0 @@ -use super::legendary_game::LegendaryGame; -use serde_json::from_str; -use std::process::Command; -use std::{error::Error}; - -pub fn get_legendary_games() -> Result, Box> { - let legendary_command = Command::new("legendary") - .arg("list-installed") - .arg("--json") - .output()?; - let json = String::from_utf8_lossy(&legendary_command.stdout); - let legendary_ouput = from_str(&json)?; - Ok(legendary_ouput) -} \ No newline at end of file diff --git a/src/legendary/legendary_game.rs b/src/legendary/legendary_game.rs index 451c5cf..a06e5f7 100644 --- a/src/legendary/legendary_game.rs +++ b/src/legendary/legendary_game.rs @@ -11,8 +11,8 @@ pub struct LegendaryGame { pub executable: String, } -impl From<&LegendaryGame> for ShortcutOwned { - fn from(game: &LegendaryGame) -> Self { +impl From for ShortcutOwned { + fn from(game: LegendaryGame) -> Self { let exe = format!("\"{}\\{}\"", game.install_path, game.executable); let launch = format!("legendary launch {}", game.app_name); let mut start_dir = game.install_path.clone(); diff --git a/src/legendary/legendary_platform.rs b/src/legendary/legendary_platform.rs new file mode 100644 index 0000000..e7d1fc9 --- /dev/null +++ b/src/legendary/legendary_platform.rs @@ -0,0 +1,35 @@ +use super::{LegendaryGame, LegendarySettings}; +use crate::platform::Platform; +use serde_json::from_str; +use std::error::Error; +use std::process::Command; + +pub struct LegendaryPlatform { + settings: LegendarySettings, +} + +impl LegendaryPlatform { + pub fn new(settings: LegendarySettings) -> LegendaryPlatform { + Self { settings } + } +} + +impl Platform> for LegendaryPlatform { + fn enabled(&self) -> bool { + self.settings.enabled + } + + fn name(&self) -> &str { + "Legendary" + } + + fn get_shortcuts(&self) -> Result, Box> { + let legendary_command = Command::new("legendary") + .arg("list-installed") + .arg("--json") + .output()?; + let json = String::from_utf8_lossy(&legendary_command.stdout); + let legendary_ouput = from_str(&json)?; + Ok(legendary_ouput) + } +} diff --git a/src/legendary/mod.rs b/src/legendary/mod.rs index c0eb0a3..765f64d 100644 --- a/src/legendary/mod.rs +++ b/src/legendary/mod.rs @@ -1,7 +1,8 @@ mod legendary_game; -mod get_legendary_games; mod settings; +mod legendary_platform; + -pub use get_legendary_games::*; pub use legendary_game::*; -pub use settings::*; \ No newline at end of file +pub use settings::*; +pub use legendary_platform::*; \ No newline at end of file diff --git a/src/legendary/settings.rs b/src/legendary/settings.rs index 96ea0fe..a9a29e5 100644 --- a/src/legendary/settings.rs +++ b/src/legendary/settings.rs @@ -1,6 +1,6 @@ use serde::Deserialize; -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] pub struct LegendarySettings { pub enabled: bool, pub executable: Option, diff --git a/src/main.rs b/src/main.rs index b2f4484..35f044d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,20 +1,23 @@ use std::{ + borrow::Borrow, collections::HashMap, env::{self}, fmt, fs::File, io::Write, + ops::Deref, path::Path, }; mod cached_search; mod egs; mod legendary; +mod platform; mod settings; mod steamgriddb; -mod platform; -use crate::{legendary::get_legendary_games, settings::Settings}; -use egs::{get_egs_manifests, ManifestItem}; +use crate::{ + egs::EpicPlatform, legendary::LegendaryPlatform, platform::Platform, settings::Settings, +}; use std::error::Error; use steam_shortcuts_util::{ parse_shortcuts, shortcut::ShortcutOwned, shortcuts_to_bytes, Shortcut, @@ -64,72 +67,34 @@ fn get_shortcuts_for_user(user: &SteamUsersInfo) -> ShortcutInfo { async fn main() -> Result<(), Box> { let settings = Settings::new()?; - let auth_key = settings.steamgrid_db.auth_key; if settings.steamgrid_db.enabled && auth_key.is_none() { - println!("auth_key not found, please add it to the steamgrid_db settings "); + println!("auth_key not found, please add it to the steamgrid_db settings "); return Ok(()); } let auth_key = auth_key.unwrap(); - + let client = steamgriddb_api::Client::new(auth_key); let mut search = CachedSearch::new(&client); - if settings.epic_games.enabled { - let egs_shortcuts = { - let egs_manifests = match get_egs_manifests(&settings.epic_games) { - Ok(manifests) => manifests, - Err(e) => { - println!("Error getting manifests for Epic Games Store: {}", e); - vec![] - } - }; - let egs_shortcuts: Vec = - egs_manifests.iter().map(|f| f.into()).collect(); - println!("Found {} installed EGS Games", egs_manifests.len()); - egs_shortcuts - }; - } - - #[cfg(target_os = "linux")] - let legendary_shortcuts = { - let legendary_games = match get_legendary_games() { - Ok(games) => games, - Err(e) => { - println!("Error getting legendary games: {}", e); - vec![] - } - }; - let legendary_shortcuts: Vec = - legendary_games.iter().map(|f| f.into()).collect(); - println!( - "Found {} installed Legendary Games", - legendary_shortcuts.len() - ); - legendary_shortcuts - }; - let userinfo_shortcuts = get_shortcuts_paths()?; println!("Found {} user(s)", userinfo_shortcuts.len()); for user in userinfo_shortcuts.iter() { let shortcut_info = get_shortcuts_for_user(user); - #[cfg(target_os = "windows")] - let new_user_shortcuts: Vec<&ShortcutOwned> = shortcut_info - .shortcuts - .iter() - .filter(|user_shortcut| !user_shortcut.tags.contains(&"EGS".to_owned())) - .chain(egs_shortcuts.iter()) - .collect(); - #[cfg(target_os = "linux")] - let new_user_shortcuts: Vec<&ShortcutOwned> = shortcut_info - .shortcuts - .iter() - .filter(|user_shortcut| !user_shortcut.tags.contains(&"Legendary".to_owned())) - .chain(legendary_shortcuts.iter()) - .collect(); + let mut new_user_shortcuts: Vec = shortcut_info.shortcuts; + + update_platform_shortcuts( + &EpicPlatform::new(settings.epic_games.clone()), + &mut new_user_shortcuts, + ); + + update_platform_shortcuts( + &LegendaryPlatform::new(settings.legendary.clone()), + &mut new_user_shortcuts, + ); let shortcuts = new_user_shortcuts.iter().map(|f| f.borrow()).collect(); @@ -330,3 +295,27 @@ fn get_shortcuts_paths() -> Result, Box> { .collect(); Ok(users_info) } + +fn update_platform_shortcuts(platform: &P, current_shortcuts: &mut Vec) +where + P: Platform, + E: std::fmt::Debug + std::fmt::Display, + T: Into, +{ + if platform.enabled() { + let shortcuts_to_add_result = platform.get_shortcuts(); + match shortcuts_to_add_result { + Ok(shortcuts_to_add) => { + current_shortcuts.retain(|f| !f.tags.contains(&platform.name().to_owned())); + for shortcut in shortcuts_to_add { + let shortcut_owned: ShortcutOwned = shortcut.into(); + current_shortcuts.push(shortcut_owned); + } + } + Err(err) => { + eprintln!("Error getting shortcuts from platform: {}", platform.name()); + eprintln!("{}", err); + } + } + } +} diff --git a/src/platform.rs b/src/platform.rs index 62bc471..d64909a 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -1,13 +1,12 @@ -use crate::{egs::EpicGamesLauncherSettings, legendary::LegendarySettings}; +use steam_shortcuts_util::shortcut::ShortcutOwned; -pub enum Platform{ - EpicGames{ - settings: EpicGamesLauncherSettings, - }, - Legendary{ - settings: LegendarySettings - } +pub trait Platform +where + T: Into, +{ + fn enabled(&self) -> bool; + + fn name(&self) -> &str; + + fn get_shortcuts(&self) -> Result, E>; } - - -