From 7403888348fa4d929f04d12b6b98aab7ddf66f21 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 3 Oct 2024 18:05:44 -0700 Subject: [PATCH] Enum --- src/runner/enum.rs | 86 ++++++++++++++++++++++++++++++++++++++++++ src/runner/instance.rs | 4 ++ src/runner/mod.rs | 1 + src/runner/runner.rs | 1 + 4 files changed, 92 insertions(+) create mode 100644 src/runner/enum.rs diff --git a/src/runner/enum.rs b/src/runner/enum.rs new file mode 100644 index 0000000..0fcda7a --- /dev/null +++ b/src/runner/enum.rs @@ -0,0 +1,86 @@ +use mlua::IntoLua; + +#[derive(Clone,Copy)] +pub struct Enum(u32); +pub struct EnumItems; +pub struct EnumItem{ + name:String +} + +impl Into 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), + } + }); + } +} +impl<'lua> mlua::FromLua<'lua> for EnumItem{ + fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult{ + match value{ + mlua::Value::UserData(ud)=>ud.take()?, + other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))), + } + } +} + +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), + } + }); + } +} +impl<'lua> mlua::FromLua<'lua> for EnumItems{ + fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult{ + match value{ + mlua::Value::UserData(ud)=>ud.take()?, + other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))), + } + } +} + +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){ + } +} +impl<'lua> mlua::FromLua<'lua> for Enum{ + fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult{ + match value{ + mlua::Value::UserData(ud)=>ud.take()?, + other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))), + } + } +} diff --git a/src/runner/instance.rs b/src/runner/instance.rs index b26e42d..2464224 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -160,6 +160,10 @@ impl Instance{ let typed_value:f32=coerce_float32(&value).ok_or(mlua::Error::runtime("Expected f32"))?; instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Float32(typed_value)); }, + rbx_types::Variant::Enum(_)=>{ + let typed_value:super::r#enum::Enum=value.as_userdata().ok_or(mlua::Error::runtime("Expected Userdata"))?.take()?; + instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Enum(typed_value.into())); + }, rbx_types::Variant::Color3(_)=>{ let typed_value:super::color3::Color3=value.as_userdata().ok_or(mlua::Error::runtime("Expected Userdata"))?.take()?; instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Color3(typed_value.into())); diff --git a/src/runner/mod.rs b/src/runner/mod.rs index 0b364fd..abec22e 100644 --- a/src/runner/mod.rs +++ b/src/runner/mod.rs @@ -1,5 +1,6 @@ mod runner; +mod r#enum; mod color3; mod cframe; mod vector3; diff --git a/src/runner/runner.rs b/src/runner/runner.rs index 658da3f..130702a 100644 --- a/src/runner/runner.rs +++ b/src/runner/runner.rs @@ -29,6 +29,7 @@ fn init(lua:&mlua::Lua)->mlua::Result<()>{ //global environment let globals=lua.globals(); + super::r#enum::set_globals(lua,&globals)?; super::color3::set_globals(lua,&globals)?; super::vector3::set_globals(lua,&globals)?; super::cframe::set_globals(lua,&globals)?;