feat: added discord rpc icons support

This commit is contained in:
Observer KRypt0n_ 2023-06-09 22:11:29 +02:00
parent 6e7dcefd99
commit b744e5d595
No known key found for this signature in database
GPG key ID: 844DA47BA25FE1E2
5 changed files with 105 additions and 20 deletions

View file

@ -1,12 +1,31 @@
use std::thread::JoinHandle; use std::thread::JoinHandle;
use std::sync::mpsc::{self, Sender, SendError}; use std::sync::mpsc::{self, Sender, SendError};
use serde::{Serialize, Deserialize};
use anime_game_core::minreq;
use discord_rich_presence::{ use discord_rich_presence::{
activity::*, activity::*,
DiscordIpc, DiscordIpc,
DiscordIpcClient 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)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiscordRpcParams { pub struct DiscordRpcParams {
pub app_id: u64, pub app_id: u64,
@ -106,6 +125,20 @@ impl DiscordRpc {
pub fn update(&self, update: RpcUpdates) -> Result<(), SendError<RpcUpdates>> { pub fn update(&self, update: RpcUpdates) -> Result<(), SendError<RpcUpdates>> {
self.sender.send(update) self.sender.send(update)
} }
pub fn get_assets(app_id: u64) -> anyhow::Result<Vec<DiscordRpcAsset>> {
Ok(minreq::get(format!("https://discord.com/api/v9/oauth2/applications/{app_id}/assets"))
.send()?
.json::<Vec<serde_json::Value>>()?
.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 { impl Drop for DiscordRpc {

View file

@ -1,9 +1,10 @@
use std::path::PathBuf; use std::path::PathBuf;
pub const FOLDER_NAME: &str = "anime-game-launcher";
/// Get default launcher dir path /// 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` /// 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<PathBuf> { pub fn launcher_dir() -> anyhow::Result<PathBuf> {
if let Ok(folder) = std::env::var("LAUNCHER_FOLDER") { if let Ok(folder) = std::env::var("LAUNCHER_FOLDER") {
return Ok(folder.into()); return Ok(folder.into());
@ -11,13 +12,25 @@ pub fn launcher_dir() -> anyhow::Result<PathBuf> {
Ok(std::env::var("XDG_DATA_HOME") Ok(std::env::var("XDG_DATA_HOME")
.or_else(|_| std::env::var("HOME").map(|home| home + "/.local/share")) .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` /// If `CACHE_FOLDER` variable is set, then its value will be returned. Otherwise return `$HOME/.cache/anime-game-launcher`
#[inline] pub fn cache_dir() -> anyhow::Result<PathBuf> {
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<PathBuf> { pub fn config_file() -> anyhow::Result<PathBuf> {
launcher_dir().map(|dir| dir.join("config.json")) launcher_dir().map(|dir| dir.join("config.json"))
} }

View file

@ -1,9 +1,10 @@
use std::path::PathBuf; use std::path::PathBuf;
pub const FOLDER_NAME: &str = "honkers-launcher";
/// Get default launcher dir path /// Get default launcher dir path
/// ///
/// If `LAUNCHER_FOLDER` variable is set, then its value will be returned. Otherwise return `$HOME/.local/share/honkers-launcher` /// 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<PathBuf> { pub fn launcher_dir() -> anyhow::Result<PathBuf> {
if let Ok(folder) = std::env::var("LAUNCHER_FOLDER") { if let Ok(folder) = std::env::var("LAUNCHER_FOLDER") {
return Ok(folder.into()); return Ok(folder.into());
@ -11,13 +12,25 @@ pub fn launcher_dir() -> anyhow::Result<PathBuf> {
Ok(std::env::var("XDG_DATA_HOME") Ok(std::env::var("XDG_DATA_HOME")
.or_else(|_| std::env::var("HOME").map(|home| home + "/.local/share")) .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` /// If `CACHE_FOLDER` variable is set, then its value will be returned. Otherwise return `$HOME/.cache/honkers-launcher`
#[inline] pub fn cache_dir() -> anyhow::Result<PathBuf> {
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<PathBuf> { pub fn config_file() -> anyhow::Result<PathBuf> {
launcher_dir().map(|dir| dir.join("config.json")) launcher_dir().map(|dir| dir.join("config.json"))
} }

View file

@ -1,9 +1,10 @@
use std::path::PathBuf; use std::path::PathBuf;
pub const FOLDER_NAME: &str = "anime-borb-launcher";
/// Get default launcher dir path /// 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` /// 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<PathBuf> { pub fn launcher_dir() -> anyhow::Result<PathBuf> {
if let Ok(folder) = std::env::var("LAUNCHER_FOLDER") { if let Ok(folder) = std::env::var("LAUNCHER_FOLDER") {
return Ok(folder.into()); return Ok(folder.into());
@ -11,13 +12,25 @@ pub fn launcher_dir() -> anyhow::Result<PathBuf> {
Ok(std::env::var("XDG_DATA_HOME") Ok(std::env::var("XDG_DATA_HOME")
.or_else(|_| std::env::var("HOME").map(|home| home + "/.local/share")) .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` /// If `CACHE_FOLDER` variable is set, then its value will be returned. Otherwise return `$HOME/.cache/anime-borb-launcher`
#[inline] pub fn cache_dir() -> anyhow::Result<PathBuf> {
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<PathBuf> { pub fn config_file() -> anyhow::Result<PathBuf> {
launcher_dir().map(|dir| dir.join("config.json")) launcher_dir().map(|dir| dir.join("config.json"))
} }

View file

@ -1,9 +1,10 @@
use std::path::PathBuf; use std::path::PathBuf;
pub const FOLDER_NAME: &str = "honkers-railway-launcher";
/// Get default launcher dir path /// 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` /// 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<PathBuf> { pub fn launcher_dir() -> anyhow::Result<PathBuf> {
if let Ok(folder) = std::env::var("LAUNCHER_FOLDER") { if let Ok(folder) = std::env::var("LAUNCHER_FOLDER") {
return Ok(folder.into()); return Ok(folder.into());
@ -11,13 +12,25 @@ pub fn launcher_dir() -> anyhow::Result<PathBuf> {
Ok(std::env::var("XDG_DATA_HOME") Ok(std::env::var("XDG_DATA_HOME")
.or_else(|_| std::env::var("HOME").map(|home| home + "/.local/share")) .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` /// If `CACHE_FOLDER` variable is set, then its value will be returned. Otherwise return `$HOME/.cache/honkers-railway-launcher`
#[inline] pub fn cache_dir() -> anyhow::Result<PathBuf> {
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<PathBuf> { pub fn config_file() -> anyhow::Result<PathBuf> {
launcher_dir().map(|dir| dir.join("config.json")) launcher_dir().map(|dir| dir.join("config.json"))
} }