Update to current winapi, split project

This commit is contained in:
Your Name
2024-08-25 22:24:06 -04:00
parent a647fc5186
commit 82153d31c9
18 changed files with 593 additions and 145 deletions

View File

@@ -0,0 +1,52 @@
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::{
Com::{CoInitializeEx, COINIT_MULTITHREADED},
Threading::{WaitForSingleObject, INFINITE}
},
}, core::w,
};
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()))
}
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(())
}
}
fn main() {
unsafe {
CoInitializeEx(None, COINIT_MULTITHREADED).unwrap();
}
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));
await_file_change(&mute_file).unwrap();
}
}