-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor to use steamlocate and improve error handling when loading settings. --------- Co-authored-by: Bash <>
- Loading branch information
Showing
6 changed files
with
167 additions
and
177 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,128 +1,45 @@ | ||
use std::{ | ||
fs, | ||
path::{Path, PathBuf}, | ||
}; | ||
use std::path::PathBuf; | ||
|
||
use anyhow::{anyhow, Result}; | ||
use steamid_ng::SteamID; | ||
use steamlocate::SteamDir; | ||
|
||
pub const TF2_GAME_ID: &str = "440"; | ||
pub const TF2_GAME_ID: u32 = 440; | ||
|
||
pub fn locate_steam_logged_in_users() -> Option<PathBuf> { | ||
pub fn locate_steam_logged_in_users() -> Result<PathBuf> { | ||
tracing::debug!("Fetching Steam loginusers.vdf"); | ||
let mut base_folder: PathBuf = find_base_lib(); | ||
let mut base_folder: PathBuf = SteamDir::locate() | ||
.ok_or(anyhow!("Failed to locate Steam directory"))? | ||
.path; | ||
base_folder.push::<PathBuf>("config/loginusers.vdf".into()); | ||
if base_folder.as_path().exists() { | ||
tracing::info!("Located current Steam user data."); | ||
Some(base_folder) | ||
Ok(base_folder) | ||
} else { | ||
tracing::error!("Could not locate loginusers.vdf in the Steam dir"); | ||
None | ||
Err(anyhow!("Could not locate loginusers.vdf in the Steam dir")) | ||
} | ||
} | ||
|
||
pub fn locate_steam_launch_configs(steam_user: SteamID) -> Option<PathBuf> { | ||
tracing::debug!("Fetching Steam userdata/<player>/config/localconfig.vdf"); | ||
pub fn locate_steam_launch_configs(steam_user: SteamID) -> Result<PathBuf> { | ||
let a_id = steam_user.account_id(); | ||
let mut base_folder: PathBuf = find_base_lib(); | ||
base_folder.push::<PathBuf>(format!("userdata/{}/config/localconfig.vdf", a_id,).into()); | ||
let local_config_path = format!("userdata/{}/config/localconfig.vdf", a_id); | ||
tracing::debug!("Fetching Steam {}", local_config_path); | ||
|
||
let steam = SteamDir::locate().ok_or(anyhow!("Failed to locate Steam directory."))?; | ||
let mut base_folder: PathBuf = steam.path; | ||
base_folder.push(local_config_path); | ||
if base_folder.as_path().exists() { | ||
tracing::info!("Located local launch configs."); | ||
Some(base_folder) | ||
Ok(base_folder) | ||
} else { | ||
tracing::error!("Could not find local configs (player not found)."); | ||
None | ||
Err(anyhow!("Could not find local configs (player not found).")) | ||
} | ||
} | ||
|
||
/// Attempts to open the TF2 directory or locate it if it's not in the expected place | ||
pub fn locate_tf2_folder() -> Option<PathBuf> { | ||
tracing::debug!("Fetching TF2 Folder"); | ||
let libs: Vec<PathBuf> = fetch_libraryfolders(); | ||
|
||
for lib in libs { | ||
let mut path = lib.to_path_buf(); | ||
path.push::<PathBuf>(get_rel_tf2_path().into()); | ||
tracing::debug!("Found TF2 Folder: {:?}", path); | ||
|
||
if path.exists() && verify_tf_location(&path) { | ||
tracing::info!("Using TF2 directory: {}", path.to_string_lossy()); | ||
return Some(path); | ||
} | ||
} | ||
None | ||
} | ||
|
||
fn fetch_libraryfolders() -> Vec<PathBuf> { | ||
tracing::debug!("Attempting to open libraryfolders.vdf"); | ||
const LIBFILE: &str = "libraryfolders.vdf"; | ||
let libraryfolders = fs::read_to_string(Path::join(&find_default_lib(), LIBFILE)); | ||
|
||
match libraryfolders { | ||
Ok(content) => { | ||
let mut paths = Vec::new(); | ||
let lines = content.lines(); | ||
|
||
for line in lines { | ||
if line.contains("path") { | ||
if let Some(path_str) = line.split('"').nth(3) { | ||
paths.push(PathBuf::from(path_str)); | ||
} | ||
} | ||
} | ||
|
||
tracing::debug!("Successfully read libraryfolders"); | ||
paths | ||
} | ||
Err(err) => { | ||
tracing::error!("Failed to read libraryfolders.vdf: {:?}", err); | ||
Vec::new() | ||
} | ||
} | ||
} | ||
|
||
fn find_default_lib() -> PathBuf { | ||
let mut path: PathBuf = find_base_lib(); | ||
path.push::<PathBuf>("steamapps/".into()); | ||
|
||
path | ||
} | ||
|
||
fn find_base_lib() -> PathBuf { | ||
#[cfg(target_os = "windows")] | ||
let default_dir = r"C:\Program Files (x86)\Steam\".into(); | ||
|
||
#[cfg(not(target_os = "windows"))] | ||
let default_dir = { | ||
use std::env::var_os; | ||
var_os("HOME") | ||
.map(PathBuf::from) | ||
.unwrap_or("~".into()) | ||
.join(".steam/steam/") | ||
}; | ||
|
||
default_dir | ||
} | ||
|
||
fn verify_tf_location(lib: &Path) -> bool { | ||
tracing::debug!("Start TF2 Verification of {:?}", lib.to_string_lossy()); | ||
let gameinfo = "tf/gameinfo.txt"; | ||
let mut path = lib.to_path_buf(); | ||
path.push(gameinfo); | ||
|
||
if path.exists() { | ||
tracing::debug!("Passed Verification Check"); | ||
return true; | ||
} | ||
tracing::debug!("Failed Verification Check"); | ||
false | ||
} | ||
|
||
#[cfg(target_os = "windows")] | ||
fn get_rel_tf2_path() -> String { | ||
r"steamapps\common\Team Fortress 2".to_string() | ||
} | ||
|
||
#[cfg(not(target_os = "windows"))] | ||
fn get_rel_tf2_path() -> String { | ||
r"steamapps/common/Team Fortress 2".to_string() | ||
pub fn locate_tf2_folder() -> Result<PathBuf> { | ||
Ok(SteamDir::locate() | ||
.ok_or(anyhow!("Failed to locate Steam directory"))? | ||
.app(&TF2_GAME_ID) | ||
.ok_or(anyhow!("Failed to locate TF2 installation."))? | ||
.path | ||
.clone()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.