Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit 3d6be66

Browse files
committed
Add source manipulation & Run hook.lua during autorun. Fixes #13
* Return a string in your hook.lua file to override the code being ran * hook.lua will now run even if autorun.lua just ran (If you want to override init.lua completely for some reason)
1 parent 49dfbe8 commit 3d6be66

File tree

6 files changed

+42
-36
lines changed

6 files changed

+42
-36
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "autorun"
3-
version = "0.5.0"
3+
version = "0.5.1"
44
authors = ["Vurv78 <Vurv78@users.noreply.github.com>"]
55
edition = "2018"
66

src/detours/mod.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ use rglua::{
1414

1515
use detour::static_detour;
1616

17+
const LUA_BOOL: i32 = rglua::globals::Lua::Type::Bool as i32;
18+
const LUA_STRING: i32 = rglua::globals::Lua::Type::String as i32;
19+
1720
static_detour! {
1821
pub static luaL_newstate_h: extern "C" fn() -> LuaState;
1922
pub static luaL_loadbufferx_h: extern "C" fn(LuaState, CharBuf, SizeT, CharBuf, CharBuf) -> CInt;
@@ -23,7 +26,7 @@ static_detour! {
2326

2427
fn luaL_newstate() -> LuaState {
2528
let state = luaL_newstate_h.call();
26-
info!("Got client state through luaL_newstate");
29+
debug!("Got client state through luaL_newstate");
2730
setClientState(state);
2831
state
2932
}
@@ -40,7 +43,6 @@ fn luaL_loadbufferx(state: LuaState, code: CharBuf, size: SizeT, identifier: Cha
4043
let raw_path = &rstring!(identifier)[1 ..]; // Remove the @ from the beginning of the path.
4144
let server_ip = CURRENT_SERVER_IP.load( Ordering::Relaxed );
4245

43-
let mut autoran = false;
4446
let mut do_run = true;
4547
if raw_path == "lua/includes/init.lua" {
4648
if HAS_AUTORAN.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed).is_ok() {
@@ -51,27 +53,30 @@ fn luaL_loadbufferx(state: LuaState, code: CharBuf, size: SizeT, identifier: Cha
5153
if let Err(why) = runLuaEnv(&script, identifier, code, server_ip, true) {
5254
error!("{}", why);
5355
}
54-
autoran = true;
5556
} else {
5657
error!( "Couldn't read your autorun script file at {}/{}", SAUTORUN_DIR.display(), AUTORUN_SCRIPT_PATH.display() );
5758
}
5859
}
5960
}
6061

61-
if !autoran {
62-
if let Ok(script) = fs::read_to_string(&*HOOK_SCRIPT_PATH) {
63-
match runLuaEnv(&script, identifier, code, server_ip, false) {
64-
Ok(_) => {
65-
// If you return ``true`` in your sautorun/hook.lua file, then don't run the sautorun.CODE that is about to run.
66-
if lua_type(state, 1) == rglua::globals::Lua::Type::Bool as i32 {
67-
do_run = lua_toboolean(state, 1) == 0;
68-
lua_pop(state, 1);
69-
}
70-
},
71-
Err(why) => error!("{}", why)
72-
}
62+
let mut new_code = None;
63+
if let Ok(script) = fs::read_to_string(&*HOOK_SCRIPT_PATH) {
64+
match runLuaEnv(&script, identifier, code, server_ip, false) {
65+
Ok(top) => {
66+
// If you return ``true`` in your sautorun/hook.lua file, then don't run the sautorun.CODE that is about to run.
67+
match lua_type(state, top + 1) {
68+
LUA_BOOL => {
69+
do_run = lua_toboolean(state, top + 1) == 0;
70+
},
71+
LUA_STRING => {
72+
new_code = Some( lua_tostring(state, top + 1) );
73+
},
74+
_ => ()
75+
}
76+
lua_settop(state, top);
77+
},
78+
Err(_why) => ()
7379
}
74-
7580
}
7681

7782
if let Some(mut file) = getAutorunHandle(raw_path, server_ip) {
@@ -82,7 +87,7 @@ fn luaL_loadbufferx(state: LuaState, code: CharBuf, size: SizeT, identifier: Cha
8287

8388
if do_run {
8489
// Call the original function and return the value.
85-
return luaL_loadbufferx_h.call( state, code, size, identifier, mode );
90+
return luaL_loadbufferx_h.call( state, new_code.unwrap_or(code), size, identifier, mode );
8691
}
8792
0
8893
}
@@ -127,7 +132,7 @@ fn paint_traverse(this: &'static IPanel, panel_id: usize, force_repaint: bool, f
127132
lua_pop(state, 1);
128133
error!("{}", rstring!(err));
129134
} else {
130-
info!("Code [len {}] ran successfully.", script.len())
135+
info!("Code [#{}] ran successfully.", script.len())
131136
}
132137
}
133138
}

src/input.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,19 @@ pub(crate) fn try_process_input() -> anyhow::Result<()> {
1212
debug!("Command used: [{}], rest [{}]", word, rest);
1313

1414
match word {
15-
"lua_run_cl" => match runLua(REALM_CLIENT, rest.to_owned()) {
16-
Err(why) => error!("{}", why),
17-
_ => () // We don't know if it was successful yet. The code will run later in painttraverse and print there.
15+
"lua_run_cl" => if let Err(why) = runLua(REALM_CLIENT, rest.to_owned()) {
16+
error!("{}", why);
17+
// We don't know if it was successful yet. The code will run later in painttraverse and print there.
1818
},
1919
"lua_openscript_cl" => match std::fs::read_to_string( Path::new(rest_trim) ) {
2020
Err(why) => error!("Errored on lua_openscript. [{}]", why),
21-
Ok(contents) => match runLua( REALM_CLIENT, contents ) {
22-
Err(why) => error!("{}", why),
23-
_ => ()
21+
Ok(contents) => if let Err(why) = runLua( REALM_CLIENT, contents ) {
22+
error!("{}", why);
2423
}
2524
},
2625

27-
"lua_run_menu" => match runLua(REALM_MENU, rest.to_owned()) {
28-
Err(why) => error!("{}", why),
29-
_ => ()
26+
"lua_run_menu" => if let Err(why) = runLua(REALM_MENU, rest.to_owned()) {
27+
error!("{}", why);
3028
},
3129

3230
"lua_openscript_menu" => match std::fs::read_to_string( Path::new( rest ) ) {

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ fn cleanup() {
7575
unsafe {
7676
FreeConsole();
7777
};
78+
7879
if let Some(sender) = SENDER.get() {
7980
sender.send(()).expect("Couldn't send mpsc kill message");
8081
}

src/logging.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ pub fn init() -> anyhow::Result<()> {
1212
let log_file_handle = File::create( SAUTORUN_LOG_DIR.join( format!("{}.log", Local::now().format("%B %d, %Y %I-%M %P") ) ) )?;
1313

1414
let configs = ConfigBuilder::new()
15-
.set_level_color( Level::Info, Some( Color::Cyan) )
16-
.set_level_color( Level::Error, Some( Color::Red) )
17-
.set_level_color( Level::Warn, Some( Color::Yellow) )
15+
.set_level_color( Level::Info, Some( Color::Cyan ) )
16+
.set_level_color( Level::Error, Some( Color::Red ) )
17+
.set_level_color( Level::Warn, Some( Color::Yellow ) )
1818
.build();
1919

2020
CombinedLogger::init(
21-
vec![
21+
vec![
2222
TermLogger::new(
2323
// Logs that are level 'info' or above will be sent to the console.
2424
LevelFilter::Info,

src/sys/runlua.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ extern fn log(state: LuaState) -> i32 {
6969
}
7070

7171
// Runs lua, but inside of the sautorun environment.
72-
pub fn runLuaEnv(script: &str, identifier: CharBuf, dumped_script: CharBuf, ip: &str, startup: bool) -> Result<(), String> {
72+
pub fn runLuaEnv(script: &str, identifier: CharBuf, dumped_script: CharBuf, ip: &str, startup: bool) -> Result<i32, String> {
7373
let state = getClientState();
7474

7575
if state == std::ptr::null_mut() {
@@ -78,6 +78,8 @@ pub fn runLuaEnv(script: &str, identifier: CharBuf, dumped_script: CharBuf, ip:
7878

7979
let cscript = CString::new(script).map_err(|x| format!("Couldn't convert script to CString. [{}]", x))?;
8080

81+
let top = lua_gettop(state);
82+
8183
if luaL_loadbufferx_h.call(
8284
state,
8385
cscript.as_ptr(),
@@ -93,9 +95,9 @@ pub fn runLuaEnv(script: &str, identifier: CharBuf, dumped_script: CharBuf, ip:
9395

9496
lua_createtable( state, 0, 0 ); // Create our custom environment
9597

96-
lua_createtable( state, 0, 0 ); // Create the 'sautorun' table
98+
lua_createtable( state, 0, 0 ); // Create the 'sautorun' table
9799

98-
lua_pushstring( state, identifier );
100+
lua_pushstring( state, identifier );
99101
lua_setfield( state, -2, "NAME\0".as_ptr() as CharBuf );
100102

101103
lua_pushstring( state, dumped_script );
@@ -130,5 +132,5 @@ pub fn runLuaEnv(script: &str, identifier: CharBuf, dumped_script: CharBuf, ip:
130132
return Err( rstring!(err_runtime).to_owned() );
131133
}
132134

133-
Ok(())
135+
Ok(top)
134136
}

0 commit comments

Comments
 (0)