From 1161b9f4e8aa1a5a8627f07850be34223972bdb2 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Fri, 17 Mar 2023 09:12:46 +0100 Subject: [PATCH] Handle OptionalCFrame in roblox conversion layer --- .../lib-roblox/src/datatypes/conversion.rs | 42 ++++++++++++------- .../lib-roblox/src/datatypes/types/cframe.rs | 2 + 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/packages/lib-roblox/src/datatypes/conversion.rs b/packages/lib-roblox/src/datatypes/conversion.rs index 73eb715..0947d09 100644 --- a/packages/lib-roblox/src/datatypes/conversion.rs +++ b/packages/lib-roblox/src/datatypes/conversion.rs @@ -1,5 +1,3 @@ -use std::any::type_name; - use mlua::prelude::*; use rbx_dom_weak::types::{Variant as RbxVariant, VariantType as RbxVariantType}; @@ -121,7 +119,6 @@ impl<'lua> RbxVariantToLua<'lua> for LuaAnyUserData<'lua> { // check `EnumItem::from_instance_property` for specifics Ok(match variant.clone() { // Not yet implemented datatypes - // Rbx::OptionalCFrame(_) => todo!(), // Rbx::PhysicalProperties(_) => todo!(), // Rbx::Ray(_) => todo!(), // Rbx::Region3(_) => todo!(), @@ -131,6 +128,10 @@ impl<'lua> RbxVariantToLua<'lua> for LuaAnyUserData<'lua> { Rbx::Faces(value) => lua.create_userdata(Faces::from(value))?, Rbx::CFrame(value) => lua.create_userdata(CFrame::from(value))?, + Rbx::OptionalCFrame(value) => match value { + Some(value) => lua.create_userdata(CFrame::from(value))?, + None => lua.create_userdata(CFrame::IDENTITY)? + }, Rbx::BrickColor(value) => lua.create_userdata(BrickColor::from(value))?, Rbx::Color3(value) => lua.create_userdata(Color3::from(value))?, @@ -174,7 +175,11 @@ impl<'lua> LuaToRbxVariant<'lua> for LuaAnyUserData<'lua> { RbxVariantType::Axes => convert::, RbxVariantType::Faces => convert::, - RbxVariantType::CFrame => convert::, + RbxVariantType::CFrame => convert::, + RbxVariantType::OptionalCFrame => return match self.borrow::() { + Ok(value) => Ok(RbxVariant::OptionalCFrame(Some(rbx::CFrame::from(*value)))), + Err(e) => Err(lua_userdata_error_to_conversion_error(variant_type, e)), + }, RbxVariantType::BrickColor => convert::, RbxVariantType::Color3 => convert::, @@ -216,15 +221,24 @@ where { match userdata.borrow::() { Ok(value) => Ok(RbxType::from(value.clone()).into()), - Err(LuaError::UserDataTypeMismatch) => Err(DatatypeConversionError::ToRbxVariant { - to: variant_type.variant_name(), - from: type_name::(), - detail: Some("Type mismatch".to_string()), - }), - Err(e) => Err(DatatypeConversionError::ToRbxVariant { - to: variant_type.variant_name(), - from: type_name::(), - detail: Some(format!("Internal error: {e}")), - }), + Err(e) => Err(lua_userdata_error_to_conversion_error(variant_type, e)), + } +} + +fn lua_userdata_error_to_conversion_error( + variant_type: RbxVariantType, + error: LuaError, +) -> DatatypeConversionError { + match error { + LuaError::UserDataTypeMismatch => DatatypeConversionError::ToRbxVariant { + to: variant_type.variant_name(), + from: "userdata", + detail: Some("Type mismatch".to_string()), + }, + e => DatatypeConversionError::ToRbxVariant { + to: variant_type.variant_name(), + from: "userdata", + detail: Some(format!("Internal error: {e}")), + }, } } diff --git a/packages/lib-roblox/src/datatypes/types/cframe.rs b/packages/lib-roblox/src/datatypes/types/cframe.rs index 1d83978..8fced77 100644 --- a/packages/lib-roblox/src/datatypes/types/cframe.rs +++ b/packages/lib-roblox/src/datatypes/types/cframe.rs @@ -18,6 +18,8 @@ use super::{super::*, Vector3}; pub struct CFrame(Mat4); impl CFrame { + pub const IDENTITY: Self = Self(Mat4::IDENTITY); + fn position(&self) -> Vec3 { self.0.w_axis.truncate() }