diff --git a/Cargo.toml b/Cargo.toml index 9e1f42a..0a6bb4c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "Autorun" -version = "0.3.0" +name = "autorun" +version = "0.4.0" authors = ["Vurv78 "] edition = "2018" @@ -18,9 +18,9 @@ rglua = { git = "https://github.com/Vurv78/rglua" } detour = { version = "0.8.0", default-features = false } # Global Mutable Variables -once_cell = "1.7.2" +once_cell = "1.8.0" atomic = "0.5.0" # Misc -dirs = "3.0.1" # To get your home directory. -anyhow = "1.0.41" \ No newline at end of file +dirs = "3.0.2" # To get your home directory. +anyhow = "1.0.42" \ No newline at end of file diff --git a/src/hooks.rs b/src/hooks.rs index fd84bc4..6bd1792 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -12,7 +12,8 @@ use crate::sys::{ use rglua::{ lua_shared::*, - types::* + types::*, + rstring }; pub extern fn loadbufferx(state: LuaState, code: CharBuf, size: SizeT, identifier: CharBuf, mode: CharBuf) -> CInt { diff --git a/src/lib.rs b/src/lib.rs index 68617c5..9411161 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,4 @@ #![allow(non_snake_case)] -#[macro_use] extern crate rglua; - use std::{ path::Path, thread, @@ -65,55 +63,71 @@ extern "system" { fn AllocConsole() -> i32; } +fn init() { + assert_eq!( unsafe { AllocConsole() }, 1, "Couldn't allocate console" ); + println!("<---> Autorun-rs <--->"); + println!("Type [help] for the list of commands"); + + &*LUAL_LOADBUFFERX; + + let (sender, receiver) = mpsc::channel(); + + thread::spawn(move || loop { + if let Ok(_) = try_process_input() { + continue; + } + match receiver.try_recv() { + Ok(_) => { + break; + }, + Err( mpsc::TryRecvError::Disconnected ) => { + // println!("Disconnected! What happened?"); + // ?TODO: Think we also have to break here, but this kept running randomly for me. + break; + }, + Err( mpsc::TryRecvError::Empty ) => () + } + }); + + SENDER.set(sender).expect("Couldn't set mpsc kill channel!"); +} + +fn cleanup() { + // Detour cleanups + unsafe { + LUAL_LOADBUFFERX.disable().unwrap(); + if let Some(hook) = JOIN_SERVER.get() { + hook.disable().unwrap(); + } + }; + if let Some(sender) = SENDER.get() { + sender.send(()).expect("Couldn't send mpsc kill message"); + } +} + const SENDER: OnceCell< mpsc::Sender<()> > = OnceCell::new(); +const DLL_PROCESS_ATTACH: u32 = 1; +const DLL_PROCESS_DETACH: u32 = 0; // Windows Only. I'm not going to half-ass cross-operating system support. #[no_mangle] pub extern "stdcall" fn DllMain(_: *const u8, reason: u32, _: *const u8) -> u32 { match reason { - 1 => { - // DLL_PROCESS_ATTACH - let (sender, receiver) = mpsc::channel(); - thread::spawn(move || { - assert_eq!( unsafe { AllocConsole() }, 1, "Hi" ); - println!("<---> Autorun-rs <--->"); - println!("Type [help] for the list of commands"); - - &*LUAL_LOADBUFFERX; - - loop { - if let Ok(_) = try_process_input() { - continue; - } - - match receiver.try_recv() { - Ok(_) => { - break; - }, - Err( mpsc::TryRecvError::Disconnected ) => { - println!("Disconnected! What happened?"); - // Todo I think we have to break here as well but I kept getting this falsely running. - }, - Err( mpsc::TryRecvError::Empty ) => () - } - } - }); - SENDER.set(sender).expect("Couldn't set mpsc kill channel!"); - } - 0 => { - // DLL_PROCESS_DETACH - // Detour cleanups - unsafe { - LUAL_LOADBUFFERX.disable().unwrap(); - if let Some(hook) = JOIN_SERVER.get() { - hook.disable().unwrap(); - } - }; - if let Some(sender) = SENDER.get() { - sender.send(()).expect("Couldn't send mpsc kill message"); - } - } + DLL_PROCESS_ATTACH => init(), + DLL_PROCESS_DETACH => cleanup(), _ => () } 1 +} + +use rglua::types::LuaState; + +pub extern "C" fn gmod13_open(_state: LuaState) -> i32 { + init(); + 0 +} + +pub extern "C" fn gmod13_close(_state: LuaState) -> i32 { + cleanup(); + 0 } \ No newline at end of file diff --git a/src/sys/runlua.rs b/src/sys/runlua.rs index 30a2f4a..402e15b 100644 --- a/src/sys/runlua.rs +++ b/src/sys/runlua.rs @@ -11,7 +11,8 @@ use rglua::{ types::{ CharBuf, LuaState - } + }, + rstring }; // Runs lua code through loadbufferx. Returns whether it successfully ran.