41 lines
1.7 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! 小壹的启动程序。桌面和开始菜单的快捷方式指向它:按 current.txt 打开 versions 下当前版本的
//! XiaoYi.exe;带 --previous 时按 previous.txt 打开上一版。在线更新只改这两个指针,快捷方式不用变。
#![windows_subsystem = "windows"]
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
use std::path::PathBuf;
use std::process::Command;
#[link(name = "user32")]
extern "system" {
fn MessageBoxW(window: *mut std::ffi::c_void, text: *const u16, caption: *const u16, kind: u32) -> i32;
}
fn wide(text: &str) -> Vec<u16> {
OsStr::new(text).encode_wide().chain(Some(0)).collect()
}
fn fail(text: &str) -> ! {
unsafe { MessageBoxW(std::ptr::null_mut(), wide(text).as_ptr(), wide("小壹").as_ptr(), 0x10) };
std::process::exit(1)
}
fn main() {
let root = std::env::current_exe().ok().and_then(|exe| exe.parent().map(PathBuf::from))
.unwrap_or_else(|| fail("无法确定小壹的安装位置,请重新安装小壹。"));
let mut args: Vec<_> = std::env::args_os().skip(1).collect();
let previous = args.first().is_some_and(|arg| arg == "--previous");
if previous {
args.remove(0);
}
let version = std::fs::read_to_string(root.join(if previous { "previous.txt" } else { "current.txt" })).unwrap_or_default();
let program = root.join("versions").join(version.trim()).join("XiaoYi.exe");
if version.trim().is_empty() || !program.is_file() {
fail(if previous { "没有可以打开的上一版小壹。" } else { "小壹的程序文件不完整,请重新安装小壹。" });
}
if Command::new(&program).args(args).current_dir(program.parent().unwrap()).spawn().is_err() {
fail("小壹没能启动,请重新安装小壹。");
}
}