Compare commits

..

No commits in common. "75c868df2c061046ed190d681d8d20faca38dfba" and "e02e455990685cf67d96663a8815d478f2a8d389" have entirely different histories.

3 changed files with 160 additions and 66 deletions

View File

@ -19,7 +19,7 @@ impl Context{
pub const fn new(dom:WeakDom)->Self{
Self{dom}
}
pub fn script_singleton(source:String)->(Context,crate::runner::instance::Instance,Services){
pub fn script_singleton(source:String)->(Context,crate::runner::instance::Script,Services){
let script=InstanceBuilder::new("Script")
.with_property("Source",rbx_types::Variant::String(source));
let script_ref=script.referent();
@ -28,7 +28,7 @@ impl Context{
.with_child(script)
));
let services=context.convert_into_place();
(context,crate::runner::instance::Instance::new(script_ref),services)
(context,crate::runner::instance::Script::new(script_ref),services)
}
pub fn from_ref(dom:&WeakDom)->&Context{
unsafe{&*(dom as *const WeakDom as *const Context)}
@ -42,8 +42,8 @@ impl Context{
class_is_a(instance.class.as_ref(),superclass)
).map(|instance|instance.referent())
}
pub fn scripts(&self)->Vec<crate::runner::instance::Instance>{
self.superclass_iter("LuaSourceContainer").map(crate::runner::instance::Instance::new).collect()
pub fn scripts(&self)->Vec<crate::runner::instance::Script>{
self.superclass_iter("LuaSourceContainer").map(crate::runner::instance::Script::new).collect()
}
pub fn find_services(&self)->Option<Services>{

View File

@ -1,26 +1,10 @@
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
@ -45,10 +29,6 @@ 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{
@ -68,17 +48,6 @@ 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)
@ -94,22 +63,63 @@ 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)
}
#[derive(Clone,Copy)]
pub struct Instance{
//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
}
}
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 Instance{
impl $class{
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"))
impl Referent for $class{
fn referent(&self)->Ref{
self.referent
}
}
type_from_lua_userdata!(Instance);
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);
)*
}
}
};
}
//TODO: update rbx_reflection and use dom.superclasses_iter
pub struct SuperClassIter<'a> {
@ -130,18 +140,21 @@ impl<'a> Iterator for SuperClassIter<'a> {
}
}
impl mlua::UserData for Instance{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
class!(Instance);
class_composition!(Instance,(Instance));
impl Instance{
fn composition_add_fields<'lua,T:Referent,F:mlua::UserDataFields<'lua,T>>(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:Option<Instance>|{
let parent=val.ok_or(mlua::Error::runtime("Nil Parent not yet supported"))?;
fields.add_field_method_set("Parent",|lua,this,val:mlua::AnyUserData|{
let Dereferent(referent)=mlua::AnyUserDataExt::get(&val,"Referent")?;
dom_mut(lua,|dom|{
dom.transfer_within(this.referent,parent.referent);
dom.transfer_within(this.referent(),referent);
Ok(())
})
});
@ -166,7 +179,7 @@ impl mlua::UserData for Instance{
})
});
}
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
fn composition_add_methods<'lua,T:Referent,M:mlua::UserDataMethods<'lua,T>>(methods:&mut M){
methods.add_method("GetChildren",|lua,this,_:()|
dom_mut(lua,|dom|{
let instance=this.get(dom)?;
@ -179,7 +192,7 @@ impl mlua::UserData for Instance{
Ok(children)
})
);
let ffc=|lua,this:&Self,(name,search_descendants):(mlua::String,Option<bool>)|{
let ffc=|lua,this:&T,(name,search_descendants):(mlua::String,Option<bool>)|{
let name_str=name.to_str()?;
dom_mut(lua,|dom|{
let instance=this.get(dom)?;
@ -213,7 +226,7 @@ impl mlua::UserData for 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())
)
@ -229,14 +242,15 @@ impl mlua::UserData for 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):(Instance,mlua::String)|{
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(this,index):(mlua::AnyUserData,mlua::String)|{
let index_str=index.to_str()?;
let dereferent:Dereferent=mlua::AnyUserDataExt::get(&this,"Referent")?;
dom_mut(lua,|dom|{
let instance=this.get(dom)?;
let instance=dereferent.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)),
@ -251,9 +265,10 @@ impl mlua::UserData for Instance{
)
})
});
methods.add_meta_function(mlua::MetaMethod::NewIndex,|lua,(this,index,value):(Instance,mlua::String,mlua::Value)|{
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")?;
dom_mut(lua,|dom|{
let instance=this.get_mut(dom)?;
let instance=dereferent.get_mut(dom)?;
//println!("__newindex t={} i={index:?} v={value:?}",instance.name);
let index_str=index.to_str()?;
let db=rbx_reflection_database::get();
@ -307,3 +322,82 @@ impl mlua::UserData for 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)
)
}
}

View File

@ -53,8 +53,8 @@ impl Runner{
pub fn runnable_context_with_services<'a>(self,context:&'a mut Context,services:&crate::context::Services)->Result<Runnable<'a>,Error>{
{
let globals=self.lua.globals();
globals.set("game",super::instance::Instance::new(services.game)).map_err(Error::RustLua)?;
globals.set("workspace",super::instance::Instance::new(services.workspace)).map_err(Error::RustLua)?;
globals.set("game",super::instance::DataModel::new(services.game)).map_err(Error::RustLua)?;
globals.set("workspace",super::instance::Workspace::new(services.workspace)).map_err(Error::RustLua)?;
}
//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)});
@ -77,8 +77,8 @@ impl Runnable<'_>{
lua:self.lua,
}
}
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)?;
pub fn run_script(&self,script:super::instance::Script)->Result<(),Error>{
let (name,source)=script.get_name_source(&self.lua).map_err(Error::RustLua)?;
self.lua.globals().set("script",script).map_err(Error::RustLua)?;
self.lua.load(source.as_str())
.set_name(name)