Enum
This commit is contained in:
parent
c06ba65662
commit
7403888348
86
src/runner/enum.rs
Normal file
86
src/runner/enum.rs
Normal file
@ -0,0 +1,86 @@
|
||||
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),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
impl<'lua> mlua::FromLua<'lua> for EnumItem{
|
||||
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
|
||||
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<Self>{
|
||||
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<Self>{
|
||||
match value{
|
||||
mlua::Value::UserData(ud)=>ud.take()?,
|
||||
other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))),
|
||||
}
|
||||
}
|
||||
}
|
@ -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()));
|
||||
|
@ -1,5 +1,6 @@
|
||||
mod runner;
|
||||
|
||||
mod r#enum;
|
||||
mod color3;
|
||||
mod cframe;
|
||||
mod vector3;
|
||||
|
@ -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)?;
|
||||
|
Loading…
Reference in New Issue
Block a user