From c46027c9f40e3f927237ebd0db60bc02d52922f0 Mon Sep 17 00:00:00 2001 From: Tormak <63308171+Tormak9970@users.noreply.github.com> Date: Thu, 4 May 2023 11:14:47 -0500 Subject: [PATCH] fix: completely fixed shortcut icon bug --- src-tauri/src/main.rs | 26 +++++--------- src/lib/controllers/AppController.ts | 51 +++++++++++++--------------- src/lib/controllers/RustInterop.ts | 5 ++- 3 files changed, 33 insertions(+), 49 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 04ffb4d7..27622218 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -119,14 +119,7 @@ fn filter_paths(app_handle: &AppHandle, steam_active_user_id: String, current_pa } /// Checks for shortcut grid changes. -fn check_for_shortcut_changes(changed_paths: Vec, shortcut_ids: Vec, shortcut_icons: &Map, original_shortcut_icons: &Map) -> bool { - for changed_path in changed_paths.into_iter() { - let appid = changed_path.appId; - if shortcut_ids.contains(&appid) { - return true; - } - } - +fn check_for_shortcut_changes(shortcut_icons: &Map, original_shortcut_icons: &Map) -> bool { for (shortcut_id, icon) in shortcut_icons.to_owned().into_iter() { let icon: &str = icon.as_str().expect("Should have been able to convert icon to &str."); let original_icon: &str = original_shortcut_icons.get(&shortcut_id).expect("Original hortcut should have had an icon.").as_str().expect("Should have been able to convert original icon to &str."); @@ -253,16 +246,13 @@ 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_ids_str: String, shortcut_icons: Map, original_shortcut_icons: Map) -> String { - let shortcut_ids: Vec = shortcut_ids_str.split(", ").map(| appid | { - return appid.to_owned(); - }).collect(); +async fn save_changes(app_handle: AppHandle, steam_active_user_id: String, current_art: String, original_art: String, shortcuts_str: String, shortcut_icons: Map, original_shortcut_icons: Map) -> 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_file(app_handle.to_owned(), "Converting current path entries to grid paths...", 0); let paths_to_set: Vec = filter_paths(&app_handle, steam_active_user_id.clone(), ¤t_art_dict, &original_art_dict); - let paths_id_map: HashMap = paths_to_set.clone().iter().map(| entry | (entry.appId.to_owned(), entry.to_owned())).collect(); + let paths_id_map: HashMap = 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_file(app_handle.to_owned(), "Current path entries converted to grid paths.", 0); for changed_path in (&paths_to_set).into_iter() { @@ -301,7 +291,7 @@ async fn save_changes(app_handle: AppHandle, steam_active_user_id: String, curre } } - let should_change_shortcuts = check_for_shortcut_changes(paths_to_set.clone(), shortcut_ids, &shortcut_icons, &original_shortcut_icons); + let should_change_shortcuts = check_for_shortcut_changes(&shortcut_icons, &original_shortcut_icons); if should_change_shortcuts { logger::log_to_file(app_handle.to_owned(), "Changes to shortcuts detected. Writing shortcuts.vdf...", 0); @@ -316,8 +306,10 @@ async fn save_changes(app_handle: AppHandle, steam_active_user_id: String, curre let shortcut_appid_num: i64 = shortcut_appid_val.as_i64().expect("should have been able to convert shortcut appid to str."); let shortcut_appid: String = shortcut_appid_num.to_string(); - if paths_id_map.contains_key(&shortcut_appid) { - let changed_path: &ChangedPath = paths_id_map.get(&shortcut_appid).expect("entry should have existed."); + let path_key: String = format!("{}_icon", shortcut_appid.to_owned()).to_string(); + + if paths_id_map.contains_key(&path_key) { + let changed_path: &ChangedPath = paths_id_map.get(&path_key).expect("entry should have existed."); shortcut_map.insert(String::from("icon"), Value::String(changed_path.targetPath.to_owned())); } } @@ -325,8 +317,6 @@ async fn save_changes(app_handle: AppHandle, steam_active_user_id: String, curre let mut modified_shortcuts_data: Map = Map::new(); modified_shortcuts_data.insert(String::from("shortcuts"), shortcuts_obj_map.to_owned()); shortcuts_data = Value::Object(modified_shortcuts_data); - - println!("shortcuts data: {}", serde_json::to_string_pretty(&shortcuts_data).unwrap()); let shortcuts_vdf_path: PathBuf = PathBuf::from(steam::get_shortcuts_path(app_handle.to_owned(), steam_active_user_id)); write_shortcuts_vdf(&shortcuts_vdf_path, shortcuts_data); diff --git a/src/lib/controllers/AppController.ts b/src/lib/controllers/AppController.ts index 5d5c0741..a94d7b2b 100644 --- a/src/lib/controllers/AppController.ts +++ b/src/lib/controllers/AppController.ts @@ -395,34 +395,27 @@ export class AppController { const originalIconEntries = get(originalSteamShortcuts).map((shortcut) => [shortcut.appid, shortcut.icon]); const originalShortcutIcons = Object.fromEntries(originalIconEntries); - console.log("Library Cache:", libraryCache); - console.log("Original Cache:", originalCache); - console.log("Shortcuts:", shortcuts); - console.log("Shortcut Ids:", shortcutIds); - console.log("Shortcut Icons:", shortcutIcons); - console.log("Original Shortcuts Icons:", originalShortcutIcons); - - const changedPaths = await RustInterop.saveChanges(get(activeUserId).toString(), libraryCache, originalCache, shortcuts, shortcutIds, shortcutIcons, originalShortcutIcons); + const changedPaths = await RustInterop.saveChanges(get(activeUserId).toString(), libraryCache, originalCache, shortcuts, shortcutIcons, originalShortcutIcons); - // if ((changedPaths as any).error !== undefined) { - // ToastController.showSuccessToast("Changes failed."); - // LogController.log("Changes failed."); - // } else { - // for (const changedPath of (changedPaths as ChangedPath[])) { - // libraryCache[changedPath.appId][changedPath.gridType] = changedPath.targetPath == "REMOVE" ? "" : changedPath.targetPath; - // if (changedPath.gridType == GridTypes.ICON && shortcutIds.includes(changedPath.appId)) { - // const shortcut = shortcuts.find((s) => s.appid.toString() == changedPath.appId); - // shortcut.icon = changedPath.targetPath == "REMOVE" ? "" : changedPath.targetPath; - // } - // } - // originalAppLibraryCache.set(JSON.parse(JSON.stringify(libraryCache))); - // appLibraryCache.set(libraryCache); + if ((changedPaths as any).error !== undefined) { + ToastController.showSuccessToast("Changes failed."); + LogController.log("Changes failed."); + } else { + for (const changedPath of (changedPaths as ChangedPath[])) { + libraryCache[changedPath.appId][changedPath.gridType] = changedPath.targetPath == "REMOVE" ? "" : changedPath.targetPath; + if (changedPath.gridType == GridTypes.ICON && shortcutIds.includes(changedPath.appId)) { + const shortcut = shortcuts.find((s) => s.appid.toString() == changedPath.appId); + shortcut.icon = changedPath.targetPath == "REMOVE" ? "" : changedPath.targetPath; + } + } + originalAppLibraryCache.set(JSON.parse(JSON.stringify(libraryCache))); + appLibraryCache.set(libraryCache); - // originalSteamShortcuts.set(JSON.parse(JSON.stringify(shortcuts))); - // steamShortcuts.set(shortcuts); - // ToastController.showSuccessToast("Changes saved!"); - // LogController.log("Saved changes."); - // } + originalSteamShortcuts.set(JSON.parse(JSON.stringify(shortcuts))); + steamShortcuts.set(shortcuts); + ToastController.showSuccessToast("Changes saved!"); + LogController.log("Saved changes."); + } canSave.set(false); } @@ -553,6 +546,7 @@ export class AppController { * ? Logging complete. */ static async setCustomArt(path: string): Promise { + const type = get(gridType); const selectedGameId = get(selectedGameAppId); const gameName = get(selectedGameName); const selectedGridType = get(gridType); @@ -565,7 +559,7 @@ export class AppController { gameImages[selectedGameId][selectedGridType] = path; - if (get(currentPlatform) == Platforms.NON_STEAM) { + if (get(currentPlatform) == Platforms.NON_STEAM && type == GridTypes.ICON) { const shortcuts = get(steamShortcuts); const shortcut = shortcuts.find((s) => s.appid == selectedGameId); shortcut.icon = path; @@ -587,6 +581,7 @@ export class AppController { static async setSteamGridArt(appId: number, url: URL): Promise { const localPath = await AppController.cacheController.getGridImage(appId, url.toString()); + const type = get(gridType); const selectedGameId = get(selectedGameAppId); const gameName = get(selectedGameName); const selectedGridType = get(gridType); @@ -599,7 +594,7 @@ export class AppController { gameImages[selectedGameId][selectedGridType] = localPath; - if (get(currentPlatform) == Platforms.NON_STEAM) { + if (get(currentPlatform) == Platforms.NON_STEAM && type == GridTypes.ICON) { const shortcuts = get(steamShortcuts); const shortcut = shortcuts.find((s) => s.appid == selectedGameId); shortcut.icon = localPath; diff --git a/src/lib/controllers/RustInterop.ts b/src/lib/controllers/RustInterop.ts index 16ef884e..549e1ce7 100644 --- a/src/lib/controllers/RustInterop.ts +++ b/src/lib/controllers/RustInterop.ts @@ -152,16 +152,15 @@ export class RustInterop { * @param currentArt The current changes. * @param originalArt The original art dictionary. * @param shortcuts The list of shortcuts. - * @param shortcutIds The list of shortcut ids. * @param shortcutIcons The map of shortcutIds to updated icons. * @param originalShortcutIcons The map of shortcutIds to original icons. * @returns A promise resolving to a string of serialized changed tuples. */ - static async saveChanges(activeUserId: string, currentArt: { [appid: string]: LibraryCacheEntry }, originalArt: { [appid: string]: LibraryCacheEntry }, shortcuts: SteamShortcut[], shortcutIds: string[], shortcutIcons: { [id: string]: string }, originalShortcutIcons: { [id: string]: string }): Promise { + static async saveChanges(activeUserId: string, currentArt: { [appid: string]: LibraryCacheEntry }, originalArt: { [appid: string]: LibraryCacheEntry }, shortcuts: SteamShortcut[], shortcutIcons: { [id: string]: string }, originalShortcutIcons: { [id: string]: string }): Promise { const shortcutsObj = { "shortcuts": {...shortcuts} } - const res = await invoke("save_changes", { currentArt: JSON.stringify(currentArt), originalArt: JSON.stringify(originalArt), shortcutsStr: JSON.stringify(shortcutsObj), shortcutIdsStr: shortcutIds.join(", "), steamActiveUserId: activeUserId, shortcutIcons: shortcutIcons, originalShortcutIcons: originalShortcutIcons }); + const res = await invoke("save_changes", { currentArt: JSON.stringify(currentArt), originalArt: JSON.stringify(originalArt), shortcutsStr: JSON.stringify(shortcutsObj), steamActiveUserId: activeUserId, shortcutIcons: shortcutIcons, originalShortcutIcons: originalShortcutIcons }); return JSON.parse(res); }