-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepass.luau
More file actions
457 lines (398 loc) · 12.3 KB
/
Copy pathprepass.luau
File metadata and controls
457 lines (398 loc) · 12.3 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
--[[
USSI Decompilation PrePass
Prefetches and caches decompiled scripts via external API,
then loads saveinstance with cached results.
]]
local VERSION = "1.0.0"
local VERSION_URL = "https://raw.githubusercontent.com/achmdfzn/ModernSaveInstance/main/VERSION"
task.spawn(function()
local ok, latest = pcall(game.HttpGet, game, VERSION_URL, true)
if ok and latest and #latest > 0 then
latest = latest:gsub("%s+", "")
if latest ~= VERSION then
warn(
"[ModernSaveInstance/PrePass] Update available: " .. latest
.. " (current: " .. VERSION .. ")\n"
.. "Download: https://github.com/achmdfzn/ModernSaveInstance"
)
end
end
end)
assert(getscriptbytecode, "executor does not support getscriptbytecode")
local HttpRequest = request or http_request
assert(HttpRequest, "executor does not support an http request function")
local cloneref = cloneref or function(o) return o end
local game = cloneref(game)
local workspace = cloneref(workspace)
local HttpService = cloneref(game:GetService("HttpService"))
local RunService = cloneref(game:GetService("RunService"))
-- Core containers to filter out
local CoreContainers = {}
for _, ServiceName in { "CoreGui", "CorePackages", "RobloxReplicatedStorage", "RobloxLocalReplicatedStorage" } do
local Ok, Service = pcall(game.GetService, game, ServiceName)
if Ok and Service then
CoreContainers[#CoreContainers + 1] = cloneref(Service)
end
end
local function IsCoreInstance(o)
for _, Container in CoreContainers do
local Ok, Res = pcall(function()
return o == Container or o:IsDescendantOf(Container)
end)
if Ok and Res then
return true
end
end
local OkLocked, Locked = pcall(function()
return o.RobloxLocked
end)
return OkLocked and Locked
end
-- ---------------------------------------------------------------------------
-- Config
-- ---------------------------------------------------------------------------
local Config = {
RequestsPerMinute = 1400,
MaxInFlight = 30,
RequestTimeout = 20,
ApiUrl = "https://api.lua.expert/decompile",
Verbose = true,
-- Where to fetch saveinstance from (default: local repo)
UssiRepoURL = "https://raw.githubusercontent.com/achmdfzn/ModernSaveInstance/main/",
UssiScript = "saveinstance",
}
-- ---------------------------------------------------------------------------
-- Base64 (fallback if executor lacks it)
-- ---------------------------------------------------------------------------
local function Base64Encode(Data)
if base64_encode then
return base64_encode(Data)
end
local B = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
return ((Data:gsub(".", function(x)
local R, Byte = "", x:byte()
for i = 8, 1, -1 do
R = R .. (Byte % 2 ^ i - Byte % 2 ^ (i - 1) > 0 and "1" or "0")
end
return R
end) .. "0000"):gsub("%d%d%d?%d?%d?%d?", function(x)
if #x < 6 then return "" end
local C = 0
for i = 1, 6 do
C = C + (x:sub(i, i) == "1" and 2 ^ (6 - i) or 0)
end
return B:sub(C + 1, C + 1)
end) .. ({ "", "==", "=" })[#Data % 3 + 1])
end
-- ---------------------------------------------------------------------------
-- Decompiled script cache
-- ---------------------------------------------------------------------------
local Cache = {}
-- ---------------------------------------------------------------------------
-- Rate-limited API caller
-- ---------------------------------------------------------------------------
local NextSlot = 0
local function AcquireRateSlot()
local MinGap = 60 / Config.RequestsPerMinute
local Now = os.clock()
local MySlot = NextSlot > Now and NextSlot or Now
NextSlot = MySlot + MinGap
local Delay = MySlot - Now
if Delay > 0 then
task.wait(Delay)
end
end
local function DecompileViaAPI(Bytecode)
AcquireRateSlot()
local Settled, Result = false, nil
task.spawn(function()
local Ok, Res = pcall(HttpRequest, {
Url = Config.ApiUrl,
Method = "POST",
Headers = { ["content-type"] = "application/json" },
Body = HttpService:JSONEncode({ script = Base64Encode(Bytecode) }),
})
if not Settled then
Settled = true
Result = { Ok = Ok, Res = Res }
end
end)
local Deadline = os.clock() + Config.RequestTimeout
while not Settled and os.clock() < Deadline do
task.wait(0.05)
end
if not Settled then
Settled = true
return false, "timeout"
end
if not Result.Ok then
return false, tostring(Result.Res)
end
if not Result.Res or Result.Res.StatusCode ~= 200 then
return false, Result.Res and Result.Res.Body or "no response"
end
return true, Result.Res.Body
end
-- ---------------------------------------------------------------------------
-- Pooled parallel executor
-- ---------------------------------------------------------------------------
local function RunPool(Items, Worker)
local Total = #Items
if Total == 0 then
return
end
local NextIndex, Done = 1, 0
local function StartNext()
if NextIndex > Total then
return
end
local i = NextIndex
NextIndex += 1
task.spawn(function()
Worker(Items[i])
Done += 1
StartNext()
end)
end
for _ = 1, math.min(Config.MaxInFlight, Total) do
StartNext()
end
while Done < Total do
task.wait()
end
end
-- ---------------------------------------------------------------------------
-- Instance name resolver
-- ---------------------------------------------------------------------------
local function ResolveName(o)
local Ok, Name = pcall(function()
return o:GetFullName()
end)
if Ok and type(Name) == "string" and Name ~= "" then
return Name
end
local Ok2, Short = pcall(function()
return o.Name
end)
return (Ok2 and Short) or "<unknown>"
end
-- ---------------------------------------------------------------------------
-- Gather unique scripts (deduplicated by bytecode)
-- ---------------------------------------------------------------------------
local function GatherUniqueScripts()
local Objs = {}
-- Descendants
for _, o in game:GetDescendants() do
if o:IsA("LuaSourceContainer") and not IsCoreInstance(o) then
local IsServer = o:IsA("Script")
and (o.RunContext == Enum.RunContext.Legacy or o.RunContext == Enum.RunContext.Server)
if not IsServer then
Objs[#Objs + 1] = cloneref(o)
end
end
end
-- Loaded modules (if available)
if getloadedmodules then
local OkM, Mods = pcall(getloadedmodules)
if OkM and type(Mods) == "table" then
for _, m in Mods do
if not IsCoreInstance(m) then
Objs[#Objs + 1] = cloneref(m)
end
end
end
end
-- Deduplicate by bytecode
local Seen, Unique = {}, {}
for _, o in Objs do
local Ok, Bc = pcall(getscriptbytecode, o)
if Ok and type(Bc) == "string" and Bc ~= "" and not Seen[Bc] then
Seen[Bc] = true
Unique[#Unique + 1] = { Bytecode = Bc, Name = ResolveName(o) }
end
end
return Unique
end
-- ---------------------------------------------------------------------------
-- Progress UI (Drawing API)
-- ---------------------------------------------------------------------------
local NoOpUi = { Update = function() end, Destroy = function() end }
local function CreateProgressUI(Total)
if type(Drawing) ~= "table" and type(Drawing) ~= "userdata" then
return NoOpUi
end
local State = { Done = 0, Dispatched = 0, Name = "" }
local Objects = {}
local Connection
local Ok = pcall(function()
local Camera = workspace.CurrentCamera and cloneref(workspace.CurrentCamera)
local Viewport = (Camera and Camera.ViewportSize) or Vector2.new(1280, 720)
local BarWidth, BarHeight = 420, 14
local BarX = math.floor((Viewport.X - BarWidth) / 2)
local BarY = 24
local function Square(Color, Filled, Thickness)
local S = Drawing.new("Square")
S.Color = Color
S.Filled = Filled
S.Thickness = Thickness or 1
S.Visible = true
Objects[#Objects + 1] = S
return S
end
local Bg = Square(Color3.fromRGB(20, 20, 20), true)
Bg.Size = Vector2.new(BarWidth, BarHeight)
Bg.Position = Vector2.new(BarX, BarY)
Bg.Transparency = 0.85
local DispatchedFill = Square(Color3.fromRGB(200, 160, 60), true)
DispatchedFill.Size = Vector2.new(0, BarHeight)
DispatchedFill.Position = Vector2.new(BarX, BarY)
DispatchedFill.Transparency = 0.55
local CompletedFill = Square(Color3.fromRGB(80, 180, 90), true)
CompletedFill.Size = Vector2.new(0, BarHeight)
CompletedFill.Position = Vector2.new(BarX, BarY)
local Border = Square(Color3.fromRGB(220, 220, 220), false, 1)
Border.Size = Vector2.new(BarWidth, BarHeight)
Border.Position = Vector2.new(BarX, BarY)
local function Text(Size, Color)
local T = Drawing.new("Text")
T.Size = Size
T.Color = Color
T.Center = true
T.Outline = true
T.Visible = true
T.Text = ""
Objects[#Objects + 1] = T
return T
end
local Progress = Text(14, Color3.fromRGB(255, 255, 255))
Progress.Position = Vector2.new(Viewport.X / 2, BarY + BarHeight + 4)
Progress.Text = ("0 / %d"):format(Total)
local Name = Text(13, Color3.fromRGB(200, 200, 200))
Name.Position = Vector2.new(Viewport.X / 2, BarY + BarHeight + 22)
Connection = RunService.RenderStepped:Connect(function()
CompletedFill.Size = Vector2.new(BarWidth * (State.Done / Total), BarHeight)
DispatchedFill.Size = Vector2.new(BarWidth * (State.Dispatched / Total), BarHeight)
Progress.Text = ("%d / %d"):format(State.Done, Total)
local Display = State.Name
if #Display > 80 then
Display = "..." .. Display:sub(-77)
end
Name.Text = Display
end)
end)
if not Ok then
if Connection then
pcall(function() Connection:Disconnect() end)
end
for _, d in Objects do
pcall(function() d:Remove() end)
end
return NoOpUi
end
return {
Update = function(_, Done, Dispatched, ScriptName)
State.Done = Done
State.Dispatched = Dispatched
if ScriptName then
State.Name = ScriptName
end
end,
Destroy = function()
if Connection then
pcall(function() Connection:Disconnect() end)
end
for _, d in Objects do
pcall(function() d:Remove() end)
end
end,
}
end
-- ---------------------------------------------------------------------------
-- Prepass: fetch all unique scripts via API
-- ---------------------------------------------------------------------------
local function Prepass()
local Unique = GatherUniqueScripts()
local Total = #Unique
if Config.Verbose then
print(("[prepass] %d unique scripts"):format(Total))
end
if Total == 0 then
return
end
local Ui = CreateProgressUI(Total)
local OkCount, Completed, Dispatched = 0, 0, 0
RunPool(Unique, function(Item)
Dispatched += 1
Ui:Update(Completed, Dispatched, Item.Name)
if not Cache[Item.Bytecode] then
local Ok, Body = DecompileViaAPI(Item.Bytecode)
if Ok then
Cache[Item.Bytecode] = Body
OkCount += 1
end
end
Completed += 1
Ui:Update(Completed, Dispatched, Item.Name)
end)
Ui:Destroy()
if Config.Verbose then
print(("[prepass] cached %d / %d"):format(OkCount, Total))
end
end
-- ---------------------------------------------------------------------------
-- Hook global decompile() to serve cached results
-- ---------------------------------------------------------------------------
local HookInstalled = false
local function InstallDecompileHook()
if HookInstalled then
return
end
HookInstalled = true
local OldDecompile = getgenv().decompile
getgenv().decompile = function(Scr)
local Ok, Bc = pcall(getscriptbytecode, Scr)
if Ok and type(Bc) == "string" and Bc ~= "" then
local Hit = Cache[Bc]
if Hit then
return Hit
end
end
if OldDecompile then
return OldDecompile(Scr)
end
if Ok and type(Bc) == "string" and Bc ~= "" then
local Good, Body = DecompileViaAPI(Bc)
if Good then
Cache[Bc] = Body
end
return Body
end
return "-- could not read script bytecode"
end
end
-- ---------------------------------------------------------------------------
-- Entry point
-- ---------------------------------------------------------------------------
return function(Options, PrepassOptions)
PrepassOptions = PrepassOptions or {}
for k, v in PrepassOptions do
Config[k] = v
end
-- Install hook so saveinstance uses our cache
InstallDecompileHook()
-- Run prepass unless skipped
if not PrepassOptions.SkipPrepass then
Prepass()
end
-- Optionally run saveinstance
if PrepassOptions.SkipSaveInstance then
return
end
local RepoURL = PrepassOptions.UssiRepoURL or Config.UssiRepoURL
local ScriptName = PrepassOptions.UssiScript or Config.UssiScript
local synsaveinstance = loadstring(
game:HttpGet(RepoURL .. ScriptName .. ".luau", true),
ScriptName
)()
return synsaveinstance(Options or {})
end