From abafc943792a90bb59a2ac567aa69d7734b81d65 Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Wed, 13 Jul 2022 18:49:06 -0700 Subject: [PATCH] rework process watcher to be not terrible --- src-tauri/src/main.rs | 47 ++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 3bbf69a..216f855 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -22,9 +22,6 @@ mod web; static WATCH_GAME_PROCESS: Lazy> = Lazy::new(|| Mutex::new(String::new())); fn main() { - // Start the game process watcher. - process_watcher(); - tauri::Builder::default() .invoke_handler(tauri::generate_handler![ enable_process_watcher, @@ -58,16 +55,30 @@ fn main() { .expect("error while running tauri application"); } -fn process_watcher() { - // Every 5 seconds, see if the game process is still running. - // If it is not, then we assume the game has closed and disable the proxy - // to prevent any requests from being sent to the game. +#[tauri::command] +fn is_game_running() -> bool { + // Grab the game process name + let proc = WATCH_GAME_PROCESS.lock().unwrap().to_string(); - // Start a thread so as to not block the main thread. - thread::spawn(|| { + !proc.is_empty() +} + +#[tauri::command] +fn enable_process_watcher(window: tauri::Window,process: String) { + *WATCH_GAME_PROCESS.lock().unwrap() = process; + + window.listen("disable_process_watcher", |_e| { + *WATCH_GAME_PROCESS.lock().unwrap() = "".to_string(); + }); + + println!("Starting process watcher..."); + + thread::spawn(move || { let mut system = System::new_all(); loop { + thread::sleep(std::time::Duration::from_secs(5)); + // Refresh system info system.refresh_all(); @@ -80,28 +91,18 @@ fn process_watcher() { // If the game process closes, disable the proxy. if !exists { + println!("Game closed"); + *WATCH_GAME_PROCESS.lock().unwrap() = "".to_string(); disconnect(); + + break; } } - thread::sleep(std::time::Duration::from_secs(5)); } }); } -#[tauri::command] -fn is_game_running() -> bool { - // Grab the game process name - let proc = WATCH_GAME_PROCESS.lock().unwrap().to_string(); - - !proc.is_empty() -} - -#[tauri::command] -fn enable_process_watcher(process: String) { - *WATCH_GAME_PROCESS.lock().unwrap() = process; -} - #[tauri::command] async fn connect(port: u16, certificate_path: String) { // Log message to console.