Skip to content

Commit

Permalink
fix: shortcut icons no longer point to other dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tormak9970 committed May 4, 2023
1 parent 226a687 commit f52d9ed
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
29 changes: 26 additions & 3 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ async fn save_changes(app_handle: AppHandle, steam_active_user_id: String, curre

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();
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,11 +302,32 @@ 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);
println!("Should change shortcuts: {}", should_change_shortcuts);

if should_change_shortcuts {
logger::log_to_file(app_handle.to_owned(), "Changes to shortcuts detected. Writing shortcuts.vdf...", 0);
let shortcuts_data: Value = serde_json::from_str(shortcuts_str.as_str()).expect("Should have been able to parse json string.");
let mut shortcuts_data: Value = serde_json::from_str(shortcuts_str.as_str()).expect("Should have been able to parse json string.");

let shortcuts_obj_map: &mut Value = shortcuts_data.get_mut("shortcuts").expect("key: shortcuts should have existed.");
let shortcuts_map: &mut Map<String, Value> = shortcuts_obj_map.as_object_mut().expect("Should have been able to convert shortcuts to map");

for (_, shortcut) in shortcuts_map.into_iter() {
let shortcut_map: &mut Map<String, Value> = shortcut.as_object_mut().expect("should have been able to convert shortcut to map.");
let shortcut_appid_val: &Value = shortcut_map.get("appid").expect("shortcut should have had an appid");
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.");
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);
logger::log_to_file(app_handle.to_owned(), "Changes to shortcuts saved.", 0);
Expand All @@ -319,7 +341,8 @@ async fn save_changes(app_handle: AppHandle, steam_active_user_id: String, curre
return changed_res.unwrap();
} else {
let err = changed_res.err().unwrap();
panic!("{}", err.to_string());
logger::log_to_file(app_handle, format!("{}", err.to_string()).as_str(), 2);
return String::from("[]");
}
}

Expand Down
43 changes: 25 additions & 18 deletions src/lib/controllers/AppController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,27 +395,34 @@ 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);

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

0 comments on commit f52d9ed

Please sign in to comment.