forked from Asnxthaony/Roco-Kingdom-World-Script
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserpent.lua
More file actions
272 lines (256 loc) · 8.31 KB
/
Copy pathserpent.lua
File metadata and controls
272 lines (256 loc) · 8.31 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
local n, v = "serpent", "0.30"
local c, d = "Paul Kulchenko", "Lua serializer and pretty printer"
local snum = {
[tostring(1 / 0)] = "1/0 --[[math.huge]]",
[tostring(-1 / 0)] = "-1/0 --[[-math.huge]]",
[tostring(0 / 0)] = "0/0"
}
local badtype = {
thread = true,
userdata = true,
cdata = true
}
local getmetatable = debug and debug.getmetatable or _ENV.getmetatable
local function pairs(t)
return next, t
end
local keyword, globals, G = {}, {}, _G or _ENV
for _, k in ipairs({
"and",
"break",
"do",
"else",
"elseif",
"end",
"false",
"for",
"function",
"goto",
"if",
"in",
"local",
"nil",
"not",
"or",
"repeat",
"return",
"then",
"true",
"until",
"while"
}) do
keyword[k] = true
end
for k, v in pairs(G) do
globals[v] = k
end
for _, g in ipairs({
"coroutine",
"debug",
"io",
"math",
"string",
"table",
"os"
}) do
for k, v in pairs("table" == type(G[g]) and G[g] or {}) do
globals[v] = g .. "." .. k
end
end
local function s(t, opts)
local name, indent, fatal, maxnum = opts.name, opts.indent, opts.fatal, opts.maxnum
local sparse, custom, huge = opts.sparse, opts.custom, not opts.nohuge
local space, maxl = opts.compact and "" or " ", opts.maxlevel or math.huge
local maxlen, metatostring = tonumber(opts.maxlength), opts.metatostring
local iname, comm = "_" .. (name or ""), not opts.comment or tonumber(opts.comment) or math.huge
local numformat = opts.numformat or "%.17g"
local seen, sref, syms, symn = {}, {
"local " .. iname .. "={}"
}, {}, 0
local function gensym(val)
return "_" .. tostring(tostring(val)):gsub("[^%w]", ""):gsub("(%d%w+)", function(s)
if not syms[s] then
symn = symn + 1
syms[s] = symn
end
return tostring(syms[s])
end)
end
local function safestr(s)
return type(s) == "number" and tostring(huge and snum[tostring(s)] or numformat:format(s)) or type(s) ~= "string" and tostring(s) or ("%q"):format(s):gsub("\n", "n"):gsub("\026", "\\026")
end
local function comment(s, l)
return comm and (l or 0) < comm and " --[[" .. select(2, pcall(tostring, s)) .. "]]" or ""
end
local function globerr(s, l)
return globals[s] and globals[s] .. comment(s, l) or not fatal and safestr(select(2, pcall(tostring, s))) or error("Can't serialize " .. tostring(s))
end
local function safename(path, name)
local n = nil == name and "" or name
local plain = type(n) == "string" and n:match("^[%l%u_][%w_]*$") and not keyword[n]
local safe = plain and n or "[" .. safestr(n) .. "]"
return (path or "") .. (plain and path and "." or "") .. safe, safe
end
local alphanumsort = type(opts.sortkeys) == "function" and opts.sortkeys or function(k, o, n)
local maxn, to = tonumber(n) or 12, {number = "a", string = "b"}
local function padnum(d)
return ("%0" .. tostring(maxn) .. "d"):format(tonumber(d))
end
table.sort(k, function(a, b)
return (nil ~= k[a] and 0 or to[type(a)] or "z") .. tostring(a):gsub("%d+", padnum) < (nil ~= k[b] and 0 or to[type(b)] or "z") .. tostring(b):gsub("%d+", padnum)
end)
end
local function val2str(t, name, indent, insref, path, plainindex, level)
local ttype, level, mt = type(t), level or 0, getmetatable(t)
local spath, sname = safename(path, name)
local tag = plainindex and (type(name) == "number" and "" or name .. space .. "=" .. space) or nil ~= name and sname .. space .. "=" .. space or ""
if seen[t] then
sref[#sref + 1] = spath .. space .. "=" .. space .. seen[t]
return tag .. "nil" .. comment("ref", level)
end
if type(mt) == "table" then
local to, tr = pcall(function()
return mt.__tostring(t)
end)
local so, sr = pcall(function()
return mt.__serialize(t)
end)
if opts.metatostring ~= false and to or so then
seen[t] = insref or spath
t = so and sr or tr
ttype = type(t)
end
end
if "table" == ttype then
if level >= maxl then
return tag .. "{}" .. comment("maxlvl", level)
end
seen[t] = insref or spath
if nil == next(t) then
return tag .. "{}" .. comment(t, level)
end
if maxlen and maxlen < 0 then
return tag .. "{}" .. comment("maxlen", level)
end
local maxn, o, out = math.min(#t, maxnum or #t), {}, {}
for key = 1, maxn do
o[key] = key
end
if not maxnum or #o < maxnum then
local n = #o
for key in pairs(t) do
if o[key] ~= key then
n = n + 1
o[n] = key
end
end
end
if maxnum and #o > maxnum then
o[maxnum + 1] = nil
end
if opts.sortkeys and maxn < #o then
alphanumsort(o, t, opts.sortkeys)
end
local sparse = sparse and maxn < #o
for n, key in ipairs(o) do
local value, ktype, plainindex = t[key], type(key), n <= maxn and not sparse
if not ((not opts.valignore or not opts.valignore[value]) and (not opts.keyallow or opts.keyallow[key])) or opts.keyignore and opts.keyignore[key] or opts.valtypeignore and opts.valtypeignore[type(value)] or sparse and nil == value then
elseif "table" == ktype or "function" == ktype or badtype[ktype] then
if not seen[key] and not globals[key] then
sref[#sref + 1] = "placeholder"
local sname = safename(iname, gensym(key))
sref[#sref] = val2str(key, sname, indent, sname, iname, true)
end
sref[#sref + 1] = "placeholder"
local path = seen[t] .. "[" .. tostring(seen[key] or globals[key] or gensym(key)) .. "]"
sref[#sref] = path .. space .. "=" .. space .. tostring(seen[value] or val2str(value, nil, indent, path))
else
out[#out + 1] = val2str(value, key, indent, insref, seen[t], plainindex, level + 1)
if maxlen then
maxlen = maxlen - #out[#out]
if maxlen < 0 then
break
end
end
end
end
local prefix = string.rep(indent or "", level)
local head = indent and "{\n" .. prefix .. indent or "{"
local body = table.concat(out, "," .. (indent and "\n" .. prefix .. indent or space))
local tail = indent and "\n" .. prefix .. "}" or "}"
return (custom and custom(tag, head, body, tail, level) or tag .. head .. body .. tail) .. comment(t, level)
elseif badtype[ttype] then
seen[t] = insref or spath
return tag .. globerr(t, level)
elseif "function" == ttype then
seen[t] = insref or spath
if opts.nocode then
return tag .. "function() --[[..skipped..]] end" .. comment(t, level)
end
local ok, res = pcall(string.dump, t)
local func = ok and "((loadstring or load)(" .. safestr(res) .. ",'@serialized'))" .. comment(t, level)
return tag .. (func or globerr(t, level))
else
return tag .. safestr(t)
end
end
local sepr = indent and "\n" or ";" .. space
local body = val2str(t, name, indent)
local tail = #sref > 1 and table.concat(sref, sepr) .. sepr or ""
local warn = opts.comment and #sref > 1 and space .. "--[[incomplete output with shared/self-references skipped]]" or ""
return not name and body .. warn or "do local " .. body .. sepr .. tail .. "return " .. name .. sepr .. "end"
end
local function deserialize(data, opts)
local env = opts and opts.safe == false and G or setmetatable({}, {
__index = function(t, k)
return t
end,
__call = function(t, ...)
error("cannot call functions")
end
})
local f, res = (loadstring or load)("return " .. data, nil, nil, env)
if not f then
f, res = (loadstring or load)(data, nil, nil, env)
end
if not f then
return f, res
end
if setfenv then
setfenv(f, env)
end
return pcall(f)
end
local function merge(a, b)
if b then
for k, v in pairs(b) do
a[k] = v
end
end
return a
end
return {
_NAME = n,
_COPYRIGHT = c,
_DESCRIPTION = d,
_VERSION = v,
serialize = s,
load = deserialize,
dump = function(a, opts)
return s(a, merge({
name = "_",
compact = true,
sparse = true
}, opts))
end,
line = function(a, opts)
return s(a, merge({sortkeys = true, comment = true}, opts))
end,
block = function(a, opts)
return s(a, merge({
indent = " ",
sortkeys = true,
comment = true
}, opts))
end
}