roblox_emulator/src/runner/enum.rs

62 lines
1.6 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;
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
}
}
pub fn set_globals(_lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Error>{
globals.set("Enum",EnumItems)
}
2024-10-05 00:33:59 +00:00
impl mlua::UserData for EnumItem<'_>{
2024-10-04 01:05:44 +00:00
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-05 00:33:59 +00:00
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(this,val):(EnumItem<'lua>,mlua::String)|{
match this.ed.items.get(val.to_str()?){
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{
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-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 00:33:59 +00:00
match db.enums.get(val.to_str()?){
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{
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);