diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 825d511..82238fd 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1,11 +1,13 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "CS工具箱" version = "0.0.1" dependencies = [ + "anyhow", + "log", "serde", "serde_json", "tauri", @@ -85,9 +87,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.91" +version = "1.0.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c042108f3ed77fd83760a5fd79b53be043192bb3b9dba91d8c574c0ada7850c8" +checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f" [[package]] name = "arboard" @@ -2439,9 +2441,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.22" +version = "0.4.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e" [[package]] name = "mac" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 75d6e04..7b03650 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -7,7 +7,7 @@ license = "" repository = "" default-run = "CS工具箱" edition = "2021" -rust-version = "1.66" +rust-version = "1.85" [profile.release] panic = "abort" # Strip expensive panic clean-up logic @@ -21,6 +21,7 @@ strip = true # Remove debug symbols tauri-build = { version = "2.0.6", features = [] } [dependencies] +log = "0.4.26" serde_json = "1.0.140" serde = { version = "1.0.219", features = ["derive"] } tauri = { version = "2.3.1", features = [ "macos-private-api", @@ -41,6 +42,7 @@ tauri-plugin-system-info = "2.0.9" tauri-plugin-theme = "2.1.3" tauri-plugin-single-instance = { version = "2.0.0", features = ["deep-link"] } tauri-plugin-deep-link = "2.0.0" +anyhow = "1.0.97" [target.'cfg(windows)'.dependencies] # Windows Only winreg = "0.55.0" diff --git a/src-tauri/src/cmds.rs b/src-tauri/src/cmds.rs index ce639d6..3df0113 100644 --- a/src-tauri/src/cmds.rs +++ b/src-tauri/src/cmds.rs @@ -1,39 +1,63 @@ use crate::steam; use crate::tool::*; +use crate::wrap_err; +use anyhow::Result; + +// pub type Result = Result; #[tauri::command] -pub fn greet(name: &str) -> String { - format!("Hello, {}! You've been greeted from Rust!", name) +pub fn greet(name: &str) -> Result { + 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 { + 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 { + Ok(common::kill("cs2.exe")) } #[tauri::command] -pub fn kill_steam() -> String { - common::kill("steam.exe") +pub fn kill_steam() -> Result { + 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 { + wrap_err!(steam::path::get_steam_path()) } +#[tauri::command] +pub fn get_cs_path(name: &str) -> Result { + wrap_err!(steam::path::get_cs_path(name)) +} + +#[tauri::command] +pub fn get_powerplan() -> Result { + #[cfg(target_os = "windows")] + let powerplan = powerplan::get_powerplan()?; + + Ok(powerplan) +} + +#[tauri::command] +pub fn set_powerplan(plan: &str) -> Result { + #[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 { #[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 { + Ok(std::path::Path::new(&path).exists()) } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 54dc6e3..b016a65 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -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 diff --git a/src-tauri/src/steam/mod.rs b/src-tauri/src/steam/mod.rs index f7a9f81..0849532 100644 --- a/src-tauri/src/steam/mod.rs +++ b/src-tauri/src/steam/mod.rs @@ -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}; diff --git a/src-tauri/src/steam/path.rs b/src-tauri/src/steam/path.rs index 36e3b92..f666caf 100644 --- a/src-tauri/src/steam/path.rs +++ b/src-tauri/src/steam/path.rs @@ -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 { +pub fn get_steam_path() -> Result { // 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 { return Ok(steam_path); } - Err("no steam path found") + Err("no steam path found".into()) } -pub fn get_cs_path<'a>(name: &str) -> Result { - if name != "csgo" || name != "cs2" { - return Err("invalid cs name"); +pub fn get_cs_path(name: &str) -> Result { + 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 { return Ok(cs_path); } - Err("no cs path found") + Err("no cs path found".into()) } #[cfg(test)] diff --git a/src-tauri/src/steam/reg.rs b/src-tauri/src/steam/reg.rs index ac36165..4269629 100644 --- a/src-tauri/src/steam/reg.rs +++ b/src-tauri/src/steam/reg.rs @@ -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::>(); + // let users = cur_ver + // .open_subkey("Users") + // .expect("users") + // .enum_keys() + // .map(|x| x.unwrap().to_string()) + // .collect::>(); Ok(Self { steam_path, @@ -52,7 +52,7 @@ impl SteamReg { suppress_auto_run, remember_password, language, - users: users, + users: vec![], }) } } diff --git a/src-tauri/src/tool/macros.rs b/src-tauri/src/tool/macros.rs index b6b7e28..a017980 100644 --- a/src-tauri/src/tool/macros.rs +++ b/src-tauri/src/tool/macros.rs @@ -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) } } }; -} +} \ No newline at end of file diff --git a/src-tauri/src/tool/mod.rs b/src-tauri/src/tool/mod.rs index edeeeb5..5cdeca4 100644 --- a/src-tauri/src/tool/mod.rs +++ b/src-tauri/src/tool/mod.rs @@ -1,2 +1,3 @@ pub mod common; pub mod macros; +pub mod powerplan; \ No newline at end of file diff --git a/src-tauri/src/tool/powerplan.rs b/src-tauri/src/tool/powerplan.rs new file mode 100644 index 0000000..1296b62 --- /dev/null +++ b/src-tauri/src/tool/powerplan.rs @@ -0,0 +1,8 @@ +pub fn get_powerplan<'a>() -> Result { + Ok(String::from("")) +} + +pub fn set_powerplan<'a>(plan: &str) -> Result { + println!("{}", plan); + Ok(String::from("")) +} diff --git a/src/components/cstb/Prepare.tsx b/src/components/cstb/Prepare.tsx index ecaeb5e..b9c0a48 100644 --- a/src/components/cstb/Prepare.tsx +++ b/src/components/cstb/Prepare.tsx @@ -85,6 +85,22 @@ export function Prepare() { } } + const autoGetPaths = async () => { + try { + const steam_path = await invoke("get_steam_path") + if (steam_path) steam.setDir(steam_path) + } catch (e) { + addToast({ title: "自动获取Steam路径失败", color: "danger" }) + } + + try { + const cs2_path = await invoke("get_cs_path", { name: "cs2" }) + if (cs2_path) steam.setCsDir(cs2_path.replace(/\\[^\\]+$/, "")) + } catch (e) { + addToast({ title: "自动获取CS2路径失败", color: "danger" }) + } + } + const handleSelectCs2Dir = async () => { const selected = await open({ title: "选择 CS2.exe 文件", @@ -147,7 +163,7 @@ export function Prepare() { {links.length > 0 &&

{links}

}
- + {pathname !== "/" && ( + + + {(onClose) => ( + <> + 重置设置 + +

+ 确认后会恢复CS工具箱的偏好设置为默认设置 +

+
+ + + + + + )} +
+
+ + ) +} + export default Nav