Skip to content

Instantly share code, notes, and snippets.

@MaximumADHD
Last active October 30, 2022 23:13
Show Gist options
  • Save MaximumADHD/e4cc3640ed616cc2ad53460c5c39ae23 to your computer and use it in GitHub Desktop.
Save MaximumADHD/e4cc3640ed616cc2ad53460c5c39ae23 to your computer and use it in GitHub Desktop.
---------------------------------------------------------------------------------------------------------------------------
-- XmlNode
---------------------------------------------------------------------------------------------------------------------------
local function XmlNode(class: string)
local node = Instance.new("Configuration")
node.Name = class
return function (attributes)
if next(attributes) then
for key, value in pairs(attributes) do
if typeof(value) == "Instance" then
value.Parent = node
else
node:SetAttribute(key, value)
end
end
end
return node
end
end
---------------------------------------------------------------------------------------------------------------------------
-- XmlWriter
---------------------------------------------------------------------------------------------------------------------------
local XmlWriter = {}
XmlWriter.__index = XmlWriter
function XmlWriter.new()
local writer = { Buffer = "" }
return setmetatable(writer, XmlWriter)
end
function XmlWriter:__tostring()
return self.Buffer
end
function XmlWriter:Write(str: string, ...)
if select('#', ...) > 0 then
str = str:format(...)
end
self.Buffer ..= str
end
function XmlWriter:NextLine()
self:Write("\r\n")
end
function XmlWriter:WriteLine(str: string, ...)
self:Write(str, ...)
self:NextLine()
end
function XmlWriter:WriteAttribute(key: string, value: any)
local valueType = typeof(value)
self:Write(' %s="', key)
if valueType == "Color3" then
local r, g, b = value.R, value.G, value.B
self:Write("%.4f %.4f %.4f", r, g, b)
elseif valueType == "Vector3" then
local x, y, z = value.X, value.Y, value.Z
self:Write("%.2f %.2f %.2f", x, y, z)
else
local str = tostring(value)
self:Write(str)
end
self:Write('"')
end
function XmlWriter:WriteNode(node: Instance, stack: number?)
local attributes = node:GetAttributes()
local children = node:GetChildren()
local stack = stack or 0
for i = 1, stack do
self:Write('\t')
end
self:Write('<')
self:Write(node.Name)
if next(attributes) then
for key, value in pairs(attributes) do
self:WriteAttribute(key, value)
end
end
if next(children) then
self:Write('>')
self:NextLine()
for i, child in ipairs(children) do
self:WriteNode(child, stack + 1)
end
self:Write("</%s>", node.Name)
else
self:Write("/>")
end
self:NextLine()
end
---------------------------------------------------------------------------------------------------------------------------
-- Generate main.xml
---------------------------------------------------------------------------------------------------------------------------
local VOXEL_SIZE = 10
local MAP_SCALE = 0.5
local scene = XmlNode "scene"
{
version = "0.6.0";
shadowVolume = Vector3.new(200, 50, 200);
XmlNode "environment" { template = "sunset" };
XmlNode "script" { file = "main.lua" };
XmlNode "script" { file = "sandbox.lua" };
}
for _,part in pairs(workspace:GetDescendants()) do
if not part:IsA("Part") then
continue
end
local cf = part.CFrame
local size = part.Size
local rot = part.Rotation
local pos = cf * CFrame.new(-size / 2)
pos = pos.Position * MAP_SCALE
size = (size * VOXEL_SIZE) * MAP_SCALE
local node = XmlNode "voxbox"
{
pos = pos;
rot = rot;
size = size;
color = part.Color;
material = "plaster";
tags = "nocull";
}
node.Parent = scene
end
local main = XmlWriter.new()
main:WriteNode(scene)
print(main)
---------------------------------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment