Skip to content

Instantly share code, notes, and snippets.

@cxmeel
Last active February 23, 2020 18:57
Show Gist options
  • Save cxmeel/81c188721f06ec020aa83cb5b63e6930 to your computer and use it in GitHub Desktop.
Save cxmeel/81c188721f06ec020aa83cb5b63e6930 to your computer and use it in GitHub Desktop.
Serialises Roblox Instance properties into a JSON-formatted string. Run this via the `Command Bar` or `Model -> Run Script`.
-- clockworksquirrel.instance-encode
local dump = {}
local http = game:GetService("HttpService")
local response = http:RequestAsync({
Url = "https://raw.githubusercontent.com/ClockworkSquirrel/roblox-client-dump/master/API-Dump-Tree.json"
})
dump = http:JSONDecode(response.Body)
local function buildTree(currentRoot, obj, tree)
tree = tree or {}
if (currentRoot) then
for class, entry in next, currentRoot do
if (obj:IsA(class)) then
tree[#tree + 1] = entry
return buildTree(entry.Children, obj, tree)
end
end
end
return tree
end
local function treeToClass(tree)
local class = {}
local function mergeTables(table1, table2)
for key, value in next, table2 do
if (typeof(table1[key]) == "table" and typeof(value) == "table") then
table1[key] = mergeTables(table1[key], value)
else
table1[key] = value
end
end
return table1
end
for _, node in next, tree do
mergeTables(class, node)
class.Children = node.Children
end
return class
end
local function findInTable(haystack, needle)
for _, value in next, haystack do
if (value == needle) then
return true
end
end
end
local function JSONEncodeProperties(obj)
local objTree = buildTree(dump.Classes, obj)
local class = treeToClass(objTree)
local properties = {}
for prop, value in next, class.Properties or {} do
if not (value.isDeprecated or value.Security.Read ~= "None" or value.Security.Write ~= "None" or findInTable(value.Tags or {}, "NotScriptable") or findInTable(value.Tags or {}, "ReadOnly") and not (value.Serialization.CanLoad and value.Serialization.CanSave)) then
local ok, objProp = pcall(function() return obj[prop] end)
if (ok) then
if (type(objProp) == "userdata") then
local ok, tryEncode = pcall(function()
return http:JSONDecode("[" .. tostring(objProp) .. "]")
end)
if (not ok) then
if (typeof(objProp) == "EnumItem") then
tryEncode = objProp.Name
elseif (typeof(objProp) == "Instance") then
tryEncode = objProp == game and "game" or "game." .. objProp:GetFullName()
else
tryEncode = tostring(objProp)
end
end
properties[prop] = { datatype = typeof(objProp), value = tryEncode }
else
properties[prop] = objProp
end
end
end
end
return http:JSONEncode(properties)
end
_G.dump = dump
_G.encodeProps = JSONEncodeProperties
print(
string.format("[%d]", #response.Body),
"Fetched latest client dump from server. Accessible via _G.dump.",
"Serialise Instance properties with _G.encodeProps(<Instance>)."
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment