Update to latest rbx_dom_lua

This commit is contained in:
Lucien Greathouse
2021-02-18 23:41:14 -05:00
parent 59ef5f05ea
commit 0d951c8ad1
4 changed files with 650 additions and 508 deletions

View File

@@ -23,332 +23,433 @@ end
local ALL_AXES = {"X", "Y", "Z"}
local ALL_FACES = {"Right", "Top", "Back", "Left", "Bottom", "Front"}
local encoders
encoders = {
Bool = identity,
Content = identity,
Float32 = serializeFloat,
Float64 = serializeFloat,
Int32 = identity,
Int64 = identity,
String = identity,
local types
types = {
Axes = {
fromPod = function(pod)
local axes = {}
BinaryString = base64.encode,
SharedString = base64.encode,
Axes = function(value)
local output = {}
for _, axis in ipairs(ALL_AXES) do
if value[axis] then
table.insert(output, axis)
for index, axisName in ipairs(pod) do
axes[index] = Enum.Axis[axisName]
end
end
return output
end,
return Axes.new(unpack(axes))
end,
Faces = function(value)
local output = {}
toPod = function(roblox)
local json = {}
for _, face in ipairs(ALL_FACES) do
if value[face] then
table.insert(output, face)
for _, axis in ipairs(ALL_AXES) do
if roblox[axis] then
table.insert(json, axis)
end
end
end
return output
end,
return json
end,
},
Enum = function(value)
if typeof(value) == "number" then
return value
else
return value.Value
end
end,
BinaryString = {
fromPod = base64.decode,
toPod = base64.encode,
},
BrickColor = function(value)
return value.Number
end,
Bool = {
fromPod = identity,
toPod = identity,
},
CFrame = function(value)
local x, y, z,
r00, r01, r02,
r10, r11, r12,
r20, r21, r22 = value:GetComponents()
BrickColor = {
fromPod = function(pod)
return BrickColor.new(pod)
end,
return {
Position = {x, y, z},
Orientation = {
{r00, r10, r20},
{r01, r11, r21},
{r02, r12, r22},
},
}
end,
Color3 = function(value)
return {value.r, value.g, value.b}
end,
NumberRange = function(value)
return {value.Min, value.Max}
end,
NumberSequence = function(value)
local keypoints = {}
toPod = function(roblox)
return roblox.Number
end,
},
for index, keypoint in ipairs(value.Keypoints) do
keypoints[index] = {
Time = keypoint.Time,
Value = keypoint.Value,
Envelope = keypoint.Envelope,
}
end
CFrame = {
fromPod = function(pod)
local pos = pod.Position
local orient = pod.Orientation
return {
Keypoints = keypoints,
}
end,
ColorSequence = function(value)
local keypoints = {}
return CFrame.new(
pos[1], pos[2], pos[3],
orient[1][1], orient[1][2], orient[1][3],
orient[2][1], orient[2][2], orient[2][3],
orient[3][1], orient[3][2], orient[3][3]
)
end,
for index, keypoint in ipairs(value.Keypoints) do
keypoints[index] = {
Time = keypoint.Time,
Color = encoders.Color3(keypoint.Value),
}
end
toPod = function(roblox)
local x, y, z,
r00, r01, r02,
r10, r11, r12,
r20, r21, r22 = roblox:GetComponents()
return {
Keypoints = keypoints,
}
end,
Rect = function(value)
return {
encoders.Vector2(value.Min),
encoders.Vector2(value.Max),
}
end,
UDim = function(value)
return {value.Scale, value.Offset}
end,
UDim2 = function(value)
return {
encoders.UDim(value.X),
encoders.UDim(value.Y),
}
end,
Vector2 = function(value)
return {
serializeFloat(value.X),
serializeFloat(value.Y),
}
end,
Vector2int16 = function(value)
return {value.X, value.Y}
end,
Vector3 = function(value)
return {
serializeFloat(value.X),
serializeFloat(value.Y),
serializeFloat(value.Z),
}
end,
Vector3int16 = function(value)
return {value.X, value.Y, value.Z}
end,
PhysicalProperties = function(value)
if value == nil then
return nil
else
return {
Density = value.Density,
Friction = value.Friction,
Elasticity = value.Elasticity,
FrictionWeight = value.FrictionWeight,
ElasticityWeight = value.ElasticityWeight,
Position = {x, y, z},
Orientation = {
{r00, r01, r02},
{r10, r11, r12},
{r20, r21, r22},
},
}
end
end,
end,
},
Ray = function(value)
return {
Origin = encoders.Vector3(value.Origin),
Direction = encoders.Vector3(value.Direction),
}
end,
Color3 = {
fromPod = unpackDecoder(Color3.new),
Ref = function(value)
return nil
end,
toPod = function(roblox)
return {roblox.r, roblox.g, roblox.b}
end,
},
Region3int16 = function(value)
return {
encoders.Vector3int16(value.Min),
encoders.Vector3int16(value.Max),
}
end,
Color3uint8 = {
fromPod = unpackDecoder(Color3.fromRGB),
Color3uint8 = function(value)
return {
math.round(value.R * 255),
math.round(value.G * 255),
math.round(value.B * 255),
}
end,
}
toPod = function(roblox)
return {
math.round(roblox.R * 255),
math.round(roblox.G * 255),
math.round(roblox.B * 255),
}
end,
},
local decoders
decoders = {
Bool = identity,
Content = identity,
Enum = identity,
Float32 = identity,
Float64 = identity,
Int32 = identity,
Int64 = identity,
String = identity,
ColorSequence = {
fromPod = function(pod)
local keypoints = {}
BinaryString = base64.decode,
SharedString = base64.decode,
for index, keypoint in ipairs(pod.Keypoints) do
keypoints[index] = ColorSequenceKeypoint.new(
keypoint.Time,
types.Color3.fromPod(keypoint.Color)
)
end
BrickColor = BrickColor.new,
return ColorSequence.new(keypoints)
end,
Color3 = unpackDecoder(Color3.new),
Color3uint8 = unpackDecoder(Color3.fromRGB),
NumberRange = unpackDecoder(NumberRange.new),
UDim = unpackDecoder(UDim.new),
Vector2 = unpackDecoder(Vector2.new),
Vector2int16 = unpackDecoder(Vector2int16.new),
Vector3 = unpackDecoder(Vector3.new),
Vector3int16 = unpackDecoder(Vector3int16.new),
toPod = function(roblox)
local keypoints = {}
UDim2 = function(value)
return UDim2.new(
value[1][1],
value[1][2],
value[2][1],
value[2][2]
)
end,
for index, keypoint in ipairs(roblox.Keypoints) do
keypoints[index] = {
Time = keypoint.Time,
Color = types.Color3.toPod(keypoint.Value),
}
end
Axes = function(value)
local axes = {}
for index, axisName in ipairs(value) do
axes[index] = Enum.Axis[axisName]
end
return {
Keypoints = keypoints,
}
end,
},
return Axes.new(unpack(axes))
end,
Content = {
fromPod = identity,
toPod = identity,
},
Faces = function(value)
local normalIds = {}
for index, faceName in ipairs(value) do
normalIds[index] = Enum.NormalId[faceName]
end
Enum = {
fromPod = identity,
return Faces.new(unpack(normalIds))
end,
toPod = function(roblox)
-- FIXME: More robust handling of enums
if typeof(roblox) == "number" then
return roblox
else
return roblox.Value
end
end,
},
CFrame = function(value)
return CFrame.fromMatrix(
decoders.Vector3(value.Position),
decoders.Vector3(value.Orientation[1]),
decoders.Vector3(value.Orientation[2]),
decoders.Vector3(value.Orientation[3])
)
end,
Faces = {
fromPod = function(pod)
local faces = {}
Rect = function(value)
return Rect.new(
decoders.Vector2(value[1]),
decoders.Vector2(value[2])
)
end,
for index, faceName in ipairs(pod) do
faces[index] = Enum.NormalId[faceName]
end
Ray = function(value)
return Ray.new(
decoders.Vector3(value.Origin),
decoders.Vector3(value.Direction)
)
end,
return Faces.new(unpack(faces))
end,
NumberSequence = function(value)
local keypoints = {}
toPod = function(roblox)
local pod = {}
for index, keypoint in ipairs(value.Keypoints) do
keypoints[index] = NumberSequenceKeypoint.new(
keypoint.Time,
keypoint.Value,
keypoint.Envelope
for _, face in ipairs(ALL_FACES) do
if roblox[face] then
table.insert(pod, face)
end
end
return pod
end,
},
Float32 = {
fromPod = identity,
toPod = serializeFloat,
},
Float64 = {
fromPod = identity,
toPod = serializeFloat,
},
Int32 = {
fromPod = identity,
toPod = identity,
},
Int64 = {
fromPod = identity,
toPod = identity,
},
NumberRange = {
fromPod = unpackDecoder(NumberRange.new),
toPod = function(roblox)
return {roblox.Min, roblox.Max}
end,
},
NumberSequence = {
fromPod = function(pod)
local keypoints = {}
for index, keypoint in ipairs(pod.Keypoints) do
keypoints[index] = NumberSequenceKeypoint.new(
keypoint.Time,
keypoint.Value,
keypoint.Envelope
)
end
return NumberSequence.new(keypoints)
end,
toPod = function(roblox)
local keypoints = {}
for index, keypoint in ipairs(roblox.Keypoints) do
keypoints[index] = {
Time = keypoint.Time,
Value = keypoint.Value,
Envelope = keypoint.Envelope,
}
end
return {
Keypoints = keypoints,
}
end,
},
PhysicalProperties = {
fromPod = function(pod)
if pod == "Default" then
return nil
else
return PhysicalProperties.new(
pod.Density,
pod.Friction,
pod.Elasticity,
pod.FrictionWeight,
pod.ElasticityWeight
)
end
end,
toPod = function(roblox)
if roblox == nil then
return "Default"
else
return {
Density = roblox.Density,
Friction = roblox.Friction,
Elasticity = roblox.Elasticity,
FrictionWeight = roblox.FrictionWeight,
ElasticityWeight = roblox.ElasticityWeight,
}
end
end,
},
Ray = {
fromPod = function(pod)
return Ray.new(
types.Vector3.fromPod(pod.Origin),
types.Vector3.fromPod(pod.Direction)
)
end
end,
return NumberSequence.new(keypoints)
end,
toPod = function(roblox)
return {
Origin = types.Vector3.toPod(roblox.Origin),
Direction = types.Vector3.toPod(roblox.Direction),
}
end,
},
ColorSequence = function(value)
local keypoints = {}
for index, keypoint in ipairs(value.Keypoints) do
keypoints[index] = ColorSequenceKeypoint.new(
keypoint.Time,
Color3.new(unpack(keypoint.Color))
Rect = {
fromPod = function(pod)
return Rect.new(
types.Vector2.fromPod(pod[1]),
types.Vector2.fromPod(pod[2])
)
end
end,
return ColorSequence.new(keypoints)
end,
toPod = function(roblox)
return {
types.Vector2.toPod(roblox.Min),
types.Vector2.toPod(roblox.Max),
}
end,
},
PhysicalProperties = function(properties)
if properties == nil then
return nil
else
return PhysicalProperties.new(
properties.Density,
properties.Friction,
properties.Elasticity,
properties.FrictionWeight,
properties.ElasticityWeight
Ref = {
fromPod = function(_pod)
error("Ref cannot be decoded on its own")
end,
toPod = function(_roblox)
error("Ref can not be encoded on its own")
end,
},
Region3 = {
fromPod = function(pod)
error("Region3 is not implemented")
end,
toPod = function(roblox)
error("Region3 is not implemented")
end,
},
Region3int16 = {
fromPod = function(pod)
return Region3int16.new(
types.Vector3int16.fromPod(pod[1]),
types.Vector3int16.fromPod(pod[2])
)
end
end,
end,
Ref = function()
return nil
end,
toPod = function(roblox)
return {
types.Vector3int16.toPod(roblox.Min),
types.Vector3int16.toPod(roblox.Max),
}
end,
},
Region3int16 = function(value)
return Region3int16.new(
decoders.Vector3int16(value[1]),
decoders.Vector3int16(value[2])
)
end,
SharedString = {
fromPod = function(pod)
error("SharedString is not supported")
end,
toPod = function(roblox)
error("SharedString is not supported")
end,
},
String = {
fromPod = identity,
toPod = identity,
},
UDim = {
fromPod = unpackDecoder(UDim.new),
toPod = function(roblox)
return {roblox.Scale, roblox.Offset}
end,
},
UDim2 = {
fromPod = function(pod)
return UDim2.new(
types.UDim.fromPod(pod[1]),
types.UDim.fromPod(pod[2])
)
end,
toPod = function(roblox)
return {
types.UDim.toPod(roblox.X),
types.UDim.toPod(roblox.Y),
}
end,
},
Vector2 = {
fromPod = unpackDecoder(Vector2.new),
toPod = function(roblox)
return {
serializeFloat(roblox.X),
serializeFloat(roblox.Y),
}
end,
},
Vector2int16 = {
fromPod = unpackDecoder(Vector2int16.new),
toPod = function(roblox)
return {roblox.X, roblox.Y}
end,
},
Vector3 = {
fromPod = unpackDecoder(Vector3.new),
toPod = function(roblox)
return {
serializeFloat(roblox.X),
serializeFloat(roblox.Y),
serializeFloat(roblox.Z),
}
end,
},
Vector3int16 = {
fromPod = unpackDecoder(Vector3int16.new),
toPod = function(roblox)
return {roblox.X, roblox.Y, roblox.Z}
end,
},
}
local EncodedValue = {}
function EncodedValue.decode(encodedValue)
local decoder = decoders[encodedValue.Type]
if decoder ~= nil then
return true, decoder(encodedValue.Value)
local typeImpl = types[encodedValue.Type]
if typeImpl == nil then
return false, "Couldn't decode value " .. tostring(encodedValue.Type)
end
return false, "Couldn't decode value " .. tostring(encodedValue.Type)
return true, typeImpl.fromPod(encodedValue.Value)
end
function EncodedValue.encode(rbxValue, propertyType)
assert(propertyType ~= nil, "Property type descriptor is required")
local encoder = encoders[propertyType]
if encoder == nil then
local typeImpl = types[propertyType]
if typeImpl == nil then
return false, ("Missing encoder for property type %q"):format(propertyType)
end
return true, {
Type = propertyType,
Value = encoder(rbxValue),
Value = typeImpl.toPod(rbxValue),
}
end