This commit is contained in:
Quaternions 2024-10-03 18:05:44 -07:00
parent c06ba65662
commit 7403888348
4 changed files with 92 additions and 0 deletions

86
src/runner/enum.rs Normal file
View 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))),
}
}
}

View File

@ -160,6 +160,10 @@ impl Instance{
let typed_value:f32=coerce_float32(&value).ok_or(mlua::Error::runtime("Expected f32"))?; 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)); 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(_)=>{ rbx_types::Variant::Color3(_)=>{
let typed_value:super::color3::Color3=value.as_userdata().ok_or(mlua::Error::runtime("Expected Userdata"))?.take()?; 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())); instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Color3(typed_value.into()));

View File

@ -1,5 +1,6 @@
mod runner; mod runner;
mod r#enum;
mod color3; mod color3;
mod cframe; mod cframe;
mod vector3; mod vector3;

View File

@ -29,6 +29,7 @@ fn init(lua:&mlua::Lua)->mlua::Result<()>{
//global environment //global environment
let globals=lua.globals(); let globals=lua.globals();
super::r#enum::set_globals(lua,&globals)?;
super::color3::set_globals(lua,&globals)?; super::color3::set_globals(lua,&globals)?;
super::vector3::set_globals(lua,&globals)?; super::vector3::set_globals(lua,&globals)?;
super::cframe::set_globals(lua,&globals)?; super::cframe::set_globals(lua,&globals)?;