Live sync Attributes (#553)

* Add test project for tags

* Update rbx_dom_lua and add attributes project

* Add Attributes shorthand; not working

* Update dependencies

* Update rbx_reflection_database

* Update rbx_types and commit attributes snapshot
This commit is contained in:
Lucien Greathouse
2022-06-29 18:53:34 -04:00
committed by GitHub
parent acf7456371
commit eccb95690c
10 changed files with 3344 additions and 729 deletions

View File

@@ -23,8 +23,45 @@ end
local ALL_AXES = {"X", "Y", "Z"} local ALL_AXES = {"X", "Y", "Z"}
local ALL_FACES = {"Right", "Top", "Back", "Left", "Bottom", "Front"} local ALL_FACES = {"Right", "Top", "Back", "Left", "Bottom", "Front"}
local EncodedValue = {}
local types local types
types = { types = {
Attributes = {
fromPod = function(pod)
local output = {}
for key, value in pairs(pod) do
local ok, result = EncodedValue.decode(value)
if ok then
output[key] = result
else
local warning = ("Could not decode attribute value of type %q: %s"):format(typeof(value), tostring(result))
warn(warning)
end
end
return output
end,
toPod = function(roblox)
local output = {}
for key, value in pairs(roblox) do
local ok, result = EncodedValue.encodeNaive(value)
if ok then
output[key] = result
else
local warning = ("Could not encode attribute value of type %q: %s"):format(typeof(value), tostring(result))
warn(warning)
end
end
return output
end,
},
Axes = { Axes = {
fromPod = function(pod) fromPod = function(pod)
local axes = {} local axes = {}
@@ -433,8 +470,6 @@ types = {
}, },
} }
local EncodedValue = {}
function EncodedValue.decode(encodedValue) function EncodedValue.decode(encodedValue)
local ty, value = next(encodedValue) local ty, value = next(encodedValue)
@@ -459,4 +494,19 @@ function EncodedValue.encode(rbxValue, propertyType)
} }
end end
local propertyTypeRenames = {
number = "Float64",
boolean = "Bool",
string = "String",
}
function EncodedValue.encodeNaive(rbxValue)
local propertyType = typeof(rbxValue)
if propertyTypeRenames[propertyType] ~= nil then
propertyType = propertyTypeRenames[propertyType]
end
return EncodedValue.encode(rbxValue, propertyType)
end
return EncodedValue return EncodedValue

View File

