Files
cstb-next/src-tauri/src/tool/common.rs

108 lines
2.9 KiB
Rust
Raw Normal View History

use std::fs;
use std::process::Command;
2025-03-27 11:30:03 +08:00
#[cfg(windows)]
use std::os::windows::process::CommandExt;
const CREATE_NO_WINDOW: u32 = 0x08000000;
// const DETACHED_PROCESS: u32 = 0x00000008;
2024-09-20 09:52:13 +08:00
pub fn kill(name: &str) -> String {
2025-03-27 11:30:03 +08:00
#[cfg(windows)]
2024-09-20 09:52:13 +08:00
Command::new("taskkill")
.args(&["/IM", name, "/F"])
2025-03-22 01:16:39 +08:00
.creation_flags(CREATE_NO_WINDOW)
2024-09-20 09:52:13 +08:00
.output()
.unwrap();
format!("Killing {}", name)
}
pub fn run_steam() -> std::io::Result<std::process::Output> {
2025-03-27 11:30:03 +08:00
#[cfg(target_os = "windows")]
2025-03-27 13:32:30 +08:00
return Command::new("cmd")
2024-09-20 09:52:13 +08:00
.args(&["/C", "start", "steam://run"])
2025-03-22 01:16:39 +08:00
.creation_flags(CREATE_NO_WINDOW)
2025-03-27 11:30:03 +08:00
.output();
#[cfg(target_os = "macos")]
Command::new("open").args(&["-a", "Steam"]).output()
2024-09-20 09:52:13 +08:00
}
pub fn get_exe_path(name: &str) -> Result<String, std::io::Error> {
// [原理]
// Powershell 运行 Get-Process name | Select-Object path
// 有name.exe运行时返回
// Path
// ----
// 进程路径
let command = format!("Get-Process {} | Select-Object path", name);
2024-09-20 09:52:13 +08:00
let args = command.split_whitespace().collect::<Vec<&str>>();
2025-03-27 11:30:03 +08:00
#[cfg(windows)]
let output = Command::new("powershell.exe")
.args(&args)
.creation_flags(CREATE_NO_WINDOW)
.output()?;
2024-09-20 09:52:13 +08:00
#[cfg(target_os = "macos")]
let output = Command::new("osascript")
.args(&[
"-e",
&format!("tell application \"{}\" to get path to me", name),
])
.output()?;
2025-03-27 11:30:03 +08:00
let out = String::from_utf8_lossy(&output.stdout).to_string();
2024-09-20 09:52:13 +08:00
if out.contains("Path") {
let out = out.trim();
2024-09-20 09:52:13 +08:00
let spt: Vec<&str> = out.split("\r\n").collect();
if spt.len() > 2 {
return Ok(spt[2].to_string());
2024-09-20 09:52:13 +08:00
}
}
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"Failed to get executable path",
))
}
pub fn open_path(path: &str) -> Result<(), std::io::Error> {
// path中所有/ 转换为 \
let path = path.replace("/", "\\");
2025-11-04 23:33:36 +08:00
fs::create_dir_all(&path)?;
2025-03-27 11:30:03 +08:00
#[cfg(windows)]
Command::new("cmd.exe")
.args(["/c", "start", "", &path])
.creation_flags(CREATE_NO_WINDOW)
.spawn()
.unwrap();
Ok(())
}
mod tests {
#[test]
fn test_open_path() {
let path = "D:\\Programs\\Steam";
println!("test open path: {}", path);
2025-11-04 23:33:36 +08:00
super::open_path(path).unwrap();
let path = "D:\\Programs\\Steam\\steamapps\\common\\Counter-Strike Global Offensive\\game\\bin\\win64";
println!("test open path: {}", path);
2025-11-04 23:33:36 +08:00
super::open_path(path).unwrap();
let path = "%appdata%/Wmpvp/demo";
println!("test open path: {}", path);
2025-11-04 23:33:36 +08:00
super::open_path(path).unwrap()
}
#[test]
fn test_get_exe_path() {
2025-11-04 23:33:36 +08:00
let path = super::get_exe_path("steam").expect("failed");
println!("test get steam path: {}", path);
2025-11-04 23:33:36 +08:00
super::get_exe_path("not_running").expect("failed");
}
}