From d0f731d68ff8c5f98c3695de89ab25f1f7046af1 Mon Sep 17 00:00:00 2001 From: Nikita Podvirnyi Date: Mon, 17 Jun 2024 12:38:12 +0200 Subject: [PATCH] feat: updated background images support thanks gtk devs for providing webp support :thumbup: (no) --- shell.nix | 1 + src/background.rs | 38 +++++++++++++++++++++++--------- src/main.rs | 5 ++++- src/ui/first_run/dependencies.rs | 12 ++++++---- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/shell.nix b/shell.nix index 2d1c76c..bb939d1 100644 --- a/shell.nix +++ b/shell.nix @@ -25,6 +25,7 @@ in pkgs.mkShell { pkg-config xdelta + libwebp ]; buildInputs = with pkgs; [ diff --git a/src/background.rs b/src/background.rs index 7f27a00..9cc1601 100644 --- a/src/background.rs +++ b/src/background.rs @@ -1,3 +1,5 @@ +use std::process::Command; + use anime_launcher_sdk::anime_game_core::installer::downloader::Downloader; use anime_launcher_sdk::anime_game_core::minreq; @@ -13,11 +15,11 @@ pub fn get_uri() -> String { let lang = crate::i18n::get_lang(); if lang.language == unic_langid::langid!("zh-cn").language { - concat!("https://hyp-api.", "mi", "ho", "yo", ".com/hyp/hyp-connect/api/getGames?launcher_id=jGHBHlcOq1").to_owned() + concat!("https://hyp-api.", "mi", "ho", "yo", ".com/hyp/hyp-connect/api/getAllGameBasicInfo?launcher_id=jGHBHlcOq1").to_owned() } else { - let uri = concat!("https://sg-hyp-api.", "ho", "yo", "verse", ".com/hyp/hyp-connect/api/getGames?launcher_id=VYTpXlbWo8&language="); + let uri = concat!("https://sg-hyp-api.", "ho", "yo", "verse", ".com/hyp/hyp-connect/api/getAllGameBasicInfo?launcher_id=VYTpXlbWo8&language="); uri.to_owned() + &crate::i18n::format_lang(&lang) } @@ -27,15 +29,15 @@ pub fn get_uri() -> String { pub fn get_background_info() -> anyhow::Result { let json = serde_json::from_slice::(minreq::get(get_uri()).send()?.as_bytes())?; - let uri = json["data"]["games"].as_array() - .ok_or_else(|| anyhow::anyhow!("Failed to get background picture uri"))? + let uri = json["data"]["game_info_list"].as_array() + .ok_or_else(|| anyhow::anyhow!("Failed to list games in the backgrounds API"))? .iter() - .find(|game| game["biz"].as_str() == Some("hk4e_global")) - .and_then(|game| { - game["display"]["background"]["url"].as_str() - .or_else(|| game["display"]["thumbnail"]["url"].as_str()) - }) - .ok_or_else(|| anyhow::anyhow!("Failed to get background picture uri"))? + .find(|game| game["game"]["biz"].as_str() == Some("hk4e_global")) + .ok_or_else(|| anyhow::anyhow!("Failed to find the game in the backgrounds API"))?["backgrounds"] + .as_array() + .and_then(|backgrounds| backgrounds.iter().next()) + .and_then(|background| background["background"]["url"].as_str()) + .ok_or_else(|| anyhow::anyhow!("Failed to get background picture url"))? .to_string(); let hash = uri.split('/') @@ -67,7 +69,7 @@ pub fn download_background() -> anyhow::Result<()> { } } - let mut downloader = Downloader::new(info.uri)?; + let mut downloader = Downloader::new(&info.uri)?; downloader.continue_downloading = false; @@ -75,5 +77,19 @@ pub fn download_background() -> anyhow::Result<()> { anyhow::bail!(err); } + // Workaround for GTK weakness + if info.uri.ends_with(".webp") { + Command::new("dwebp") + .arg(crate::BACKGROUND_FILE.as_path()) + .arg("-o") + .arg(crate::BACKGROUND_PRIMARY_FILE.as_path()) + .spawn()? + .wait_with_output()?; + } + + else { + std::fs::copy(crate::BACKGROUND_FILE.as_path(), crate::BACKGROUND_PRIMARY_FILE.as_path())?; + } + Ok(()) } diff --git a/src/main.rs b/src/main.rs index 525fcdf..e03a48f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,6 +61,9 @@ lazy_static::lazy_static! { /// Path to `background` file. Standard is `$HOME/.local/share/anime-game-launcher/background` pub static ref BACKGROUND_FILE: PathBuf = LAUNCHER_FOLDER.join("background"); + /// Path to `background-primary` file. Standard is `$HOME/.local/share/anime-game-launcher/background-primary` + pub static ref BACKGROUND_PRIMARY_FILE: PathBuf = LAUNCHER_FOLDER.join("background-primary"); + /// Path to `.keep-background` file. Used to mark launcher that it shouldn't update background picture /// /// Standard is `$HOME/.local/share/anime-game-launcher/.keep-background` @@ -99,7 +102,7 @@ lazy_static::lazy_static! { .round-bin {{ border-radius: 24px; }} - ", BACKGROUND_FILE.to_string_lossy()); + ", BACKGROUND_PRIMARY_FILE.to_string_lossy()); } fn main() -> anyhow::Result<()> { diff --git a/src/ui/first_run/dependencies.rs b/src/ui/first_run/dependencies.rs index 0f5490e..0c80e90 100644 --- a/src/ui/first_run/dependencies.rs +++ b/src/ui/first_run/dependencies.rs @@ -68,7 +68,7 @@ impl SimpleAsyncComponent for DependenciesApp { }, gtk::Entry { - set_text: "sudo pacman -Syu git xdelta3 p7zip", + set_text: "sudo pacman -Syu git xdelta3 p7zip libwebp", set_editable: false } }, @@ -85,7 +85,7 @@ impl SimpleAsyncComponent for DependenciesApp { }, gtk::Entry { - set_text: "sudo apt install git xdelta3 p7zip-full", + set_text: "sudo apt install git xdelta3 p7zip-full webp", set_editable: false } }, @@ -102,7 +102,7 @@ impl SimpleAsyncComponent for DependenciesApp { }, gtk::Entry { - set_text: "sudo dnf install git xdelta p7zip", + set_text: "sudo dnf install git xdelta p7zip libwebp", set_editable: false } }, @@ -125,6 +125,10 @@ impl SimpleAsyncComponent for DependenciesApp { adw::ActionRow { set_title: "p7zip" + }, + + adw::ActionRow { + set_title: "libwebp" } } } @@ -191,7 +195,7 @@ impl SimpleAsyncComponent for DependenciesApp { match msg { #[allow(unused_must_use)] DependenciesAppMsg::Continue => { - let packages = ["git", "xdelta3"]; + let packages = ["git", "xdelta3", "dwebp"]; for package in packages { if !is_available(package) {