diff --git a/src/discord_rpc.rs b/src/discord_rpc.rs index 0d7416c..fe134c0 100644 --- a/src/discord_rpc.rs +++ b/src/discord_rpc.rs @@ -1,12 +1,31 @@ use std::thread::JoinHandle; use std::sync::mpsc::{self, Sender, SendError}; +use serde::{Serialize, Deserialize}; + +use anime_game_core::minreq; + use discord_rich_presence::{ activity::*, DiscordIpc, DiscordIpcClient }; +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct DiscordRpcAsset { + pub app_id: u64, + pub id: String, + pub r#type: u64, + pub name: String +} + +impl DiscordRpcAsset { + #[inline] + pub fn get_uri(&self) -> String { + format!("https://cdn.discordapp.com/app-assets/{}/{}.png", self.app_id, self.id) + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct DiscordRpcParams { pub app_id: u64, @@ -106,6 +125,20 @@ impl DiscordRpc { pub fn update(&self, update: RpcUpdates) -> Result<(), SendError> { self.sender.send(update) } + + pub fn get_assets(app_id: u64) -> anyhow::Result> { + Ok(minreq::get(format!("https://discord.com/api/v9/oauth2/applications/{app_id}/assets")) + .send()? + .json::>()? + .into_iter() + .map(|value| DiscordRpcAsset { + app_id, + id: value["id"].as_str().unwrap().to_string(), + r#type: value["type"].as_u64().unwrap(), + name: value["name"].as_str().unwrap().to_string() + }) + .collect()) + } } impl Drop for DiscordRpc { diff --git a/src/games/genshin/consts.rs b/src/games/genshin/consts.rs index 91117e7..9425cbe 100644 --- a/src/games/genshin/consts.rs +++ b/src/games/genshin/consts.rs @@ -1,9 +1,10 @@ use std::path::PathBuf; +pub const FOLDER_NAME: &str = "anime-game-launcher"; + /// Get default launcher dir path /// /// If `LAUNCHER_FOLDER` variable is set, then its value will be returned. Otherwise return `$HOME/.local/share/anime-game-launcher` -#[inline] pub fn launcher_dir() -> anyhow::Result { if let Ok(folder) = std::env::var("LAUNCHER_FOLDER") { return Ok(folder.into()); @@ -11,13 +12,25 @@ pub fn launcher_dir() -> anyhow::Result { Ok(std::env::var("XDG_DATA_HOME") .or_else(|_| std::env::var("HOME").map(|home| home + "/.local/share")) - .map(|home| PathBuf::from(home).join("anime-game-launcher"))?) + .map(|home| PathBuf::from(home).join(FOLDER_NAME))?) } -/// Get default config file path +/// Get launcher's cache dir path /// -/// `$HOME/.local/share/anime-game-launcher/config.json` -#[inline] +/// If `CACHE_FOLDER` variable is set, then its value will be returned. Otherwise return `$HOME/.cache/anime-game-launcher` +pub fn cache_dir() -> anyhow::Result { + if let Ok(folder) = std::env::var("CACHE_FOLDER") { + return Ok(folder.into()); + } + + Ok(std::env::var("XDG_CACHE_HOME") + .or_else(|_| std::env::var("HOME").map(|home| home + "/.cache")) + .map(|home| PathBuf::from(home).join(FOLDER_NAME))?) +} + +/// Get config file path +/// +/// Default is `$HOME/.local/share/anime-game-launcher/config.json` pub fn config_file() -> anyhow::Result { launcher_dir().map(|dir| dir.join("config.json")) } diff --git a/src/games/honkai/consts.rs b/src/games/honkai/consts.rs index a68c0e5..fd67495 100644 --- a/src/games/honkai/consts.rs +++ b/src/games/honkai/consts.rs @@ -1,9 +1,10 @@ use std::path::PathBuf; +pub const FOLDER_NAME: &str = "honkers-launcher"; + /// Get default launcher dir path /// /// If `LAUNCHER_FOLDER` variable is set, then its value will be returned. Otherwise return `$HOME/.local/share/honkers-launcher` -#[inline] pub fn launcher_dir() -> anyhow::Result { if let Ok(folder) = std::env::var("LAUNCHER_FOLDER") { return Ok(folder.into()); @@ -11,13 +12,25 @@ pub fn launcher_dir() -> anyhow::Result { Ok(std::env::var("XDG_DATA_HOME") .or_else(|_| std::env::var("HOME").map(|home| home + "/.local/share")) - .map(|home| PathBuf::from(home).join("honkers-launcher"))?) + .map(|home| PathBuf::from(home).join(FOLDER_NAME))?) } -/// Get default config file path +/// Get launcher's cache dir path /// -/// `$HOME/.local/share/honkers-launcher/config.json` -#[inline] +/// If `CACHE_FOLDER` variable is set, then its value will be returned. Otherwise return `$HOME/.cache/honkers-launcher` +pub fn cache_dir() -> anyhow::Result { + if let Ok(folder) = std::env::var("CACHE_FOLDER") { + return Ok(folder.into()); + } + + Ok(std::env::var("XDG_CACHE_HOME") + .or_else(|_| std::env::var("HOME").map(|home| home + "/.cache")) + .map(|home| PathBuf::from(home).join(FOLDER_NAME))?) +} + +/// Get config file path +/// +/// Default is `$HOME/.local/share/honkers-launcher/config.json` pub fn config_file() -> anyhow::Result { launcher_dir().map(|dir| dir.join("config.json")) } diff --git a/src/games/pgr/consts.rs b/src/games/pgr/consts.rs index 7f88432..f2bc882 100644 --- a/src/games/pgr/consts.rs +++ b/src/games/pgr/consts.rs @@ -1,9 +1,10 @@ use std::path::PathBuf; +pub const FOLDER_NAME: &str = "anime-borb-launcher"; + /// Get default launcher dir path /// /// If `LAUNCHER_FOLDER` variable is set, then its value will be returned. Otherwise return `$HOME/.local/share/anime-borb-launcher` -#[inline] pub fn launcher_dir() -> anyhow::Result { if let Ok(folder) = std::env::var("LAUNCHER_FOLDER") { return Ok(folder.into()); @@ -11,13 +12,25 @@ pub fn launcher_dir() -> anyhow::Result { Ok(std::env::var("XDG_DATA_HOME") .or_else(|_| std::env::var("HOME").map(|home| home + "/.local/share")) - .map(|home| PathBuf::from(home).join("anime-borb-launcher"))?) + .map(|home| PathBuf::from(home).join(FOLDER_NAME))?) } -/// Get default config file path +/// Get launcher's cache dir path /// -/// `$HOME/.local/share/anime-borb-launcher/config.json` -#[inline] +/// If `CACHE_FOLDER` variable is set, then its value will be returned. Otherwise return `$HOME/.cache/anime-borb-launcher` +pub fn cache_dir() -> anyhow::Result { + if let Ok(folder) = std::env::var("CACHE_FOLDER") { + return Ok(folder.into()); + } + + Ok(std::env::var("XDG_CACHE_HOME") + .or_else(|_| std::env::var("HOME").map(|home| home + "/.cache")) + .map(|home| PathBuf::from(home).join(FOLDER_NAME))?) +} + +/// Get config file path +/// +/// Default is `$HOME/.local/share/anime-borb-launcher/config.json` pub fn config_file() -> anyhow::Result { launcher_dir().map(|dir| dir.join("config.json")) } diff --git a/src/games/star_rail/consts.rs b/src/games/star_rail/consts.rs index e9bcfaa..22ee3a9 100644 --- a/src/games/star_rail/consts.rs +++ b/src/games/star_rail/consts.rs @@ -1,9 +1,10 @@ use std::path::PathBuf; +pub const FOLDER_NAME: &str = "honkers-railway-launcher"; + /// Get default launcher dir path /// /// If `LAUNCHER_FOLDER` variable is set, then its value will be returned. Otherwise return `$HOME/.local/share/honkers-railway-launcher` -#[inline] pub fn launcher_dir() -> anyhow::Result { if let Ok(folder) = std::env::var("LAUNCHER_FOLDER") { return Ok(folder.into()); @@ -11,13 +12,25 @@ pub fn launcher_dir() -> anyhow::Result { Ok(std::env::var("XDG_DATA_HOME") .or_else(|_| std::env::var("HOME").map(|home| home + "/.local/share")) - .map(|home| PathBuf::from(home).join("honkers-railway-launcher"))?) + .map(|home| PathBuf::from(home).join(FOLDER_NAME))?) } -/// Get default config file path +/// Get launcher's cache dir path /// -/// `$HOME/.local/share/honkers-railway-launcher/config.json` -#[inline] +/// If `CACHE_FOLDER` variable is set, then its value will be returned. Otherwise return `$HOME/.cache/honkers-railway-launcher` +pub fn cache_dir() -> anyhow::Result { + if let Ok(folder) = std::env::var("CACHE_FOLDER") { + return Ok(folder.into()); + } + + Ok(std::env::var("XDG_CACHE_HOME") + .or_else(|_| std::env::var("HOME").map(|home| home + "/.cache")) + .map(|home| PathBuf::from(home).join(FOLDER_NAME))?) +} + +/// Get config file path +/// +/// Default is `$HOME/.local/share/honkers-railway-launcher/config.json` pub fn config_file() -> anyhow::Result { launcher_dir().map(|dir| dir.join("config.json")) }