@@ -1,4 +1,73 @@
{ {
"Attributes": {
"value": {
"Attributes": {
"TestBool": {
"Bool": true
},
"TestBrickColor": {
"BrickColor": 24
},
"TestColor3": {
"Color3": [
1.0,
0.5,
0.0
]
},
"TestNumber": {
"Float64": 1337.0
},
"TestRect": {
"Rect": [
[
1.0,
2.0
],
[
3.0,
4.0
]
]
},
"TestString": {
"String": "Test"
},
"TestUDim": {
"UDim": [
1.0,
2
]
},
"TestUDim2": {
"UDim2": [
[
1.0,
2
],
[
3.0,
4
]
]
},
"TestVector2": {
"Vector2": [
1.0,
2.0
]
},
"TestVector3": {
"Vector3": [
1.0,
2.0,
3.0
]
}
}
},
"ty": "Attributes"
},
"Axes": { "Axes": {
"value": { "value": {
"Axes": [ "Axes": [

View File

@@ -5,6 +5,26 @@ local CollectionService = game:GetService("CollectionService")
-- The reflection database refers to these as having scriptability = "Custom" -- The reflection database refers to these as having scriptability = "Custom"
return { return {
Instance = { Instance = {
Attributes = {
read = function(instance)
return true, instance:GetAttributes()
end,
write = function(instance, _, value)
local existing = instance:GetAttributes()
for key, attr in pairs(value) do
instance:SetAttribute(key, attr)
end
for key in pairs(existing) do
if value[key] == nil then
instance:SetAttribute(key, nil)
end
end
return true
end,
},
Tags = { Tags = {
read = function(instance) read = function(instance)
return true, CollectionService:GetTags(instance) return true, CollectionService:GetTags(instance)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,24 @@
---
source: tests/tests/build.rs
assertion_line: 99
expression: contents
---
<roblox version="4">
<Item class="Folder" referent="0">
<Properties>
<string name="Name">attributes</string>
</Properties>
<Item class="Folder" referent="1">
<Properties>
<string name="Name">Explicit</string>
<BinaryString name="AttributesSerialize">AgAAAAUAAABIZWxsbwIFAAAAV29ybGQGAAAAVmVjdG9yEQAAgD8AAABAAABAQA==</BinaryString>
</Properties>
</Item>
<Item class="Folder" referent="2">
<Properties>
<string name="Name">ImplicitAttributes</string>
<BinaryString name="AttributesSerialize">AgAAAAMAAABIZXkCBwAAAEdyYW5kbWEGAAAAVmVjdG9yEQAAgEAAAKBAAADAQA==</BinaryString>
</Properties>
</Item>
</Item>
</roblox>

View File

@@ -1,6 +1,6 @@
--- ---
source: tests/tests/build.rs source: tests/tests/build.rs
assertion_line: 98 assertion_line: 99
expression: contents expression: contents
--- ---
<roblox version="4"> <roblox version="4">

View File

@@ -0,0 +1,36 @@
{
"name": "attributes",
"tree": {
"$className": "Folder",
"Explicit": {
"$className": "Folder",
"$properties": {
"Attributes": {
"Attributes": {
"Hello": {
"String": "World"
},
"Vector": {
"Vector3": [1, 2, 3]
}
}
}
}
},
"ImplicitAttributes": {
"$className": "Folder",
"$properties": {
"Attributes": {
"Hey": {
"String": "Grandma"
},
"Vector": {
"Vector3": [4, 5, 6]
}
}
}
}
}
}

View File

@@ -2,7 +2,8 @@ use std::borrow::Borrow;
use anyhow::format_err; use anyhow::format_err;
use rbx_dom_weak::types::{ use rbx_dom_weak::types::{
CFrame, Color3, Content, Enum, Matrix3, Tags, Variant, VariantType, Vector2, Vector3, Attributes, CFrame, Color3, Content, Enum, Matrix3, Tags, Variant, VariantType, Vector2,
Vector3,
}; };
use rbx_reflection::{DataType, PropertyDescriptor}; use rbx_reflection::{DataType, PropertyDescriptor};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -40,6 +41,7 @@ pub enum AmbiguousValue {
Array3([f64; 3]), Array3([f64; 3]),
Array4([f64; 4]), Array4([f64; 4]),
Array12([f64; 12]), Array12([f64; 12]),
Attributes(Attributes),
} }
impl AmbiguousValue { impl AmbiguousValue {
@@ -128,6 +130,8 @@ impl AmbiguousValue {
Ok(CFrame::new(pos, orientation).into()) Ok(CFrame::new(pos, orientation).into())
} }
(VariantType::Attributes, AmbiguousValue::Attributes(value)) => Ok(value.into()),
(_, unresolved) => Err(format_err!( (_, unresolved) => Err(format_err!(
"Wrong type of value for property {}.{}. Expected {:?}, got {}", "Wrong type of value for property {}.{}. Expected {:?}, got {}",
class_name, class_name,
@@ -154,6 +158,7 @@ impl AmbiguousValue {
AmbiguousValue::Array3(_) => "an array of three numbers", AmbiguousValue::Array3(_) => "an array of three numbers",
AmbiguousValue::Array4(_) => "an array of four numbers", AmbiguousValue::Array4(_) => "an array of four numbers",
AmbiguousValue::Array12(_) => "an array of twelve numbers", AmbiguousValue::Array12(_) => "an array of twelve numbers",
AmbiguousValue::Attributes(_) => "an object containing attributes",
} }
} }
} }

View File

@@ -0,0 +1,16 @@
{
"name": "attributes",
"tree": {
"$className": "DataModel",
"Workspace": {
"Folder": {
"$className": "Folder",
"$properties": {
"Attributes": {
"Hello": { "Vector3": [1, 2, 3] }
}
}
}
}
}
}

View File

@@ -21,6 +21,7 @@ macro_rules! gen_build_tests {
} }
gen_build_tests! { gen_build_tests! {
attributes,
client_in_folder, client_in_folder,
client_init, client_init,
csv_bug_145, csv_bug_145,