-
Notifications
You must be signed in to change notification settings - Fork 8
/
NanosTable.lua
91 lines (69 loc) · 2.6 KB
/
NanosTable.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
--[[ Nanos Table Library --]]
NanosTable = {}
function NanosTable.ShallowCopy(original_table)
return __copy_table_shallow(original_table)
end
function NanosTable.Dump(original_table)
-- Table used to store already visited tables (avoid recursion)
local visited = {}
-- Table used to store the final output, which will be concatenated in the end
local buffer = {}
-- Cache table.insert as local because it's faster
local table_insert = table.insert
-- Internal recursive function
local function DumpRecursive(object, indentation)
local object_type = type(object)
-- If it's a table and was not outputted yet
if (object_type == 'table' and not visited[object]) then
local object_metatable = getmetatable(object)
-- If it's a framework struct, just stringify it
if (object_metatable == Vector or object_metatable == Rotator or object_metatable == Vector2D or object_metatable == Color) then
-- Anything else just stringify it
table_insert(buffer, tostring(object))
return
end
-- Marks as visited
visited[object] = true
-- Stores all keys in another table, sorting it
local keys = {}
for key, v in pairs(object) do
table_insert(keys, key)
end
table.sort(keys, function(a, b)
if (type(a) == "number" and type(b) == "number") then
return a < b
else
return tostring(a) < tostring(b)
end
end)
-- Increases one indentation, as we will start outputting table elements
indentation = indentation + 1
-- Main table displays '{' in a separated line, subsequent ones will be in the same line
table_insert(buffer, indentation == 1 and "\n{" or "{")
-- For each member of the table, recursively outputs it
for k, key in ipairs(keys) do
local formatted_key = type(key) == "number" and tostring(key) or '"' .. tostring(key) .. '"'
-- Appends the Key with indentation
table_insert(buffer, "\n" .. string.rep(" ", indentation * 4) .. formatted_key .. " = ")
-- Appends the Element
DumpRecursive(object[key], indentation)
-- Appends a last comma
table_insert(buffer, ",")
end
-- After outputted the whole table, backs one indentation
indentation = indentation - 1
-- Adds the closing bracket
table_insert(buffer, "\n" .. string.rep(" ", indentation * 4) .. "}")
elseif (object_type == "string") then
-- Outputs string with quotes
table_insert(buffer, '"' .. tostring(object) .. '"')
else
-- Anything else just stringify it
table_insert(buffer, tostring(object))
end
end
-- Main call
DumpRecursive(original_table, 0)
-- After all, concatenates the results
return table.concat(buffer)
end