-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsave.lua
216 lines (187 loc) · 5.15 KB
/
save.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
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
--[==[HELP]==
[1] - string | boolean | nil
The relative file path to save to.
[2] - any
The object to parse and save to a file.
[3] - boolean | nil
If true or nil, prettify the result.
[4] - string | nil
The suffix to add to the string.
[5] - boolean | nil
If true, append the file instead of overwriting.
]==] --
--
local args = _E and _E.ARGS or {}
local FILE = args[1] or 'temp.txt'
local VALUE = args[2]
local PRETTY = args[3]
local SUFFIX = args[4]
local APPEND = args[5]
if PRETTY == nil then PRETTY = true end
if SUFFIX == nil then SUFFIX = '' end
-- #region patch parse-obj.lua
local function escape_char(c)
if c == '\n' then
return '\\n'
elseif c == '\r' then
return '\\r'
elseif c == '\t' then
return '\\t'
elseif c == '\b' then
return '\\b'
elseif c == '\f' then
return '\\f'
elseif c == '"' then
return '\\"'
elseif c == '\\' then
return '\\\\'
else
return string.format('\\u{%x}', c:byte())
end
end
local function repr_str(s)
return string.format('"%s"', s:gsub('[\000-\031%\\"]', escape_char))
end
-- Returns proper string wrapping for instances
local function obj_name(o)
local n = o.Name
if #n == 0 --
or n:match('[^%w]+') --
or n:sub(1, 1):match('[^%a]') --
then return string.format('[%s]', repr_str(n)) end
return string.format('.%s', n)
end
local function get_full(o)
local lp = game.Players.LocalPlayer
if not o then return nil end
local r = {obj_name(o)}
local p = o.Parent
while p do
if p == game then
table.insert(r, 1, 'game')
return table.concat(r, '')
elseif p == lp then
table.insert(r, 1, 'game.Players.LocalPlayer')
return table.concat(r, '')
end
table.insert(r, 1, obj_name(p))
p = p.Parent
end
table.insert(r, 1, 'NIL')
return table.concat(r, '')
end
local ARG_REPR_TYPES = { --
CFrame = true,
Vector3 = true,
Vector2 = true,
Vector3int16 = true,
Vector2int16 = true,
UDim2 = true,
}
local SEQ_REPR_TYPES = { --
ColorSequence = true,
NumberSequence = true,
}
local SEQ_KEYP_TYPES = { --
ColorSequenceKeypoint = true,
NumberSequenceKeypoint = true,
}
local function parse(obj, nl, lvl) -- Convert the types into strings
local typ = typeof(obj)
local lvl = lvl or 0
if nl == nil then nl = false end
if typ == 'string' then
if lvl == 0 then return obj end
return repr_str(obj)
end
-- Instance:GetFullName() except it's not handicapped
if typ == 'Instance' then return get_full(obj) end
if typ == 'table' then
if lvl > 666 then return 'DEEP_TABLE' end
local keyed_vals = {}
local ipair_vals = {}
local tab = ' '
local c = 0
local ws_zer = ' '
local ws_beg = ' '
local ws_cat = ' '
local ws_end = ' '
local sep = ','
if nl then
ws_beg = string.format('\n%s', string.rep(tab, lvl + 1))
ws_cat = string.format('\n%s', string.rep(tab, lvl + 1))
ws_end = string.format('\n%s', string.rep(tab, lvl))
ws_zer = string.format('\n%s', string.rep(tab, lvl))
end
for i, o in next, obj do
c = c + 1
local o_str
if o ~= obj then
o_str = parse(o, nl, lvl + 1)
else
o_str = 'THIS_TABLE'
end
if c == i then
table.insert(ipair_vals, string.format('%s%s', o_str, sep))
else
local i_str = i ~= obj and parse(i, nl, lvl + 1) or 'THIS_TABLE'
table.insert(keyed_vals, string.format('[%s] = %s%s', i_str, o_str, sep))
end
end
-- Merges keyed values with ipair values - in that order.
table.sort(keyed_vals)
table.move(ipair_vals, 1, #ipair_vals, #keyed_vals + 1, keyed_vals)
if #keyed_vals == 0 then return string.format('{%s}', ws_zer) end
local all_str = table.concat(keyed_vals, ws_cat)
return string.format('{%s%s%s}', ws_beg, all_str, ws_end)
end
if ARG_REPR_TYPES[typ] then
local f_args = {typ, tostring(obj):gsub('[{}]', '')}
return string.format('%s.new(%s)', unpack(f_args))
end
if SEQ_REPR_TYPES[typ] then
local f_args = {typ, parse(obj.Keypoints, nl, lvl)}
return string.format('%s.new(%s)', unpack(f_args))
end
if SEQ_KEYP_TYPES[typ] then
local f_args = {typ, obj.Time, parse(obj.Value, nl, lvl)}
return string.format('%s.new(%s, %s)', unpack(f_args))
end
if typ == 'Color3' then
local f_args = {typ, obj.R * 255, obj.G * 255, obj.B * 255}
return string.format('%s.fromRGB(%d, %d, %d)', unpack(f_args))
end
if typ == 'NumberRange' then
local f_args = {typ, tostring(obj.Min), tostring(obj.Max)}
return string.format('%s.new(%s, %s)', unpack(f_args))
end
if typ == 'userdata' then
local res
local meta = getrawmetatable(obj)
local m_ts = meta and meta.__tostring
-- Remove __tostring fields to counter traps.
if m_ts then
setreadonly(meta, false)
meta.__tostring = nil
res = tostring(obj)
rawset(meta, '__tostring', m_ts)
setreadonly(meta, rawget(meta, '__metatable') ~= nil)
else
res = tostring(obj)
end
return res
end
return tostring(obj)
end
-- #endregion patch
local parsed
if typeof(VALUE) ~= 'string' then
parsed = parse(VALUE, PRETTY)
else
parsed = VALUE
end
if APPEND then
appendfile(FILE, parsed .. SUFFIX)
else
writefile(FILE, parsed .. SUFFIX)
end