Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit c034cd3

Browse files
authored
Create SeralizeTable.lua
1 parent b0735b1 commit c034cd3

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

SeralizeTable.lua

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
-- created by iris https://v3rmillion.net/member.php?action=profile&uid=207161
2+
3+
type = typeof or type
4+
local str_types = {
5+
['boolean'] = true,
6+
['table'] = true,
7+
['userdata'] = true,
8+
['table'] = true,
9+
['function'] = true,
10+
['number'] = true,
11+
['nil'] = true
12+
}
13+
14+
local rawequal = rawequal or function(a, b)
15+
return a == b
16+
end
17+
18+
local function count_table(t)
19+
local c = 0
20+
for i, v in next, t do
21+
c = c + 1
22+
end
23+
24+
return c
25+
end
26+
27+
local function string_ret(o, typ)
28+
local ret, mt, old_func
29+
if not (typ == 'table' or typ == 'userdata') then
30+
return tostring(o)
31+
end
32+
mt = (getrawmetatable or getmetatable)(o)
33+
if not mt then
34+
return tostring(o)
35+
end
36+
37+
old_func = rawget(mt, '__tostring')
38+
rawset(mt, '__tostring', nil)
39+
ret = tostring(o)
40+
rawset(mt, '__tostring', old_func)
41+
return ret
42+
end
43+
44+
local function format_value(v)
45+
local typ = type(v)
46+
47+
if str_types[typ] then
48+
return string_ret(v, typ)
49+
elseif typ == 'string' then
50+
return '"'..v..'"'
51+
elseif typ == 'Instance' then
52+
return v:GetFullName()
53+
else
54+
return typ..'.new(' .. tostring(v) .. ')'
55+
end
56+
end
57+
58+
local function serialize_table(t, p, c, s)
59+
local str = ""
60+
local n = count_table(t)
61+
local ti = 1
62+
local e = n > 0
63+
64+
c = c or {}
65+
p = p or 1
66+
s = s or string.rep
67+
68+
local function localized_format(v, is_table)
69+
return is_table and (c[v][2] >= p) and serialize_table(v, p + 1, c, s) or format_value(v)
70+
end
71+
72+
c[t] = {t, 0}
73+
74+
for i, v in next, t do
75+
local typ_i, typ_v = type(i) == 'table', type(v) == 'table'
76+
c[i], c[v] = (not c[i] and typ_i) and {i, p} or c[i], (not c[v] and typ_v) and {v, p} or c[v]
77+
str = str .. s(' ', p) .. '[' .. localized_format(i, typ_i) .. '] = ' .. localized_format(v, typ_v) .. (ti < n and ',' or '') .. '
78+
'
79+
ti = ti + 1
80+
end
81+
82+
return ('{' .. (e and '
83+
' or '')) .. str .. (e and s(' ', p - 1) or '') .. '}'
84+
end
85+
86+
return serialize_table

0 commit comments

Comments
 (0)