Skip to content

Commit

Permalink
Add entrypoint through gmod modules
Browse files Browse the repository at this point in the history
Fixes #10
  • Loading branch information
Vurv78 committed Jul 23, 2021
1 parent 85921c7 commit 0120e4f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 51 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "Autorun"
version = "0.3.0"
name = "autorun"
version = "0.4.0"
authors = ["Vurv78 <[email protected]>"]
edition = "2018"

Expand All @@ -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"
dirs = "3.0.2" # To get your home directory.
anyhow = "1.0.42"
3 changes: 2 additions & 1 deletion src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
102 changes: 58 additions & 44 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#![allow(non_snake_case)]
#[macro_use] extern crate rglua;

use std::{
path::Path,
thread,
Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion src/sys/runlua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use rglua::{
types::{
CharBuf,
LuaState
}
},
rstring
};

// Runs lua code through loadbufferx. Returns whether it successfully ran.
Expand Down

0 comments on commit 0120e4f

Please sign in to comment.