diff --git a/Windows/CS/Framework4.0/Updater/AppData/Updater/config.json b/Windows/CS/Framework4.0/Updater/AppData/Updater/config.json new file mode 100644 index 0000000..949fe2b --- /dev/null +++ b/Windows/CS/Framework4.0/Updater/AppData/Updater/config.json @@ -0,0 +1,3 @@ +{ + "debug_mode": false +} \ No newline at end of file diff --git a/Windows/CS/Framework4.0/Updater/src/main.rs b/Windows/CS/Framework4.0/Updater/src/main.rs index 5b6800c..8a6f805 100644 --- a/Windows/CS/Framework4.0/Updater/src/main.rs +++ b/Windows/CS/Framework4.0/Updater/src/main.rs @@ -1,6 +1,7 @@ use serde::{Deserialize, Serialize}; use std::fs; use std::path::PathBuf; +use tokio::signal; use tokio::time; #[derive(Debug, Serialize, Deserialize)] @@ -18,7 +19,12 @@ impl Default for Config { fn get_config_path() -> PathBuf { let exe_path = std::env::current_exe().expect("Failed to get executable path"); - let drive = exe_path.parent().unwrap().parent().unwrap(); + let drive = if let Some(parent) = exe_path.parent() { + let drive = parent.as_os_str().to_str().unwrap_or("C:/").split('\\').next().unwrap_or("C:"); + std::path::Path::new(&format!("{}/", drive)).to_path_buf() + } else { + std::path::Path::new("C:/").to_path_buf() + }; let appdata = drive.join("AppData").join("Updater"); fs::create_dir_all(&appdata).expect("Failed to create config directory"); appdata.join("config.json") @@ -27,37 +33,57 @@ fn get_config_path() -> PathBuf { fn load_config() -> Config { let config_path = get_config_path(); if config_path.exists() { - let content = fs::read_to_string(&config_path).expect("Failed to read config file"); - serde_json::from_str(&content).expect("Failed to parse config file") + match fs::read_to_string(&config_path) { + Ok(content) => { + match serde_json::from_str(&content) { + Ok(config) => config, + Err(_) => Config::default(), + } + } + Err(_) => Config::default(), + } } else { let default_config = Config::default(); - let content = serde_json::to_string_pretty(&default_config).expect("Failed to serialize config"); - fs::write(&config_path, content).expect("Failed to write config file"); - default_config + match serde_json::to_string_pretty(&default_config) { + Ok(content) => { + match fs::write(&config_path, content) { + Ok(_) => default_config, + Err(_) => default_config, + } + } + Err(_) => default_config, + } } } -async fn upgrade() { - println!("开始升级"); +async fn upgrade(debug_mode: bool) { + if debug_mode { + println!("开始升级"); + } } #[tokio::main] async fn main() { let config = load_config(); - if !config.debug_mode { - #[cfg(windows)] - { - use windows::Win32::System::Console; - unsafe { - let _ = Console::FreeConsole(); + if config.debug_mode { + let mut interval = time::interval(time::Duration::from_secs(300)); + loop { + tokio::select! { + _ = interval.tick() => { + upgrade(config.debug_mode).await; + } + _ = signal::ctrl_c() => { + println!("Received Ctrl+C, exiting..."); + break; + } } } - } - - let mut interval = time::interval(time::Duration::from_secs(300)); - loop { - interval.tick().await; - upgrade().await; + } else { + let mut interval = time::interval(time::Duration::from_secs(300)); + loop { + interval.tick().await; + upgrade(config.debug_mode).await; + } } }