diff --git a/src/context.rs b/src/context.rs index 91562bd..c86320c 100644 --- a/src/context.rs +++ b/src/context.rs @@ -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::Script,Services){ + pub fn script_singleton(source:String)->(Context,crate::runner::instance::Instance,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::Script::new(script_ref),services) + (context,crate::runner::instance::Instance::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{ - self.superclass_iter("LuaSourceContainer").map(crate::runner::instance::Script::new).collect() + pub fn scripts(&self)->Vec{ + self.superclass_iter("LuaSourceContainer").map(crate::runner::instance::Instance::new).collect() } pub fn find_services(&self)->Option{ diff --git a/src/runner/instance.rs b/src/runner/instance.rs index 06355f9..4c482d3 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -48,6 +48,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 +74,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 +110,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|{ + 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 +146,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 +159,7 @@ impl Instance{ Ok(children) }) ); - let ffc=|lua,this:&T,(name,search_descendants):(mlua::String,Option)|{ + let ffc=|lua,this:&Self,(name,search_descendants):(mlua::String,Option)|{ let name_str=name.to_str()?; dom_mut(lua,|dom|{ let instance=this.get(dom)?; @@ -226,7 +193,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 +209,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)?; //Find existing property match instance.properties.get(index_str) //Find default value @@ -273,10 +239,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(); @@ -330,82 +295,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::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) - ) - } -} diff --git a/src/runner/runner.rs b/src/runner/runner.rs index b603aa0..48dee06 100644 --- a/src/runner/runner.rs +++ b/src/runner/runner.rs @@ -53,8 +53,8 @@ impl Runner{ pub fn runnable_context_with_services<'a>(self,context:&'a mut Context,services:&crate::context::Services)->Result,Error>{ { let globals=self.lua.globals(); - 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)?; + 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)?; } //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::Script)->Result<(),Error>{ - let (name,source)=script.get_name_source(&self.lua).map_err(Error::RustLua)?; + 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)