@@ -14,6 +14,9 @@ use rglua::{
1414
1515use 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+
1720static_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
2427fn 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}
0 commit comments