2022-04-15 01:52:05 +00:00
|
|
|
--!strict
|
2022-06-28 20:21:05 +00:00
|
|
|
local Format: Format = {}
|
2019-06-30 22:01:19 +00:00
|
|
|
|
2022-06-28 20:21:05 +00:00
|
|
|
export type FormatFunc = (any) -> string
|
|
|
|
export type Format = { [string]: FormatFunc }
|
|
|
|
export type IEnum = { GetEnumItems: (IEnum) -> {EnumItem} }
|
2022-04-15 01:52:05 +00:00
|
|
|
|
2022-10-13 02:19:43 +00:00
|
|
|
local function flags(flagType: any, enum: Enum): string
|
2022-06-28 20:21:05 +00:00
|
|
|
local value = 0
|
2022-04-15 01:52:05 +00:00
|
|
|
|
2022-10-13 02:19:43 +00:00
|
|
|
for i, item: EnumItem in enum:GetEnumItems() do
|
2022-06-28 20:21:05 +00:00
|
|
|
if (flags :: any)[item.Name] then
|
|
|
|
value += (2 ^ item.Value)
|
|
|
|
end
|
|
|
|
end
|
2022-04-15 01:52:05 +00:00
|
|
|
|
2022-06-28 20:21:05 +00:00
|
|
|
return tostring(value)
|
|
|
|
end
|
2022-04-15 01:52:05 +00:00
|
|
|
|
2022-06-28 20:21:05 +00:00
|
|
|
function Format.Null(value: any): string
|
|
|
|
return ""
|
2019-06-30 22:01:19 +00:00
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.Bytes(value: string): string
|
2019-06-30 22:01:19 +00:00
|
|
|
if #value > 0 then
|
2022-04-15 01:52:05 +00:00
|
|
|
return string.format("Convert.FromBase64String(%q)", value)
|
2020-08-14 17:35:27 +00:00
|
|
|
end
|
2022-04-15 01:52:05 +00:00
|
|
|
|
|
|
|
return "Array.Empty<byte>()"
|
2020-08-14 17:35:27 +00:00
|
|
|
end
|
|
|
|
|
2022-06-28 20:21:05 +00:00
|
|
|
function Format.Bool(value: boolean?): string
|
|
|
|
return (if value then "true" else "")
|
2019-06-30 22:01:19 +00:00
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.String(value: string): string
|
2019-06-30 22:01:19 +00:00
|
|
|
return string.format("%q", value)
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.Int(value: number): string
|
|
|
|
if value == 2^31-1 then
|
2020-06-22 01:02:36 +00:00
|
|
|
return "int.MaxValue"
|
2022-04-15 01:52:05 +00:00
|
|
|
elseif value == -2^31 then
|
2020-06-22 01:02:36 +00:00
|
|
|
return "int.MinValue"
|
2022-04-15 01:52:05 +00:00
|
|
|
else
|
2020-06-22 01:02:36 +00:00
|
|
|
return string.format("%i", value)
|
|
|
|
end
|
2019-06-30 22:01:19 +00:00
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.Number(value: number): string
|
2019-06-30 22:01:19 +00:00
|
|
|
local int = math.floor(value)
|
|
|
|
|
|
|
|
if math.abs(value - int) < 0.001 then
|
2022-04-15 01:52:05 +00:00
|
|
|
return Format.Int(int)
|
2019-06-30 22:01:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local result = string.format("%.5f", value)
|
|
|
|
result = result:gsub("%.?0+$", "")
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.Double(value: number): string
|
|
|
|
local result = Format.Number(value)
|
2019-06-30 22:01:19 +00:00
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
if result == "inf" then
|
2019-06-30 22:01:19 +00:00
|
|
|
return "double.MaxValue"
|
|
|
|
elseif result == "-inf" then
|
|
|
|
return "double.MinValue"
|
|
|
|
else
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.Float(value: number): string
|
|
|
|
local result = Format.Number(value)
|
2019-06-30 22:01:19 +00:00
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
if result == "inf" then
|
2019-06-30 22:01:19 +00:00
|
|
|
return "float.MaxValue"
|
|
|
|
elseif result == "-inf" then
|
|
|
|
return "float.MinValue"
|
|
|
|
else
|
|
|
|
if result:find("%.") then
|
|
|
|
result = result .. 'f'
|
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-28 20:21:05 +00:00
|
|
|
function Format.Axes(axes: Axes): string
|
|
|
|
return "(Axes)" .. flags(axes, Enum.Axis)
|
2019-06-30 22:01:19 +00:00
|
|
|
end
|
|
|
|
|
2022-06-28 20:21:05 +00:00
|
|
|
function Format.Faces(faces: Faces): string
|
|
|
|
return "(Faces)" .. flags(faces, Enum.NormalId)
|
2019-06-30 22:01:19 +00:00
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.EnumItem(item: EnumItem): string
|
2019-06-30 22:01:19 +00:00
|
|
|
local enum = tostring(item.EnumType)
|
|
|
|
return enum .. '.' .. item.Name
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.BrickColor(brickColor: BrickColor): string
|
2019-06-30 22:01:19 +00:00
|
|
|
local fmt = "BrickColor.FromNumber(%i)"
|
|
|
|
return fmt:format(brickColor.Number)
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.Color3(color: Color3): string
|
2019-06-30 22:01:19 +00:00
|
|
|
if color == Color3.new() then
|
|
|
|
return "new Color3()"
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
local r = Format.Float(color.R)
|
|
|
|
local g = Format.Float(color.G)
|
|
|
|
local b = Format.Float(color.B)
|
2019-06-30 22:01:19 +00:00
|
|
|
|
|
|
|
local fmt = "%s(%s, %s, %s)";
|
|
|
|
local constructor = "new Color3";
|
|
|
|
|
|
|
|
if string.find(r .. g .. b, 'f') then
|
2020-08-14 17:35:27 +00:00
|
|
|
r = Format.Int(color.R * 255)
|
|
|
|
g = Format.Int(color.G * 255)
|
|
|
|
b = Format.Int(color.B * 255)
|
2019-06-30 22:01:19 +00:00
|
|
|
|
|
|
|
constructor = "Color3.FromRGB"
|
|
|
|
end
|
|
|
|
|
|
|
|
return fmt:format(constructor, r, g, b)
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.UDim(udim: UDim): string
|
2019-06-30 22:01:19 +00:00
|
|
|
if udim == UDim.new() then
|
|
|
|
return "new UDim()"
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
local scale = Format.Float(udim.Scale)
|
|
|
|
local offset = Format.Int(udim.Offset)
|
2019-06-30 22:01:19 +00:00
|
|
|
|
|
|
|
local fmt = "new UDim(%s, %s)"
|
|
|
|
return fmt:format(scale, offset)
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.UDim2(udim2: UDim2): string
|
2019-06-30 22:01:19 +00:00
|
|
|
if udim2 == UDim2.new() then
|
|
|
|
return "new UDim2()"
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
local xScale = Format.Float(udim2.X.Scale)
|
|
|
|
local yScale = Format.Float(udim2.Y.Scale)
|
2019-06-30 22:01:19 +00:00
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
local xOffset = Format.Int(udim2.X.Offset)
|
|
|
|
local yOffset = Format.Int(udim2.Y.Offset)
|
2019-06-30 22:01:19 +00:00
|
|
|
|
|
|
|
local fmt = "new UDim2(%s, %s, %s, %s)"
|
|
|
|
return fmt:format(xScale, xOffset, yScale, yOffset)
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.Vector2(v2: Vector2): string
|
2022-06-28 20:21:05 +00:00
|
|
|
if v2 == Vector2.zero then
|
2019-06-30 22:01:19 +00:00
|
|
|
return "new Vector2()"
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
local x = Format.Float(v2.X)
|
|
|
|
local y = Format.Float(v2.Y)
|
2019-06-30 22:01:19 +00:00
|
|
|
|
|
|
|
local fmt = "new Vector2(%s, %s)"
|
|
|
|
return fmt:format(x, y)
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.Vector3(v3: Vector3): string
|
2022-06-28 20:21:05 +00:00
|
|
|
if v3 == Vector3.zero then
|
2019-06-30 22:01:19 +00:00
|
|
|
return "new Vector3()"
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
local x = Format.Float(v3.X)
|
|
|
|
local y = Format.Float(v3.Y)
|
|
|
|
local z = Format.Float(v3.Z)
|
2019-06-30 22:01:19 +00:00
|
|
|
|
|
|
|
local fmt = "new Vector3(%s, %s, %s)"
|
|
|
|
return fmt:format(x, y, z)
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.CFrame(cf: CFrame): string
|
|
|
|
if cf == CFrame.identity then
|
2019-06-30 22:01:19 +00:00
|
|
|
return "new CFrame()"
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
if cf.Rotation == CFrame.identity then
|
|
|
|
local x = Format.Float(cf.X)
|
|
|
|
local y = Format.Float(cf.Y)
|
|
|
|
local z = Format.Float(cf.Z)
|
2019-06-30 22:01:19 +00:00
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
return string.format("new CFrame(%s, %s, %s)", x, y, z)
|
2019-06-30 22:01:19 +00:00
|
|
|
else
|
|
|
|
local comp = { cf:GetComponents() }
|
2022-04-15 01:52:05 +00:00
|
|
|
local matrix = ""
|
2019-06-30 22:01:19 +00:00
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
for i = 1, 12 do
|
|
|
|
local sep = (if i > 1 then ", " else "")
|
|
|
|
matrix ..= sep .. Format.Float(comp[i])
|
2019-06-30 22:01:19 +00:00
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
return string.format("new CFrame(%s)", matrix)
|
2019-06-30 22:01:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.NumberRange(nr: NumberRange): string
|
2022-06-28 20:21:05 +00:00
|
|
|
local min = Format.Float(nr.Min)
|
|
|
|
local max = Format.Float(nr.Max)
|
2019-06-30 22:01:19 +00:00
|
|
|
|
|
|
|
local fmt = "new NumberRange(%s)"
|
2022-06-28 20:21:05 +00:00
|
|
|
local value = min
|
2019-06-30 22:01:19 +00:00
|
|
|
|
|
|
|
if min ~= max then
|
2022-06-28 20:21:05 +00:00
|
|
|
value ..= ", " .. max
|
2019-06-30 22:01:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return fmt:format(value)
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.Ray(ray: Ray): string
|
2022-06-28 20:21:05 +00:00
|
|
|
if ray.Origin == Vector3.zero then
|
|
|
|
if ray.Direction == Vector3.zero then
|
|
|
|
return "new Ray()"
|
|
|
|
end
|
2019-06-30 22:01:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local origin = Format.Vector3(ray.Origin)
|
|
|
|
local direction = Format.Vector3(ray.Direction)
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
local fmt = "new Ray(%s, %s)"
|
2019-06-30 22:01:19 +00:00
|
|
|
return fmt:format(origin, direction)
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.Rect(rect: Rect): string
|
|
|
|
local min: any = rect.Min
|
|
|
|
local max: any = rect.Max
|
|
|
|
|
|
|
|
if min == max and min == Vector2.zero then
|
|
|
|
return "new Rect()"
|
|
|
|
end
|
2019-06-30 22:01:19 +00:00
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
min = Format.Vector2(min)
|
|
|
|
max = Format.Vector2(max)
|
2019-06-30 22:01:19 +00:00
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
local fmt = "new Rect(%s, %s)"
|
2019-06-30 22:01:19 +00:00
|
|
|
return fmt:format(min, max)
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.ColorSequence(cs: ColorSequence): string
|
2019-06-30 22:01:19 +00:00
|
|
|
local csKey = cs.Keypoints[1]
|
2020-06-22 01:02:36 +00:00
|
|
|
local value = tostring(csKey.Value)
|
2019-06-30 22:01:19 +00:00
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
local fmt = "new ColorSequence(%s)"
|
2019-06-30 22:01:19 +00:00
|
|
|
return fmt:format(value)
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.NumberSequence(ns: NumberSequence): string
|
2019-06-30 22:01:19 +00:00
|
|
|
local nsKey = ns.Keypoints[1]
|
|
|
|
local fmt = "new NumberSequence(%s)"
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
local value = Format.Float(nsKey.Value)
|
2019-06-30 22:01:19 +00:00
|
|
|
return fmt:format(value)
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.Vector3int16(v3: Vector3int16): string
|
2019-06-30 22:01:19 +00:00
|
|
|
if v3 == Vector3int16.new() then
|
|
|
|
return "new Vector3int16()"
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
local x = Format.Int(v3.X)
|
|
|
|
local y = Format.Int(v3.Y)
|
|
|
|
local z = Format.Int(v3.Z)
|
2019-06-30 22:01:19 +00:00
|
|
|
|
|
|
|
local fmt = "new Vector3int16(%s, %s, %s)"
|
|
|
|
return fmt:format(x, y, z)
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.SharedString(str: string): string
|
2019-06-30 22:01:19 +00:00
|
|
|
local fmt = "SharedString.FromBase64(%q)"
|
|
|
|
return fmt:format(str)
|
|
|
|
end
|
|
|
|
|
2022-04-15 01:52:05 +00:00
|
|
|
function Format.FontFace(font: Font): string
|
2022-06-28 20:21:05 +00:00
|
|
|
local family = string.format("%q", font.Family)
|
|
|
|
local args = { family }
|
|
|
|
|
|
|
|
local style = font.Style
|
|
|
|
local weight = font.Weight
|
|
|
|
|
|
|
|
if style ~= Enum.FontStyle.Normal then
|
|
|
|
table.insert(args, "FontStyle." .. style.Name)
|
2022-04-15 01:52:05 +00:00
|
|
|
end
|
2022-05-06 19:37:23 +00:00
|
|
|
|
2022-06-28 20:21:05 +00:00
|
|
|
if #args > 1 or weight ~= Enum.FontWeight.Regular then
|
|
|
|
table.insert(args, "FontWeight." .. weight.Name)
|
|
|
|
end
|
|
|
|
|
|
|
|
local fmt = "new FontFace(%s)"
|
|
|
|
local argStr = table.concat(args, ", ")
|
|
|
|
|
|
|
|
return fmt:format(argStr)
|
2022-04-15 01:52:05 +00:00
|
|
|
end
|
|
|
|
|
2019-06-30 22:01:19 +00:00
|
|
|
return Format
|