2024-10-04 01:05:44 +00:00
|
|
|
use mlua::IntoLua;
|
|
|
|
|
|
|
|
#[derive(Clone,Copy)]
|
|
|
|
pub struct Enum(u32);
|
2024-10-05 19:35:38 +00:00
|
|
|
#[derive(Clone,Copy)]
|
2024-10-04 01:05:44 +00:00
|
|
|
pub struct EnumItems;
|
2024-10-05 19:35:38 +00:00
|
|
|
#[derive(Clone,Copy)]
|
2024-10-05 00:33:59 +00:00
|
|
|
pub struct EnumItem<'a>{
|
|
|
|
ed:&'a rbx_reflection::EnumDescriptor<'a>,
|
2024-10-04 01:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<rbx_types::Enum> for Enum{
|
|
|
|
fn into(self)->rbx_types::Enum{
|
|
|
|
rbx_types::Enum::from_u32(self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-05 00:33:59 +00:00
|
|
|
impl<'a> EnumItem<'a>{
|
|
|
|
const fn new(ed:&'a rbx_reflection::EnumDescriptor)->Self{
|
|
|
|
Self{ed}
|
2024-10-04 01:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-05 19:35:38 +00:00
|
|
|
pub fn set_globals(_lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
2024-10-04 01:05:44 +00:00
|
|
|
globals.set("Enum",EnumItems)
|
|
|
|
}
|
|
|
|
|
2024-10-05 00:33:59 +00:00
|
|
|
impl mlua::UserData for EnumItem<'_>{
|
2024-10-05 19:35:38 +00:00
|
|
|
fn add_fields<F:mlua::UserDataFields<Self>>(_fields:&mut F){
|
2024-10-04 01:05:44 +00:00
|
|
|
}
|
2024-10-05 19:35:38 +00:00
|
|
|
fn add_methods<M:mlua::UserDataMethods<Self>>(methods:&mut M){
|
|
|
|
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(this,val):(EnumItem<'_>,mlua::String)|{
|
|
|
|
match this.ed.items.get(&*val.to_str()?){
|
2024-10-05 00:33:59 +00:00
|
|
|
Some(&id)=>Enum(id).into_lua(lua),
|
2024-10-04 01:05:44 +00:00
|
|
|
None=>mlua::Value::Nil.into_lua(lua),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2024-10-05 00:33:59 +00:00
|
|
|
type_from_lua_userdata_lua_lifetime!(EnumItem);
|
2024-10-04 01:05:44 +00:00
|
|
|
|
|
|
|
impl mlua::UserData for EnumItems{
|
2024-10-05 19:35:38 +00:00
|
|
|
fn add_fields<F:mlua::UserDataFields<Self>>(_fields:&mut F){
|
2024-10-04 01:05:44 +00:00
|
|
|
}
|
2024-10-05 19:35:38 +00:00
|
|
|
fn add_methods<M:mlua::UserDataMethods<Self>>(methods:&mut M){
|
2024-10-05 00:33:59 +00:00
|
|
|
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(_,val):(Self,mlua::String)|{
|
2024-10-04 01:05:44 +00:00
|
|
|
let db=rbx_reflection_database::get();
|
2024-10-05 19:35:38 +00:00
|
|
|
match db.enums.get(&*val.to_str()?){
|
2024-10-05 00:33:59 +00:00
|
|
|
Some(ed)=>EnumItem::new(ed).into_lua(lua),
|
2024-10-04 01:05:44 +00:00
|
|
|
None=>mlua::Value::Nil.into_lua(lua),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2024-10-04 01:53:17 +00:00
|
|
|
type_from_lua_userdata!(EnumItems);
|
2024-10-04 01:05:44 +00:00
|
|
|
|
|
|
|
impl mlua::UserData for Enum{
|
2024-10-05 19:35:38 +00:00
|
|
|
fn add_fields<F:mlua::UserDataFields<Self>>(_fields:&mut F){
|
2024-10-04 01:05:44 +00:00
|
|
|
}
|
2024-10-05 19:35:38 +00:00
|
|
|
fn add_methods<M:mlua::UserDataMethods<Self>>(_methods:&mut M){
|
2024-10-04 01:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-04 01:53:17 +00:00
|
|
|
type_from_lua_userdata!(Enum);
|