extend emulator

This commit is contained in:
Quaternions 2024-10-03 16:27:27 -07:00
parent 6b66009c44
commit 45aba39fe4
5 changed files with 111 additions and 3 deletions

52
src/runner/color3.rs Normal file
View File

@ -0,0 +1,52 @@
#[derive(Clone,Copy)]
pub struct Color3{
r:u8,
g:u8,
b:u8,
}
impl Color3{
pub const fn new(r:u8,g:u8,b:u8)->Self{
Self{r,g,b}
}
pub fn from_float(r:f32,g:f32,b:f32)->Self{
Color3{
r:(r*255.0) as u8,
g:(g*255.0) as u8,
b:(b*255.0) as u8,
}
}
}
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Error>{
let color3_table=lua.create_table()?;
color3_table.raw_set("new",
lua.create_function(|ctx,(r,g,b):(f32,f32,f32)|
Ok(ctx.create_userdata(Color3::from_float(r,g,b)))
)?
)?;
color3_table.raw_set("fromRGB",
lua.create_function(|ctx,(r,g,b):(u8,u8,u8)|
Ok(ctx.create_userdata(Color3::new(r,g,b)))
)?
)?;
globals.set("Color3",color3_table)?;
Ok(())
}
impl mlua::UserData for Color3{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
}
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
}
}
impl<'lua> mlua::FromLua<'lua> for Color3{
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
match value{
mlua::Value::UserData(ud)=>ud.take()?,
other=>Err(mlua::Error::runtime(format!("Expected Color3 got {:?}",other))),
}
}
}

17
src/runner/enum.rs Normal file
View File

@ -0,0 +1,17 @@
#[derive(Clone,Copy)]
pub struct Enum(pub(crate)rbx_types::Enum);
impl mlua::UserData for Enum{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
}
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
}
}
impl<'lua> mlua::FromLua<'lua> for Enum{
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
match value{
mlua::Value::UserData(ud)=>ud.take()?,
other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))),
}
}
}

View File

@ -1,5 +1,6 @@
use mlua::IntoLua;
use rbx_types::Ref;
use rbx_dom_weak::WeakDom;
use rbx_dom_weak::{InstanceBuilder,WeakDom};
use super::vector3::Vector3;
@ -112,6 +113,12 @@ impl Instance{
Ok(())
})
});
fields.add_field_method_get("ClassName",|lua,this|{
dom(lua,|dom|{
let instance=this.get(dom)?;
Ok(instance.class.clone())
})
});
}
fn composition_add_methods<'lua,T:Referent,M:mlua::UserDataMethods<'lua,T>>(methods:&mut M){
methods.add_method("GetChildren",|lua,this,_:()|
@ -172,13 +179,27 @@ class!(DataModel);
class_composition!(DataModel,(Instance,DataModel));
impl DataModel{
fn composition_add_fields<'lua,T:Referent,F:mlua::UserDataFields<'lua,T>>(fields:&mut F){
fields.add_field_method_get("PlaceId",|lua,this|{
Ok(mlua::Value::Nil)
});
}
fn composition_add_methods<'lua,T,M:mlua::UserDataMethods<'lua,T>>(methods:&mut M){
methods.add_method("GetService",|lua,this,service:String|
dom(lua,|dom|{
match service.as_str(){
//"Lighting"=>Ok(Lighting::new()),
other=>Err::<(),_>(mlua::Error::runtime("Service '{other}' not supported")),
"Lighting"=>{
let referent=dom.root()
.children()
.iter()
.find(|&&c|
dom.get_by_ref(c).is_some_and(|c|c.class=="Lighting")
).map(|r|*r)
.unwrap_or_else(||
dom.insert(dom.root_ref(),InstanceBuilder::new("Lighting"))
);
Lighting::new(referent).into_lua(lua)
},
other=>Err::<mlua::Value,_>(mlua::Error::runtime(format!("Service '{other}' not supported"))),
}
})
);
@ -206,3 +227,18 @@ impl Script{
Ok((get_full_name(&context.dom,instance),source))
}
}
class!(Terrain);
class_composition!(Terrain,(Instance,Terrain));
impl Terrain{
fn composition_add_fields<'lua,T:Referent,F:mlua::UserDataFields<'lua,T>>(fields:&mut F){
fields.add_field_method_get("PlaceId",|lua,this|{
Ok(mlua::Value::Nil)
});
}
fn composition_add_methods<'lua,T,M:mlua::UserDataMethods<'lua,T>>(methods:&mut M){
methods.add_method("FillBlock",|lua,this,_:(super::cframe::CFrame,Vector3,super::r#enum::Enum)|
Ok(())//Ok(mlua::Value::Nil)
)
}
}

View File

@ -1,5 +1,7 @@
mod runner;
mod r#enum;
mod color3;
mod cframe;
mod vector3;
pub mod instance;

View File

@ -28,6 +28,7 @@ fn init(lua:&mlua::Lua)->mlua::Result<()>{
//global environment
let globals=lua.globals();
super::color3::set_globals(lua,&globals)?;
super::vector3::set_globals(lua,&globals)?;
super::cframe::set_globals(lua,&globals)?;