创建配置
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"debug_mode": false
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use tokio::signal;
|
||||||
use tokio::time;
|
use tokio::time;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
@@ -18,7 +19,12 @@ impl Default for Config {
|
|||||||
|
|
||||||
fn get_config_path() -> PathBuf {
|
fn get_config_path() -> PathBuf {
|
||||||
let exe_path = std::env::current_exe().expect("Failed to get executable path");
|
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");
|
let appdata = drive.join("AppData").join("Updater");
|
||||||
fs::create_dir_all(&appdata).expect("Failed to create config directory");
|
fs::create_dir_all(&appdata).expect("Failed to create config directory");
|
||||||
appdata.join("config.json")
|
appdata.join("config.json")
|
||||||
@@ -27,37 +33,57 @@ fn get_config_path() -> PathBuf {
|
|||||||
fn load_config() -> Config {
|
fn load_config() -> Config {
|
||||||
let config_path = get_config_path();
|
let config_path = get_config_path();
|
||||||
if config_path.exists() {
|
if config_path.exists() {
|
||||||
let content = fs::read_to_string(&config_path).expect("Failed to read config file");
|
match fs::read_to_string(&config_path) {
|
||||||
serde_json::from_str(&content).expect("Failed to parse config file")
|
Ok(content) => {
|
||||||
|
match serde_json::from_str(&content) {
|
||||||
|
Ok(config) => config,
|
||||||
|
Err(_) => Config::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => Config::default(),
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let default_config = Config::default();
|
let default_config = Config::default();
|
||||||
let content = serde_json::to_string_pretty(&default_config).expect("Failed to serialize config");
|
match serde_json::to_string_pretty(&default_config) {
|
||||||
fs::write(&config_path, content).expect("Failed to write config file");
|
Ok(content) => {
|
||||||
default_config
|
match fs::write(&config_path, content) {
|
||||||
|
Ok(_) => default_config,
|
||||||
|
Err(_) => default_config,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => default_config,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn upgrade() {
|
async fn upgrade(debug_mode: bool) {
|
||||||
println!("开始升级");
|
if debug_mode {
|
||||||
|
println!("开始升级");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let config = load_config();
|
let config = load_config();
|
||||||
|
|
||||||
if !config.debug_mode {
|
if config.debug_mode {
|
||||||
#[cfg(windows)]
|
let mut interval = time::interval(time::Duration::from_secs(300));
|
||||||
{
|
loop {
|
||||||
use windows::Win32::System::Console;
|
tokio::select! {
|
||||||
unsafe {
|
_ = interval.tick() => {
|
||||||
let _ = Console::FreeConsole();
|
upgrade(config.debug_mode).await;
|
||||||
|
}
|
||||||
|
_ = signal::ctrl_c() => {
|
||||||
|
println!("Received Ctrl+C, exiting...");
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
let mut interval = time::interval(time::Duration::from_secs(300));
|
||||||
let mut interval = time::interval(time::Duration::from_secs(300));
|
loop {
|
||||||
loop {
|
interval.tick().await;
|
||||||
interval.tick().await;
|
upgrade(config.debug_mode).await;
|
||||||
upgrade().await;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user