From 075b6cca30f12bc5d087308e15114b397d7e4ae5 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Thu, 30 May 2019 18:37:56 -0700 Subject: [PATCH] Use new rbx_dom_lua API --- plugin/modules/rbx-dom | 2 +- plugin/src/setCanonicalProperty.lua | 42 +++++++++++++++-------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/plugin/modules/rbx-dom b/plugin/modules/rbx-dom index 74ec3e7d..4b5f8c22 160000 --- a/plugin/modules/rbx-dom +++ b/plugin/modules/rbx-dom @@ -1 +1 @@ -Subproject commit 74ec3e7d885868252b02f227c0e0ad86e3332120 +Subproject commit 4b5f8c22c7a377becac6b9929ce01467bd561481 diff --git a/plugin/src/setCanonicalProperty.lua b/plugin/src/setCanonicalProperty.lua index eb3fd5aa..8314cfcd 100644 --- a/plugin/src/setCanonicalProperty.lua +++ b/plugin/src/setCanonicalProperty.lua @@ -3,30 +3,32 @@ local RbxDom = require(script:FindFirstAncestor("Rojo").RbxDom) --[[ Attempts to set a property on the given instance. ]] -local function setCanonicalProperty(instance, key, value) - -- If we don't have permissions to access this value at all, we can skip it. - local readSuccess, existingValue = RbxDom.readProperty(instance, key) +local function setCanonicalProperty(instance, propertyName, value) + local descriptor = RbxDom.findCanonicalPropertyDescriptor(instance.ClassName, propertyName) - if not readSuccess then - if existingValue.kind == RbxDom.Error.Kind.UnknownProperty - or existingValue.kind == RbxDom.Error.Kind.PropertyNotReadable then - -- this is fine - return false - end - - -- If we don't have permission to write a property, we just silently - -- ignore it. - if existingValue.kind == RbxDom.Error.Kind.Roblox and existingValue.extra:find("lacking permission") then - return false - end - - error(("Invalid property %s on class %s: %s"):format(tostring(key), instance.ClassName, tostring(existingValue)), 2) + -- We can skip unknown properties; they're not likely reflected to Lua. + -- + -- A good example of a property like this is `Model.ModelInPrimary`, which + -- is serialized but not reflected to Lua. + if descriptor == nil then + return false, "unknown property" end - local writeSuccess, err = RbxDom.writeProperty(instance, key, value) + if descriptor.scriptability == "None" or descriptor.scriptability == "Read" then + return false, "unwritable property" + end - if not writeSuccess then - error(("Cannot set property %s on class %s: %s"):format(tostring(key), instance.ClassName, tostring(err)), 2) + local success, err = descriptor:write(instance, value) + + if not success then + -- If we don't have permission to write a property, we just silently + -- ignore it. + if err.kind == RbxDom.Error.Kind.Roblox and err.extra:find("lacking permission") then + return false, "permission error" + end + + local message = ("Invalid property %s.%s: %s"):format(descriptor.className, descriptor.name, tostring(err)) + error(message, 2) end return true