40 lines
1.2 KiB
Rust
40 lines
1.2 KiB
Rust
//! 主程序启动时检测是否由 updater 拉起,否则转交 updater.exe。
|
|
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::Command;
|
|
|
|
pub const ENV_FROM_UPDATER: &str = "AICLIENT_FROM_UPDATER";
|
|
pub const MAIN_EXE: &str = "aiclient.exe";
|
|
pub const UPDATER_EXE: &str = "updater.exe";
|
|
|
|
pub fn launched_by_updater() -> bool {
|
|
std::env::var(ENV_FROM_UPDATER)
|
|
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
|
|
.unwrap_or(false)
|
|
}
|
|
|
|
pub fn exe_dir() -> Result<PathBuf, String> {
|
|
std::env::current_exe()
|
|
.map_err(|e| format!("无法获取当前可执行文件路径: {e}"))
|
|
.and_then(|p| {
|
|
p.parent()
|
|
.map(Path::to_path_buf)
|
|
.ok_or_else(|| "无法获取可执行文件所在目录".to_string())
|
|
})
|
|
}
|
|
|
|
/// 主程序未由 updater 启动时,尝试拉起 updater。
|
|
pub fn try_launch_updater() -> Result<(), String> {
|
|
let dir = exe_dir()?;
|
|
let updater_path = dir.join(UPDATER_EXE);
|
|
if !updater_path.is_file() {
|
|
return Err(format!("未找到 {}", updater_path.display()));
|
|
}
|
|
|
|
Command::new(&updater_path)
|
|
.current_dir(&dir)
|
|
.spawn()
|
|
.map_err(|e| format!("spawn {} 失败: {e}", updater_path.display()))?;
|
|
Ok(())
|
|
}
|