Add setting optional location for steam

This commit is contained in:
Philip Kristoffersen
2021-09-25 12:56:13 +02:00
parent f68db38a7f
commit 8d0996c67b
6 changed files with 80 additions and 54 deletions
+2
View File
@@ -1,4 +1,6 @@
debug= false debug= false
[steam]
[steamgrid_db] [steamgrid_db]
enabled = true enabled = true
+1 -1
View File
@@ -32,7 +32,7 @@ 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);
let userinfo_shortcuts = get_shortcuts_paths()?; let userinfo_shortcuts = get_shortcuts_paths(&settings.steam)?;
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() {
+2 -3
View File
@@ -1,6 +1,4 @@
use crate::{ use crate::{egs::EpicGamesLauncherSettings, legendary::LegendarySettings, steam::SteamSettings, steamgriddb::SteamGridDbSettings};
egs::EpicGamesLauncherSettings, legendary::LegendarySettings, steamgriddb::SteamGridDbSettings,
};
use config::{Config, ConfigError, Environment, File}; use config::{Config, ConfigError, Environment, File};
use serde::Deserialize; use serde::Deserialize;
@@ -12,6 +10,7 @@ pub struct Settings {
pub epic_games: EpicGamesLauncherSettings, pub epic_games: EpicGamesLauncherSettings,
pub legendary: LegendarySettings, pub legendary: LegendarySettings,
pub steamgrid_db: SteamGridDbSettings, pub steamgrid_db: SteamGridDbSettings,
pub steam: SteamSettings
} }
//https://github.com/JosefNemec/Playnite/tree/master/source/Plugins/OriginLibrary //https://github.com/JosefNemec/Playnite/tree/master/source/Plugins/OriginLibrary
+3 -1
View File
@@ -1,3 +1,5 @@
mod utils; mod utils;
mod settings;
pub use utils::*; pub use utils::*;
pub use settings::SteamSettings;
+6
View File
@@ -0,0 +1,6 @@
use serde::Deserialize;
#[derive(Debug, Deserialize, Clone)]
pub struct SteamSettings{
pub location: Option<String>,
}
+66 -49
View File
@@ -1,47 +1,50 @@
use std::error::Error;
use std::{
fmt,
path::Path,
};
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
use std::env::{self}; use std::env::{self};
use std::error::Error;
use std::path::PathBuf;
use std::{fmt, path::Path};
use steam_shortcuts_util::{parse_shortcuts, shortcut::ShortcutOwned}; use steam_shortcuts_util::{parse_shortcuts, shortcut::ShortcutOwned};
use super::SteamSettings;
pub fn get_shortcuts_for_user(user: &SteamUsersInfo) -> ShortcutInfo { pub fn get_shortcuts_for_user(user: &SteamUsersInfo) -> ShortcutInfo {
let mut shortcuts = vec![]; let mut shortcuts = vec![];
let mut new_path = user.shortcut_path.clone();
if let Some(shortcut_path) = &user.shortcut_path { let new_path = match &user.shortcut_path {
let content = std::fs::read(shortcut_path).unwrap(); Some(shortcut_path) => {
shortcuts = parse_shortcuts(content.as_slice()) let content = std::fs::read(shortcut_path).unwrap();
.unwrap() shortcuts = parse_shortcuts(content.as_slice())
.iter() .unwrap()
.map(|s| s.to_owned()) .iter()
.collect(); .map(|s| s.to_owned())
println!( .collect();
"Found {} shortcuts , for user: {}", println!(
shortcuts.len(), "Found {} shortcuts , for user: {}",
user.steam_user_data_folder shortcuts.len(),
); user.steam_user_data_folder
} else { );
println!( Path::new(&shortcut_path).to_path_buf()
"Did not find a shortcut file for user {}, createing a new", }
user.steam_user_data_folder None => {
); println!(
std::fs::create_dir_all(format!("{}/{}", user.steam_user_data_folder, "config")).unwrap(); "Did not find a shortcut file for user {}, creating a new",
new_path = Some(format!( user.steam_user_data_folder
"{}/{}", );
user.steam_user_data_folder, "config/shortcuts.vdf" let path = Path::new(&user.steam_user_data_folder).join("config");
)); std::fs::create_dir_all(path.clone()).unwrap();
} path.join("shortcuts.vdf")
}
};
ShortcutInfo { ShortcutInfo {
shortcuts, shortcuts,
path: new_path.unwrap(), path: new_path,
} }
} }
pub struct ShortcutInfo { pub struct ShortcutInfo {
pub path: String, pub path: PathBuf,
pub shortcuts: Vec<ShortcutOwned>, pub shortcuts: Vec<ShortcutOwned>,
} }
@@ -51,28 +54,42 @@ pub struct SteamUsersInfo {
} }
/// Get the paths to the steam users shortcuts (one for each user) /// Get the paths to the steam users shortcuts (one for each user)
pub fn get_shortcuts_paths() -> Result<Vec<SteamUsersInfo>, Box<dyn Error>> { pub fn get_shortcuts_paths(
#[cfg(target_os = "windows")] settings: &SteamSettings,
let path_string = { ) -> Result<Vec<SteamUsersInfo>, Box<dyn Error>> {
let key = "PROGRAMFILES(X86)"; let user_location = settings.location.clone();
let program_files = env::var(key)?; let steam_path_str = match user_location {
format!( Some(location) => location,
"{program_files}//Steam//userdata//", None => {
program_files = program_files #[cfg(target_os = "windows")]
) let path_string = {
let key = "PROGRAMFILES(X86)";
let program_files = env::var(key)?;
format!("{program_files}//Steam//", program_files = program_files)
};
#[cfg(target_os = "linux")]
let path_string = {
let home = std::env::var("HOME")?;
format!("{}/.steam/steam/", home)
};
path_string
}
}; };
#[cfg(target_os = "linux")] let steam_path = Path::new(&steam_path_str);
let path_string = { if !steam_path.exists() {
let home = std::env::var("HOME")?;
format!("{}/.steam/steam/userdata/", home)
};
let user_data_path = Path::new(path_string.as_str());
if !user_data_path.exists() {
return Result::Err(Box::new(SteamFolderNotFound { return Result::Err(Box::new(SteamFolderNotFound {
location_tried: path_string, location_tried: format!("{:?}", steam_path),
})); }));
} }
let user_data_path = steam_path.join("userdata");
if !user_data_path.exists() {
return Result::Err(Box::new(SteamFolderNotFound {
location_tried: format!("{:?}", user_data_path),
}));
}
if !user_data_path.exists() {}
let user_folders = std::fs::read_dir(&user_data_path)?; let user_folders = std::fs::read_dir(&user_data_path)?;
let users_info = user_folders let users_info = user_folders
.filter_map(|f| f.ok()) .filter_map(|f| f.ok())