feat: updated background images support

thanks gtk devs for providing webp support :thumbup: (no)
This commit is contained in:
Nikita Podvirnyi 2024-06-17 12:38:12 +02:00
parent d991f5aaf7
commit d0f731d68f
No known key found for this signature in database
GPG key ID: 859D416E5142AFF3
4 changed files with 40 additions and 16 deletions

View file

@ -25,6 +25,7 @@ in pkgs.mkShell {
pkg-config
xdelta
libwebp
];
buildInputs = with pkgs; [

View file

@ -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<Background> {
let json = serde_json::from_slice::<serde_json::Value>(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(())
}

View file

@ -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<()> {

View file

@ -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) {