Make platforms generic

This commit is contained in:
Philip Kristoffersen
2021-09-25 08:50:03 +02:00
parent 99079dcaff
commit 9493262076
13 changed files with 136 additions and 93 deletions
Generated
+2 -2
View File
@@ -1045,9 +1045,9 @@ dependencies = [
[[package]] [[package]]
name = "steam_shortcuts_util" name = "steam_shortcuts_util"
version = "1.1.1" version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffa391cd87de757cdc91153362307680c6ec4ad50fe7a582509678f1ede863eb" checksum = "51eecf84626e7ba46667be999ca124d9cd16c9feb09918dd991eccbb4ac298e7"
dependencies = [ dependencies = [
"ascii", "ascii",
"crc32fast", "crc32fast",
+1 -1
View File
@@ -6,7 +6,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
steam_shortcuts_util="1.1.1" steam_shortcuts_util="1.1.2"
steamgriddb_api="0.2,0" steamgriddb_api="0.2,0"
serde={version="*", features=["derive"]} serde={version="*", features=["derive"]}
serde_json="*" serde_json="*"
+30
View File
@@ -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<ManifestItem, EpicGamesManifestsError> for EpicPlatform {
fn enabled(&self) -> bool {
self.settings.enabled
}
fn name(&self) -> &str {
"EGS"
}
fn get_shortcuts(&self) -> Result<Vec<ManifestItem>, EpicGamesManifestsError> {
get_egs_manifests(&self.settings)
}
}
+2 -2
View File
@@ -19,8 +19,8 @@ pub struct ManifestItem {
pub app_name: String, pub app_name: String,
} }
impl From<&ManifestItem> for ShortcutOwned { impl From<ManifestItem> for ShortcutOwned {
fn from(manifest: &ManifestItem) -> Self { fn from(manifest: ManifestItem) -> Self {
let exe = format!( let exe = format!(
"\"{}\\{}\"", "\"{}\\{}\"",
manifest.install_location, manifest.launch_executable manifest.install_location, manifest.launch_executable
+4 -1
View File
@@ -1,6 +1,9 @@
mod get_manifests; mod get_manifests;
mod manifest_item; mod manifest_item;
mod settings; mod settings;
mod epic_platform;
pub use manifest_item::*; pub use manifest_item::*;
pub use get_manifests::get_egs_manifests; use get_manifests::get_egs_manifests;
pub use settings::EpicGamesLauncherSettings; pub use settings::EpicGamesLauncherSettings;
pub use epic_platform::*;
+1 -1
View File
@@ -1,6 +1,6 @@
use serde::Deserialize; use serde::Deserialize;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize,Clone)]
pub struct EpicGamesLauncherSettings { pub struct EpicGamesLauncherSettings {
pub enabled: bool, pub enabled: bool,
pub location: Option<String>, pub location: Option<String>,
-14
View File
@@ -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<Vec<LegendaryGame>, Box<dyn Error>> {
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)
}
+2 -2
View File
@@ -11,8 +11,8 @@ pub struct LegendaryGame {
pub executable: String, pub executable: String,
} }
impl From<&LegendaryGame> for ShortcutOwned { impl From<LegendaryGame> for ShortcutOwned {
fn from(game: &LegendaryGame) -> Self { fn from(game: LegendaryGame) -> Self {
let exe = format!("\"{}\\{}\"", game.install_path, game.executable); let exe = format!("\"{}\\{}\"", game.install_path, game.executable);
let launch = format!("legendary launch {}", game.app_name); let launch = format!("legendary launch {}", game.app_name);
let mut start_dir = game.install_path.clone(); let mut start_dir = game.install_path.clone();
+35
View File
@@ -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<LegendaryGame, Box<dyn Error>> for LegendaryPlatform {
fn enabled(&self) -> bool {
self.settings.enabled
}
fn name(&self) -> &str {
"Legendary"
}
fn get_shortcuts(&self) -> Result<Vec<LegendaryGame>, Box<dyn Error>> {
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)
}
}
+3 -2
View File
@@ -1,7 +1,8 @@
mod legendary_game; mod legendary_game;
mod get_legendary_games;
mod settings; mod settings;
mod legendary_platform;
pub use get_legendary_games::*;
pub use legendary_game::*; pub use legendary_game::*;
pub use settings::*; pub use settings::*;
pub use legendary_platform::*;
+1 -1
View File
@@ -1,6 +1,6 @@
use serde::Deserialize; use serde::Deserialize;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize, Clone)]
pub struct LegendarySettings { pub struct LegendarySettings {
pub enabled: bool, pub enabled: bool,
pub executable: Option<String>, pub executable: Option<String>,
+41 -52
View File
@@ -1,20 +1,23 @@
use std::{ use std::{
borrow::Borrow,
collections::HashMap, collections::HashMap,
env::{self}, env::{self},
fmt, fmt,
fs::File, fs::File,
io::Write, io::Write,
ops::Deref,
path::Path, path::Path,
}; };
mod cached_search; mod cached_search;
mod egs; mod egs;
mod legendary; mod legendary;
mod platform;
mod settings; mod settings;
mod steamgriddb; mod steamgriddb;
mod platform;
use crate::{legendary::get_legendary_games, settings::Settings}; use crate::{
use egs::{get_egs_manifests, ManifestItem}; egs::EpicPlatform, legendary::LegendaryPlatform, platform::Platform, settings::Settings,
};
use std::error::Error; use std::error::Error;
use steam_shortcuts_util::{ use steam_shortcuts_util::{
parse_shortcuts, shortcut::ShortcutOwned, shortcuts_to_bytes, Shortcut, parse_shortcuts, shortcut::ShortcutOwned, shortcuts_to_bytes, Shortcut,
@@ -64,7 +67,6 @@ fn get_shortcuts_for_user(user: &SteamUsersInfo) -> ShortcutInfo {
async fn main() -> Result<(), Box<dyn Error>> { async fn main() -> Result<(), Box<dyn Error>> {
let settings = Settings::new()?; let settings = Settings::new()?;
let auth_key = settings.steamgrid_db.auth_key; let auth_key = settings.steamgrid_db.auth_key;
if settings.steamgrid_db.enabled && auth_key.is_none() { 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 ");
@@ -76,60 +78,23 @@ async fn main() -> Result<(), Box<dyn Error>> {
let client = steamgriddb_api::Client::new(auth_key); let client = steamgriddb_api::Client::new(auth_key);
let mut search = CachedSearch::new(&client); 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<ShortcutOwned> =
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<ShortcutOwned> =
legendary_games.iter().map(|f| f.into()).collect();
println!(
"Found {} installed Legendary Games",
legendary_shortcuts.len()
);
legendary_shortcuts
};
let userinfo_shortcuts = get_shortcuts_paths()?; let userinfo_shortcuts = get_shortcuts_paths()?;
println!("Found {} user(s)", userinfo_shortcuts.len()); println!("Found {} user(s)", userinfo_shortcuts.len());
for user in userinfo_shortcuts.iter() { for user in userinfo_shortcuts.iter() {
let shortcut_info = get_shortcuts_for_user(user); let shortcut_info = get_shortcuts_for_user(user);
#[cfg(target_os = "windows")] let mut new_user_shortcuts: Vec<ShortcutOwned> = shortcut_info.shortcuts;
let new_user_shortcuts: Vec<&ShortcutOwned> = shortcut_info
.shortcuts update_platform_shortcuts(
.iter() &EpicPlatform::new(settings.epic_games.clone()),
.filter(|user_shortcut| !user_shortcut.tags.contains(&"EGS".to_owned())) &mut new_user_shortcuts,
.chain(egs_shortcuts.iter()) );
.collect();
#[cfg(target_os = "linux")] update_platform_shortcuts(
let new_user_shortcuts: Vec<&ShortcutOwned> = shortcut_info &LegendaryPlatform::new(settings.legendary.clone()),
.shortcuts &mut new_user_shortcuts,
.iter() );
.filter(|user_shortcut| !user_shortcut.tags.contains(&"Legendary".to_owned()))
.chain(legendary_shortcuts.iter())
.collect();
let shortcuts = new_user_shortcuts.iter().map(|f| f.borrow()).collect(); let shortcuts = new_user_shortcuts.iter().map(|f| f.borrow()).collect();
@@ -330,3 +295,27 @@ fn get_shortcuts_paths() -> Result<Vec<SteamUsersInfo>, Box<dyn Error>> {
.collect(); .collect();
Ok(users_info) Ok(users_info)
} }
fn update_platform_shortcuts<P, T, E>(platform: &P, current_shortcuts: &mut Vec<ShortcutOwned>)
where
P: Platform<T, E>,
E: std::fmt::Debug + std::fmt::Display,
T: Into<ShortcutOwned>,
{
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);
}
}
}
}
+10 -11
View File
@@ -1,13 +1,12 @@
use crate::{egs::EpicGamesLauncherSettings, legendary::LegendarySettings}; use steam_shortcuts_util::shortcut::ShortcutOwned;
pub enum Platform{ pub trait Platform<T, E>
EpicGames{ where
settings: EpicGamesLauncherSettings, T: Into<ShortcutOwned>,
}, {
Legendary{ fn enabled(&self) -> bool;
settings: LegendarySettings
fn name(&self) -> &str;
fn get_shortcuts(&self) -> Result<Vec<T>, E>;
} }
}