[fix] steam id32 + avatar

This commit is contained in:
Purp1e
2025-03-23 22:41:19 +08:00
parent 45e4ab1c6a
commit 1a0477d6b9
6 changed files with 238 additions and 64 deletions

View File

@@ -6,6 +6,7 @@ use serde_json::Value;
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use tauri_plugin_http::reqwest::blocking::get;
use walkdir::WalkDir;
use crate::steam;
@@ -189,51 +190,32 @@ pub fn parse_local_users(steam_dir: &str) -> Result<Vec<LocalUser>> {
Ok(local_users)
}
fn read_avatar(steam_dir: &str, steam_id64: &str) -> Option<String> {
let t_path = Path::new(steam_dir).join(format!("avatarcache/{}.png", steam_id64));
if !t_path.exists() {
return None;
}
if let Ok(img) = fs::read(t_path) {
Some(STANDARD.encode(img))
} else {
None
}
}
pub fn get_users(steam_dir: &str) -> Result<Vec<User>> {
let login_users = parse_login_users(steam_dir)?;
let local_users = parse_local_users(steam_dir)?;
let users = merge_users(login_users, local_users);
Ok(users)
}
pub fn merge_users(login: Vec<LoginUser>, local: Vec<LocalUser>) -> Vec<User> {
let mut users = Vec::new();
for i in login {
let mut id32: u32 = 0;
let mut avatar = i.avatar;
let avatar: String;
let mut avatar_key = String::new();
// 匹配获取 avatar_key 在线获取头像使用
let t_usr: Vec<&LocalUser> = local
.iter()
.filter(|j| i.persona_name == j.persona_name)
.filter(|j| i.steam_id32 == j.steam_id32)
.collect();
if t_usr.len() > 1 {
id32 = steam::id::id64_to_32(i.steam_id64);
} else if t_usr.len() == 1 {
avatar_key = t_usr[0].avatar_key.clone();
if let Some(usr) = t_usr.first() {
avatar_key = usr.avatar_key.clone();
}
if avatar.is_empty() && !avatar_key.is_empty() {
// 获取头像的base64 本地头像文件不存在时使用在线api获取
if i.avatar.is_empty() && !avatar_key.is_empty() {
avatar = download_avatar(&avatar_key).unwrap_or_default();
} else {
avatar = i.avatar;
}
users.push(User {
steam_id64: i.steam_id64,
steam_id32: id32,
steam_id32: i.steam_id32,
account_name: i.account_name,
persona_name: i.persona_name,
recent: i.most_recent.parse().unwrap_or(0),
@@ -253,7 +235,48 @@ pub fn merge_users(login: Vec<LoginUser>, local: Vec<LocalUser>) -> Vec<User> {
users
}
fn download_avatar(avatar_key: &str) -> Result<String> {
// Implement avatar download logic here
Ok(String::new())
pub fn get_users(steam_dir: &str) -> Result<Vec<User>> {
let login_users = parse_login_users(steam_dir)?;
let local_users = parse_local_users(steam_dir)?;
let users = merge_users(login_users, local_users);
Ok(users)
}
fn download_avatar(avatar_key: &str) -> Result<String> {
// 下载并转换成Base64 "https://avatars.cloudflare.steamstatic.com/" + avatarKey + "_full.jpg"
let url = format!(
"https://avatars.cloudflare.steamstatic.com/{}_full.jpg",
avatar_key
);
if let Ok(resp) = get(&url) {
if let Ok(bytes) = resp.bytes() {
return Ok(STANDARD.encode(bytes));
}
}
return Err(anyhow::anyhow!("Failed to download avatar"));
}
fn read_avatar(steam_dir: &str, steam_id64: &str) -> Option<String> {
let t_path = Path::new(steam_dir).join(format!("config\\avatarcache\\{}.png", steam_id64));
if !t_path.exists() {
return None;
}
if let Ok(img) = fs::read(t_path) {
Some(STANDARD.encode(img))
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_users() {
let users = get_users("D:\\Programs\\Steam").unwrap();
println!("{:?}", users);
}
}