Compare commits

...

3 Commits

Author SHA1 Message Date
03e9d989f8 make scheduler into a crate feature 2024-10-16 20:55:42 -07:00
5ddb3dacfd run script as a thread 2024-10-16 20:55:42 -07:00
8950bcbf02 game ticks api 2024-10-16 20:55:42 -07:00
3 changed files with 39 additions and 4 deletions

View File

@ -7,6 +7,10 @@ license = "MIT OR Apache-2.0"
description = "Run embedded Luau scripts which manipulate the DOM."
authors = ["Rhys Lloyd <krakow20@gmail.com>"]
[features]
default=["run-service"]
run-service=[]
[dependencies]
glam = "0.29.0"
mlua = { version = "0.10.0-beta", features = ["luau"] }

View File

@ -1,5 +1,6 @@
pub mod runner;
pub mod context;
#[cfg(feature="run-service")]
pub(crate) mod scheduler;
#[cfg(test)]

View File

@ -1,4 +1,5 @@
use crate::context::Context;
#[cfg(feature="run-service")]
use crate::scheduler::scheduler_mut;
pub struct Runner{
@ -30,6 +31,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::r#enum::set_globals(lua,&globals)?;
super::color3::set_globals(lua,&globals)?;
@ -62,6 +64,7 @@ impl Runner{
}
//this makes set_app_data shut up about the lifetime
self.lua.set_app_data::<&'static mut rbx_dom_weak::WeakDom>(unsafe{core::mem::transmute(&mut context.dom)});
#[cfg(feature="run-service")]
self.lua.set_app_data::<crate::scheduler::Scheduler>(crate::scheduler::Scheduler::default());
Ok(Runnable{
lua:self.lua,
@ -78,6 +81,7 @@ pub struct Runnable<'a>{
impl Runnable<'_>{
pub fn drop_context(self)->Runner{
self.lua.remove_app_data::<&'static mut rbx_dom_weak::WeakDom>();
#[cfg(feature="run-service")]
self.lua.remove_app_data::<crate::scheduler::Scheduler>();
Runner{
lua:self.lua,
@ -85,9 +89,35 @@ impl Runnable<'_>{
}
pub fn run_script(&self,script:super::instance::Instance)->Result<(),Error>{
let (name,source)=super::instance::get_name_source(&self.lua,script).map_err(Error::RustLua)?;
self.lua.globals().set("script",script).map_err(Error::RustLua)?;
self.lua.load(source.as_str())
.set_name(name)
.exec().map_err(|error|Error::Lua{source,error})
self.lua.globals().raw_set("script",script).map_err(Error::RustLua)?;
let f=self.lua.load(source.as_str())
.set_name(name).into_function().map_err(Error::RustLua)?;
// TODO: set_environment without losing the ability to print from Lua
let thread=self.lua.create_thread(f).map_err(Error::RustLua)?;
thread.resume::<mlua::MultiValue>(()).map_err(|error|Error::Lua{source,error})?;
// wait() is called from inside Lua and goes to a rust function that schedules the thread and then yields
// No need to schedule the thread here
Ok(())
}
#[cfg(feature="run-service")]
pub fn has_scheduled_threads(&self)->Result<bool,mlua::Error>{
scheduler_mut(&self.lua,|scheduler|
Ok(scheduler.has_scheduled_threads())
)
}
#[cfg(feature="run-service")]
pub fn game_tick(&self)->Result<(),mlua::Error>{
if let Some(threads)=scheduler_mut(&self.lua,|scheduler|Ok(scheduler.tick_threads()))?{
for thread in threads{
//TODO: return dt and total run time
let result=thread.resume::<mlua::MultiValue>((1.0/30.0,0.0))
.map_err(|error|Error::Lua{source:"source unavailable".to_owned(),error});
match result{
Ok(_)=>(),
Err(e)=>println!("game_tick Error: {e}"),
}
}
}
Ok(())
}
}