From d8358ec25cccf3c9f36fb420ed077757b0d4f3c6 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 23 Apr 2025 13:47:16 -0700 Subject: [PATCH] roblox_emulator: create task module --- lib/roblox_emulator/src/runner/mod.rs | 1 + lib/roblox_emulator/src/runner/runner.rs | 3 +- lib/roblox_emulator/src/runner/task.rs | 35 ++++++++++++++++++++++++ lib/roblox_emulator/src/scheduler.rs | 31 +-------------------- 4 files changed, 38 insertions(+), 32 deletions(-) create mode 100644 lib/roblox_emulator/src/runner/task.rs diff --git a/lib/roblox_emulator/src/runner/mod.rs b/lib/roblox_emulator/src/runner/mod.rs index 6786a5686..9c620a3f4 100644 --- a/lib/roblox_emulator/src/runner/mod.rs +++ b/lib/roblox_emulator/src/runner/mod.rs @@ -3,6 +3,7 @@ mod macros; mod runner; mod r#enum; +mod task; mod udim; mod udim2; mod color3; diff --git a/lib/roblox_emulator/src/runner/runner.rs b/lib/roblox_emulator/src/runner/runner.rs index eb0bc05f1..fff425d6f 100644 --- a/lib/roblox_emulator/src/runner/runner.rs +++ b/lib/roblox_emulator/src/runner/runner.rs @@ -32,8 +32,7 @@ fn init(lua:&mlua::Lua)->mlua::Result<()>{ //global environment let globals=lua.globals(); - #[cfg(feature="run-service")] - crate::scheduler::set_globals(lua,&globals)?; + super::task::set_globals(lua,&globals)?; super::script_signal::set_globals(lua,&globals)?; super::r#enum::set_globals(lua,&globals)?; super::udim::set_globals(lua,&globals)?; diff --git a/lib/roblox_emulator/src/runner/task.rs b/lib/roblox_emulator/src/runner/task.rs new file mode 100644 index 000000000..8f934aae1 --- /dev/null +++ b/lib/roblox_emulator/src/runner/task.rs @@ -0,0 +1,35 @@ +#[cfg(not(feature="run-service"))] +fn no_op(_lua:&mlua::Lua,_time:Option)->mlua::Result{ + Ok(0.0) +} +// This is used to avoid calling coroutine.yield from the rust side. +const LUA_WAIT:&str= +"local coroutine_yield=coroutine.yield +local schedule_thread=schedule_thread +return function(dt) + schedule_thread(dt) + return coroutine_yield() +end"; + +pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{ + let coroutine_table=globals.get::("coroutine")?; + #[cfg(feature="run-service")] + let schedule_thread=lua.create_function(crate::scheduler::schedule_thread)?; + #[cfg(not(feature="run-service"))] + let schedule_thread=lua.create_function(no_op)?; + + //create wait function environment + let wait_env=lua.create_table()?; + wait_env.raw_set("coroutine",coroutine_table)?; + wait_env.raw_set("schedule_thread",schedule_thread)?; + + //construct wait function from Lua code + let wait=lua.load(LUA_WAIT) + .set_name("wait") + .set_environment(wait_env) + .call::(())?; + + globals.raw_set("wait",wait)?; + + Ok(()) +} diff --git a/lib/roblox_emulator/src/scheduler.rs b/lib/roblox_emulator/src/scheduler.rs index 996197916..32c013e40 100644 --- a/lib/roblox_emulator/src/scheduler.rs +++ b/lib/roblox_emulator/src/scheduler.rs @@ -51,7 +51,7 @@ pub fn scheduler_mut(lua:&mlua::Lua,mut f:impl FnMut(&mut crate::scheduler::S f(&mut *scheduler) } -fn schedule_thread(lua:&mlua::Lua,dt:mlua::Value)->Result<(),mlua::Error>{ +pub fn schedule_thread(lua:&mlua::Lua,dt:mlua::Value)->Result<(),mlua::Error>{ let delay=match dt{ mlua::Value::Integer(i)=>i.max(0) as u64*60, mlua::Value::Number(f)=>{ @@ -75,32 +75,3 @@ fn schedule_thread(lua:&mlua::Lua,dt:mlua::Value)->Result<(),mlua::Error>{ Ok(()) }) } - -// This is used to avoid calling coroutine.yield from the rust side. -const LUA_WAIT:&str= -"local coroutine_yield=coroutine.yield -local schedule_thread=schedule_thread -return function(dt) - schedule_thread(dt) - return coroutine_yield() -end"; - -pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{ - let coroutine_table=globals.get::("coroutine")?; - let schedule_thread=lua.create_function(schedule_thread)?; - - //create wait function environment - let wait_env=lua.create_table()?; - wait_env.raw_set("coroutine",coroutine_table)?; - wait_env.raw_set("schedule_thread",schedule_thread)?; - - //construct wait function from Lua code - let wait=lua.load(LUA_WAIT) - .set_name("wait") - .set_environment(wait_env) - .call::(())?; - - globals.raw_set("wait",wait)?; - - Ok(()) -}