Skip to content

Commit

Permalink
feat: added check for Steam being installed
Browse files Browse the repository at this point in the history
  • Loading branch information
Tormak9970 committed May 19, 2023
1 parent afa251b commit d2864dd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 35 deletions.
51 changes: 31 additions & 20 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use reqwest;
use steam::get_steam_root_dir;
use tauri::{
AppHandle,
api::dialog::blocking::FileDialogBuilder,
api::dialog::{blocking::FileDialogBuilder, self},
FsScope, Manager
};
use keyvalues_parser::Vdf;
Expand Down Expand Up @@ -405,26 +405,37 @@ async fn download_grid(app_handle: AppHandle, grid_url: String, dest_path: Strin

/// Adds the user's steam directory to Tauri FS and Asset scope.
fn add_steam_to_scope(app_handle: &AppHandle) {
let steam_path = get_steam_root_dir();

let fs_scope = app_handle.fs_scope();
let asset_scope = app_handle.asset_protocol_scope();

let fs_res = FsScope::allow_directory(&fs_scope, &steam_path, true);
let asset_res = FsScope::allow_directory(&asset_scope, &steam_path, true);

if fs_res.is_ok() && asset_res.is_ok() {
logger::log_to_core_file(app_handle.to_owned(), "Added Steam directory to scope.", 0);
} else if fs_res.is_err() {
let err = fs_res.err().unwrap();
logger::log_to_core_file(app_handle.to_owned(), format!("Error adding Steam directory to scope. FS Scope Error: {}", err.to_string()).as_str(), 0);
} else if asset_res.is_err() {
let err = asset_res.err().unwrap();
logger::log_to_core_file(app_handle.to_owned(), format!("Error adding Steam directory to scope. Asset Scope Error: {}", err.to_string()).as_str(), 0);
let steam_path_res = get_steam_root_dir();

if steam_path_res.is_ok() {
let steam_path: PathBuf = steam_path_res.ok().expect("Should have been able to get Steam install path result.");

let fs_scope = app_handle.fs_scope();
let asset_scope = app_handle.asset_protocol_scope();

let fs_res = FsScope::allow_directory(&fs_scope, &steam_path, true);
let asset_res = FsScope::allow_directory(&asset_scope, &steam_path, true);

if fs_res.is_ok() && asset_res.is_ok() {
logger::log_to_core_file(app_handle.to_owned(), "Added Steam directory to scope.", 0);
} else if fs_res.is_err() {
let err = fs_res.err().unwrap();
logger::log_to_core_file(app_handle.to_owned(), format!("Error adding Steam directory to scope. FS Scope Error: {}", err.to_string()).as_str(), 0);
} else if asset_res.is_err() {
let err = asset_res.err().unwrap();
logger::log_to_core_file(app_handle.to_owned(), format!("Error adding Steam directory to scope. Asset Scope Error: {}", err.to_string()).as_str(), 0);
} else {
let fs_err = fs_res.err().unwrap();
let asset_err = asset_res.err().unwrap();
logger::log_to_core_file(app_handle.to_owned(), format!("Error adding Steam directory to scope. FS Scope Error: {}. Asset Scope Error: {}", fs_err.to_string(), asset_err.to_string()).as_str(), 0);
}
} else {
let fs_err = fs_res.err().unwrap();
let asset_err = asset_res.err().unwrap();
logger::log_to_core_file(app_handle.to_owned(), format!("Error adding Steam directory to scope. FS Scope Error: {}. Asset Scope Error: {}", fs_err.to_string(), asset_err.to_string()).as_str(), 0);
let err_message = steam_path_res.err().expect("Should have been able to get Steam install path error.");
logger::log_to_core_file(app_handle.to_owned(), &err_message, 2);

let main_window = app_handle.get_window("main").expect("Main window should always exist.");
dialog::message(Some(&main_window), "SARM Initialization Error", "Steam was not found on your PC. Steam needs to be installed for SARM to work.");
app_handle.exit(0);
}
}

Expand Down
47 changes: 32 additions & 15 deletions src-tauri/src/steam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,41 @@ use home::home_dir;

#[cfg(target_os = "windows")]
/// Gets the steam root dir for windows systems.
pub fn get_steam_root_dir() -> PathBuf {
pub fn get_steam_root_dir() -> Result<PathBuf, String> {
let hkcu: RegKey = RegKey::predef(HKEY_CURRENT_USER);

let steam_install_data: RegKey = hkcu.open_subkey("SOFTWARE\\Valve\\Steam").expect("Couldn't get Steam Install Data from the registry");
let steam_install_path: String = steam_install_data.get_value("SteamPath").expect("Couldn't get SteamPath from the registry");
let steam_install_data_res = hkcu.open_subkey("SOFTWARE\\Valve\\Steam");

return Path::new(&(steam_install_path.replace("\\", "/"))).to_path_buf();
if steam_install_data_res.is_ok() {
let steam_install_data: RegKey = steam_install_data_res.ok().expect("Should have been able to get steam install registry result.");
let steam_install_path_res = steam_install_data.get_value("SteamPath");

if steam_install_path_res.is_ok() {
let steam_install_path: String = steam_install_path_res.ok().expect("Should have been able to get steam install from registry.");
return Ok(Path::new(&(steam_install_path.replace("\\", "/"))).to_path_buf());
} else {
return Err(String::from("Couldn't get SteamPath from the registry."));
}
} else {
return Err(String::from("Couldn't get Steam Install Data from the registry."));
}
}

#[cfg(target_os = "linux")]
/// Gets the steam root dir for linux systems.
pub fn get_steam_root_dir() -> PathBuf {
let pc_home_dir = home_dir().expect("Couldn't get user's home dir.");
pub fn get_steam_root_dir() -> Result<PathBuf, String> {
let mut pc_home_dir: PathBuf = home_dir().expect("Couldn't get user's home dir.");

if pc_home_dir.join(".var/app/com.valvesoftware.Steam/data/steam").exists() {
return pc_home_dir.join(".var/app/com.valvesoftware.Steam/data/steam");
pc_home_dir = pc_home_dir.join(".var/app/com.valvesoftware.Steam/data/steam");
} else {
pc_home_dir = pc_home_dir.join(".steam/steam");
}

if pc_home_dir.exists() {
return Ok(pc_home_dir);
} else {
return pc_home_dir.join(".steam/steam");
return Err(String::from("Steam install path does not exist."));
}
}

Expand All @@ -42,8 +59,8 @@ pub fn get_steam_root_dir() -> PathBuf {
pub fn get_grids_directory(app_handle: AppHandle, steam_active_user_id: String) -> String {
logger::log_to_core_file(app_handle.to_owned(), "Getting steam grids folder...", 0);

let steam_root = get_steam_root_dir();
let grids_dir = steam_root.join("userdata").join(steam_active_user_id.to_string()).join("config/grid").to_str().expect("Should have been able to convert to a string.").to_owned().replace("\\", "/");
let steam_root: PathBuf = get_steam_root_dir().ok().expect("Steam install path should have been fine if this point is reached.");
let grids_dir: String = steam_root.join("userdata").join(steam_active_user_id.to_string()).join("config/grid").to_str().expect("Should have been able to convert to a string.").to_owned().replace("\\", "/");

let dir_create_res = fs::create_dir_all(grids_dir.clone());
if dir_create_res.is_err() {
Expand All @@ -59,7 +76,7 @@ pub fn get_grids_directory(app_handle: AppHandle, steam_active_user_id: String)
pub fn get_library_cache_directory(app_handle: AppHandle) -> String {
logger::log_to_core_file(app_handle.to_owned(), "Getting steam library cache folder...", 0);

let steam_root = get_steam_root_dir();
let steam_root: PathBuf = get_steam_root_dir().ok().expect("Steam install path should have been fine if this point is reached.");
return steam_root.join("appcache/librarycache").to_str().expect("Should have been able to convert to a string.").to_owned().replace("\\", "/");
}

Expand All @@ -68,7 +85,7 @@ pub fn get_library_cache_directory(app_handle: AppHandle) -> String {
pub fn get_appinfo_path(app_handle: AppHandle) -> String {
logger::log_to_core_file(app_handle.to_owned(), "Getting steam appinfo.vdf...", 0);

let steam_root = get_steam_root_dir();
let steam_root: PathBuf = get_steam_root_dir().ok().expect("Steam install path should have been fine if this point is reached.");
return steam_root.join("appcache/appinfo.vdf").to_str().expect("Should have been able to convert to a string.").to_owned().replace("\\", "/");
}

Expand All @@ -77,7 +94,7 @@ pub fn get_appinfo_path(app_handle: AppHandle) -> String {
pub fn get_shortcuts_path(app_handle: AppHandle, steam_active_user_id: String) -> String {
logger::log_to_core_file(app_handle.to_owned(), "Getting steam shortcuts.vdf...", 0);

let steam_root = get_steam_root_dir();
let steam_root: PathBuf = get_steam_root_dir().ok().expect("Steam install path should have been fine if this point is reached.");
return steam_root.join("userdata").join(steam_active_user_id.to_string()).join("config/shortcuts.vdf").to_str().expect("Should have been able to convert to a string.").to_owned().replace("\\", "/");
}

Expand All @@ -86,7 +103,7 @@ pub fn get_shortcuts_path(app_handle: AppHandle, steam_active_user_id: String) -
pub fn get_localconfig_path(app_handle: AppHandle, steam_active_user_id: String) -> String {
logger::log_to_core_file(app_handle.to_owned(), "Getting steam localconfig.vdf...", 0);

let steam_root = get_steam_root_dir();
let steam_root: PathBuf = get_steam_root_dir().ok().expect("Steam install path should have been fine if this point is reached.");
return steam_root.join("userdata").join(steam_active_user_id.to_string()).join("config/localconfig.vdf").to_str().expect("Should have been able to convert to a string.").to_owned().replace("\\", "/");
}

Expand Down Expand Up @@ -128,7 +145,7 @@ fn read_steam_user(user_id: &str, user_block: &str) -> Map<String, Value> {
fn read_steam_users() -> Map<String, Value> {
let mut steam_users: Map<String, Value> = Map::new();

let steam_root: PathBuf = get_steam_root_dir();
let steam_root: PathBuf = get_steam_root_dir().ok().expect("Steam install path should have been fine if this point is reached.");
let loginusers_vdf: PathBuf = steam_root.join("config/loginusers.vdf");
let contents: String = fs::read_to_string(loginusers_vdf).unwrap();

Expand Down

0 comments on commit d2864dd

Please sign in to comment.