Skip to content

Commit

Permalink
Add plugin command(s) & Allow hook.lua in plugins to override scr…
Browse files Browse the repository at this point in the history
…ipt (with returns)

* plugin list
* plugin help
* plugin new <name>
* Now you can return <string> or return true to replace script or don't run it inside of hook.lua (IN PLUGINS) since it only worked in autorun/hook.lua.
  • Loading branch information
Vurv78 committed Apr 2, 2022
1 parent 618944d commit bff6ddd
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 95 deletions.
2 changes: 1 addition & 1 deletion autorun/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "autorun"
version = "1.2.0-beta5"
version = "1.2.0-beta6"
authors = ["Vurv78 <Vurv78@users.noreply.github.com>"]
edition = "2021"
publish = false
Expand Down
8 changes: 8 additions & 0 deletions autorun/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,11 @@ pub fn traverse_dir<P: AsRef<Path>, F: FnMut(&FSPath, fs::DirEntry)>(

Ok(())
}

pub fn create_dir(path: &FSPath) -> std::io::Result<()> {
fs::create_dir(in_autorun(path))
}

pub fn create_file(path: &FSPath) -> std::io::Result<fs::File> {
fs::File::create(in_autorun(path))
}
3 changes: 2 additions & 1 deletion autorun/src/hooks/dumper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ pub fn dump(params: &mut DispatchParams) {
fmt = fmt.replace("<hostname>", &hostname);
}

let code = unsafe { CStr::from_ptr(params.code) };
let (code, _) = params.get_code();
let code = unsafe { CStr::from_ptr(code) };
let code = code.to_string_lossy().to_string();

fmt = strip_invalid(&fmt);
Expand Down
126 changes: 63 additions & 63 deletions autorun/src/hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ static CONNECTED: AtomicU64 = AtomicU64::new(99999);

pub struct DispatchParams<'a> {
ip: LuaString,

code: LuaString,
code_len: usize,

identifier: LuaString,

startup: bool,
Expand All @@ -62,87 +64,85 @@ pub struct DispatchParams<'a> {
net: &'a mut interface::NetChannelInfo,
}

