Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live sync Attributes #553

Merged
merged 11 commits into from
Jun 29, 2022
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