49 lines
1.4 KiB
Rust
49 lines
1.4 KiB
Rust
use std::{
|
|
collections::HashSet,
|
|
env,
|
|
error::Error,
|
|
fs::{self, File},
|
|
io::{BufRead, BufReader}
|
|
};
|
|
|
|
use auto_mute_lib::muter::MuterThread;
|
|
use windows::{
|
|
Win32::{
|
|
Foundation::HANDLE,
|
|
Storage::FileSystem::{
|
|
FindCloseChangeNotification, FindFirstChangeNotificationW, FindNextChangeNotification,
|
|
FILE_NOTIFY_CHANGE_LAST_WRITE,
|
|
},
|
|
System::Threading::{WaitForSingleObject, INFINITE},
|
|
}, core::w,
|
|
};
|
|
|
|
pub fn await_file_change(file_name: &str) -> Result<(), Box<dyn Error>> {
|
|
unsafe {
|
|
let md = fs::metadata(file_name)?.modified()?;
|
|
let handle = FindFirstChangeNotificationW(w!("."), false, FILE_NOTIFY_CHANGE_LAST_WRITE)?;
|
|
while md == fs::metadata(file_name)?.modified()? {
|
|
WaitForSingleObject(HANDLE(handle.0), INFINITE);
|
|
FindNextChangeNotification(handle)?;
|
|
}
|
|
println!("File change detected, restarting");
|
|
FindCloseChangeNotification(handle)?;
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
|
|
pub fn load_mute_txt(file_name: &str) -> HashSet<String> {
|
|
let file = File::open(file_name).unwrap();
|
|
HashSet::from_iter(BufReader::new(file).lines().map(|line| line.unwrap()))
|
|
}
|
|
|
|
fn main() {
|
|
MuterThread::com_init();
|
|
let mute_file: String = env::args().nth(1).unwrap_or("mute.txt".to_string());
|
|
loop {
|
|
let mut _mt = MuterThread::new(load_mute_txt(&mute_file), None);
|
|
await_file_change(&mute_file).unwrap();
|
|
}
|
|
}
|