macro-ify classes + bring script into instances
This commit is contained in:
parent
b765f4cb21
commit
66d9279445
@ -28,7 +28,7 @@ impl Context{
|
|||||||
class_is_a(instance.class.as_ref(),superclass)
|
class_is_a(instance.class.as_ref(),superclass)
|
||||||
).map(|instance|instance.referent())
|
).map(|instance|instance.referent())
|
||||||
}
|
}
|
||||||
pub fn scripts(&self)->Vec<crate::script::Script>{
|
pub fn scripts(&self)->Vec<crate::runner::instance::Script>{
|
||||||
self.superclass_iter("LuaSourceContainer").map(crate::script::Script::new).collect()
|
self.superclass_iter("LuaSourceContainer").map(crate::runner::instance::Script::new).collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,5 @@
|
|||||||
pub mod script;
|
|
||||||
pub mod context;
|
pub mod context;
|
||||||
|
|
||||||
pub mod runner;
|
pub mod runner;
|
||||||
|
|
||||||
pub type Result<T>=core::result::Result<T,script::Error>;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
@ -17,6 +17,17 @@ fn coerce_float32(value:&mlua::Value)->Option<f32>{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_full_name(dom:&rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance)->String{
|
||||||
|
let mut full_name=instance.name.clone();
|
||||||
|
let mut pref=instance.parent();
|
||||||
|
while let Some(parent)=dom.get_by_ref(pref){
|
||||||
|
full_name.insert(0,'.');
|
||||||
|
full_name.insert_str(0,parent.name.as_str());
|
||||||
|
pref=parent.parent();
|
||||||
|
}
|
||||||
|
full_name
|
||||||
|
}
|
||||||
|
|
||||||
trait Referent{
|
trait Referent{
|
||||||
fn referent(&self)->Ref;
|
fn referent(&self)->Ref;
|
||||||
fn get<'a>(&self,dom:&'a WeakDom)->mlua::Result<&'a rbx_dom_weak::Instance>{
|
fn get<'a>(&self,dom:&'a WeakDom)->mlua::Result<&'a rbx_dom_weak::Instance>{
|
||||||
@ -27,26 +38,54 @@ trait Referent{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Instance{
|
macro_rules! class{
|
||||||
|
($class:ident)=>{
|
||||||
|
pub struct $class{
|
||||||
referent:Ref,
|
referent:Ref,
|
||||||
}
|
}
|
||||||
impl Referent for Instance{
|
impl $class{
|
||||||
|
pub const fn new(referent:Ref)->Self{
|
||||||
|
Self{referent}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Referent for $class{
|
||||||
fn referent(&self)->Ref{
|
fn referent(&self)->Ref{
|
||||||
self.referent
|
self.referent
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl<'lua> mlua::FromLua<'lua> for $class{
|
||||||
|
fn from_lua(value:mlua::Value<'lua>,_lua:&'lua mlua::Lua)->mlua::Result<Self>{
|
||||||
|
match value{
|
||||||
|
mlua::Value::UserData(ud)=>ud.take(),
|
||||||
|
other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!($class),other))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
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){
|
||||||
|
$(
|
||||||
|
$superclass::composition_add_fields(fields);
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
|
||||||
|
$(
|
||||||
|
$superclass::composition_add_methods(methods);
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
impl From<crate::script::Script> for Instance{
|
|
||||||
fn from(value:crate::script::Script)->Self{
|
class!(Instance);
|
||||||
Self{referent:value.script}
|
class_composition!(Instance,(Instance));
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Instance{
|
impl Instance{
|
||||||
pub const fn new(referent:Ref)->Self{
|
fn composition_add_fields<'lua,T:Referent,F:mlua::UserDataFields<'lua,T>>(fields:&mut F){
|
||||||
Self{referent}
|
|
||||||
}
|
|
||||||
fn add_fields<'lua,T:Referent,F:mlua::UserDataFields<'lua,T>>(fields:&mut F){
|
|
||||||
fields.add_field_method_get("Parent",|lua,this|{
|
fields.add_field_method_get("Parent",|lua,this|{
|
||||||
dom(lua,|dom|{
|
dom(lua,|dom|{
|
||||||
let instance=this.get(dom)?;
|
let instance=this.get(dom)?;
|
||||||
@ -74,8 +113,7 @@ impl Instance{
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
fn composition_add_methods<'lua,T:Referent,M:mlua::UserDataMethods<'lua,T>>(methods:&mut M){
|
||||||
fn add_methods<'lua,T:Referent,M:mlua::UserDataMethods<'lua,T>>(methods:&mut M){
|
|
||||||
methods.add_method("GetChildren",|lua,this,_:()|
|
methods.add_method("GetChildren",|lua,this,_:()|
|
||||||
dom(lua,|dom|{
|
dom(lua,|dom|{
|
||||||
let instance=this.get(dom)?;
|
let instance=this.get(dom)?;
|
||||||
@ -129,33 +167,13 @@ impl Instance{
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl mlua::UserData for Instance{
|
|
||||||
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
|
|
||||||
Instance::add_fields(fields);
|
|
||||||
}
|
|
||||||
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
|
|
||||||
Instance::add_methods(methods);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<'lua> mlua::FromLua<'lua> for Instance{
|
|
||||||
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 Instance got {:?}",other))),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct DataModel{
|
class!(DataModel);
|
||||||
referent:Ref,
|
class_composition!(DataModel,(Instance,DataModel));
|
||||||
}
|
|
||||||
impl Referent for DataModel{
|
|
||||||
fn referent(&self)->Ref{
|
|
||||||
self.referent
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl DataModel{
|
impl DataModel{
|
||||||
fn add_methods<'lua,T,M:mlua::UserDataMethods<'lua,T>>(methods:&mut M){
|
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("GetService",|lua,this,service:String|
|
methods.add_method("GetService",|lua,this,service:String|
|
||||||
dom(lua,|dom|{
|
dom(lua,|dom|{
|
||||||
match service.as_str(){
|
match service.as_str(){
|
||||||
@ -166,30 +184,25 @@ impl DataModel{
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl mlua::UserData for DataModel{
|
|
||||||
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
|
class!(Lighting);
|
||||||
Instance::add_fields(fields);
|
class_composition!(Lighting,(Instance));
|
||||||
//DataModel::add_fields(fields);
|
|
||||||
}
|
#[derive(Debug)]
|
||||||
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
|
pub enum GetScriptError{
|
||||||
Instance::add_methods(methods);
|
NoScript,
|
||||||
DataModel::add_methods(methods);
|
NoSource,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Lighting{
|
class!(Script);
|
||||||
referent:Ref,
|
class_composition!(Script,(Instance));
|
||||||
}
|
impl Script{
|
||||||
impl Referent for Lighting{
|
pub fn get_name_source(&self,context:&crate::context::Context)->Result<(String,String),GetScriptError>{
|
||||||
fn referent(&self)->Ref{
|
let instance=context.dom.get_by_ref(self.referent).ok_or(GetScriptError::NoScript)?;
|
||||||
self.referent
|
let source=match instance.properties.get("Source").ok_or(GetScriptError::NoSource)?{
|
||||||
}
|
rbx_dom_weak::types::Variant::String(s)=>s.clone(),
|
||||||
}
|
_=>Err(GetScriptError::NoSource)?,
|
||||||
impl mlua::UserData for Lighting{
|
};
|
||||||
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
|
Ok((get_full_name(&context.dom,instance),source))
|
||||||
Instance::add_fields(fields);
|
|
||||||
}
|
|
||||||
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
|
|
||||||
Instance::add_methods(methods);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,6 @@ mod runner;
|
|||||||
|
|
||||||
mod cframe;
|
mod cframe;
|
||||||
mod vector3;
|
mod vector3;
|
||||||
mod instance;
|
pub mod instance;
|
||||||
|
|
||||||
pub use runner::Runner;
|
pub use runner::Runner;
|
||||||
|
@ -12,7 +12,7 @@ pub enum Error{
|
|||||||
source:String,
|
source:String,
|
||||||
error:mlua::Error
|
error:mlua::Error
|
||||||
},
|
},
|
||||||
Script(crate::script::Error),
|
Script(super::instance::GetScriptError),
|
||||||
/// If the lua.remove_app_data function fails
|
/// If the lua.remove_app_data function fails
|
||||||
RemoveAppData,
|
RemoveAppData,
|
||||||
}
|
}
|
||||||
@ -79,9 +79,9 @@ impl Runner{
|
|||||||
init(&runner.lua)?;
|
init(&runner.lua)?;
|
||||||
Ok(runner)
|
Ok(runner)
|
||||||
}
|
}
|
||||||
pub fn run_script(&self,script:crate::script::Script,context:&mut Context)->Result<(),Error>{
|
pub fn run_script(&self,script:super::instance::Script,context:&mut Context)->Result<(),Error>{
|
||||||
let (name,source)=script.name_source(context).map_err(Error::Script)?;
|
let (name,source)=script.get_name_source(context).map_err(Error::Script)?;
|
||||||
self.lua.globals().set("script",super::instance::Instance::from(script)).map_err(|error|Error::Lua{source:source.clone(),error})?;
|
self.lua.globals().set("script",script).map_err(|error|Error::Lua{source:source.clone(),error})?;
|
||||||
//this makes set_app_data shut up about the lifetime
|
//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)});
|
self.lua.set_app_data::<&'static mut rbx_dom_weak::WeakDom>(unsafe{core::mem::transmute(&mut context.dom)});
|
||||||
let r=self.lua.load(source.as_str())
|
let r=self.lua.load(source.as_str())
|
||||||
|
@ -1,37 +0,0 @@
|
|||||||
use rbx_dom_weak::types::Ref;
|
|
||||||
|
|
||||||
use crate::context::Context;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum Error{
|
|
||||||
NoScript,
|
|
||||||
NoSource,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_full_name(dom:&rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance)->String{
|
|
||||||
let mut full_name=instance.name.clone();
|
|
||||||
let mut pref=instance.parent();
|
|
||||||
while let Some(parent)=dom.get_by_ref(pref){
|
|
||||||
full_name.insert(0,'.');
|
|
||||||
full_name.insert_str(0,parent.name.as_str());
|
|
||||||
pref=parent.parent();
|
|
||||||
}
|
|
||||||
full_name
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Script{
|
|
||||||
pub(crate)script:Ref,
|
|
||||||
}
|
|
||||||
impl Script{
|
|
||||||
pub const fn new(script:Ref)->Self{
|
|
||||||
Self{script}
|
|
||||||
}
|
|
||||||
pub fn name_source(&self,context:&Context)->Result<(String,String),Error>{
|
|
||||||
let instance=context.dom.get_by_ref(self.script).ok_or(Error::NoScript)?;
|
|
||||||
let source=match instance.properties.get("Source").ok_or(Error::NoSource)?{
|
|
||||||
rbx_dom_weak::types::Variant::String(s)=>s.clone(),
|
|
||||||
_=>Err(Error::NoSource)?,
|
|
||||||
};
|
|
||||||
Ok((get_full_name(&context.dom,instance),source))
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user