Skip to content

Commit

Permalink
Live sync Attributes (#553)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
LPGhatguy committed Jun 29, 2022
1 parent acf7456 commit eccb956
Show file tree
Hide file tree
Showing 10 changed files with 3,340 additions and 725 deletions.
54 changes: 52 additions & 2 deletions plugin/rbx_dom_lua/EncodedValue.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,45 @@ end
local ALL_AXES = {"X", "Y", "Z"}
local ALL_FACES = {"Right", "Top", "Back", "Left", "Bottom", "Front"}

local EncodedValue = {}

local 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 = {
fromPod = function(pod)
local axes = {}
Expand Down Expand Up @@ -433,8 +470,6 @@ types = {
},
}

local EncodedValue = {}

function EncodedValue.decode(encodedValue)
local ty, value = next(encodedValue)

Expand All @@ -459,4 +494,19 @@ function EncodedValue.encode(rbxValue, propertyType)
}
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
69 changes: 69 additions & 0 deletions plugin/rbx_dom_lua/allValues.json
Original file line number Diff line number Diff line change
@@ -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": {
"value": {
"Axes": [
Expand Down
20 changes: 20 additions & 0 deletions plugin/rbx_dom_lua/customProperties.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ local CollectionService = game:GetService("CollectionService")
-- The reflection database refers to these as having scriptability = "Custom"
return {
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 = {
read = function(instance)
return true, CollectionService:GetTags(instance)
Expand Down
Loading

0 comments on commit eccb956

Please sign in to comment.