|
|
|
@ -1,10 +1,26 @@
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
|
|
use mlua::IntoLua;
|
|
|
|
|
use rbx_types::Ref;
|
|
|
|
|
use rbx_dom_weak::{InstanceBuilder,WeakDom};
|
|
|
|
|
|
|
|
|
|
use super::vector3::Vector3;
|
|
|
|
|
|
|
|
|
|
/// A store of created functions for each Roblox class.
|
|
|
|
|
/// Functions are created the first time they are accessed and stored in this data structure.
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct ClassFunctions<'a>{
|
|
|
|
|
classes:HashMap<&'a str,//ClassName
|
|
|
|
|
HashMap<&'a str,//Function name
|
|
|
|
|
mlua::Function<'a>
|
|
|
|
|
>
|
|
|
|
|
>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Error>{
|
|
|
|
|
//class functions store
|
|
|
|
|
lua.set_app_data(ClassFunctions::default());
|
|
|
|
|
|
|
|
|
|
let instance_table=lua.create_table()?;
|
|
|
|
|
|
|
|
|
|
//Instance.new
|
|
|
|
@ -29,6 +45,10 @@ fn dom_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut WeakDom)->mlua::Result<T>)->m
|
|
|
|
|
let mut dom=lua.app_data_mut::<&'static mut WeakDom>().ok_or(mlua::Error::runtime("DataModel missing"))?;
|
|
|
|
|
f(&mut *dom)
|
|
|
|
|
}
|
|
|
|
|
fn class_functions_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut ClassFunctions<'_>)->mlua::Result<T>)->mlua::Result<T>{
|
|
|
|
|
let mut dom=lua.app_data_mut::<ClassFunctions<'static>>().ok_or(mlua::Error::runtime("ClassFunctions missing"))?;
|
|
|
|
|
f(&mut *dom)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn coerce_float32(value:&mlua::Value)->Option<f32>{
|
|
|
|
|
match value{
|
|
|
|
@ -48,6 +68,17 @@ fn get_full_name(dom:&rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance)->S
|
|
|
|
|
}
|
|
|
|
|
full_name
|
|
|
|
|
}
|
|
|
|
|
//helper function for script
|
|
|
|
|
pub fn get_name_source(lua:&mlua::Lua,script:Instance)->Result<(String,String),mlua::Error>{
|
|
|
|
|
dom_mut(lua,|dom|{
|
|
|
|
|
let instance=script.get(dom)?;
|
|
|
|
|
let source=match instance.properties.get("Source"){
|
|
|
|
|
Some(rbx_dom_weak::types::Variant::String(s))=>s.clone(),
|
|
|
|
|
_=>Err(mlua::Error::external("Missing script.Source"))?,
|
|
|
|
|
};
|
|
|
|
|
Ok((get_full_name(dom,instance),source))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn find_first_child<'a>(dom:&'a rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance,name:&str)->Option<&'a rbx_dom_weak::Instance>{
|
|
|
|
|
instance.children().iter().filter_map(|&r|dom.get_by_ref(r)).find(|inst|inst.name==name)
|
|
|
|
@ -63,63 +94,22 @@ pub fn find_first_descendant_of_class<'a>(dom:&'a rbx_dom_weak::WeakDom,instance
|
|
|
|
|
dom.descendants_of(instance.referent()).find(|&inst|inst.class==class)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//workaround until I have an enum of classes
|
|
|
|
|
struct Dereferent(Ref);
|
|
|
|
|
impl mlua::UserData for Dereferent{}
|
|
|
|
|
type_from_lua_userdata!(Dereferent);
|
|
|
|
|
impl Referent for Dereferent{
|
|
|
|
|
fn referent(&self)->Ref{
|
|
|
|
|
self.0
|
|
|
|
|
#[derive(Clone,Copy)]
|
|
|
|
|
pub struct Instance{
|
|
|
|
|
referent:Ref,
|
|
|
|
|
}
|
|
|
|
|
impl Instance{
|
|
|
|
|
pub const fn new(referent:Ref)->Self{
|
|
|
|
|
Self{referent}
|
|
|
|
|
}
|
|
|
|
|
pub fn get<'a>(&self,dom:&'a WeakDom)->mlua::Result<&'a rbx_dom_weak::Instance>{
|
|
|
|
|
dom.get_by_ref(self.referent).ok_or(mlua::Error::runtime("Instance missing"))
|
|
|
|
|
}
|
|
|
|
|
pub fn get_mut<'a>(&self,dom:&'a mut WeakDom)->mlua::Result<&'a mut rbx_dom_weak::Instance>{
|
|
|
|
|
dom.get_by_ref_mut(self.referent).ok_or(mlua::Error::runtime("Instance missing"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trait Referent{
|
|
|
|
|
fn referent(&self)->Ref;
|
|
|
|
|
fn get<'a>(&self,dom:&'a WeakDom)->mlua::Result<&'a rbx_dom_weak::Instance>{
|
|
|
|
|
dom.get_by_ref(self.referent()).ok_or(mlua::Error::runtime("Instance missing"))
|
|
|
|
|
}
|
|
|
|
|
fn get_mut<'a>(&self,dom:&'a mut WeakDom)->mlua::Result<&'a mut rbx_dom_weak::Instance>{
|
|
|
|
|
dom.get_by_ref_mut(self.referent()).ok_or(mlua::Error::runtime("Instance missing"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! class{
|
|
|
|
|
($class:ident)=>{
|
|
|
|
|
pub struct $class{
|
|
|
|
|
referent:Ref,
|
|
|
|
|
}
|
|
|
|
|
impl $class{
|
|
|
|
|
pub const fn new(referent:Ref)->Self{
|
|
|
|
|
Self{referent}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl Referent for $class{
|
|
|
|
|
fn referent(&self)->Ref{
|
|
|
|
|
self.referent
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
type_from_lua_userdata!($class);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
macro_rules! class_composition{
|
|
|
|
|
($class:ident,($($superclass:ident),*))=>{
|
|
|
|
|
impl mlua::UserData for $class{
|
|
|
|
|
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
|
|
|
|
|
fields.add_field_method_get("Referent",|_,this|{
|
|
|
|
|
Ok(Dereferent(this.referent()))
|
|
|
|
|
});
|
|
|
|
|
$(
|
|
|
|
|
$superclass::composition_add_fields(fields);
|
|
|
|
|
)*
|
|
|
|
|
}
|
|
|
|
|
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
|
|
|
|
|
$(
|
|
|
|
|
$superclass::composition_add_methods(methods);
|
|
|
|
|
)*
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
type_from_lua_userdata!(Instance);
|
|
|
|
|
|
|
|
|
|
//TODO: update rbx_reflection and use dom.superclasses_iter
|
|
|
|
|
pub struct SuperClassIter<'a> {
|
|
|
|
@ -140,21 +130,18 @@ impl<'a> Iterator for SuperClassIter<'a> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class!(Instance);
|
|
|
|
|
class_composition!(Instance,(Instance));
|
|
|
|
|
|
|
|
|
|
impl Instance{
|
|
|
|
|
fn composition_add_fields<'lua,T:Referent,F:mlua::UserDataFields<'lua,T>>(fields:&mut F){
|
|
|
|
|
impl mlua::UserData for Instance{
|
|
|
|
|
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
|
|
|
|
|
fields.add_field_method_get("Parent",|lua,this|{
|
|
|
|
|
dom_mut(lua,|dom|{
|
|
|
|
|
let instance=this.get(dom)?;
|
|
|
|
|
Ok(Instance::new(instance.parent()))
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
fields.add_field_method_set("Parent",|lua,this,val:mlua::AnyUserData|{
|
|
|
|
|
let Dereferent(referent)=mlua::AnyUserDataExt::get(&val,"Referent")?;
|
|
|
|
|
fields.add_field_method_set("Parent",|lua,this,val:Option<Instance>|{
|
|
|
|
|
let parent=val.ok_or(mlua::Error::runtime("Nil Parent not yet supported"))?;
|
|
|
|
|
dom_mut(lua,|dom|{
|
|
|
|
|
dom.transfer_within(this.referent(),referent);
|
|
|
|
|
dom.transfer_within(this.referent,parent.referent);
|
|
|
|
|
Ok(())
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
@ -179,7 +166,7 @@ impl Instance{
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
fn composition_add_methods<'lua,T:Referent,M:mlua::UserDataMethods<'lua,T>>(methods:&mut M){
|
|
|
|
|
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
|
|
|
|
|
methods.add_method("GetChildren",|lua,this,_:()|
|
|
|
|
|
dom_mut(lua,|dom|{
|
|
|
|
|
let instance=this.get(dom)?;
|
|
|
|
@ -192,7 +179,7 @@ impl Instance{
|
|
|
|
|
Ok(children)
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
let ffc=|lua,this:&T,(name,search_descendants):(mlua::String,Option<bool>)|{
|
|
|
|
|
let ffc=|lua,this:&Self,(name,search_descendants):(mlua::String,Option<bool>)|{
|
|
|
|
|
let name_str=name.to_str()?;
|
|
|
|
|
dom_mut(lua,|dom|{
|
|
|
|
|
let instance=this.get(dom)?;
|
|
|
|
@ -226,7 +213,7 @@ impl Instance{
|
|
|
|
|
methods.add_method("GetDescendants",|lua,this,_:()|
|
|
|
|
|
dom_mut(lua,|dom|{
|
|
|
|
|
let children:Vec<_>=dom
|
|
|
|
|
.descendants_of(this.referent())
|
|
|
|
|
.descendants_of(this.referent)
|
|
|
|
|
.map(|instance|
|
|
|
|
|
Instance::new(instance.referent())
|
|
|
|
|
)
|
|
|
|
@ -242,15 +229,14 @@ impl Instance{
|
|
|
|
|
);
|
|
|
|
|
methods.add_method("Destroy",|lua,this,()|
|
|
|
|
|
dom_mut(lua,|dom|{
|
|
|
|
|
dom.destroy(this.referent());
|
|
|
|
|
dom.destroy(this.referent);
|
|
|
|
|
Ok(())
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(this,index):(mlua::AnyUserData,mlua::String)|{
|
|
|
|
|
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(this,index):(Instance,mlua::String)|{
|
|
|
|
|
let index_str=index.to_str()?;
|
|
|
|
|
let dereferent:Dereferent=mlua::AnyUserDataExt::get(&this,"Referent")?;
|
|
|
|
|
dom_mut(lua,|dom|{
|
|
|
|
|
let instance=dereferent.get(dom)?;
|
|
|
|
|
let instance=this.get(dom)?;
|
|
|
|
|
match instance.properties.get(index_str){
|
|
|
|
|
Some(&rbx_types::Variant::CFrame(cf))=>return Ok(Into::<super::cframe::CFrame>::into(cf).into_lua(lua)),
|
|
|
|
|
Some(&rbx_types::Variant::Vector3(v))=>return Ok(Into::<super::vector3::Vector3>::into(v).into_lua(lua)),
|
|
|
|
@ -265,10 +251,9 @@ impl Instance{
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
methods.add_meta_function(mlua::MetaMethod::NewIndex,|lua,(this,index,value):(mlua::AnyUserData,mlua::String,mlua::Value)|{
|
|
|
|
|
let dereferent:Dereferent=mlua::AnyUserDataExt::get(&this,"Referent")?;
|
|
|
|
|
methods.add_meta_function(mlua::MetaMethod::NewIndex,|lua,(this,index,value):(Instance,mlua::String,mlua::Value)|{
|
|
|
|
|
dom_mut(lua,|dom|{
|
|
|
|
|
let instance=dereferent.get_mut(dom)?;
|
|
|
|
|
let instance=this.get_mut(dom)?;
|
|
|
|
|
//println!("__newindex t={} i={index:?} v={value:?}",instance.name);
|
|
|
|
|
let index_str=index.to_str()?;
|
|
|
|
|
let db=rbx_reflection_database::get();
|
|
|
|
@ -322,82 +307,3 @@ impl Instance{
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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("workspace",|lua,this|{
|
|
|
|
|
dom_mut(lua,|dom|{
|
|
|
|
|
Ok(find_first_child_of_class(dom,this.get(dom)?,"Workspace")
|
|
|
|
|
.map(|inst|Workspace::new(inst.referent()))
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
fields.add_field_method_get("PlaceId",|lua,this|{
|
|
|
|
|
Ok(mlua::Value::Integer(0))
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
fn composition_add_methods<'lua,T,M:mlua::UserDataMethods<'lua,T>>(methods:&mut M){
|
|
|
|
|
methods.add_method("GetService",|lua,this,service:String|
|
|
|
|
|
dom_mut(lua,|dom|{
|
|
|
|
|
match service.as_str(){
|
|
|
|
|
"Lighting"=>{
|
|
|
|
|
let referent=find_first_child_of_class(dom,dom.root(),"Lighting")
|
|
|
|
|
.map(|instance|instance.referent())
|
|
|
|
|
.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"))),
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class!(Workspace);
|
|
|
|
|
class_composition!(Workspace,(Instance,Workspace));
|
|
|
|
|
impl Workspace{
|
|
|
|
|
fn composition_add_fields<'lua,T:Referent,F:mlua::UserDataFields<'lua,T>>(fields:&mut F){
|
|
|
|
|
fields.add_field_method_get("Terrain",|lua,this|{
|
|
|
|
|
dom_mut(lua,|dom|{
|
|
|
|
|
Ok(find_first_child_of_class(dom,this.get(dom)?,"Terrain")
|
|
|
|
|
.map(|inst|Terrain::new(inst.referent()))
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
fn composition_add_methods<'lua,T,M:mlua::UserDataMethods<'lua,T>>(_methods:&mut M){
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
class!(Lighting);
|
|
|
|
|
class_composition!(Lighting,(Instance));
|
|
|
|
|
|
|
|
|
|
class!(Script);
|
|
|
|
|
class_composition!(Script,(Instance));
|
|
|
|
|
impl Script{
|
|
|
|
|
pub fn get_name_source(&self,lua:&mlua::Lua)->Result<(String,String),mlua::Error>{
|
|
|
|
|
dom_mut(lua,|dom|{
|
|
|
|
|
let instance=self.get(dom)?;
|
|
|
|
|
let source=match instance.properties.get("Source"){
|
|
|
|
|
Some(rbx_dom_weak::types::Variant::String(s))=>s.clone(),
|
|
|
|
|
_=>Err(mlua::Error::external("Missing script.Source"))?,
|
|
|
|
|
};
|
|
|
|
|
Ok((get_full_name(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){
|
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|