fn loadbufferx_hook(
impl<'a> DispatchParams<'a> {
pub fn set_code(&mut self, code: LuaString, code_len: usize) {
self.code = code;
self.code_len = code_len;
}

pub fn get_code(&self) -> (LuaString, usize) {
(self.code, self.code_len)
}
}

extern "C" fn loadbufferx_h(
l: LuaState,
code: LuaString,
code_len: usize,
mut code: LuaString,
mut code_len: SizeT,
identifier: LuaString,
mode: LuaString,
) -> Result<i32, interface::Error> {
let mut engine = iface!(EngineClient)?;

let do_run;
if engine.IsConnected() {
let net = engine.GetNetChannelInfo();

if let Some(net) = unsafe { net.as_mut() } {
let ip = net.GetAddress();
let mut startup = false;

// TODO: It'd be great to hook net connections instead of doing this.
// However, this works fine for now.
let curtime = net.GetTimeConnected() as u64;
if curtime < CONNECTED.load(Ordering::Relaxed) {
debug!("Curtime is less than last time connected, assuming startup");
startup = true;

if let Err(why) = close_dylibs() {
debug!("Failed to close dynamic libs: {why}");
) -> i32 {
if let Ok(mut engine) = iface!(EngineClient) {
let do_run;
if engine.IsConnected() {
let net = engine.GetNetChannelInfo();

if let Some(net) = unsafe { net.as_mut() } {
let ip = net.GetAddress();
let mut startup = false;

// TODO: It'd be great to hook net connections instead of doing this.
// However, this works fine for now.
let curtime = net.GetTimeConnected() as u64;
if curtime < CONNECTED.load(Ordering::Relaxed) {
debug!("Curtime is less than last time connected, assuming startup");
startup = true;

if let Err(why) = close_dylibs() {
debug!("Failed to close dynamic libs: {why}");
}
}
}

// Awful
CONNECTED.store(curtime, Ordering::Relaxed);

let path = unsafe { CStr::from_ptr(identifier) };
let path = &path.to_string_lossy()[1..]; // Remove the @ from the beginning of the path
// Awful
CONNECTED.store(curtime, Ordering::Relaxed);

// There's way too many params here
let params = DispatchParams {
ip,
code,
code_len,
identifier,
startup,
path,
let path = unsafe { CStr::from_ptr(identifier) };
let path = &path.to_string_lossy()[1..]; // Remove the @ from the beginning of the path

engine: &mut engine,
net,
};
// There's way too many params here
let mut params = DispatchParams {
ip,

do_run = dispatch(l, params);
if !do_run {
return Ok(0);
}
}
}
code: code,
code_len: code_len,

unsafe { Ok(LUAL_LOADBUFFERX_H.call(l, code, code_len, identifier, mode)) }
}
identifier,
startup,
path,

extern "C" fn loadbufferx_h(
l: LuaState,
code: LuaString,
len: SizeT,
identifier: LuaString,
mode: LuaString,
) -> i32 {
match loadbufferx_hook(l, code, len, identifier, mode) {
Ok(x) => x,
Err(why) => {
error!("Failed to run loadbufferx hook: {}", why);
engine: &mut engine,
net,
};

unsafe { LUAL_LOADBUFFERX_H.call(l, code, len, identifier, mode) }
do_run = dispatch(l, &mut params);
if do_run {
(code, code_len) = params.get_code();
} else {
return 0;
}
}
}
}

unsafe { LUAL_LOADBUFFERX_H.call(l, code, code_len, identifier, mode) }
}

pub fn dispatch(l: LuaState, mut params: DispatchParams) -> bool {
pub fn dispatch(l: LuaState, params: &mut DispatchParams) -> bool {
let mut do_run = true;

scripthook::execute(l, &mut params, &mut do_run);
scripthook::execute(l, params, &mut do_run);
if SETTINGS.filesteal.enabled {
dumper::dump(&mut params);
dumper::dump(params);
}

do_run
Expand Down
30 changes: 19 additions & 11 deletions autorun/src/hooks/scripthook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ pub fn execute(l: LuaState, params: &mut DispatchParams, do_run: &mut bool) {

{
// hook.lua
let env = AutorunEnv {
let mut env = AutorunEnv {
is_autorun_file: false,
startup: params.startup,

identifier: params.identifier,

code: params.code,
code_len: params.code_len,

Expand All @@ -58,25 +59,32 @@ pub fn execute(l: LuaState, params: &mut DispatchParams, do_run: &mut bool) {
};

if SETTINGS.plugins.enabled {
if let Err(why) = plugins::call_hook(l, &env) {
error!("Failed to call plugins (hook): {why}");
match plugins::call_hook(l, &mut env, do_run) {
Err(why) => {
error!("Failed to call plugins (hook): {why}");
},
Ok( Some( (code, len) )) => {
params.set_code(code, len);
},
Ok(_) => ()
}
}

let path = afs::in_autorun(HOOK_PATH);

if let Ok(script) = fs::read_to_string(&path) {
match lua::run_env(l, &script, &path, &env) {
if let Ok(script) = afs::read_to_string(HOOK_PATH) {
match lua::run_env(l, &script, HOOK_PATH, &env) {
Ok(top) => {
// If you return ``true`` in your sautorun/hook.lua file, then don't run the sautorun.CODE that is about to run.
// If you return ``true`` in your hook.lua file, then don't run the Autorun.CODE that is about to run.
match lua_type(l, top + 1) {
rglua::lua::TBOOLEAN => {
*do_run = lua_toboolean(l, top + 1) == 0;
if lua_toboolean(l, top + 1) != 0 {
*do_run = false;
}
}
rglua::lua::TSTRING => {
// lua_tolstring sets len to new length automatically.
let nul_str = lua_tolstring(l, top + 1, &mut params.code_len);
params.code = nul_str;
let mut len: usize = 0;
let newcode = lua_tolstring(l, top + 1, &mut len);
params.set_code(newcode, len);
}
_ => (),
}
Expand Down
8 changes: 4 additions & 4 deletions autorun/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extern "system" fn DllMain(_: *const u8, reason: u32, _: *const u8) -> u32 {
match reason {
DLL_PROCESS_ATTACH => {
if let Err(why) = cross::startup() {
format!("Failed to start: {why}");
error!("Failed to start: {why}");
}
}
DLL_PROCESS_DETACH => {
Expand All @@ -46,8 +46,8 @@ pub fn main(l: LuaState) -> i32 {
// So only initialize what we haven't already.
#[cfg(not(feature = "inject"))]
if let Err(why) = cross::startup() {
printgm!(l, "Failed to start Autorun: `{}`", why);
error!("Failed to start Autorun: `{}`", why);
printgm!(l, "Failed to start Autorun: `{why}`");
error!("Failed to start Autorun: `{why}`");
}

0
Expand All @@ -57,7 +57,7 @@ pub fn main(l: LuaState) -> i32 {
pub fn close(_l: LuaState) -> i32 {
#[cfg(not(feature = "inject"))]
if let Err(why) = cross::cleanup() {
error!("Failed to cleanup at gmod13_close: {}", why);
error!("Failed to cleanup at gmod13_close: {why}");
}
0
}
9 changes: 3 additions & 6 deletions autorun/src/lua/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,10 @@ pub fn requirebin(l: LuaState) -> Result<i32, RequireError> {
n_symbols = autorun_sym(l);
} else if let Ok(gmod13_sym) = unsafe { lib.get::<Gmod13Entry>(b"gmod13_open\0") } {
n_symbols = gmod13_sym(l);
} else if let Ok(lua_sym) = unsafe { lib.get::<LuaEntry>(b"lua_open\0") } {
n_symbols = lua_sym(l);
} else {
let lua_sym = format!("luaopen_{}\0", dlname);
if let Ok(lua_sym) = unsafe { lib.get::<LuaEntry>(lua_sym.as_bytes()) } {
n_symbols = lua_sym(l);
} else {
return Err(RequireError::SymbolNotFound);
}
return Err(RequireError::SymbolNotFound);
}

if let Ok(mut libs) = LOADED_LIBS.try_lock() {
Expand Down
13 changes: 12 additions & 1 deletion autorun/src/lua/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ pub struct AutorunEnv {
pub plugin: Option<crate::plugins::Plugin>,
}

impl AutorunEnv {
pub fn set_code(&mut self, code: LuaString, code_len: usize) {
self.code = code;
self.code_len = code_len;
}

pub fn get_code(&self) -> (LuaString, usize) {
(self.code, self.code_len)
}
}

// Functions to interact with lua without triggering the detours
pub fn compile<S: AsRef<str>>(l: LuaState, code: S) -> Result<(), Cow<'static, str>> {
let s = code.as_ref();
Expand Down Expand Up @@ -199,7 +210,7 @@ pub fn run_env_prep<S: AsRef<str>, F: Fn(LuaState), P: AsRef<Path>>(
lua_pushstring(l, env.ip);
lua_setfield(l, -2, cstr!("IP")); // stack[2].IP = table.remove(stack, 3)

// If this is running before autorun, set SAUTORUN.STARTUP to true.
// If this is running before autorun, set Autorun.STARTUP to true.
lua_pushboolean(l, i32::from(env.startup)); // stack[3] = startup
lua_setfield(l, -2, cstr!("STARTUP")); // stack[2].STARTUP = table.remove(stack, 3)

Expand Down
Loading

0 comments on commit bff6ddd

Please # to comment.