Skip to content

Commit

Permalink
fix: steam game icons now work properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Tormak9970 committed Jul 12, 2023
1 parent 1e41768 commit f16cd65
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
18 changes: 14 additions & 4 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,19 @@ struct ChangedPath {
sourcePath: String
}

/// Checks if an appid belongs to a shortcut
fn is_appid_shortcut(appid: &str, shortcut_icons: &Map<String, Value>) -> bool {
return shortcut_icons.contains_key(appid);
}

/// Gets a grid's file name based on its type.
fn get_grid_filename(app_handle: &AppHandle, appid: &str, grid_type: &str, image_type: &str) -> String {
match grid_type {
"Capsule" => return format!("{}p{}", appid, image_type),
"Wide Capsule" => return format!("{}{}", appid, image_type),
"Hero" => return format!("{}_hero{}", appid, image_type),
"Logo" => return format!("{}_logo{}", appid, image_type),
"Icon" => return format!("{}_icon{}", appid, image_type),
"Icon" => return format!("{}_icon.jpg", appid),
_ => {
logger::log_to_core_file(app_handle.to_owned(), format!("Unexpected grid type {}", grid_type).as_str(), 2);
panic!("Unexpected grid type {}", grid_type);
Expand All @@ -80,8 +85,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) -> Vec<ChangedPath> {
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()));
let mut res:Vec<ChangedPath> = Vec::new();

for (appid, grids_map) in current_paths.into_iter() {
Expand All @@ -100,7 +106,11 @@ fn filter_paths(app_handle: &AppHandle, steam_active_user_id: String, current_pa

if source_path != "REMOVE" {
let adjusted_path = adjust_path(app_handle, appid.as_str(), source_path_owned.as_str(), grid_type.as_str()).replace("\\", "/");
target_path = String::from(grids_dir.join(adjusted_path).to_str().unwrap()).replace("\\", "/");
if grid_type == "Icon" && !is_appid_shortcut(&appid, shortcut_icons) {
target_path = String::from(lib_cache_dir.join(adjusted_path).to_str().unwrap()).replace("\\", "/");
} else {
target_path = String::from(grids_dir.join(adjusted_path).to_str().unwrap()).replace("\\", "/");
}
} else {
target_path = String::from("REMOVE");
}
Expand Down Expand Up @@ -262,7 +272,7 @@ async fn save_changes(app_handle: AppHandle, steam_active_user_id: String, curre
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);
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_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
17 changes: 9 additions & 8 deletions src/lib/controllers/AppController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,11 @@ export class AppController {
/**
* Filters and structures the library grids based on the app's needs.
* @param gridsDirContents The contents of the grids dir.
* @param shortcutIds The list of loaded shortcuts ids.
* @returns The filtered and structured grids dir.
* ? Logging complete.
*/
private static filterGridsDir(gridsDirContents: fs.FileEntry[]): [{ [appid: string]: LibraryCacheEntry }, fs.FileEntry[]] {
private static filterGridsDir(gridsDirContents: fs.FileEntry[], shortcutsIds: string[]): [{ [appid: string]: LibraryCacheEntry }, fs.FileEntry[]] {
let resKeys = [];
const logoConfigs = [];
const res: { [appid: string]: LibraryCacheEntry } = {};
Expand All @@ -234,7 +235,8 @@ export class AppController {
ToastController.showWarningToast(`Duplicate grid found. Try cleaning`);
LogController.warn(`Duplicate grid found for ${appid}.`);
} else {
if (gridTypeLUT[type]) {
//? Since we have to poison the cache for icons, we also don't want to load them from the grids folder. Shortcuts don't need this.
if (gridTypeLUT[type] && (type !== "icon" || shortcutsIds.includes(appid))) {
if (!resKeys.includes(appid)) {
resKeys.push(appid);
res[appid] = {} as LibraryCacheEntry;
Expand All @@ -252,13 +254,11 @@ export class AppController {
* Filters and structures the library cache based on the app's needs.
* @param libraryCacheContents The contents of the library cache.
* @param gridsInfos The filtered grid infos.
* @param shortcuts The list of loaded shortcuts
* @param shortcutIds The list of loaded shortcuts ids.
* @returns The filtered and structured library cache.
* ? Logging complete.
*/
private static filterLibraryCache(libraryCacheContents: fs.FileEntry[], gridsInfos: { [appid: string]: LibraryCacheEntry }, shortcuts: GameStruct[]): { [appid: string]: LibraryCacheEntry } {
const shortcutIds = Object.values(shortcuts).map((shortcut) => shortcut.appid.toString());

private static filterLibraryCache(libraryCacheContents: fs.FileEntry[], gridsInfos: { [appid: string]: LibraryCacheEntry }, shortcutIds: string[]): { [appid: string]: LibraryCacheEntry } {
let resKeys = Object.keys(gridsInfos);
const res: { [appid: string]: LibraryCacheEntry } = gridsInfos;

Expand Down Expand Up @@ -299,11 +299,12 @@ export class AppController {
*/
private static async getCacheData(shortcuts: GameStruct[]): Promise<{ [appid: string]: LibraryCacheEntry }> {
const gridDirContents = (await fs.readDir(await RustInterop.getGridsDirectory(get(activeUserId).toString())));
const [filteredGrids, logoConfigs] = AppController.filterGridsDir(gridDirContents);
const shortcutIds = Object.values(shortcuts).map((shortcut) => shortcut.appid.toString());
const [filteredGrids, logoConfigs] = AppController.filterGridsDir(gridDirContents, shortcutIds);
LogController.log("Grids loaded.");

const libraryCacheContents = (await fs.readDir(await RustInterop.getLibraryCacheDirectory()));
const filteredCache = AppController.filterLibraryCache(libraryCacheContents, filteredGrids, shortcuts);
const filteredCache = AppController.filterLibraryCache(libraryCacheContents, filteredGrids, shortcutIds);
LogController.log("Library Cache loaded.");

await AppController.cacheLogoConfigs(logoConfigs);
Expand Down

0 comments on commit f16cd65

Please sign in to comment.