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
[steam]
[steamgrid_db]
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 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());
for user in userinfo_shortcuts.iter() {
+2 -3
View File
@@ -1,6 +1,4 @@
use crate::{
egs::EpicGamesLauncherSettings, legendary::LegendarySettings, steamgriddb::SteamGridDbSettings,
};
use crate::{egs::EpicGamesLauncherSettings, legendary::LegendarySettings, steam::SteamSettings, steamgriddb::SteamGridDbSettings};
use config::{Config, ConfigError, Environment, File};
use serde::Deserialize;
@@ -12,6 +10,7 @@ pub struct Settings {
pub epic_games: EpicGamesLauncherSettings,
pub legendary: LegendarySettings,
pub steamgrid_db: SteamGridDbSettings,
pub steam: SteamSettings
}
//https://github.com/JosefNemec/Playnite/tree/master/source/Plugins/OriginLibrary
+2
View File
@@ -1,3 +1,5 @@
mod utils;
mod settings;
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>,
}
+43 -26
View File
@@ -1,17 +1,18 @@
use std::error::Error;
use std::{
fmt,
path::Path,
};
#[cfg(target_os = "windows")]
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 super::SteamSettings;
pub fn get_shortcuts_for_user(user: &SteamUsersInfo) -> ShortcutInfo {
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 {
Some(shortcut_path) => {
let content = std::fs::read(shortcut_path).unwrap();
shortcuts = parse_shortcuts(content.as_slice())
.unwrap()
@@ -23,25 +24,27 @@ pub fn get_shortcuts_for_user(user: &SteamUsersInfo) -> ShortcutInfo {
shortcuts.len(),
user.steam_user_data_folder
);
} else {
Path::new(&shortcut_path).to_path_buf()
}
None => {
println!(
"Did not find a shortcut file for user {}, createing a new",
"Did not find a shortcut file for user {}, creating a new",
user.steam_user_data_folder
);
std::fs::create_dir_all(format!("{}/{}", user.steam_user_data_folder, "config")).unwrap();
new_path = Some(format!(
"{}/{}",
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 {
shortcuts,
path: new_path.unwrap(),
path: new_path,
}
}
pub struct ShortcutInfo {
pub path: String,
pub path: PathBuf,
pub shortcuts: Vec<ShortcutOwned>,
}
@@ -51,28 +54,42 @@ pub struct SteamUsersInfo {
}
/// 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(
settings: &SteamSettings,
) -> Result<Vec<SteamUsersInfo>, Box<dyn Error>> {
let user_location = settings.location.clone();
let steam_path_str = match user_location {
Some(location) => location,
None => {
#[cfg(target_os = "windows")]
let path_string = {
let key = "PROGRAMFILES(X86)";
let program_files = env::var(key)?;
format!(
"{program_files}//Steam//userdata//",
program_files = program_files
)
format!("{program_files}//Steam//", program_files = program_files)
};
#[cfg(target_os = "linux")]
let path_string = {
let home = std::env::var("HOME")?;
format!("{}/.steam/steam/userdata/", home)
format!("{}/.steam/steam/", home)
};
let user_data_path = Path::new(path_string.as_str());
if !user_data_path.exists() {
path_string
}
};
let steam_path = Path::new(&steam_path_str);
if !steam_path.exists() {
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 users_info = user_folders
.filter_map(|f| f.ok())