Skip to content

Commit

Permalink
feat: nonsteam import/export complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Tormak9970 committed Apr 18, 2023
1 parent 737b7ba commit 0d6d79e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
41 changes: 34 additions & 7 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,23 @@ fn filter_paths(app_handle: &AppHandle, steam_active_user_id: String, current_pa
return res;
}

fn check_for_shortcut_changes(changed_paths: Vec<ChangedPath>, shortcut_ids: Vec<String>) -> bool {
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;
}
}

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.");

if icon != original_icon {
return true;
}
}

return false;
}

Expand Down Expand Up @@ -176,15 +185,15 @@ async fn read_shortcuts_vdf(app_handle: AppHandle, steam_active_user_id: String)
}

#[tauri::command]
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) -> String {
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();
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, &current_art_dict, &original_art_dict);
let paths_to_set: Vec<ChangedPath> = filter_paths(&app_handle, steam_active_user_id.clone(), &current_art_dict, &original_art_dict);
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 @@ -212,13 +221,13 @@ 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);
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_vdf_path = PathBuf::from("C:/Users/Tormak/Desktop/shortcuts_write_test.vdf");
let shortcuts_data = serde_json::from_str(shortcuts_str.as_str()).expect("Should have been able to parse json string.");
let shortcuts_data: Value = serde_json::from_str(shortcuts_str.as_str()).expect("Should have been able to parse json string.");
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);
} else {
Expand All @@ -235,6 +244,23 @@ async fn save_changes(app_handle: AppHandle, steam_active_user_id: String, curre
}
}

#[tauri::command]
async fn write_shortcuts(app_handle: AppHandle, steam_active_user_id: String, shortcuts_str: String) -> bool {
logger::log_to_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_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);

if success {
logger::log_to_file(app_handle.to_owned(), "Changes to shortcuts saved.", 0);
return true;
} else {
logger::log_to_file(app_handle.to_owned(), "Changes to shortcuts failed.", 0);
return false;
}
}

fn add_steam_to_scope(app_handle: &AppHandle) {
let steam_path = get_steam_root_dir();
let steam_parent_dir = steam_path.parent().unwrap();
Expand Down Expand Up @@ -275,7 +301,8 @@ fn main() {
import_grids_from_zip,
read_appinfo_vdf,
read_shortcuts_vdf,
save_changes
save_changes,
write_shortcuts
])
.setup(| app | {
let app_handle = app.handle();
Expand Down
22 changes: 20 additions & 2 deletions src/lib/controllers/AppController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,13 @@ export class AppController {
const shortcuts = get(steamShortcuts);
const shortcutIds = Object.values(shortcuts).map((shortcut) => shortcut.appid.toString());

const changedPaths = await RustInterop.saveChanges(get(activeUserId).toString(), libraryCache, originalCache, shortcuts, shortcutIds);
const shortcutEntries = shortcuts.map((shortcut) => [shortcut.appid, shortcut.icon]);
const shortcutIcons = Object.fromEntries(shortcutEntries);

const originalIconEntries = get(originalSteamShortcuts).map((shortcut) => [shortcut.appid, shortcut.icon]);
const originalShortcutIcons = Object.fromEntries(originalIconEntries);

const changedPaths = await RustInterop.saveChanges(get(activeUserId).toString(), libraryCache, originalCache, shortcuts, shortcutIds, shortcutIcons, originalShortcutIcons);

if ((changedPaths as any).error !== undefined) {
ToastController.showSuccessToast("Changes failed.");
Expand Down Expand Up @@ -467,11 +473,23 @@ export class AppController {
const idsMapEntries: [string, string][] = Object.entries(shortcuts).map(([shortcutId, shortcut]) => { return [shortcut.AppName, shortcutId]; });
const shortcutIdsMap = Object.fromEntries(idsMapEntries);

const succeeded = await RustInterop.importGridsFromZip(get(activeUserId).toString(), shortcutIdsMap);
const [succeeded, iconsToSet] = await RustInterop.importGridsFromZip(get(activeUserId).toString(), shortcutIdsMap);

if (succeeded) {
const shortcuts = get(steamShortcuts);
const shortcutIds = Object.values(shortcuts).map((shortcut) => shortcut.appid.toString());

for (const [id, path] of Object.entries(iconsToSet)) {
if (shortcutIds.includes(id)) {
const shortcut = shortcuts.find((s) => s.appid.toString() == id);
shortcut.icon = path;
}
}

ToastController.showSuccessToast("Import successful!");
LogController.log("Successfully imported user's grids.");

await AppController.saveChanges();

const filteredCache = await AppController.getCacheData(get(nonSteamGames));
originalAppLibraryCache.set(filteredCache);
Expand Down
20 changes: 14 additions & 6 deletions src/lib/controllers/RustInterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,29 @@ export class RustInterop {
* @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[]): Promise<ChangedPath[] | { error: string }> {
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 }> {
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 });
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 });
return JSON.parse(res);
}

/**
* Adds the steam directory to the fsScope.
* @returns Whether the scope was successfully added.
* Writes changes to the steam shortcuts.
* @param activeUserId The id of the active user.
* @param shortcuts The list of shortcuts.
* @returns A promise resolving to true if the write was successful.
*/
static async addSteamToScope(): Promise<boolean> {
return await invoke<boolean>("add_steam_to_scope", {});
static async writeShortcuts(activeUserId: string, shortcuts: SteamShortcut[]): Promise<boolean> {
const shortcutsObj = {
"shortcuts": {...shortcuts}
}
const res = await invoke<string>("write_shortcuts", { shortcutsStr: JSON.stringify(shortcutsObj), steamActiveUserId: activeUserId });
return JSON.parse(res);
}
}

0 comments on commit 0d6d79e

Please sign in to comment.