77 lines
1.6 KiB
Rust
Raw Normal View History

2024-09-17 17:17:12 -07:00
use super::vector3::Vector3;
use super::cframe::CFrame;
2024-09-17 17:16:57 -07:00
pub struct Runner{
lua:mlua::Lua,
2024-09-16 18:54:04 -07:00
}
2024-09-17 17:16:57 -07:00
fn init(lua:&mlua::Lua)->mlua::Result<()>{
2024-09-17 17:17:12 -07:00
lua.sandbox(true)?;
//global environment
let globals=lua.globals();
//Vector3
{
let vector3_table=lua.create_table()?;
//Vector3.new
vector3_table.raw_set("new",
lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|
Ok(ctx.create_userdata(Vector3::new(x,y,z)))
)?
)?;
globals.set("Vector3",vector3_table)?;
}
//CFrame
{
let cframe_table=lua.create_table()?;
//CFrame.new
cframe_table.raw_set("new",
lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|
Ok(ctx.create_userdata(CFrame::new(x,y,z)))
)?
)?;
//CFrame.Angles
cframe_table.raw_set("Angles",
lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|
Ok(ctx.create_userdata(CFrame::angles(x,y,z)))
)?
)?;
globals.set("CFrame",cframe_table)?;
}
2024-09-16 19:01:16 -07:00
Ok(())
2024-09-16 18:54:04 -07:00
}
2024-09-17 17:16:57 -07:00
impl Runner{
pub fn new()->mlua::Result<Self>{
let runner=Self{
lua:mlua::Lua::new(),
};
init(&runner.lua)?;
Ok(runner)
}
2024-09-17 18:26:58 -07:00
pub fn set_script(&self,script:rbx_dom_weak::types::Ref,name:String)->mlua::Result<()>{
2024-09-17 17:17:12 -07:00
//TODO: Instance type
let script_table=self.lua.create_table()?;
//script.Name
2024-09-17 18:26:58 -07:00
script_table.raw_set("Name",name)?;
2024-09-17 17:17:12 -07:00
self.lua.globals().set("script",script_table)?;
2024-09-17 17:16:57 -07:00
Ok(())
}
pub fn run(&self,source:String,context:&mut crate::context::Context)->mlua::Result<()>{
//Set up dom access here?
2024-09-17 18:26:58 -07:00
let script:mlua::Table<'_>=self.lua.globals().raw_get("script")?;
let name:String=script.raw_get("Name")?;
self.lua.load(source)
.set_name(name)
.exec()
2024-09-17 17:16:57 -07:00
}
}