-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.lua
More file actions
222 lines (194 loc) · 6.46 KB
/
Copy pathMain.lua
File metadata and controls
222 lines (194 loc) · 6.46 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
--========================= SETTINGS =========================--
local ignore_empty_scripts = true
local empty_threshold = 12
local randomize_name = false
local kick_when_dumping = true
local kick_message = "Freezing game for a clean script dump. You can rejoin after."
local max_retries = 3
local retry_delay = 0.03
local yield_interval = 0.4
local progress_interval = 0.25
--============================================================--
local Players = game:GetService("Players")
local prefix = "scripts_" .. tostring(game.PlaceId)
local CoreGui = game:FindFirstChildOfClass("CoreGui") or game:FindFirstChild("CoreGui")
local CorePackages = game:FindFirstChild("CorePackages")
assert(type(decompile) == "function", "This executor does not expose decompile()")
--======================= FILENAME SANITIZER =======================--
local INVALID_PATTERN = "[%z\1-\31\127-\255\\/:%*%?\"<>|]"
local function makevalid(str)
local clean = (tostring(str):gsub(INVALID_PATTERN, "_"))
if clean == "" or clean == "." or clean == ".." then
clean = "_"
end
return clean
end
--========================= GATHER SCRIPTS =========================--
local scripts = {}
local seen = {}
local function add_script(inst)
if seen[inst] then return end
if not inst:IsA("LuaSourceContainer") then return end
if CoreGui and inst:IsDescendantOf(CoreGui) then return end
if CorePackages and inst:IsDescendantOf(CorePackages) then return end
seen[inst] = true
scripts[#scripts + 1] = inst
end
if type(getscripts) == "function" then
local ok, list = pcall(getscripts)
if ok and type(list) == "table" then
for _, inst in ipairs(list) do
pcall(add_script, inst)
end
end
end
local function gather(inst)
pcall(add_script, inst)
for _, child in ipairs(inst:GetChildren()) do
gather(child)
end
end
if type(getnilinstances) == "function" then
local ok, nils = pcall(getnilinstances)
if ok and type(nils) == "table" then
for _, v in ipairs(nils) do
pcall(gather, v)
end
end
end
gather(game)
local total = #scripts
print(("Found %d scripts"):format(total))
if total == 0 then return end
--======================= PATH RESOLUTION =======================--
local function get_folder(inst)
local parts = {}
local cur = inst.Parent
while cur and cur ~= game do
table.insert(parts, 1, makevalid(cur.Name))
cur = cur.Parent
end
if not inst.Parent then
table.insert(parts, 1, "_Nil")
end
return table.concat(parts, "/")
end
local used_paths = {}
local function unique_path(folder, base)
local path = folder == "" and base or (folder .. "/" .. base)
if not used_paths[path] then
used_paths[path] = true
return path
end
local name, ext = base:match("^(.*)(%.lua)$")
name = name or base
ext = ext or ""
local n = 1
repeat
n += 1
local candidate = ("%s (%d)%s"):format(name, n, ext)
path = folder == "" and candidate or (folder .. "/" .. candidate)
until not used_paths[path]
used_paths[path] = true
return path
end
--======================= FOLDER CREATION =======================--
local folder_cache = {}
local function ensure_folder(folder)
if folder == "" or folder_cache[folder] then return end
local accum = ""
for segment in folder:gmatch("[^/]+") do
accum = accum == "" and segment or (accum .. "/" .. segment)
local full = prefix .. "/" .. accum
if not folder_cache[full] then
if not isfolder(full) then
pcall(makefolder, full)
end
folder_cache[full] = true
end
end
folder_cache[folder] = true
end
--========================= DECOMPILE =========================--
local function try_decompile(inst)
for attempt = 1, max_retries do
local ok, res = pcall(decompile, inst)
if ok and type(res) == "string" and #res > 0 then
return res
end
if attempt < max_retries then
task.wait(retry_delay)
end
end
return nil
end
--========================= KICK / FREEZE =========================--
if not isfolder(prefix) then pcall(makefolder, prefix) end
folder_cache[prefix] = true
if kick_when_dumping then
pcall(function()
local lp = Players.LocalPlayer
if lp then lp:Kick(kick_message) end
end)
task.wait(0.5)
end
--========================= MAIN DUMP =========================--
local saved, skipped, failed = 0, 0, 0
local failures = {}
local function process(i, inst)
local folder = get_folder(inst)
local display = folder == "" and inst.Name or (folder .. "/" .. inst.Name)
local src = try_decompile(inst)
if not src then
failed += 1
failures[#failures + 1] = display
return
end
if ignore_empty_scripts and #src < empty_threshold then
skipped += 1
return
end
local base = makevalid(inst.Name) .. "." .. inst.ClassName .. ".lua"
if randomize_name then
local ok, id = pcall(inst.GetDebugId, inst)
base = (ok and id or tostring(i)) .. "_" .. base
end
ensure_folder(folder)
local file_path = prefix .. "/" .. unique_path(folder, base)
local wok, werr = pcall(writefile, file_path, src)
if wok then
saved += 1
return
end
local short = prefix .. "/" .. (folder == "" and "" or (folder .. "/"))
.. inst.ClassName .. "_" .. tostring(i) .. ".lua"
if pcall(writefile, short, src) then
saved += 1
else
failed += 1
failures[#failures + 1] = display .. " (write error: " .. tostring(werr) .. ")"
end
end
local clock = os.clock
local last_yield, last_progress = clock(), 0
for i = 1, total do
pcall(process, i, scripts[i])
local now = clock()
if now - last_progress >= progress_interval then
last_progress = now
print(("[%d/%d] saved %d skipped %d failed %d"):format(i, total, saved, skipped, failed))
end
if now - last_yield >= yield_interval then
last_yield = now
task.wait()
end
end
--========================= SUMMARY =========================--
print(("Dump finished! %d saved, %d skipped, %d failed (of %d). Folder: %s")
:format(saved, skipped, failed, total, prefix))
if #failures > 0 then
warn(("%d scripts could not be dumped:"):format(#failures))
for _, name in ipairs(failures) do
warn(" - " .. name)
end
end