Skip to content

Commit

Permalink
Bigpicture optimization (#69)
Browse files Browse the repository at this point in the history
* Download icons  when not in bigpicture mode
  • Loading branch information
PhilipK authored Apr 1, 2022
1 parent a1ea4da commit 077f445
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 62 deletions.
2 changes: 2 additions & 0 deletions configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ wine_c_drive="/home/username/Games/gog-galaxy/drive_c" #Only for Linux, Is manda

[steam]
location="C:\\Program Files (x86)\\Steam\\" #If this value is not defined, the tool will try to find it automatically. If it can't find it, it will fail and tell you.
optimize_for_big_picture=false #Set icons to wide images, that big picture mode will use. This will make the icons have a wrong ratio in desktop mode, but will improve the look in big picture mode
create_collections=false #Will try to create a steam collection for each platform

[steamgrid_db]
enabled = true #If false, the whole download of custom art will be skipped.
Expand Down
3 changes: 2 additions & 1 deletion src/defaultconfig.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ enabled = true
enabled = true

[steam]
create_collections = false
create_collections = false
optimize_for_big_picture = false
2 changes: 1 addition & 1 deletion src/egs/manifest_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn exe_shortcut(manifest: ManifestItem) -> ShortcutOwned {
manifest.display_name.as_str(),
exe,
start_dir,
"",
exe,
"",
"",
)
Expand Down
2 changes: 1 addition & 1 deletion src/heroic/heroic_game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl From<HeroicGame> for ShortcutOwned {
game.title.as_str(),
&target,
&install_path,
"",
&target,
"",
&game.launch_parameters.as_str(),
);
Expand Down
1 change: 1 addition & 0 deletions src/steam/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ use serde::{Deserialize, Serialize};
pub struct SteamSettings {
pub location: Option<String>,
pub create_collections: bool,
pub optimize_for_big_picture: bool,
}
157 changes: 110 additions & 47 deletions src/steamgriddb/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub async fn download_images_for_users<'b>(
search,
client,
download_animated,
settings.steam.optimize_for_big_picture,
)
.await;
res.unwrap_or_default()
Expand Down Expand Up @@ -78,6 +79,7 @@ pub async fn download_images_for_users<'b>(
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PublicGameResponseMetadata {
store_asset_mtime: Option<u64>,
clienticon: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -109,17 +111,25 @@ async fn search_fo_to_download(
search: &CachedSearch<'_>,
client: &Client,
download_animated: bool,
download_big_picture: bool,
) -> Result<Vec<ToDownload>, Box<dyn Error>> {
let shortcuts_to_search_for = shortcuts.iter().filter(|s| {
let images = vec![
format!("{}_hero.png", s.app_id),
format!("{}p.png", s.app_id),
format!("{}.png", s.app_id),
format!("{}_logo.png", s.app_id),
format!("{}_bigpicture.png", s.app_id),
let types = {
let mut types = vec![
ImageType::Logo,
ImageType::Hero,
ImageType::Grid,
ImageType::WideGrid,
ImageType::Icon,
];
if download_big_picture {
types.push(ImageType::BigPicture);
}
types
};

let shortcuts_to_search_for = shortcuts.iter().filter(|s| {
// if we are missing any of the images we need to search for them
images.iter().any(|image| !known_images.contains(image)) && !s.app_name.is_empty()
types.iter().map(|t| t.file_name(s.app_id)).any(|image| !known_images.contains(&image)) && !s.app_name.is_empty()
});
let shortcuts_to_search_for: Vec<&ShortcutOwned> = shortcuts_to_search_for.collect();
if shortcuts_to_search_for.is_empty() {
Expand All @@ -143,14 +153,9 @@ async fn search_fo_to_download(
for (app_id, search) in search_results_a.into_iter().flatten() {
search_results.insert(app_id, search);
}
let types = vec![
ImageType::Logo,
ImageType::Hero,
ImageType::Grid,
ImageType::WideGrid,
ImageType::BigPicture,
];

let mut to_download = vec![];
let grid_folder = Path::new(user_data_folder).join("config").join("grid");
for image_type in types {
let images_needed = shortcuts
.iter()
Expand All @@ -163,41 +168,59 @@ async fn search_fo_to_download(
.collect();

let shortcuts: Vec<&ShortcutOwned> = images_needed.collect();
let image_search_result =
get_images_for_ids(client, &image_ids, &image_type, download_animated).await;
match image_search_result {
Ok(images) => {
let grid_folder = Path::new(user_data_folder).join("config").join("grid");
let images = images
.iter()
.enumerate()
.map(|(index, image)| (image, shortcuts[index], image_ids[index]));
let download_for_this_type = stream::iter(images)
.filter_map(|(image, shortcut, game_id)| {
let path = grid_folder.join(image_type.file_name(shortcut.app_id));
async move {
let image_url = match image {
Ok(img) => Some(img.url.clone()),
Err(_) => get_steam_image_url(game_id, &image_type).await,
};
if let Some(url) = image_url {
Some(ToDownload {
path,
url,
app_name: shortcut.app_name.clone(),
image_type: image_type.clone(),
})
} else {
None


if let ImageType::Icon = image_type {
let mut index = 0;
for image_id in image_ids{
let shortcut = shortcuts[index];
if let Some(url) = get_steam_icon_url(image_id).await{
let path = grid_folder.join(image_type.file_name(shortcut.app_id));
to_download.push(ToDownload {
path,
url,
app_name: shortcut.app_name.clone(),
image_type: image_type.clone(),
});
}
index = index + 1;
}
} else {
let image_search_result =
get_images_for_ids(client, &image_ids, &image_type, download_animated).await;
match image_search_result {
Ok(images) => {
let images = images
.iter()
.enumerate()
.map(|(index, image)| (image, shortcuts[index], image_ids[index]));
let download_for_this_type = stream::iter(images)
.filter_map(|(image, shortcut, game_id)| {
let path = grid_folder.join(image_type.file_name(shortcut.app_id));
async move {
let image_url = match image {
Ok(img) => Some(img.url.clone()),
Err(_) => get_steam_image_url(game_id, &image_type).await,
};
if let Some(url) = image_url {
Some(ToDownload {
path,
url,
app_name: shortcut.app_name.clone(),
image_type: image_type.clone(),
})
} else {
None
}
}
}
})
.collect::<Vec<ToDownload>>()
.await;
})
.collect::<Vec<ToDownload>>()
.await;

to_download.extend(download_for_this_type);
to_download.extend(download_for_this_type);
}
Err(err) => println!("Error getting images: {}", err),
}
Err(err) => println!("Error getting images: {}", err),
}
}
Ok(to_download)
Expand Down Expand Up @@ -244,15 +267,18 @@ async fn get_images_for_ids(
nsfw: Some(&Nsfw::False),
..Default::default()
};

let query_type = match image_type {
ImageType::Hero => Hero(Some(hero_parameters)),
ImageType::BigPicture => Grid(Some(big_picture_parameters)),
ImageType::Grid => Grid(Some(grid_parameters)),
ImageType::WideGrid => Grid(Some(big_picture_parameters)),
ImageType::Logo => Logo(Some(logo_parameters)),
_ => panic!("Unsupported image type"),
};

let image_search_result = client.get_images_for_ids(image_ids, &query_type).await;

image_search_result
}

Expand Down Expand Up @@ -286,6 +312,43 @@ async fn get_steam_image_url(game_id: usize, image_type: &ImageType) -> Option<S
return None;
}

async fn get_steam_icon_url(game_id: usize) -> Option<String> {
let steamgriddb_page_url = format!("https://www.steamgriddb.com/api/public/game/{}/", game_id);
let response = reqwest::get(steamgriddb_page_url).await;
match response {
Ok(response) => {
let text_response = response.json::<PublicGameResponse>().await;
match text_response {
Ok(response) => {
let game_id = response
.data
.clone()
.map(|d| d.platforms.map(|p| p.steam.map(|s| s.id)));
let mtime = response.data.map(|d| {
d.platforms
.map(|p| p.steam.map(|s| s.metadata.map(|m| m.clienticon)))
});
if let (Some(Some(Some(steam_app_id))), Some(Some(Some(Some(Some(mtime)))))) =
(game_id, mtime)
{
return Some(icon_url(&steam_app_id, &mtime));
}
}
Err(_) => (),
}
}
Err(_) => (),
}
return None;
}

fn icon_url(steam_app_id: &str, icon_id: &str) -> String {
format!(
"https://cdn.cloudflare.steamstatic.com/steamcommunity/public/images/apps/{}/{}.ico",
steam_app_id, icon_id
)
}

async fn download_to_download(to_download: &ToDownload) -> Result<(), Box<dyn Error>> {
println!(
"Downloading {:?} for {} to {:?}",
Expand Down
10 changes: 8 additions & 2 deletions src/steamgriddb/image_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub enum ImageType {
WideGrid,
Logo,
BigPicture,
Icon
}

impl ImageType {
Expand All @@ -15,6 +16,7 @@ impl ImageType {
ImageType::WideGrid => format!("{}.png", app_id),
ImageType::Logo => format!("{}_logo.png", app_id),
ImageType::BigPicture => format!("{}_bigpicture.png", app_id),
ImageType::Icon => format!("{}.ico", app_id),
}
}

Expand All @@ -25,7 +27,11 @@ impl ImageType {
ImageType::Grid => format!("https://cdn.cloudflare.steamstatic.com/steam/apps/{}/library_600x900_2x.jpg?t={}",steam_app_id,mtime),
ImageType::WideGrid => format!("https://cdn.cloudflare.steamstatic.com/steam/apps/{}/header.jpg?t={}",steam_app_id,mtime),
ImageType::Logo => format!("https://cdn.cloudflare.steamstatic.com/steam/apps/{}/logo.png?t={}",steam_app_id,mtime),
ImageType::BigPicture => format!("https://cdn.cloudflare.steamstatic.com/steam/apps/{}/header.jpg?t={}",steam_app_id,mtime),
}
ImageType::BigPicture => format!("https://cdn.cloudflare.steamstatic.com/steam/apps/{}/header.jpg?t={}",steam_app_id,mtime),
// This should not happen
_ => "".to_string()
}
}


}
32 changes: 22 additions & 10 deletions src/sync/synchronization.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use steam_shortcuts_util::{shortcut::ShortcutOwned, shortcuts_to_bytes};

use crate::{
egs::EpicPlatform,
egs::EpicPlatform,
legendary::LegendaryPlatform,
lutris::lutris_platform::LutrisPlatform,
platform::Platform,
Expand All @@ -10,7 +10,7 @@ use crate::{
get_shortcuts_for_user, get_shortcuts_paths, setup_proton_games, write_collections,
Collection, ShortcutInfo, SteamUsersInfo,
},
steamgriddb::download_images_for_users,
steamgriddb::{download_images_for_users, ImageType},
uplay::Uplay,
};

Expand Down Expand Up @@ -50,7 +50,11 @@ pub async fn run_sync(settings: &Settings) -> Result<(), Box<dyn Error>> {

shortcut_info.shortcuts.extend(all_shortcuts.clone());

fix_shortcut_icons(user, &mut shortcut_info.shortcuts);
fix_shortcut_icons(
user,
&mut shortcut_info.shortcuts,
settings.steam.optimize_for_big_picture,
);

save_shortcuts(&shortcut_info.shortcuts, Path::new(&shortcut_info.path));

Expand Down Expand Up @@ -86,10 +90,20 @@ fn remove_old_shortcuts(shortcut_info: &mut ShortcutInfo) {
.retain(|shortcut| !shortcut.dev_kit_game_id.starts_with(&boilr_tag));
}

fn fix_shortcut_icons(user: &SteamUsersInfo, shortcuts: &mut Vec<ShortcutOwned>) {
fn fix_shortcut_icons(
user: &SteamUsersInfo,
shortcuts: &mut Vec<ShortcutOwned>,
big_picture_mode: bool,
) {
let image_folder = Path::new(&user.steam_user_data_folder)
.join("config")
.join("grid");
let image_type = if big_picture_mode {
ImageType::BigPicture
} else {
ImageType::Icon
};

for shortcut in shortcuts {
#[cfg(not(target_family = "unix"))]
let replace_icon = shortcut.icon.trim().eq("");
Expand All @@ -100,11 +114,10 @@ fn fix_shortcut_icons(user: &SteamUsersInfo, shortcuts: &mut Vec<ShortcutOwned>)
&shortcut.exe,
&shortcut.app_name,
);
let new_icon = image_folder
.join(format!("{}_bigpicture.png", app_id))
.to_string_lossy()
.to_string();
shortcut.icon = new_icon;
let new_icon_path = image_folder.join(image_type.file_name(app_id));
if new_icon_path.exists() {
shortcut.icon = new_icon_path.to_string_lossy().to_string();
}
}
}
}
Expand Down Expand Up @@ -144,7 +157,6 @@ fn get_platform_shortcuts(settings: &Settings) -> Vec<(String, Vec<ShortcutOwned
update_platform_shortcuts(&LutrisPlatform {
settings: settings.lutris.clone(),
}),

];
#[cfg(target_family = "unix")]
{
Expand Down

0 comments on commit 077f445

Please sign in to comment.