Skip to content

Commit d9f0841

Browse files
Implement enum attributes (rojo-rbx#470)
1 parent 8ecd2fa commit d9f0841

22 files changed

+335
-25
lines changed

docs/attributes.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This document describes the Attribute binary format. In this format there is no
1818
- [Vector2](#vector2)
1919
- [Vector3](#vector3)
2020
- [CFrame](#cframe)
21-
- [EnumItem](#EnumItem)
21+
- [EnumItem](#enumitem)
2222
- [NumberSequence](#numbersequence)
2323
- [ColorSequence](#colorsequence)
2424
- [NumberRange](#numberrange)
@@ -27,7 +27,7 @@ This document describes the Attribute binary format. In this format there is no
2727

2828
## Document Conventions
2929

30-
This document assumes a basic understanding of Rust's convention for numeric types. For example:
30+
This document assumes a basic understanding of Rust's convention for numeric types. For example:
3131

3232
- `u32` is an unsigned 32-bit integer
3333
- `f32` is a 32-bit floating point
@@ -189,7 +189,7 @@ Demonstrating the axis-aligned rotation matrix case, a `CFrame` with the value `
189189
### EnumItem
190190
**Type ID `0x15`**
191191

192-
The `EnumItem` type is composed of two parts:
192+
The `EnumItem` type is composed of two parts:
193193

194194
| Field Name | Format | Value |
195195
|:-----------|:--------------------|:-------------------------------------------------------|

rbx_binary/src/serializer/state.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use ahash::{HashMap, HashMapExt, HashSetExt};
99
use rbx_dom_weak::{
1010
types::{
1111
Attributes, Axes, BinaryString, BrickColor, CFrame, Color3, Color3uint8, ColorSequence,
12-
ColorSequenceKeypoint, Content, Enum, Faces, Font, MaterialColors, Matrix3, NumberRange,
13-
NumberSequence, NumberSequenceKeypoint, PhysicalProperties, Ray, Rect, Ref,
12+
ColorSequenceKeypoint, Content, Enum, EnumItem, Faces, Font, MaterialColors, Matrix3,
13+
NumberRange, NumberSequence, NumberSequenceKeypoint, PhysicalProperties, Ray, Rect, Ref,
1414
SecurityCapabilities, SharedString, Tags, UDim, UDim2, UniqueId, Variant, VariantType,
1515
Vector2, Vector3, Vector3int16,
1616
},
@@ -970,10 +970,10 @@ impl<'dom, 'db, W: Write> SerializerState<'dom, 'db, W> {
970970
let mut buf = Vec::with_capacity(values.len());
971971

972972
for (i, rbx_value) in values {
973-
if let Variant::Enum(value) = rbx_value.as_ref() {
974-
buf.push(value.to_u32());
975-
} else {
976-
return type_mismatch(i, &rbx_value, "Enum");
973+
match rbx_value.as_ref() {
974+
Variant::Enum(value) => buf.push(value.to_u32()),
975+
Variant::EnumItem(EnumItem { value, .. }) => buf.push(*value),
976+
_ => return type_mismatch(i, &rbx_value, "Enum or EnumItem"),
977977
}
978978
}
979979

rbx_binary/src/tests/models.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,5 @@ binary_tests! {
6666
folder_with_font_attribute,
6767
number_values_with_security_capabilities,
6868
lighting_with_int32_attribute,
69+
folder_with_enum_attribute,
6970
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
source: rbx_binary/src/tests/util.rs
3+
expression: decoded_viewed
4+
---
5+
- referent: referent-0
6+
name: Folder
7+
class: Folder
8+
properties:
9+
Attributes:
10+
Attributes:
11+
AnEnumValue:
12+
EnumItem:
13+
type: Material
14+
value: 512
15+
Capabilities:
16+
SecurityCapabilities: 0
17+
Sandboxed:
18+
Bool: false
19+
SourceAssetId:
20+
Int64: -1
21+
Tags:
22+
Tags: []
23+
children: []
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
source: rbx_binary/src/tests/util.rs
3+
expression: text_roundtrip
4+
---
5+
num_types: 1
6+
num_instances: 1
7+
chunks:
8+
- Inst:
9+
type_id: 0
10+
type_name: Folder
11+
object_format: 0
12+
referents:
13+
- 0
14+
- Prop:
15+
type_id: 0
16+
prop_name: AttributesSerialize
17+
prop_type: String
18+
values:
19+
- "\u0001\u0000\u0000\u0000\u000b\u0000\u0000\u0000AnEnumValue\u0015\b\u0000\u0000\u0000Material\u0000\u0002\u0000\u0000"
20+
- Prop:
21+
type_id: 0
22+
prop_name: Capabilities
23+
prop_type: SecurityCapabilities
24+
values:
25+
- 0
26+
- Prop:
27+
type_id: 0
28+
prop_name: Name
29+
prop_type: String
30+
values:
31+
- Folder
32+
- Prop:
33+
type_id: 0
34+
prop_name: DefinesCapabilities
35+
prop_type: Bool
36+
values:
37+
- false
38+
- Prop:
39+
type_id: 0
40+
prop_name: SourceAssetId
41+
prop_type: Int64
42+
values:
43+
- -1
44+
- Prop:
45+
type_id: 0
46+
prop_name: Tags
47+
prop_type: String
48+
values:
49+
- ""
50+
- Prnt:
51+
version: 0
52+
links:
53+
- - 0
54+
- -1
55+
- End
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
source: rbx_binary/src/tests/util.rs
3+
expression: text_decoded
4+
---
5+
num_types: 1
6+
num_instances: 1
7+
chunks:
8+
- Meta:
9+
entries:
10+
- - ExplicitAutoJoints
11+
- "true"
12+
- Inst:
13+
type_id: 0
14+
type_name: Folder
15+
object_format: 0
16+
referents:
17+
- 0
18+
- Prop:
19+
type_id: 0
20+
prop_name: AttributesSerialize
21+
prop_type: String
22+
values:
23+
- "\u0001\u0000\u0000\u0000\u000b\u0000\u0000\u0000AnEnumValue\u0015\b\u0000\u0000\u0000Material\u0000\u0002\u0000\u0000"
24+
- Prop:
25+
type_id: 0
26+
prop_name: Capabilities
27+
prop_type: SecurityCapabilities
28+
values:
29+
- 0
30+
- Prop:
31+
type_id: 0
32+
prop_name: DefinesCapabilities
33+
prop_type: Bool
34+
values:
35+
- false
36+
- Prop:
37+
type_id: 0
38+
prop_name: Name
39+
prop_type: String
40+
values:
41+
- Folder
42+
- Prop:
43+
type_id: 0
44+
prop_name: SourceAssetId
45+
prop_type: Int64
46+
values:
47+
- -1
48+
- Prop:
49+
type_id: 0
50+
prop_name: Tags
51+
prop_type: String
52+
values:
53+
- ""
54+
- Prnt:
55+
version: 0
56+
links:
57+
- - 0
58+
- -1
59+
- End

rbx_dom_lua/src/EncodedValue.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,19 @@ types = {
205205
end,
206206
},
207207

208+
EnumItem = {
209+
fromPod = function(pod)
210+
return Enum[pod.type]:FromValue(pod.value)
211+
end,
212+
213+
toPod = function(roblox)
214+
return {
215+
type = tostring(roblox.EnumType),
216+
value = roblox.Value,
217+
}
218+
end,
219+
},
220+
208221
Faces = {
209222
fromPod = function(pod)
210223
local faces = {}

rbx_dom_lua/src/allValues.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
0.0
1616
]
1717
},
18+
"TestEnumItem": {
19+
"EnumItem": {
20+
"type": "Material",
21+
"value": 256
22+
}
23+
},
1824
"TestNumber": {
1925
"Float64": 1337.0
2026
},
@@ -182,6 +188,15 @@
182188
},
183189
"ty": "Enum"
184190
},
191+
"EnumItem": {
192+
"value": {
193+
"EnumItem": {
194+
"type": "Material",
195+
"value": 256
196+
}
197+
},
198+
"ty": "EnumItem"
199+
},
185200
"Faces": {
186201
"value": {
187202
"Faces": [

rbx_dom_lua/test

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/sh
22

3+
cargo run --bin rbx_reflector values ./src/allValues.json
34
rojo build test-place.project.json -o TestPlace.rbxlx
4-
run-in-roblox --script run-tests.lua --place TestPlace.rbxlx
5+
run-in-roblox --script run-tests.lua --place TestPlace.rbxlx

rbx_reflector/src/cli/values.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use anyhow::bail;
44
use clap::Parser;
55
use rbx_types::{
66
Attributes, Axes, BinaryString, BrickColor, CFrame, Color3, Color3uint8, ColorSequence,
7-
ColorSequenceKeypoint, Content, CustomPhysicalProperties, Enum, Faces, Font, MaterialColors,
8-
Matrix3, NumberRange, NumberSequence, NumberSequenceKeypoint, PhysicalProperties, Ray, Rect,
9-
Region3int16, Tags, TerrainMaterials, UDim, UDim2, Variant, VariantType, Vector2, Vector2int16,
10-
Vector3, Vector3int16,
7+
ColorSequenceKeypoint, Content, CustomPhysicalProperties, Enum, EnumItem, Faces, Font,
8+
MaterialColors, Matrix3, NumberRange, NumberSequence, NumberSequenceKeypoint,
9+
PhysicalProperties, Ray, Rect, Region3int16, Tags, TerrainMaterials, UDim, UDim2, Variant,
10+
VariantType, Vector2, Vector2int16, Vector3, Vector3int16,
1111
};
1212
use serde::Serialize;
1313

@@ -45,6 +45,13 @@ impl ValuesSubcommand {
4545
"TestUDim2",
4646
UDim2::new(UDim::new(1.0, 2), UDim::new(3.0, 4)),
4747
)
48+
.with(
49+
"TestEnumItem",
50+
EnumItem {
51+
ty: "Material".into(),
52+
value: 256,
53+
},
54+
)
4855
.into(),
4956
);
5057
values.insert("Axes", Axes::all().into());
@@ -80,6 +87,14 @@ impl ValuesSubcommand {
8087
);
8188
values.insert("Content", Content::from("rbxassetid://12345").into());
8289
values.insert("Enum", Enum::from_u32(1234).into());
90+
values.insert(
91+
"EnumItem",
92+
EnumItem {
93+
ty: "Material".into(),
94+
value: 256,
95+
}
96+
.into(),
97+
);
8398
values.insert("Faces", Faces::all().into());
8499
values.insert("Float32", 15.0f32.into());
85100
values.insert("Float64", 15123.0f64.into());

0 commit comments

Comments
 (0)