Skip to content

Commit

Permalink
feat: added custom steam path support
Browse files Browse the repository at this point in the history
  • Loading branch information
Tormak9970 committed Aug 19, 2023
1 parent 8be49ee commit 3bec562
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 51 deletions.
50 changes: 27 additions & 23 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ fn adjust_path(app_handle: &AppHandle, appid: &str, path: &str, grid_type: &str)
}

/// Filters the grid paths based on which have change.
fn filter_paths(app_handle: &AppHandle, steam_active_user_id: String, current_paths: &GridImageCache, original_paths: &GridImageCache, shortcut_icons: &Map<String, Value>) -> Vec<ChangedPath> {
let grids_dir = PathBuf::from(steam::get_grids_directory(app_handle.to_owned(), steam_active_user_id));
let lib_cache_dir = PathBuf::from(steam::get_library_cache_directory(app_handle.to_owned()));
fn filter_paths(app_handle: &AppHandle, steam_path: String, steam_active_user_id: String, current_paths: &GridImageCache, original_paths: &GridImageCache, shortcut_icons: &Map<String, Value>) -> Vec<ChangedPath> {
let grids_dir = PathBuf::from(steam::get_grids_directory(app_handle.to_owned(), steam_path.to_owned(), steam_active_user_id));
let lib_cache_dir = PathBuf::from(steam::get_library_cache_directory(app_handle.to_owned(), steam_path.to_owned()));
let mut res:Vec<ChangedPath> = Vec::new();

for (appid, grids_map) in current_paths.into_iter() {
Expand Down Expand Up @@ -156,7 +156,7 @@ fn check_for_shortcut_changes(shortcut_icons: &Map<String, Value>, original_shor

#[tauri::command]
/// Exports the users grids to a Grids zip file.
async fn export_grids_to_zip(app_handle: AppHandle, steam_active_user_id: String, platform_id_map: Map<String, Value>, id_name_map: Map<String, Value>) -> bool {
async fn export_grids_to_zip(app_handle: AppHandle, steam_path: String, steam_active_user_id: String, platform_id_map: Map<String, Value>, id_name_map: Map<String, Value>) -> bool {
let file_dialog = FileDialogBuilder::new()
.set_title("Save Grids Zip")
.set_file_name("Steam_Grids_Export.zip")
Expand All @@ -169,7 +169,7 @@ async fn export_grids_to_zip(app_handle: AppHandle, steam_active_user_id: String
let zip_path = file_path.unwrap();
logger::log_to_core_file(app_handle.to_owned(), format!("Got save path: {}", zip_path.to_str().expect("Should have been able to convert path to string.")).as_str(), 0);

let grids_dir_path = steam::get_grids_directory(app_handle.to_owned(), steam_active_user_id);
let grids_dir_path = steam::get_grids_directory(app_handle.to_owned(), steam_path, steam_active_user_id);
let succeeded = zip_controller::generate_grids_zip(&app_handle, PathBuf::from(grids_dir_path), zip_path, &platform_id_map, &id_name_map);

if succeeded {
Expand All @@ -187,7 +187,7 @@ async fn export_grids_to_zip(app_handle: AppHandle, steam_active_user_id: String

#[tauri::command]
/// Sets the users grids from a Grids zip file.
async fn import_grids_from_zip(app_handle: AppHandle, steam_active_user_id: String, name_id_map: Map<String, Value>) -> (bool, Map<String, Value>) {
async fn import_grids_from_zip(app_handle: AppHandle, steam_path: String, steam_active_user_id: String, name_id_map: Map<String, Value>) -> (bool, Map<String, Value>) {
let file_dialog = FileDialogBuilder::new()
.set_title("Pick a Grids Zip")
.add_filter("zip", &["zip"])
Expand All @@ -199,7 +199,7 @@ async fn import_grids_from_zip(app_handle: AppHandle, steam_active_user_id: Stri
let zip_path = file_path.unwrap();
logger::log_to_core_file(app_handle.to_owned(), format!("Got file path: {}", zip_path.to_str().expect("Should have been able to convert path to string.")).as_str(), 0);

let grids_dir_path = steam::get_grids_directory(app_handle.to_owned(), steam_active_user_id);
let grids_dir_path = steam::get_grids_directory(app_handle.to_owned(), steam_path, steam_active_user_id);
let (success, icon_map) = zip_controller::set_grids_from_zip(&app_handle, PathBuf::from(grids_dir_path), zip_path, &name_id_map);

if success {
Expand All @@ -217,16 +217,16 @@ async fn import_grids_from_zip(app_handle: AppHandle, steam_active_user_id: Stri

#[tauri::command]
/// Reads the user's appinfo.vdf file.
async fn read_appinfo_vdf(app_handle: AppHandle) -> String {
let appinfo_path: PathBuf = PathBuf::from(steam::get_appinfo_path(app_handle.to_owned()));
async fn read_appinfo_vdf(app_handle: AppHandle, steam_path: String) -> String {
let appinfo_path: PathBuf = PathBuf::from(steam::get_appinfo_path(app_handle.to_owned(), steam_path));
let appinfo_vdf: Map<String, Value> = open_appinfo_vdf(&appinfo_path);
return serde_json::to_string(&appinfo_vdf).expect("Should have been able to serialize AppInfo vdf to string.");
}

#[tauri::command]
/// Reads the user's shortcuts.vdf file.
async fn read_shortcuts_vdf(app_handle: AppHandle, steam_active_user_id: String) -> String {
let shortcuts_path = PathBuf::from(steam::get_shortcuts_path(app_handle.to_owned(), steam_active_user_id));
async fn read_shortcuts_vdf(app_handle: AppHandle, steam_path: String, steam_active_user_id: String) -> String {
let shortcuts_path = PathBuf::from(steam::get_shortcuts_path(app_handle.to_owned(), steam_path, steam_active_user_id));

if shortcuts_path.as_path().exists() {
logger::log_to_core_file(app_handle.to_owned(), "shortcuts.vdf exists, reading...", 0);
Expand All @@ -240,8 +240,8 @@ async fn read_shortcuts_vdf(app_handle: AppHandle, steam_active_user_id: String)

#[tauri::command]
/// Reads the user's localconfig.vdf file.
async fn read_localconfig_vdf(app_handle: AppHandle, steam_active_user_id: String) -> String {
let localconfig_path = PathBuf::from(steam::get_localconfig_path(app_handle.to_owned(), steam_active_user_id));
async fn read_localconfig_vdf(app_handle: AppHandle, steam_path: String, steam_active_user_id: String) -> String {
let localconfig_path = PathBuf::from(steam::get_localconfig_path(app_handle.to_owned(), steam_path, steam_active_user_id));

if localconfig_path.as_path().exists() {
logger::log_to_core_file(app_handle.to_owned(), "localconfig.vdf exists, reading...", 0);
Expand All @@ -268,12 +268,12 @@ async fn read_localconfig_vdf(app_handle: AppHandle, steam_active_user_id: Strin

#[tauri::command]
/// Applies the changes the user has made.
async fn save_changes(app_handle: AppHandle, steam_active_user_id: String, current_art: String, original_art: String, shortcuts_str: String, shortcut_icons: Map<String, Value>, original_shortcut_icons: Map<String, Value>, changed_logo_positions: Map<String, Value>) -> String {
async fn save_changes(app_handle: AppHandle, steam_path: String, steam_active_user_id: String, current_art: String, original_art: String, shortcuts_str: String, shortcut_icons: Map<String, Value>, original_shortcut_icons: Map<String, Value>, changed_logo_positions: Map<String, Value>) -> String {
let current_art_dict: GridImageCache = serde_json::from_str(current_art.as_str()).unwrap();
let original_art_dict: GridImageCache = serde_json::from_str(original_art.as_str()).unwrap();

logger::log_to_core_file(app_handle.to_owned(), "Converting current path entries to grid paths...", 0);
let paths_to_set: Vec<ChangedPath> = filter_paths(&app_handle, steam_active_user_id.clone(), &current_art_dict, &original_art_dict, &shortcut_icons);
let paths_to_set: Vec<ChangedPath> = filter_paths(&app_handle, steam_path.to_owned(), steam_active_user_id.clone(), &current_art_dict, &original_art_dict, &shortcut_icons);
let paths_id_map: HashMap<String, ChangedPath> = paths_to_set.clone().iter().map(| entry | (format!("{}_{}", entry.appId.to_owned(), entry.gridType.to_owned()).to_string(), entry.to_owned())).collect();
logger::log_to_core_file(app_handle.to_owned(), "Current path entries converted to grid paths.", 0);

Expand Down Expand Up @@ -313,7 +313,7 @@ async fn save_changes(app_handle: AppHandle, steam_active_user_id: String, curre
}
}

let grids_directory: PathBuf = PathBuf::from(steam::get_grids_directory(app_handle.to_owned(), steam_active_user_id.clone()));
let grids_directory: PathBuf = PathBuf::from(steam::get_grids_directory(app_handle.to_owned(), steam_path.to_owned(), steam_active_user_id.clone()));
for (appid, steam_logo_str_val) in changed_logo_positions.into_iter() {
let steam_logo_str: &str = steam_logo_str_val.as_str().expect("Should have been able to convert steamLogo pos into str.");
let logo_config_path: PathBuf = grids_directory.join(format!("{}.json", appid));
Expand Down Expand Up @@ -365,7 +365,7 @@ async fn save_changes(app_handle: AppHandle, steam_active_user_id: String, curre
modified_shortcuts_data.insert(String::from("shortcuts"), shortcuts_obj_map.to_owned());
shortcuts_data = Value::Object(modified_shortcuts_data);

let shortcuts_vdf_path: PathBuf = PathBuf::from(steam::get_shortcuts_path(app_handle.to_owned(), steam_active_user_id));
let shortcuts_vdf_path: PathBuf = PathBuf::from(steam::get_shortcuts_path(app_handle.to_owned(), steam_path.to_owned(), steam_active_user_id));
write_shortcuts_vdf(&shortcuts_vdf_path, shortcuts_data);
logger::log_to_core_file(app_handle.to_owned(), "Changes to shortcuts saved.", 0);
} else {
Expand All @@ -385,9 +385,9 @@ async fn save_changes(app_handle: AppHandle, steam_active_user_id: String, curre

#[tauri::command]
/// Writes the user's shortcuts.vdf file.
async fn write_shortcuts(app_handle: AppHandle, steam_active_user_id: String, shortcuts_str: String) -> bool {
async fn write_shortcuts(app_handle: AppHandle, steam_path: String, steam_active_user_id: String, shortcuts_str: String) -> bool {
logger::log_to_core_file(app_handle.to_owned(), "Writing shortcuts.vdf...", 0);
let shortcuts_vdf_path: PathBuf = PathBuf::from(steam::get_shortcuts_path(app_handle.to_owned(), steam_active_user_id));
let shortcuts_vdf_path: PathBuf = PathBuf::from(steam::get_shortcuts_path(app_handle.to_owned(), steam_path, steam_active_user_id));
let shortcuts_data: Value = serde_json::from_str(shortcuts_str.as_str()).expect("Should have been able to parse json string.");

let success: bool = write_shortcuts_vdf(&shortcuts_vdf_path, shortcuts_data);
Expand Down Expand Up @@ -435,12 +435,12 @@ async fn download_grid(app_handle: AppHandle, grid_url: String, dest_path: Strin

#[tauri::command]
/// Downloads a file from a url.
async fn clean_grids(app_handle: AppHandle, steam_active_user_id: String, preset: String, all_appids: String, selected_game_ids: String) -> String {
async fn clean_grids(app_handle: AppHandle, steam_path: String, steam_active_user_id: String, preset: String, all_appids: String, selected_game_ids: String) -> String {
logger::log_to_core_file(app_handle.to_owned(), format!("Starting {} grid cleaning.", preset).as_str(), 0);

let appids_arr: Vec<String> = serde_json::from_str(all_appids.as_str()).expect("Should have been able to deserialize appids array.");

let grids_dir_path: String = steam::get_grids_directory(app_handle.to_owned(), steam_active_user_id);
let grids_dir_path: String = steam::get_grids_directory(app_handle.to_owned(), steam_path, steam_active_user_id);
let grids_dir_contents = fs::read_dir(grids_dir_path).unwrap();

let mut found_apps: HashMap<String, (String, String)> = HashMap::new();
Expand Down Expand Up @@ -551,11 +551,15 @@ async fn add_steam_to_scope(app_handle: AppHandle) -> String {

if steam_path_res.is_ok() {
let steam_path: PathBuf = steam_path_res.ok().expect("Should have been able to get steam path from result.");
let steam_path_str: String = String::from(steam_path.as_os_str().to_str().expect("Should have been able to convert Steam path PathBuf to str."));
let steam_path_str: String = steam_path.as_path().display().to_string();
let was_added: bool = add_path_to_scope(app_handle, steam_path_str.to_owned()).await;

if was_added {
return steam_path_str;
if &steam_path_str == "c:/program files (x86)/steam" {
return String::from("C:/Program Files (x86)/Steam");
} else {
return steam_path_str;
}
} else {
return String::from("");
}
Expand Down
28 changes: 14 additions & 14 deletions src-tauri/src/steam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ pub fn get_steam_root_dir() -> Result<PathBuf, String> {

#[tauri::command]
/// Gets the steam grids directory.
pub fn get_grids_directory(app_handle: AppHandle, steam_active_user_id: String) -> String {
pub fn get_grids_directory(app_handle: AppHandle, steam_path: String, steam_active_user_id: String) -> String {
logger::log_to_core_file(app_handle.to_owned(), "Getting steam grids folder...", 0);

let steam_root: PathBuf = get_steam_root_dir().ok().expect("Steam install path should have been fine if this point is reached.");
let steam_root: PathBuf = PathBuf::from(steam_path);
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());
Expand All @@ -73,37 +73,37 @@ pub fn get_grids_directory(app_handle: AppHandle, steam_active_user_id: String)

#[tauri::command]
/// Gets the steam library cache directory.
pub fn get_library_cache_directory(app_handle: AppHandle) -> String {
pub fn get_library_cache_directory(app_handle: AppHandle, steam_path: String) -> String {
logger::log_to_core_file(app_handle.to_owned(), "Getting steam library cache folder...", 0);

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

#[tauri::command]
/// Gets the steam appinfo.vdf path.
pub fn get_appinfo_path(app_handle: AppHandle) -> String {
pub fn get_appinfo_path(app_handle: AppHandle, steam_path: String) -> String {
logger::log_to_core_file(app_handle.to_owned(), "Getting steam appinfo.vdf...", 0);

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

#[tauri::command]
/// Gets the steam shortcuts.vdf path.
pub fn get_shortcuts_path(app_handle: AppHandle, steam_active_user_id: String) -> String {
pub fn get_shortcuts_path(app_handle: AppHandle, steam_path: String, steam_active_user_id: String) -> String {
logger::log_to_core_file(app_handle.to_owned(), "Getting steam shortcuts.vdf...", 0);

let steam_root: PathBuf = get_steam_root_dir().ok().expect("Steam install path should have been fine if this point is reached.");
let steam_root: PathBuf = PathBuf::from(steam_path);
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("\\", "/");
}

#[tauri::command]
/// Gets the steam localconfig.vdf path.
pub fn get_localconfig_path(app_handle: AppHandle, steam_active_user_id: String) -> String {
pub fn get_localconfig_path(app_handle: AppHandle, steam_path: String, steam_active_user_id: String) -> String {
logger::log_to_core_file(app_handle.to_owned(), "Getting steam localconfig.vdf...", 0);

let steam_root: PathBuf = get_steam_root_dir().ok().expect("Steam install path should have been fine if this point is reached.");
let steam_root: PathBuf = PathBuf::from(steam_path);
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 @@ -142,10 +142,10 @@ fn read_steam_user(user_id: &str, user_block: &str) -> Map<String, Value> {
}

/// Reads the steam users.
fn read_steam_users() -> Map<String, Value> {
fn read_steam_users(steam_path: String) -> Map<String, Value> {
let mut steam_users: Map<String, Value> = Map::new();

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

Expand All @@ -170,10 +170,10 @@ fn read_steam_users() -> Map<String, Value> {

#[tauri::command]
/// Gets all steam users that have logged in on this computer.
pub fn get_steam_users(app_handle: AppHandle) -> String {
pub fn get_steam_users(app_handle: AppHandle, steam_path: String) -> String {
logger::log_to_core_file(app_handle.to_owned(), "Checking config/loginusers.vdf for current user info.", 0);

let steam_users = read_steam_users();
let steam_users = read_steam_users(steam_path);

logger::log_to_core_file(app_handle.to_owned(), format!("Loaded {} steam users.", steam_users.len()).as_str(), 0);

Expand Down
Loading

0 comments on commit 3bec562

Please sign in to comment.