1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use sysinfo::{System,SystemExt,ProcessExt};
use std::fs;
use std::process::Command;
use std::time::Duration;
use tokio::time::sleep;

#[macro_use]
extern crate wei_log;

// #[tokio::main]
// async fn main() -> std::io::Result<()> {
//     use single_instance::SingleInstance;
//     let instance = SingleInstance::new("daemon").unwrap();
//     if !instance.is_single() { 
//         info!(" 已经存在相同的应用程序,请检查系统托盘。");
//         tokio::time::sleep(Duration::from_secs(10)).await;
//         std::process::exit(1);
//     };

//     if let Err(e) = check_and_start().await { 
//         error!("{}", e); 
//     }
//     Ok(())
// }

pub async fn check_and_start() -> Result<(), Box<dyn std::error::Error>> {
    //如果是windows 系统就隐藏控制台
    #[cfg(target_os = "windows")]
    hide()?;

    // 设定目录和文件路径
    let dir = dirs::data_local_dir().ok_or("failed data_local_dir")?.join("Ai");
    let file_path = dir.join("start.dat");
    let exe_path = dir.join("ai-x86_64-pc-windows-msvc.exe");

    loop {
        // 读取文件内容
        let content = fs::read_to_string(&file_path)?;

        info!("正在检查是否开启应用程序...");
        info!("当前状态为:{}",content.trim());
        info!("是否存在进程ai.exe:{}",is_process_running("ai-x86_64-pc-windows-msvc.exe"));

        if content.trim() == "1"
        && !is_process_running("ai-x86_64-pc-windows-msvc.exe") {
            info!("检测ai.exe路径是否存在:{}", exe_path.exists());
            // 检查ai.exe是否存在
            if exe_path.exists() {
                // 启动ai.exe
                Command::new("powershell")
                .args(&["/C", "start", &format!("\"{}\"", exe_path.display())])
                .spawn()?;

                info!("{}", exe_path.to_string_lossy());
            }
        }

        if content.trim() == "0" {
            break;
        }
        
        sleep(Duration::from_secs(30)).await;
    }
    Ok(())
}

pub fn is_process_running(process_name: &str) -> bool {
    let mut sys = System::new_all();
    sys.refresh_all();
    let processes = sys.processes();

    for (_pid, proc) in processes {
        if proc.name().to_lowercase() == process_name.to_lowercase() {
            return true;
        }
    }

    false
}

#[cfg(target_os = "windows")]
use std::ptr;
#[cfg(target_os = "windows")]
use winapi::um::wincon::GetConsoleWindow;
#[cfg(target_os = "windows")]
use winapi::um::winuser::{ShowWindow, SW_HIDE};

#[cfg(target_os = "windows")]
pub fn hide() -> Result<(), Box<dyn std::error::Error>> {
    if !is_debug()? {
        let window = unsafe { winapi::um::wincon::GetConsoleWindow() };
        if window != ptr::null_mut() {
            unsafe {
                ShowWindow(window, SW_HIDE);
            }
        }
    }
    Ok(())
}

pub fn is_debug() -> Result<bool, Box<dyn std::error::Error>> {
    let home_dir = std::env::var("USERPROFILE")?;
    if std::path::Path::new(&home_dir).join("AppData\\Local\\Ai\\debug.dat").exists() {
        return Ok(true);
    }

    return Ok(false);
}