From e80ae6cc95cd2b8c64ae6e400d4d3e7aac00f566 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 11:13:37 +0000 Subject: [PATCH 01/19] Add EnumItem variant --- rbx_types/src/basic_types.rs | 27 +++++++++++++++++++++++++++ rbx_types/src/variant.rs | 7 ++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/rbx_types/src/basic_types.rs b/rbx_types/src/basic_types.rs index 4d601311..84bfa7b4 100644 --- a/rbx_types/src/basic_types.rs +++ b/rbx_types/src/basic_types.rs @@ -29,6 +29,22 @@ impl Enum { } } +#[derive(Debug, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize,))] +pub struct EnumItem { + #[serde(rename = "type")] + pub ty: String, + pub value: u32, +} + +impl From for Enum { + fn from(enum_item: EnumItem) -> Self { + Self { + value: enum_item.value, + } + } +} + /// The standard 2D vector type used in Roblox. /// /// ## See Also @@ -718,4 +734,15 @@ mod serde_test { "[[1.0,2.0,3.0],[4.0,5.0,6.0],[7.0,8.0,9.0]]", ); } + + #[test] + fn tagged_enum_json() { + test_ser( + EnumItem { + ty: "PlayTag".to_string(), + value: 3, + }, + r#"{"type":"PlayTag","value":3}"#, + ); + } } diff --git a/rbx_types/src/variant.rs b/rbx_types/src/variant.rs index 597c8747..801a41b5 100644 --- a/rbx_types/src/variant.rs +++ b/rbx_types/src/variant.rs @@ -1,8 +1,8 @@ use crate::{ Attributes, Axes, BinaryString, BrickColor, CFrame, Color3, Color3uint8, ColorSequence, - Content, Enum, Faces, Font, MaterialColors, NumberRange, NumberSequence, PhysicalProperties, - Ray, Rect, Ref, Region3, Region3int16, SecurityCapabilities, SharedString, Tags, UDim, UDim2, - UniqueId, Vector2, Vector2int16, Vector3, Vector3int16, + Content, Enum, EnumItem, Faces, Font, MaterialColors, NumberRange, NumberSequence, + PhysicalProperties, Ray, Rect, Ref, Region3, Region3int16, SecurityCapabilities, SharedString, + Tags, UDim, UDim2, UniqueId, Vector2, Vector2int16, Vector3, Vector3int16, }; /// Reduces boilerplate from listing different values of Variant by wrapping @@ -129,6 +129,7 @@ make_variant! { UniqueId(UniqueId), MaterialColors(MaterialColors), SecurityCapabilities(SecurityCapabilities), + EnumItem(EnumItem), } impl From<&'_ str> for Variant { From 2b0dc9123589d8fa9329e06ee91b4417829bba07 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 11:14:54 +0000 Subject: [PATCH 02/19] Add enum attribute tests to rbx_xml and rbx_binary --- rbx_binary/src/tests/models.rs | 1 + rbx_xml/src/tests/models.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/rbx_binary/src/tests/models.rs b/rbx_binary/src/tests/models.rs index 5105d6bc..d47df849 100644 --- a/rbx_binary/src/tests/models.rs +++ b/rbx_binary/src/tests/models.rs @@ -66,4 +66,5 @@ binary_tests! { folder_with_font_attribute, number_values_with_security_capabilities, lighting_with_int32_attribute, + folder_with_enum_attribute, } diff --git a/rbx_xml/src/tests/models.rs b/rbx_xml/src/tests/models.rs index eaffd828..b6c58abb 100644 --- a/rbx_xml/src/tests/models.rs +++ b/rbx_xml/src/tests/models.rs @@ -67,4 +67,5 @@ model_tests! { folder_with_font_attribute, number_values_with_security_capabilities, lighting_with_int32_attribute, + folder_with_enum_attribute, } From ff0bbe8d6587ae3e1409929527478b2b6bb17f02 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 11:15:58 +0000 Subject: [PATCH 03/19] Implement EnumItem attribute reader and writer --- rbx_types/src/attributes/error.rs | 3 +++ rbx_types/src/attributes/reader.rs | 13 ++++++++++++- rbx_types/src/attributes/type_id.rs | 2 +- rbx_types/src/attributes/writer.rs | 4 ++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/rbx_types/src/attributes/error.rs b/rbx_types/src/attributes/error.rs index 06386239..aba63244 100644 --- a/rbx_types/src/attributes/error.rs +++ b/rbx_types/src/attributes/error.rs @@ -30,6 +30,9 @@ pub(crate) enum AttributeError { #[error(transparent)] Io(#[from] std::io::Error), + #[error(transparent)] + Utf8(#[from] std::string::FromUtf8Error), + #[error(transparent)] BadAttributeValue(#[from] crate::Error), diff --git a/rbx_types/src/attributes/reader.rs b/rbx_types/src/attributes/reader.rs index 74c17ad5..a1b4d678 100644 --- a/rbx_types/src/attributes/reader.rs +++ b/rbx_types/src/attributes/reader.rs @@ -4,7 +4,7 @@ use std::{ }; use crate::{ - BinaryString, BrickColor, CFrame, Color3, ColorSequence, ColorSequenceKeypoint, Font, + BinaryString, BrickColor, CFrame, Color3, ColorSequence, ColorSequenceKeypoint, EnumItem, Font, FontStyle, FontWeight, Matrix3, NumberRange, NumberSequence, NumberSequenceKeypoint, Rect, UDim, UDim2, Variant, VariantType, Vector2, Vector3, }; @@ -202,6 +202,17 @@ pub(crate) fn read_attributes( } .into(), + VariantType::EnumItem => { + let enum_type = read_string(&mut value)?; + let value = read_u32(&mut value)?; + + EnumItem { + ty: String::from_utf8(enum_type)?, + value, + } + } + .into(), + other => return Err(AttributeError::UnsupportedVariantType(other)), }; diff --git a/rbx_types/src/attributes/type_id.rs b/rbx_types/src/attributes/type_id.rs index 2af23ec7..af595727 100644 --- a/rbx_types/src/attributes/type_id.rs +++ b/rbx_types/src/attributes/type_id.rs @@ -41,7 +41,7 @@ type_ids! { // ??? => 0x12, // ??? => 0x13, CFrame => 0x14, - // ??? => 0x15, + EnumItem => 0x15, // ??? => 0x16, NumberSequence => 0x17, // ??? => 0x18, diff --git a/rbx_types/src/attributes/writer.rs b/rbx_types/src/attributes/writer.rs index bd32da72..2684cadf 100644 --- a/rbx_types/src/attributes/writer.rs +++ b/rbx_types/src/attributes/writer.rs @@ -99,6 +99,10 @@ pub(crate) fn write_attributes( font.cached_face_id.as_deref().unwrap_or_default(), )?; } + Variant::EnumItem(enum_item) => { + write_string(&mut writer, &enum_item.ty)?; + write_u32(&mut writer, enum_item.value)?; + } other_variant => unreachable!("variant {:?} was not implemented", other_variant), } From 5ead1f4f0236f6a8a3d0717d3fdbed0a5c9f953c Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 11:17:02 +0000 Subject: [PATCH 04/19] Run tests --- ...__folder-with-enum-attribute__decoded.snap | 23 ++++++++ ...__folder-with-enum-attribute__encoded.snap | 55 +++++++++++++++++ ...il__folder-with-enum-attribute__input.snap | 59 +++++++++++++++++++ ...__folder-with-enum-attribute__decoded.snap | 23 ++++++++ ...folder-with-enum-attribute__roundtrip.snap | 23 ++++++++ 5 files changed, 183 insertions(+) create mode 100644 rbx_binary/src/tests/snapshots/rbx_binary__tests__util__folder-with-enum-attribute__decoded.snap create mode 100644 rbx_binary/src/tests/snapshots/rbx_binary__tests__util__folder-with-enum-attribute__encoded.snap create mode 100644 rbx_binary/src/tests/snapshots/rbx_binary__tests__util__folder-with-enum-attribute__input.snap create mode 100644 rbx_xml/src/tests/snapshots/rbx_xml__tests__folder-with-enum-attribute__decoded.snap create mode 100644 rbx_xml/src/tests/snapshots/rbx_xml__tests__folder-with-enum-attribute__roundtrip.snap diff --git a/rbx_binary/src/tests/snapshots/rbx_binary__tests__util__folder-with-enum-attribute__decoded.snap b/rbx_binary/src/tests/snapshots/rbx_binary__tests__util__folder-with-enum-attribute__decoded.snap new file mode 100644 index 00000000..a6ce7553 --- /dev/null +++ b/rbx_binary/src/tests/snapshots/rbx_binary__tests__util__folder-with-enum-attribute__decoded.snap @@ -0,0 +1,23 @@ +--- +source: rbx_binary/src/tests/util.rs +expression: decoded_viewed +--- +- referent: referent-0 + name: Folder + class: Folder + properties: + Attributes: + Attributes: + AnEnumValue: + EnumItem: + type: Material + value: 512 + Capabilities: + SecurityCapabilities: 0 + Sandboxed: + Bool: false + SourceAssetId: + Int64: -1 + Tags: + Tags: [] + children: [] diff --git a/rbx_binary/src/tests/snapshots/rbx_binary__tests__util__folder-with-enum-attribute__encoded.snap b/rbx_binary/src/tests/snapshots/rbx_binary__tests__util__folder-with-enum-attribute__encoded.snap new file mode 100644 index 00000000..51d6284b --- /dev/null +++ b/rbx_binary/src/tests/snapshots/rbx_binary__tests__util__folder-with-enum-attribute__encoded.snap @@ -0,0 +1,55 @@ +--- +source: rbx_binary/src/tests/util.rs +expression: text_roundtrip +--- +num_types: 1 +num_instances: 1 +chunks: + - Inst: + type_id: 0 + type_name: Folder + object_format: 0 + referents: + - 0 + - Prop: + type_id: 0 + prop_name: AttributesSerialize + prop_type: String + values: + - "\u0001\u0000\u0000\u0000\u000b\u0000\u0000\u0000AnEnumValue\u0015\b\u0000\u0000\u0000Material\u0000\u0002\u0000\u0000" + - Prop: + type_id: 0 + prop_name: Capabilities + prop_type: SecurityCapabilities + values: + - 0 + - Prop: + type_id: 0 + prop_name: Name + prop_type: String + values: + - Folder + - Prop: + type_id: 0 + prop_name: DefinesCapabilities + prop_type: Bool + values: + - false + - Prop: + type_id: 0 + prop_name: SourceAssetId + prop_type: Int64 + values: + - -1 + - Prop: + type_id: 0 + prop_name: Tags + prop_type: String + values: + - "" + - Prnt: + version: 0 + links: + - - 0 + - -1 + - End diff --git a/rbx_binary/src/tests/snapshots/rbx_binary__tests__util__folder-with-enum-attribute__input.snap b/rbx_binary/src/tests/snapshots/rbx_binary__tests__util__folder-with-enum-attribute__input.snap new file mode 100644 index 00000000..815d4131 --- /dev/null +++ b/rbx_binary/src/tests/snapshots/rbx_binary__tests__util__folder-with-enum-attribute__input.snap @@ -0,0 +1,59 @@ +--- +source: rbx_binary/src/tests/util.rs +expression: text_decoded +--- +num_types: 1 +num_instances: 1 +chunks: + - Meta: + entries: + - - ExplicitAutoJoints + - "true" + - Inst: + type_id: 0 + type_name: Folder + object_format: 0 + referents: + - 0 + - Prop: + type_id: 0 + prop_name: AttributesSerialize + prop_type: String + values: + - "\u0001\u0000\u0000\u0000\u000b\u0000\u0000\u0000AnEnumValue\u0015\b\u0000\u0000\u0000Material\u0000\u0002\u0000\u0000" + - Prop: + type_id: 0 + prop_name: Capabilities + prop_type: SecurityCapabilities + values: + - 0 + - Prop: + type_id: 0 + prop_name: DefinesCapabilities + prop_type: Bool + values: + - false + - Prop: + type_id: 0 + prop_name: Name + prop_type: String + values: + - Folder + - Prop: + type_id: 0 + prop_name: SourceAssetId + prop_type: Int64 + values: + - -1 + - Prop: + type_id: 0 + prop_name: Tags + prop_type: String + values: + - "" + - Prnt: + version: 0 + links: + - - 0 + - -1 + - End diff --git a/rbx_xml/src/tests/snapshots/rbx_xml__tests__folder-with-enum-attribute__decoded.snap b/rbx_xml/src/tests/snapshots/rbx_xml__tests__folder-with-enum-attribute__decoded.snap new file mode 100644 index 00000000..27baa1df --- /dev/null +++ b/rbx_xml/src/tests/snapshots/rbx_xml__tests__folder-with-enum-attribute__decoded.snap @@ -0,0 +1,23 @@ +--- +source: rbx_xml/src/tests/mod.rs +expression: "DomViewer::new().view_children(&decoded)" +--- +- referent: referent-0 + name: Folder + class: Folder + properties: + Attributes: + Attributes: + AnEnumValue: + EnumItem: + type: Material + value: 512 + Capabilities: + SecurityCapabilities: 0 + Sandboxed: + Bool: false + SourceAssetId: + Int64: -1 + Tags: + Tags: [] + children: [] diff --git a/rbx_xml/src/tests/snapshots/rbx_xml__tests__folder-with-enum-attribute__roundtrip.snap b/rbx_xml/src/tests/snapshots/rbx_xml__tests__folder-with-enum-attribute__roundtrip.snap new file mode 100644 index 00000000..7f3f2704 --- /dev/null +++ b/rbx_xml/src/tests/snapshots/rbx_xml__tests__folder-with-enum-attribute__roundtrip.snap @@ -0,0 +1,23 @@ +--- +source: rbx_xml/src/tests/mod.rs +expression: "DomViewer::new().view_children(&roundtrip)" +--- +- referent: referent-0 + name: Folder + class: Folder + properties: + Attributes: + Attributes: + AnEnumValue: + EnumItem: + type: Material + value: 512 + Capabilities: + SecurityCapabilities: 0 + Sandboxed: + Bool: false + SourceAssetId: + Int64: -1 + Tags: + Tags: [] + children: [] From c1a08e4eb86243dba40f512b7380da7e9429b6ff Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 11:20:14 +0000 Subject: [PATCH 05/19] Fix EnumItem link on spec page --- docs/attributes.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/attributes.md b/docs/attributes.md index fda407fc..ff513276 100644 --- a/docs/attributes.md +++ b/docs/attributes.md @@ -18,7 +18,7 @@ This document describes the Attribute binary format. In this format there is no - [Vector2](#vector2) - [Vector3](#vector3) - [CFrame](#cframe) - - [EnumItem](#EnumItem) + - [EnumItem](#enumitem) - [NumberSequence](#numbersequence) - [ColorSequence](#colorsequence) - [NumberRange](#numberrange) @@ -27,7 +27,7 @@ This document describes the Attribute binary format. In this format there is no ## Document Conventions -This document assumes a basic understanding of Rust's convention for numeric types. For example: +This document assumes a basic understanding of Rust's convention for numeric types. For example: - `u32` is an unsigned 32-bit integer - `f32` is a 32-bit floating point @@ -189,7 +189,7 @@ Demonstrating the axis-aligned rotation matrix case, a `CFrame` with the value ` ### EnumItem **Type ID `0x15`** -The `EnumItem` type is composed of two parts: +The `EnumItem` type is composed of two parts: | Field Name | Format | Value | |:-----------|:--------------------|:-------------------------------------------------------| From 8d10dc7b5341852d6e5fab2ba8733f736c9e7f99 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 11:37:09 +0000 Subject: [PATCH 06/19] Update Enum and EnumItem docs --- rbx_types/src/basic_types.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/rbx_types/src/basic_types.rs b/rbx_types/src/basic_types.rs index 84bfa7b4..174a431e 100644 --- a/rbx_types/src/basic_types.rs +++ b/rbx_types/src/basic_types.rs @@ -2,13 +2,13 @@ use thiserror::Error; use crate::Error; -/// Represents any Roblox enum value. +/// Represents any Roblox EnumItem. /// /// Roblox enums are not strongly typed, so the meaning of a value depends on /// where they're assigned. /// /// A list of all enums and their values are available [on the Roblox Developer -/// Hub](https://developer.roblox.com/en-us/api-reference/enum). +/// Hub](https://create.roblox.com/docs/reference/engine/enums). #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[cfg_attr( feature = "serde", @@ -29,6 +29,10 @@ impl Enum { } } +/// Represents a specific Roblox EnumItem. +/// +/// A list of all enums and their values are available [on the Roblox Developer +/// Hub](https://create.roblox.com/docs/reference/engine/enums). #[derive(Debug, Clone, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize,))] pub struct EnumItem { From ac963cf59ade19c30341593a876ddff0d6044026 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 13:17:11 +0000 Subject: [PATCH 07/19] Add rbx_dom_lua codec --- rbx_dom_lua/src/EncodedValue.lua | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/rbx_dom_lua/src/EncodedValue.lua b/rbx_dom_lua/src/EncodedValue.lua index f2e9e343..71ed1396 100644 --- a/rbx_dom_lua/src/EncodedValue.lua +++ b/rbx_dom_lua/src/EncodedValue.lua @@ -205,6 +205,29 @@ types = { end, }, + EnumItem = { + fromPod = function(pod) + local enumType = Enum[pod.type] + local enumItem = 0 + + for _, item in enumType:GetEnumItems() do + if item.Value == pod.value then + enumItem = item + break + end + end + + return enumItem + end, + + toPod = function(roblox) + return { + type = roblox.EnumType, + value = roblox.Value, + } + end, + }, + Faces = { fromPod = function(pod) local faces = {} From e38c07270df2dc0e7ba7f50f0d63ab7add5c1306 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 13:26:59 +0000 Subject: [PATCH 08/19] Rename variable enumType -> enum Hopefully less confusing this way --- rbx_dom_lua/src/EncodedValue.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rbx_dom_lua/src/EncodedValue.lua b/rbx_dom_lua/src/EncodedValue.lua index 71ed1396..208e6f0c 100644 --- a/rbx_dom_lua/src/EncodedValue.lua +++ b/rbx_dom_lua/src/EncodedValue.lua @@ -207,10 +207,10 @@ types = { EnumItem = { fromPod = function(pod) - local enumType = Enum[pod.type] + local enum = Enum[pod.type] local enumItem = 0 - for _, item in enumType:GetEnumItems() do + for _, item in enum:GetEnumItems() do if item.Value == pod.value then enumItem = item break From ab1bd4b1aaf2beb3fb4cb3800471135fac12b42f Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 13:33:11 +0000 Subject: [PATCH 09/19] Whoops, mistake in edge case --- rbx_dom_lua/src/EncodedValue.lua | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/rbx_dom_lua/src/EncodedValue.lua b/rbx_dom_lua/src/EncodedValue.lua index 208e6f0c..a54ed1e3 100644 --- a/rbx_dom_lua/src/EncodedValue.lua +++ b/rbx_dom_lua/src/EncodedValue.lua @@ -208,16 +208,20 @@ types = { EnumItem = { fromPod = function(pod) local enum = Enum[pod.type] - local enumItem = 0 + local enumItems = enum:GetEnumItems() + local chosenEnumItem = enumItems[1] - for _, item in enum:GetEnumItems() do + for _, item in enumItems do if item.Value == pod.value then - enumItem = item + -- If we're not able to find the given enum item here, then that's + -- pretty weird, but we'll just end up with the first one in the + -- EnumItems list + chosenEnumItem = item break end end - return enumItem + return chosenEnumItem end, toPod = function(roblox) From 1cab24004cb362f661614c123555c6f930d2dc91 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 14:37:24 +0000 Subject: [PATCH 10/19] Add EnumItem to rbx_reflector values generation Add EnumItem to generated values --- rbx_dom_lua/src/allValues.json | 9 +++++++++ rbx_reflector/src/cli/values.rs | 16 ++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/rbx_dom_lua/src/allValues.json b/rbx_dom_lua/src/allValues.json index 9b07d7bf..0460bd41 100644 --- a/rbx_dom_lua/src/allValues.json +++ b/rbx_dom_lua/src/allValues.json @@ -182,6 +182,15 @@ }, "ty": "Enum" }, + "EnumItem": { + "value": { + "EnumItem": { + "type": "Material", + "value": 256 + } + }, + "ty": "EnumItem" + }, "Faces": { "value": { "Faces": [ diff --git a/rbx_reflector/src/cli/values.rs b/rbx_reflector/src/cli/values.rs index 387cfbc7..745ac3fd 100644 --- a/rbx_reflector/src/cli/values.rs +++ b/rbx_reflector/src/cli/values.rs @@ -4,10 +4,10 @@ use anyhow::bail; use clap::Parser; use rbx_types::{ Attributes, Axes, BinaryString, BrickColor, CFrame, Color3, Color3uint8, ColorSequence, - ColorSequenceKeypoint, Content, CustomPhysicalProperties, Enum, Faces, Font, MaterialColors, - Matrix3, NumberRange, NumberSequence, NumberSequenceKeypoint, PhysicalProperties, Ray, Rect, - Region3int16, Tags, TerrainMaterials, UDim, UDim2, Variant, VariantType, Vector2, Vector2int16, - Vector3, Vector3int16, + ColorSequenceKeypoint, Content, CustomPhysicalProperties, Enum, EnumItem, Faces, Font, + MaterialColors, Matrix3, NumberRange, NumberSequence, NumberSequenceKeypoint, + PhysicalProperties, Ray, Rect, Region3int16, Tags, TerrainMaterials, UDim, UDim2, Variant, + VariantType, Vector2, Vector2int16, Vector3, Vector3int16, }; use serde::Serialize; @@ -80,6 +80,14 @@ impl ValuesSubcommand { ); values.insert("Content", Content::from("rbxassetid://12345").into()); values.insert("Enum", Enum::from_u32(1234).into()); + values.insert( + "EnumItem", + EnumItem { + ty: "Material".into(), + value: 256, + } + .into(), + ); values.insert("Faces", Faces::all().into()); values.insert("Float32", 15.0f32.into()); values.insert("Float64", 15123.0f64.into()); From a86cbaff74736f03b1f9c57a646c8dae5f466dd8 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 15:00:20 +0000 Subject: [PATCH 11/19] Run rbx_reflector values before rbx_dom_lua tests --- rbx_dom_lua/test | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) mode change 100644 => 100755 rbx_dom_lua/test diff --git a/rbx_dom_lua/test b/rbx_dom_lua/test old mode 100644 new mode 100755 index 9ac1092f..09577d65 --- a/rbx_dom_lua/test +++ b/rbx_dom_lua/test @@ -1,4 +1,5 @@ #!/bin/sh +cargo run --bin rbx_reflector values ./src/allValues.json rojo build test-place.project.json -o TestPlace.rbxlx -run-in-roblox --script run-tests.lua --place TestPlace.rbxlx \ No newline at end of file +run-in-roblox --script run-tests.lua --place TestPlace.rbxlx From 057f6645a1c1b66589d4b43e0f1c2ff2ee5b31ec Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 15:09:24 +0000 Subject: [PATCH 12/19] Fix EnumItem not roundtripping correctly --- rbx_dom_lua/src/EncodedValue.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rbx_dom_lua/src/EncodedValue.lua b/rbx_dom_lua/src/EncodedValue.lua index a54ed1e3..2bbcd3fa 100644 --- a/rbx_dom_lua/src/EncodedValue.lua +++ b/rbx_dom_lua/src/EncodedValue.lua @@ -226,7 +226,7 @@ types = { toPod = function(roblox) return { - type = roblox.EnumType, + type = tostring(roblox.EnumType), value = roblox.Value, } end, From ac2567bc75bf60537d8c373491aeeba40963e483 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 15:31:38 +0000 Subject: [PATCH 13/19] Use Enum:FromValue in Lua decoder --- rbx_dom_lua/src/EncodedValue.lua | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/rbx_dom_lua/src/EncodedValue.lua b/rbx_dom_lua/src/EncodedValue.lua index 2bbcd3fa..060dc980 100644 --- a/rbx_dom_lua/src/EncodedValue.lua +++ b/rbx_dom_lua/src/EncodedValue.lua @@ -208,20 +208,11 @@ types = { EnumItem = { fromPod = function(pod) local enum = Enum[pod.type] - local enumItems = enum:GetEnumItems() - local chosenEnumItem = enumItems[1] - - for _, item in enumItems do - if item.Value == pod.value then - -- If we're not able to find the given enum item here, then that's - -- pretty weird, but we'll just end up with the first one in the - -- EnumItems list - chosenEnumItem = item - break - end - end - return chosenEnumItem + -- If the given EnumItem value is not valid for this Enum, then + -- that's pretty weird, but we'll just return the first one in the + -- list instead + return enum:FromValue(pod.value) or enum:GetEnumItems()[1] end, toPod = function(roblox) From e304c9217f80e24dc770c8b88972e1107c9038cb Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 15:51:45 +0000 Subject: [PATCH 14/19] It's a one-liner now, wow! --- rbx_dom_lua/src/EncodedValue.lua | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/rbx_dom_lua/src/EncodedValue.lua b/rbx_dom_lua/src/EncodedValue.lua index 060dc980..67d78e45 100644 --- a/rbx_dom_lua/src/EncodedValue.lua +++ b/rbx_dom_lua/src/EncodedValue.lua @@ -207,12 +207,7 @@ types = { EnumItem = { fromPod = function(pod) - local enum = Enum[pod.type] - - -- If the given EnumItem value is not valid for this Enum, then - -- that's pretty weird, but we'll just return the first one in the - -- list instead - return enum:FromValue(pod.value) or enum:GetEnumItems()[1] + return Enum[pod.type]:FromValue(pod.value) end, toPod = function(roblox) From 1184fa0aeb4bb97463fde7d6c54d49a3f8b7cf78 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 16:15:54 +0000 Subject: [PATCH 15/19] Add EnumItem to Attributes in allValues, too --- rbx_dom_lua/src/allValues.json | 6 ++++++ rbx_reflector/src/cli/values.rs | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/rbx_dom_lua/src/allValues.json b/rbx_dom_lua/src/allValues.json index 0460bd41..59e531fb 100644 --- a/rbx_dom_lua/src/allValues.json +++ b/rbx_dom_lua/src/allValues.json @@ -15,6 +15,12 @@ 0.0 ] }, + "TestEnumItem": { + "EnumItem": { + "type": "Material", + "value": 256 + } + }, "TestNumber": { "Float64": 1337.0 }, diff --git a/rbx_reflector/src/cli/values.rs b/rbx_reflector/src/cli/values.rs index 745ac3fd..3cccb139 100644 --- a/rbx_reflector/src/cli/values.rs +++ b/rbx_reflector/src/cli/values.rs @@ -45,6 +45,13 @@ impl ValuesSubcommand { "TestUDim2", UDim2::new(UDim::new(1.0, 2), UDim::new(3.0, 4)), ) + .with( + "TestEnumItem", + EnumItem { + ty: "Material".into(), + value: 256, + }, + ) .into(), ); values.insert("Axes", Axes::all().into()); From 4147ab98c185b9d7531e3f0062adea3d20ecd674 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 18:02:43 +0000 Subject: [PATCH 16/19] Add conversion from EnumItem to Enum for binary serializer --- rbx_binary/src/serializer/state.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rbx_binary/src/serializer/state.rs b/rbx_binary/src/serializer/state.rs index 168ee818..e83e11db 100644 --- a/rbx_binary/src/serializer/state.rs +++ b/rbx_binary/src/serializer/state.rs @@ -9,8 +9,8 @@ use ahash::{HashMap, HashMapExt, HashSetExt}; use rbx_dom_weak::{ types::{ Attributes, Axes, BinaryString, BrickColor, CFrame, Color3, Color3uint8, ColorSequence, - ColorSequenceKeypoint, Content, Enum, Faces, Font, MaterialColors, Matrix3, NumberRange, - NumberSequence, NumberSequenceKeypoint, PhysicalProperties, Ray, Rect, Ref, + ColorSequenceKeypoint, Content, Enum, EnumItem, Faces, Font, MaterialColors, Matrix3, + NumberRange, NumberSequence, NumberSequenceKeypoint, PhysicalProperties, Ray, Rect, Ref, SecurityCapabilities, SharedString, Tags, UDim, UDim2, UniqueId, Variant, VariantType, Vector2, Vector3, Vector3int16, }, @@ -970,10 +970,10 @@ impl<'dom, 'db, W: Write> SerializerState<'dom, 'db, W> { let mut buf = Vec::with_capacity(values.len()); for (i, rbx_value) in values { - if let Variant::Enum(value) = rbx_value.as_ref() { - buf.push(value.to_u32()); - } else { - return type_mismatch(i, &rbx_value, "Enum"); + match rbx_value.as_ref() { + Variant::Enum(value) => buf.push(value.to_u32()), + Variant::EnumItem(EnumItem { value, .. }) => buf.push(*value), + _ => return type_mismatch(i, &rbx_value, "Enum or EnumItem"), } } From 28a187ce52c1439fdaf89bad2c8dfe2ab70516dd Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 18:27:06 +0000 Subject: [PATCH 17/19] Add conversion to Enum from EnumItem for rbx_xml serializer --- rbx_xml/src/conversion.rs | 5 ++++- rbx_xml/src/tests/basic.rs | 33 +++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/rbx_xml/src/conversion.rs b/rbx_xml/src/conversion.rs index a878d578..56fb9c76 100644 --- a/rbx_xml/src/conversion.rs +++ b/rbx_xml/src/conversion.rs @@ -5,7 +5,7 @@ use std::borrow::{Borrow, Cow}; use std::convert::TryInto; use rbx_dom_weak::types::{ - Attributes, BrickColor, Color3uint8, MaterialColors, Tags, Variant, VariantType, + Attributes, BrickColor, Color3uint8, Enum, MaterialColors, Tags, Variant, VariantType, }; pub trait ConvertVariant: Clone + Sized { @@ -69,6 +69,9 @@ impl ConvertVariant for Variant { .map_err(|_| "invalid MaterialColors value")? .into(), )), + (Variant::EnumItem(enum_item), VariantType::Enum) => { + Ok(Cow::Owned(Enum::from_u32(enum_item.value).into())) + } (_, _) => Ok(value), } } diff --git a/rbx_xml/src/tests/basic.rs b/rbx_xml/src/tests/basic.rs index e4540eb3..7169844f 100644 --- a/rbx_xml/src/tests/basic.rs +++ b/rbx_xml/src/tests/basic.rs @@ -2,11 +2,11 @@ use rbx_dom_weak::types::{ Attributes, BinaryString, BrickColor, Color3, Color3uint8, ColorSequence, - ColorSequenceKeypoint, Enum, Font, MaterialColors, NumberRange, NumberSequence, - NumberSequenceKeypoint, Rect, Tags, TerrainMaterials, UDim, UDim2, UniqueId, Variant, Vector2, - Vector3, + ColorSequenceKeypoint, Enum, EnumItem, Font, MaterialColors, NumberRange, NumberSequence, + NumberSequenceKeypoint, Rect, Tags, TerrainMaterials, UDim, UDim2, UniqueId, Variant, + VariantType, Vector2, Vector3, }; -use rbx_dom_weak::{InstanceBuilder, WeakDom}; +use rbx_dom_weak::{ustr, InstanceBuilder, WeakDom}; #[test] fn with_bool() { @@ -352,3 +352,28 @@ fn bad_migrated_property() { crate::to_writer_default(&mut encoded, &tree, &[tree.root_ref()]).unwrap(); insta::assert_snapshot!(std::str::from_utf8(&encoded).unwrap()); } + +#[test] +fn enum_item_to_enum() { + let tree = WeakDom::new(InstanceBuilder::new("Part").with_property( + "Material", + EnumItem { + ty: "Material".into(), + value: 256, + }, + )); + + let mut encoded = Vec::new(); + crate::to_writer_default(&mut encoded, &tree, &[tree.root_ref()]).unwrap(); + + let decoded = crate::from_reader_default(encoded.as_slice()).unwrap(); + let prop_type = decoded + .get_by_ref(*decoded.root().children().first().unwrap()) + .unwrap() + .properties + .get(&ustr("Material")) + .unwrap() + .ty(); + + assert_eq!(prop_type, VariantType::Enum); +} From df04fa2b1038a57a502ae9dac247afcbbe0abbcf Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 18:50:11 +0000 Subject: [PATCH 18/19] Update rbx_dom_weak changelog --- rbx_dom_weak/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rbx_dom_weak/CHANGELOG.md b/rbx_dom_weak/CHANGELOG.md index 27d245cb..eedb8ebe 100644 --- a/rbx_dom_weak/CHANGELOG.md +++ b/rbx_dom_weak/CHANGELOG.md @@ -103,7 +103,9 @@ pub fn into_raw(self) -> (Ref, HashMap) { * Added re-exports for `ustr` (a convenience function for creating `Ustr`s), `Ustr`, `UstrMap`, and `UstrSet`. * Added `InstanceBuilder::with_property_capacity`, which can preallocate an `InstanceBuilder`'s property table. [#464] * Added `WeakDom::reserve`, which can preallocate additional space for instances in the `WeakDom`. [#465] +* Added support for `EnumItem` attributes. [#470] +[#470]: https://github.com/rojo-rbx/rbx-dom/pull/470 [#465]: https://github.com/rojo-rbx/rbx-dom/pull/465 [#464]: https://github.com/rojo-rbx/rbx-dom/pull/464 From 3a7e9022a31efd6437cbb6bb847c2a0a7eebe7b4 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 18:55:13 +0000 Subject: [PATCH 19/19] Regain the plot --- rbx_dom_weak/CHANGELOG.md | 2 -- rbx_types/CHANGELOG.md | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rbx_dom_weak/CHANGELOG.md b/rbx_dom_weak/CHANGELOG.md index eedb8ebe..27d245cb 100644 --- a/rbx_dom_weak/CHANGELOG.md +++ b/rbx_dom_weak/CHANGELOG.md @@ -103,9 +103,7 @@ pub fn into_raw(self) -> (Ref, HashMap) { * Added re-exports for `ustr` (a convenience function for creating `Ustr`s), `Ustr`, `UstrMap`, and `UstrSet`. * Added `InstanceBuilder::with_property_capacity`, which can preallocate an `InstanceBuilder`'s property table. [#464] * Added `WeakDom::reserve`, which can preallocate additional space for instances in the `WeakDom`. [#465] -* Added support for `EnumItem` attributes. [#470] -[#470]: https://github.com/rojo-rbx/rbx-dom/pull/470 [#465]: https://github.com/rojo-rbx/rbx-dom/pull/465 [#464]: https://github.com/rojo-rbx/rbx-dom/pull/464 diff --git a/rbx_types/CHANGELOG.md b/rbx_types/CHANGELOG.md index c4c4ae35..79a578c5 100644 --- a/rbx_types/CHANGELOG.md +++ b/rbx_types/CHANGELOG.md @@ -5,7 +5,9 @@ # 1.10.0 (2024-08-22) * Add support for `Int32` values within `Attributes` ([#439]) * Add `len` and `is_empty` to `Tags` ([#438]) +* Added support for `EnumItem` attributes. [#470] +[#470]: https://github.com/rojo-rbx/rbx-dom/pull/470 [#438]: https://github.com/rojo-rbx/rbx-dom/pull/438 [#439]: https://github.com/rojo-rbx/rbx-dom/pull/439