roblox_emulator/src/runner/enum.rs

66 lines
1.7 KiB
Rust
Raw Normal View History

2024-10-04 01:05:44 +00:00
use mlua::IntoLua;
#[derive(Clone,Copy)]
pub struct Enum(u32);
pub struct EnumItems;
pub struct EnumItem{
name:String
}
impl Into<rbx_types::Enum> for Enum{
fn into(self)->rbx_types::Enum{
rbx_types::Enum::from_u32(self.0)
}
}
impl EnumItem{
const fn new(name:String)->Self{
Self{name}
}
}
pub fn set_globals(_lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Error>{
globals.set("Enum",EnumItems)
}
impl mlua::UserData for EnumItem{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){
}
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(this,val):(Self,String)|{
let db=rbx_reflection_database::get();
match db.enums.get(this.name.as_str()){
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),
}
});
}
}
2024-10-04 01:53:17 +00:00
type_from_lua_userdata!(EnumItem);
2024-10-04 01:05:44 +00:00
impl mlua::UserData for EnumItems{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){
}
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(_,val):(Self,String)|{
let db=rbx_reflection_database::get();
match db.enums.get(val.as_str()){
Some(_)=>EnumItem::new(val).into_lua(lua),
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{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){
}
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(_methods:&mut M){
}
}
2024-10-04 01:53:17 +00:00
type_from_lua_userdata!(Enum);