forked from MakeSureDudeDies/Lua-Table-Reconstructor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
115 lines (104 loc) · 3.43 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
local function reconstruct_table(t_)
local function tL(t)
if type(t) ~= "table" then
return 0
end
local a = 0
for _, _ in pairs(t) do
a = a + 1
end
return a
end
local function tL_nested(t)
if type(t) ~= "table" then
return 0
end
local a = 0
for _, v in pairs(t) do
if type(v) == "table" then
a = a + tL_nested(v)
end
a = a + 1 -- Even if it was a table, we still count the table index itself as a value, not just its subvalues!
end
return a
end
if type(t_) ~= "table" then
return string.format("-- Given object is not a table, rather a %s. Cannot reconstruct.", type(t_))
end
local function inner__reconstruct_table(t, isChildTable, childDepth)
local tableConstruct = ""
if not isChildTable then
tableConstruct = "local t = {\n"
end
if childDepth > 30 then
tableConstruct = string.format("%s\n--Cannot Reconstruct, Too much nesting!\n", tableConstruct)
return tableConstruct
end
for idx, val in pairs(t) do
local idxType = type(val)
if type(idx) == "number" then
idx = idx
else
idx = string.format('"%s"', string.gsub(string.gsub(tostring(idx), "'", "'"), '"', '\\"'))
end
if idxType == "boolean" then
tableConstruct = string.format(
"%s%s[%s] = %s",
tableConstruct,
string.rep("\t", childDepth),
tostring(idx),
val and "true" or "false"
)
elseif idxType == "function" or idxType == "number" or idxType == "string" then
local v = tostring(val)
if idxType == "number" then
if string.match(tostring(v), "nan") then
v = "0 / 0"
elseif string.match(tostring(v), "inf") then
v = "math.huge"
elseif tostring(v) == tostring(math.pi) then
v = "math.pi"
end
end
if idxType == "string" then
v = string.format('"%s"', string.gsub(string.gsub(v, "'", "'"), '"', '\\"'))
end
tableConstruct =
string.format("%s%s[%s] = %s", tableConstruct, string.rep("\t", childDepth), tostring(idx), v)
elseif idxType == "table" then
local r = inner__reconstruct_table(val, true, childDepth + 1)
tableConstruct =
string.format("%s%s[%s] = {\n%s", tableConstruct, string.rep("\t", childDepth), tostring(idx), r)
elseif idxType == "nil" then
tableConstruct =
string.format("%s%s[%s] = nil", tableConstruct, string.rep("\t", childDepth), tostring(idx))
elseif idxType == "userdata" then
tableConstruct = string.format(
'%s%s[%s] = "UserData. Cannot represent."',
string.rep("\t", childDepth),
tableConstruct,
tostring(idx)
)
end
tableConstruct = string.format("%s,\n", tableConstruct)
end
if isChildTable then
return string.format("%s%s}", tableConstruct, string.rep("\t", childDepth - 1))
else
return string.format("%s}\n", tableConstruct)
end
end
local welcomeMessage = [[
-- Table reconstructed using table_reconstructor by usrDottik (Originally made by MakeSureDudeDies)
-- Reconstruction began @ %s - GMT 00:00
-- Reconstruction completed @ %s - GMT 00:00
-- Indexes Found inside of the Table (W/o Nested Tables): %d
-- (With Nested Tables): %d
]]
local begin = tostring(os.date("!%Y-%m-%d %H:%M:%S"))
local reconstruction = inner__reconstruct_table(t_, false, 1)
local finish = tostring(os.date("!%Y-%m-%d %H:%M:%S"))
welcomeMessage = string.format(welcomeMessage, begin, finish, tL(t_), tL_nested(t_))
return string.format("%s%s", welcomeMessage, reconstruction)
end
return reconstruct_table