no double enum database access

This commit is contained in:
Quaternions 2024-10-04 17:33:59 -07:00
parent d496a2dac5
commit 9b077d6db7
2 changed files with 25 additions and 17 deletions

View File

@ -3,8 +3,8 @@ use mlua::IntoLua;
#[derive(Clone,Copy)] #[derive(Clone,Copy)]
pub struct Enum(u32); pub struct Enum(u32);
pub struct EnumItems; pub struct EnumItems;
pub struct EnumItem{ pub struct EnumItem<'a>{
name:String ed:&'a rbx_reflection::EnumDescriptor<'a>,
} }
impl Into<rbx_types::Enum> for Enum{ impl Into<rbx_types::Enum> for Enum{
@ -13,9 +13,9 @@ impl Into<rbx_types::Enum> for Enum{
} }
} }
impl EnumItem{ impl<'a> EnumItem<'a>{
const fn new(name:String)->Self{ const fn new(ed:&'a rbx_reflection::EnumDescriptor)->Self{
Self{name} Self{ed}
} }
} }
@ -23,32 +23,28 @@ pub fn set_globals(_lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Er
globals.set("Enum",EnumItems) globals.set("Enum",EnumItems)
} }
impl mlua::UserData for EnumItem{ impl mlua::UserData for EnumItem<'_>{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){ fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){
} }
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){ fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(this,val):(Self,String)|{ methods.add_meta_function(mlua::MetaMethod::Index,|lua,(this,val):(EnumItem<'lua>,mlua::String)|{
let db=rbx_reflection_database::get(); match this.ed.items.get(val.to_str()?){
match db.enums.get(this.name.as_str()){ Some(&id)=>Enum(id).into_lua(lua),
Some(e)=>match e.items.get(val.as_str()){
Some(&id)=>Enum(id).into_lua(lua),
None=>mlua::Value::Nil.into_lua(lua),
},
None=>mlua::Value::Nil.into_lua(lua), None=>mlua::Value::Nil.into_lua(lua),
} }
}); });
} }
} }
type_from_lua_userdata!(EnumItem); type_from_lua_userdata_lua_lifetime!(EnumItem);
impl mlua::UserData for EnumItems{ impl mlua::UserData for EnumItems{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){ fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){
} }
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){ fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(_,val):(Self,String)|{ methods.add_meta_function(mlua::MetaMethod::Index,|lua,(_,val):(Self,mlua::String)|{
let db=rbx_reflection_database::get(); let db=rbx_reflection_database::get();
match db.enums.get(val.as_str()){ match db.enums.get(val.to_str()?){
Some(_)=>EnumItem::new(val).into_lua(lua), Some(ed)=>EnumItem::new(ed).into_lua(lua),
None=>mlua::Value::Nil.into_lua(lua), None=>mlua::Value::Nil.into_lua(lua),
} }
}); });

View File

@ -10,3 +10,15 @@ macro_rules! type_from_lua_userdata{
} }
}; };
} }
macro_rules! type_from_lua_userdata_lua_lifetime{
($asd:ident)=>{
impl<'lua> mlua::FromLua<'lua> for $asd<'lua>{
fn from_lua(value:mlua::Value<'lua>,_lua:&'lua mlua::Lua)->Result<Self,mlua::Error>{
match value{
mlua::Value::UserData(ud)=>ud.take(),
other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!($asd),other))),
}
}
}
};
}