Skip to content

Commit

Permalink
fix: completely fixed shortcut icon bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Tormak9970 committed May 4, 2023
1 parent f52d9ed commit c46027c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 49 deletions.
26 changes: 8 additions & 18 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChangedPath>, shortcut_ids: Vec<String>, shortcut_icons: &Map<String, Value>, original_shortcut_icons: &Map<String, Value>) -> 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<String, Value>, original_shortcut_icons: &Map<String, Value>) -> 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.");
Expand Down Expand Up @@ -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<String, Value>, original_shortcut_icons: Map<String, Value>) -> String {
let shortcut_ids: Vec<String> = 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<String, Value>, original_shortcut_icons: 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_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);
let paths_id_map: HashMap<String, ChangedPath> = paths_to_set.clone().iter().map(| entry | (entry.appId.to_owned(), entry.to_owned())).collect();
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_file(app_handle.to_owned(), "Current path entries converted to grid paths.", 0);

for changed_path in (&paths_to_set).into_iter() {
Expand Down Expand Up @@ -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);
Expand All @@ -316,17 +306,17 @@ 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()));
}
}

let mut modified_shortcuts_data: Map<String, Value> = 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);
Expand Down
51 changes: 23 additions & 28 deletions src/lib/controllers/AppController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -553,6 +546,7 @@ export class AppController {
* ? Logging complete.
*/
static async setCustomArt(path: string): Promise<void> {
const type = get(gridType);
const selectedGameId = get(selectedGameAppId);
const gameName = get(selectedGameName);
const selectedGridType = get(gridType);
Expand All @@ -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;
Expand All @@ -587,6 +581,7 @@ export class AppController {
static async setSteamGridArt(appId: number, url: URL): Promise<void> {
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);
Expand All @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/lib/controllers/RustInterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChangedPath[] | { error: string }> {
static async saveChanges(activeUserId: string, currentArt: { [appid: string]: LibraryCacheEntry }, originalArt: { [appid: string]: LibraryCacheEntry }, shortcuts: SteamShortcut[], shortcutIcons: { [id: string]: string }, originalShortcutIcons: { [id: string]: string }): Promise<ChangedPath[] | { error: string }> {
const shortcutsObj = {
"shortcuts": {...shortcuts}
}
const res = await invoke<string>("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<string>("save_changes", { currentArt: JSON.stringify(currentArt), originalArt: JSON.stringify(originalArt), shortcutsStr: JSON.stringify(shortcutsObj), steamActiveUserId: activeUserId, shortcutIcons: shortcutIcons, originalShortcutIcons: originalShortcutIcons });
return JSON.parse(res);
}

Expand Down

0 comments on commit c46027c

Please sign in to comment.