[feat] reset modal + basic auto get path + basic wrap err try

This commit is contained in:
Purp1e
2025-03-20 14:13:03 +08:00
parent 60782669ea
commit a9a48d2aba
12 changed files with 163 additions and 62 deletions

View File

@@ -1,39 +1,63 @@
use crate::steam;
use crate::tool::*;
use crate::wrap_err;
use anyhow::Result;
// pub type Result<T, String = ()> = Result<T, String>;
#[tauri::command]
pub fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
pub fn greet(name: &str) -> Result<String, String> {
Ok(format!("Hello, {}! You've been greeted from Rust!", name))
}
// 工具
#[tauri::command]
pub fn launch_game(steam_path: &str, launch_option: &str, server: &str) -> String {
steam::launch_game(steam_path, launch_option, server).expect("启动失败");
pub fn launch_game(steam_path: &str, launch_option: &str, server: &str) -> Result<String, String> {
wrap_err!(steam::launch_game(steam_path, launch_option, server));
format!(
Ok(format!(
"Launching game on server: {}, with launch Option {}",
server, launch_option
)
))
}
#[tauri::command]
pub fn kill_game() -> String {
common::kill("cs2.exe")
pub fn kill_game() -> Result<String, String> {
Ok(common::kill("cs2.exe"))
}
#[tauri::command]
pub fn kill_steam() -> String {
common::kill("steam.exe")
pub fn kill_steam() -> Result<String, String> {
Ok(common::kill("steam.exe"))
}
// Steam
#[tauri::command]
pub fn get_steam_path() -> String {
steam::path::get_steam_path().expect("获取Steam路径失败")
pub fn get_steam_path() -> Result<String, String> {
wrap_err!(steam::path::get_steam_path())
}
#[tauri::command]
pub fn get_cs_path(name: &str) -> Result<String, String> {
wrap_err!(steam::path::get_cs_path(name))
}
#[tauri::command]
pub fn get_powerplan() -> Result<String, String> {
#[cfg(target_os = "windows")]
let powerplan = powerplan::get_powerplan()?;
Ok(powerplan)
}
#[tauri::command]
pub fn set_powerplan(plan: &str) -> Result<String, String> {
#[cfg(target_os = "windows")]
powerplan::set_powerplan(plan)?;
Ok(format!("Set powerplan to {}", plan))
}
// TODO get_cs_path
// TODO get_powerplan
// TODO set_powerplan
@@ -44,14 +68,14 @@ pub fn get_steam_path() -> String {
// TODO fs_watch_dir
#[tauri::command]
pub fn set_auto_login_user(user: &str) -> String {
pub fn set_auto_login_user(user: &str) -> Result<String, String> {
#[cfg(target_os = "windows")]
steam::reg::set_auto_login_user(user).expect("设置自动登录用户失败");
steam::reg::set_auto_login_user(user)?;
format!("Set auto login user to {}", user)
Ok(format!("Set auto login user to {}", user))
}
#[tauri::command]
pub fn check_path(path: &str) -> bool {
std::path::Path::new(&path).exists()
pub fn check_path(path: &str) -> Result<bool, String> {
Ok(std::path::Path::new(&path).exists())
}

View File

@@ -92,6 +92,9 @@ fn main() {
cmds::kill_game,
cmds::kill_steam,
cmds::get_steam_path,
cmds::get_cs_path,
cmds::get_powerplan,
cmds::set_powerplan,
cmds::set_auto_login_user,
cmds::check_path,
on_button_clicked

View File

@@ -4,6 +4,7 @@ pub mod path;
pub mod reg;
// common steam utils
use anyhow::Result;
use std::error::Error;
use std::process::{Command, Output};

View File

@@ -3,11 +3,12 @@
// - CS2(CS:GO) Path
#![allow(unused)]
use anyhow::Result;
use crate::tool::common::get_exe_path;
use super::reg;
pub fn get_steam_path<'a>() -> Result<String, &'a str> {
pub fn get_steam_path() -> Result<String, String> {
// Windows Registry
#[cfg(target_os = "windows")]
if let Ok(reg) = reg::SteamReg::get_all() {
@@ -20,12 +21,12 @@ pub fn get_steam_path<'a>() -> Result<String, &'a str> {
return Ok(steam_path);
}
Err("no steam path found")
Err("no steam path found".into())
}
pub fn get_cs_path<'a>(name: &str) -> Result<String, &'a str> {
if name != "csgo" || name != "cs2" {
return Err("invalid cs name");
pub fn get_cs_path(name: &str) -> Result<String, String> {
if name != "csgo" && name != "cs2" {
return Err("invalid cs name".into());
}
#[cfg(target_os = "windows")]
@@ -33,7 +34,7 @@ pub fn get_cs_path<'a>(name: &str) -> Result<String, &'a str> {
return Ok(cs_path);
}
Err("no cs path found")
Err("no cs path found".into())
}
#[cfg(test)]

View File

@@ -29,7 +29,7 @@ impl SteamReg {
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let cur_ver = hkcu.open_subkey(STEAM_REG).expect("steam reg");
let steam_path: String = cur_ver.get_value("SteamExe").expect("steam path");
let steam_path: String = cur_ver.get_value("SteamPath").expect("steam path");
let auto_login_user: String = cur_ver.get_value("AutoLoginUser").expect("auto login user");
let suppress_auto_run: u32 = cur_ver
.get_value("SuppressAutoRun")
@@ -39,12 +39,12 @@ impl SteamReg {
.expect("remember password");
let language: String = cur_ver.get_value("Language").expect("language");
let users = cur_ver
.open_subkey("Users")
.expect("users")
.enum_keys()
.map(|x| x.unwrap().to_string())
.collect::<Vec<String>>();
// let users = cur_ver
// .open_subkey("Users")
// .expect("users")
// .enum_keys()
// .map(|x| x.unwrap().to_string())
// .collect::<Vec<String>>();
Ok(Self {
steam_path,
@@ -52,7 +52,7 @@ impl SteamReg {
suppress_auto_run,
remember_password,
language,
users: users,
users: vec![],
})
}
}

View File

@@ -36,9 +36,10 @@ macro_rules! wrap_err {
match $stat {
Ok(a) => Ok(a),
Err(err) => {
log::error!(target: "app", "{}", err.to_string());
Err(format!("{}", err.to_string()))
let err_str = err.to_string();
log::error!(target: "app", "{}", err_str);
Err(err_str)
}
}
};
}
}

View File

@@ -1,2 +1,3 @@
pub mod common;
pub mod macros;
pub mod powerplan;

View File

@@ -0,0 +1,8 @@
pub fn get_powerplan<'a>() -> Result<String, &'a str> {
Ok(String::from(""))
}
pub fn set_powerplan<'a>(plan: &str) -> Result<String, &'a str> {
println!("{}", plan);
Ok(String::from(""))
}