-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDecompile.luau
More file actions
1461 lines (1182 loc) · 37.9 KB
/
Copy pathDecompile.luau
File metadata and controls
1461 lines (1182 loc) · 37.9 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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
if you can help me decompile FORGLOOP/FORGPREP/FORGPREP_INEXT/FORGPREP_NEXT (fork this and add those)
i will be thankful
FREE TO USE
]]
local string_format = string.format
local getfenv = getfenv
local function cerror(chunk, reason)
return string_format("--CELESTIALERROR[%s]: Failed to decompile reason: %s\nC\nC\nC\nC\nC\nC\nC\nC\nC", chunk, reason)
end
-- opcode list pls dont remove it you can add more but if you remove the code stops working
local opList = {
NOP = 0, BREAK = 1, -- new added ones that i forgot to add lol
LOADNIL = 2, LOADB = 3, LOADN = 4, LOADK = 5, MOVE = 6,
GETGLOBAL = 7, SETGLOBAL = 8, GETUPVAL = 9, SETUPVAL = 10,
CLOSEUPVALS = 11, GETIMPORT = 12, GETTABLE = 13, SETTABLE = 14,
GETTABLEKS = 15, SETTABLEKS = 16, GETTABLEN = 17, SETTABLEN = 18,
NEWCLOSURE = 19, NAMECALL = 20, CALL = 21, RETURN = 22,
JUMP = 23, JUMPBACK = 24, JUMPIF = 25, JUMPIFNOT = 26,
JUMPIFEQ = 27, JUMPIFLE = 28, JUMPIFLT = 29, JUMPIFNOTEQ = 30,
JUMPIFNOTLE = 31, JUMPIFNOTLT = 32, ADD = 33, SUB = 34,
MUL = 35, DIV = 36, MOD = 37, POW = 38, ADDK = 39, SUBK = 40,
MULK = 41, DIVK = 42, MODK = 43, POWK = 44, AND = 45, OR = 46,
ANDK = 47, ORK = 48, CONCAT = 49, NOT = 50, MINUS = 51, LENGTH = 52,
NEWTABLE = 53, DUPTABLE = 54, SETLIST = 55, FORNPREP = 56,
FORNLOOP = 57, FORGLOOP = 58, FORGPREP_INEXT = 59, FASTCALL3 = 60,
FORGPREP_NEXT = 61, PREPVARARGS = 62, GETVARARGS = 63, DUPCLOSURE = 64,
LOADKX = 66, JUMPX = 67, COVERAGE = 69, CAPTURE = 70, SUBRK = 71,
DIVRK = 72, FASTCALL2 = 74, FASTCALL2K = 75, FORGPREP = 76,
JUMPXEQKNIL = 77, JUMPXEQKB = 78, JUMPXEQKN = 79, JUMPXEQKS = 80,
IDIV = 81, IDIVK = 82
}
-- Helper to get constant text (string/number)
local function constToString(k)
if type(k) == "string" then
return ("%q"):format(k)
elseif type(k) == "number" then
return tostring(k)
elseif k == nil then
return "nil"
elseif type(k) == "boolean" then
return k and "true" or "false"
else
return "<const>"
end
end
local old_tostring = tostring
local tostring = function(x)
if type(x) == "string" then
return "'"..x.."'"
else
return old_tostring(x)
end
end
local firstnfor = nil
local namenfor = ""
-- Helper for var naming fallback
local function varName(proto, reg, pc)
if reg == firstnfor then
return namenfor
end
if proto.locvars then
for _, v in ipairs(proto.locvars) do
if v.startpc <= pc and v.endpc >= pc and v.reg == reg then
return v.varname
end
end
end
return "upvalue_" .. tostring(reg)
end
-- Core decompile function for a proto (function or main chunk)
local function decompileBlock(proto, indent, startpc, endpc, mainproto)
indent = indent or ""
startpc = startpc or 1
endpc = endpc or #proto.code
local env = getfenv(2)
local old_env_script = env.script
env.script = nil
local stack = {}
local output = {}
local pc = startpc
local globals = 0
-- idk why i did this lol
local isinsideforloop = 0
-- these are extreme important btw
local firstparamforloop = nil
local secondparamforloop = nil
local thirdparamforloop = nil
local forloopidk = {}
local calltype = nil
local callargs = nil -- i aint doing this yet
while pc <= endpc do
local instr = proto.code[pc]
local op = instr.opcode
local inst = instr
if instr.opname == "auxvalue" then
pc = pc + 1
continue
end
if op == opList.NOP then
pc = pc + 1
continue
end
if not op then
table.insert(output, indent .. "--CELESTIALERROR: opcode nil at pc=" .. pc)
pc = pc + 1
elseif op == opList.BREAK then
table.insert(output, indent .. "break")
pc = pc + 1
elseif op == opList.LOADK then
local k = instr.K
table.insert(output, indent .. ("local %s = %s"):format(varName(proto, instr.A, pc), constToString(k)))
pc = pc + 1
stack[instr.A] = k
elseif op == opList.LOADNIL then
table.insert(output, indent .. ("local %s = nil"):format(varName(proto, instr.A, pc)))
pc = pc + 1
stack[instr.A] = nil
elseif op == opList.LOADB then
table.insert(output, indent .. ("local %s = %s"):format(varName(proto, instr.A, pc), (instr.B ~= 0 and "true" or "false")))
pc = pc + 1
stack[instr.A] = instr.B == 1
elseif op == opList.LOADN then
local pct = proto.code[pc + 3]
local pcs = proto.code[pc + 2]
local pco = proto.code[pc + 1]
if pct.opcode == opList.FORNPREP then
stack[instr.A] = instr.D
firstnfor = instr.A - 1
pc += 1
continue
end
if pcs.opcode == opList.FORNPREP then
stack[instr.A] = instr.D
pc += 1
continue
end
if pco.opcode == opList.FORNPREP then
stack[instr.A] = instr.D
pc += 1
continue
end
table.insert(output, indent .. ("local %s = %d"):format(varName(proto, instr.A, pc), instr.D))
pc = pc + 1
stack[instr.A] = instr.D
elseif op == opList.MOVE then
local pass = false
for i,v in output do
if v:find(varName(proto, instr.B, pc)) then
pass = true
end
end
if pass == false then
pc = pc + 1
continue
end
table.insert(output, indent .. ("%s = %s"):format(varName(proto, instr.A, pc), varName(proto, instr.B, pc)))
pc = pc + 1
stack[instr.A] = stack[instr.B]
elseif op == opList.LOADKX then
local kv = inst.K
table.insert(output, indent.."local "..varName(proto, instr.A, pc).." = "..tostring(kv))
stack[inst.A] = kv
pc = pc + 1
elseif op == opList.FORNPREP then
isinsideforloop += 1
local A = instr.A
local Limit = stack[A]
local Step = stack[A + 1]
local Index = stack[A + 2]
thirdparamforloop = Step
secondparamforloop = Limit
firstparamforloop = Index
namenfor = string.rep("i", isinsideforloop)
if thirdparamforloop == 1 then
if not firstparamforloop or not secondparamforloop then
table.insert("--FOR LOOP STRUCTURE DETECTED BUT CAUSED ERROR WILL BE FIXED ON THE NEXT UPDATE")
end
pcall(function(...) -- error handling i guess
table.insert(output, indent..("for %s = %s, %s do"):format(string.rep("i", isinsideforloop), firstparamforloop, secondparamforloop))
end)
else
if not firstparamforloop or not secondparamforloop then
table.insert("--FOR LOOP STRUCTURE DETECTED BUT CAUSED ERROR WILL BE FIXED ON THE NEXT UPDATE")
end
pcall(function(...)
table.insert(output, indent..("for i = %s, %s, %s do"):format(firstparamforloop, secondparamforloop, thirdparamforloop))
end)
end
firstparamforloop = nil
secondparamforloop = nil
thirdparamforloop = nil
indent = indent.."\t"
pc = pc + 1
elseif op == opList.FORNLOOP then
if isinsideforloop > 0 then
isinsideforloop = isinsideforloop - 1
end
table.insert(output, string.sub(indent, 2, 2).."end")
indent = string.sub(indent, 2, 2)
pc = pc + 1
elseif op == opList.FORGPREP or op == opList.FORGPREP_NEXT or op == opList.FORGPREP_INEXT then
table.insert(output, "-- FOR LOOP STRUCTURE DETECTED SORRY WE WILL TRY TO ADD THIS NEXT")
-- Move to body
indent = indent .. "\t"
pc = pc + 1
elseif op == opList.FORGLOOP then
table.insert(output, string.sub(indent, 2, 2).."--FOR LOOP STRUCTURE END")
indent = string.sub(indent, 2, 2)
pc = pc + 1
elseif op == opList.GETGLOBAL then
local name = instr.K or "global_" .. globals
globals += 1
table.insert(output, indent .. ("local %s = %s"):format(varName(proto, instr.A, pc), name))
pc = pc + 1
stack[instr.A] = env[name]
elseif op == opList.SETGLOBAL then
local name = instr.K or "global_" .. globals
globals += 1
table.insert(output, indent .. (name .. " = " .. varName(proto, instr.A, pc)))
pc = pc + 1
env[name] = stack[instr.A]
elseif op == opList.GETUPVAL then
table.insert(output, indent .. ("local %s = upval%d"):format(varName(proto, instr.A, pc), instr.B))
pc = pc + 1
elseif op == opList.SETUPVAL then
table.insert(output, indent .. ("local upval%d = %s"):format(instr.B, varName(proto, instr.A, pc)))
pc = pc + 1
elseif op >= opList.ADD and op <= opList.POW then
local ops = {
[opList.ADD] = "+", [opList.SUB] = "-", [opList.MUL] = "*",
[opList.DIV] = "/", [opList.MOD] = "%", [opList.POW] = "^"
}
table.insert(output, indent .. ("%s = %s %s %s"):format(
varName(proto, instr.A, pc),
varName(proto, instr.B, pc),
ops[op],
varName(proto, instr.C, pc)
))
pc = pc + 1
if op == opList.ADD then
stack[instr.A] = stack[instr.B] + stack[instr.C]
elseif op == opList.SUB then
stack[inst.A] = stack[inst.B] - stack[inst.C]
elseif op == opList.MUL then
stack[inst.A] = stack[inst.B] * stack[inst.C]
elseif op == opList.DIV then
stack[inst.A] = stack[inst.B] / stack[inst.C]
elseif op == opList.MOD then
stack[inst.A] = stack[inst.B] % stack[inst.C]
elseif op == opList.POW then
stack[inst.A] = stack[inst.B] ^ stack[inst.C]
end
elseif op >= opList.ADDK and op <= opList.POWK then
local ops = {
[opList.ADDK] = "+", [opList.SUBK] = "-", [opList.MULK] = "*",
[opList.DIVK] = "/", [opList.MODK] = "%", [opList.POWK] = "^"
}
table.insert(output, indent .. ("local %s = %s %s %s"):format(
varName(proto, instr.A, pc),
varName(proto, instr.B, pc),
ops[op],
tostring(instr.K)
))
if op == opList.ADDK then
stack[inst.A] = stack[inst.B] + inst.K
elseif op == opList.SUBK then
stack[inst.A] = stack[inst.B] - inst.K
elseif op == opList.MULK then
stack[inst.A] = stack[inst.B] * inst.K
elseif op == opList.DIVK then
stack[inst.A] = stack[inst.B] / inst.K
elseif op == opList.MODK then
stack[inst.A] = stack[inst.B] % inst.K
elseif op == opList.POWK then
stack[inst.A] = stack[inst.B] ^ inst.K
end
pc = pc + 1
elseif op == opList.AND then
local value = stack[inst.B]
table.insert(output, indent..varName(proto, inst.A, pc).." = "..if value then varName(proto, inst.C, pc) or "false" else tostring(value))
stack[instr.A] = if value then stack[inst.C] or false else value
pc = pc + 1
elseif op == opList.OR then
local value = stack[inst.B]
table.insert(output, indent..varName(proto, inst.A, pc).." = "..if value then tostring(value) else varName(proto, inst.C, pc) or "false")
stack[inst.A] = if value then value else stack[inst.C] or false
pc = pc + 1
elseif op == opList.ANDK then
local value = stack[inst.B]
table.insert(output, indent..varName(proto, inst.A, pc).." = "..if value then tostring(instr.K) or "false" else tostring(value))
stack[inst.A] = if value then inst.K or false else value
pc = pc + 1
elseif op == opList.ORK then
local value = stack[inst.B]
table.insert(output, indent..varName(proto, inst.A, pc).." = "..if value then tostring(value) else tostring(inst.K) or "false")
stack[inst.A] = if value then value else inst.K or false
pc = pc + 1
elseif op == opList.CONCAT then
local s = ""
for i = inst.B, inst.C do
s ..= stack[i] -- idk how tf this line works just let it work tbh
end
table.insert(output, indent..varName(proto, inst.A, pc).." = '"..s.."'")
stack[inst.A] = s
pc = pc + 1
elseif op == opList.NOT then
table.insert(output, indent..varName(proto, inst.A, pc).." = not "..varName(proto, inst.B, pc))
stack[inst.A] = not stack[inst.B]
pc = pc + 1
elseif op == opList.MINUS then
table.insert(output, indent..varName(proto, inst.A, pc).." = -"..varName(proto, inst.B, pc))
stack[inst.A] = -stack[inst.B]
pc = pc + 1
elseif op == opList.LENGTH then
table.insert(output, indent..varName(proto, inst.A, pc).." = #"..varName(proto, inst.B, pc))
stack[inst.A] = #stack[inst.B]
pc = pc + 1
elseif op == opList.CALL then
local args = {}
for i = 0, instr.B - 2 do
table.insert(args, if forloopidk[i] then forloopidk[i] else varName(proto, i+1, pc))
end
if instr.B - 1 == 0 then
args = {}
end
local callStr = if calltype then calltype .. "(" .. table.concat(args, ", ") .. ")" else varName(proto, instr.A, pc) .. "(" .. table.concat(args, ", ") .. ")"
if instr.B and instr.B - 1 > 1 then
local rets = {}
for i = instr.A, instr.A + instr.B - 2 do
table.insert(rets, varName(proto, i, pc))
end
callStr = table.concat(rets, ", ") .. " = " .. callStr
end
table.insert(output, indent .. callStr)
pc = pc + 1
elseif op == opList.NAMECALL then
-- Format: varA = varA:method(varA+1, varA+2, ...)
-- old method
--local methodName = instr.K or "unknownMethod"
--local args = {"var" .. tostring(instr.A)}
-- Arguments start from A+1 if C specifies arg count
--local argCount = instr.B - 2 or 0
--if argCount == -1 then
-- local callStr = string.format("var%d = var%d:%s(%s)", instr.A, instr.A, tostring(methodName), "")
-- table.insert(output, indent .. callStr)
-- pc = pc + 1
-- continue
--end
--for i = 1, argCount do
-- table.insert(args, "var" .. tostring(instr.A + i))
--end
--local callStr = string.format("var%d = var%d:%s(%s)", instr.A, instr.A, tostring(methodName), table.concat(args, ", "))
--table.insert(output, indent .. callStr)
local A = instr.A
local B = instr.B
local kv = instr.K
local sb = varName(proto, B, pc)
table.insert(output, indent..varName(proto, A+1, pc).." = "..sb)
pc = pc + 1
table.insert(output, indent..varName(proto, A, pc).." = "..sb.."["..kv.."]")
-- Jump opcodes
elseif op == opList.JUMP then
pc += instr.D
pc = pc + 1
elseif op == opList.JUMPBACK then
pc += instr.D
pc = pc + 1
elseif op == opList.JUMPIF then
if stack[instr.A] then
pc += instr.D
end
pc = pc + 1
elseif op == opList.JUMPIFNOT then
if not stack[instr.A] then
pc += instr.D
end
pc = pc + 1
elseif op == opList.JUMPIFEQ then
if stack[instr.A] == stack[instr.aux] then
pc += instr.D
else
pc = pc + 1
end
pc = pc + 1
elseif op == opList.JUMPIFLE then
if stack[inst.A] <= stack[inst.aux] then
pc += inst.D
else
pc += 1
end
pc = pc + 1
elseif op == opList.JUMPIFLT then
if stack[inst.A] < stack[inst.aux] then
pc += inst.D
else
pc += 1
end
pc = pc + 1
elseif op == opList.JUMPIFNOTEQ then
if stack[inst.A] == stack[inst.aux] then
pc += 1
else
pc += inst.D
end
pc += 1
elseif op == opList.JUMPIFNOTLE then
if stack[inst.A] <= stack[inst.aux] then
pc += 1
else
pc += inst.D
end
pc += 1
elseif op == opList.JUMPIFNOTLT then
if stack[inst.A] < stack[inst.aux] then
pc += 1
else
pc += inst.D
end
pc += 1
elseif op == opList.JUMPXEQKN then
local kv = inst.K
local kn = inst.KN
local ra = stack[inst.A]
if (ra == kv) ~= kn then
pc += inst.D
else
pc += 1
end
pc += 1
elseif op == opList.JUMPXEQKNIL then
local kn = instr.KN
if (stack[inst.A] == nil) ~= kn then
pc += inst.D
else
pc += 1
end
pc += 1
elseif op == opList.JUMPXEQKB then
local kv = inst.K
local kn = inst.KN
local ra = stack[inst.A]
if (type(ra) == "boolean" and (ra == kv)) ~= kn then
pc += inst.D
else
pc += 1
end
pc += 1
elseif op == opList.JUMPXEQKS then
local kv = inst.K
local kn = inst.KN
local ra = stack[inst.A]
if (ra == kv) ~= kn then
pc += inst.D
else
pc += 1
end
pc += 1
elseif op == opList.RETURN then
if not instr.B or instr.B <= 1 then
-- no return value
else
local rets = {}
for i = 0, instr.B - 2 do
table.insert(rets, varName(proto, instr.A + i, pc))
end
if #rets > 0 then
table.insert(output, indent .. "return " .. table.concat(rets, ", "))
end
end
pc = pc + 1
elseif op == opList.GETIMPORT then
local count = instr.KC
local k0 = instr.K0
local import = env[k0]
if proto.code[pc + 2].opcode == opList.CALL or proto.code[pc + 3].opcode == opList.CALL then
calltype = k0
pc = pc + 1
continue
end
if count == 1 then
table.insert(output, indent..varName(proto, instr.A, pc).." = getfenv()['"..k0.."']")
stack[instr.A] = import
elseif count == 2 then
table.insert(output, indent..varName(proto, instr.A, pc).." = getfenv()['"..k0.."']['"..instr.K1.."']")
stack[instr.A] = import[instr.K1]
elseif count == 3 then
table.insert(output, indent..varName(proto, instr.A, pc).." = getfenv()['"..k0.."']['"..instr.K1.."']['"..instr.K2.."']")
stack[instr.A] = import[instr.K1][instr.K2]
end
pc = pc + 1
elseif op == opList.PREPVARARGS then -- bro what was devs thinking when making this opcode
-- This opcode prepares variable arguments, typically no direct source code line
-- We can skip it or mark it minimally
-- It should never appear repeatedly, so we just skip
pc = pc + 1
elseif op == opList.GETVARARGS then
pc = pc + 1
elseif op == opList.FASTCALL3 then
pc = pc + 1
pc = pc + 1
elseif op == opList.NEWTABLE then
xpcall(function(...)
table.insert(output, indent .. ("local %s = {%s}"):format(varName(proto, instr.A, pc), table.concat(table.create(instr.aux), ", ")))
stack[instr.A] = table.create(inst.aux)
pc = pc + 1
end, function(a0)
table.insert(output, indent.."-- Failed to add newtable operation")
pc = pc + 1
end)
elseif op == opList.DUPTABLE then -- this does the same shit bro only a little diff so this is not right if you want to be more precise you will fix it
xpcall(function(...)
table.insert(output, indent .. ("local %s = {%s}"):format(varName(proto, instr.A, pc), table.concat(table.create(instr.aux), ", ")))
stack[instr.A] = table.create(inst.aux)
pc = pc + 1
end, function(a0)
table.insert(output, indent.."-- Failed to add duptable operation")
pc = pc + 1
end)
pc = pc + 1
elseif op == opList.SETTABLE then
table.insert(output, indent .. ("%s[%s] = %s"):format(
varName(proto, instr.A, pc),
varName(proto, instr.B, pc),
varName(proto, instr.C, pc)
))
pc = pc + 1
stack[instr.B][stack[instr.C]] = stack[instr.A]
elseif op == opList.COVERAGE then
inst.E += 1
pc = pc + 1
elseif op == opList.SUBRK then
stack[inst.A] = inst.K - stack[inst.C]
local name = instr.K
table.insert(output, indent..varName(proto, inst.A, pc).." = "..name.." - "..varName(proto, inst.C, pc))
pc = pc + 1
elseif op == opList.DIVK then
stack[inst.A] = inst.K / stack[inst.C]
local name = instr.K
table.insert(output, indent..varName(proto, inst.A, pc).." = "..name.." - "..varName(proto, inst.C, pc))
pc = pc + 1
elseif op == opList.FASTCALL2 then
pc = pc + 1
pc = pc + 1
elseif op == opList.FASTCALL2K then
pc = pc + 1
pc = pc + 1
elseif op == opList.GETTABLE then
table.insert(output, indent .. ("local %s = %s[%s]"):format(
varName(proto, instr.A, pc),
varName(proto, instr.B, pc),
varName(proto, instr.C, pc)
))
pc = pc + 1
stack[instr.A] = stack[instr.B][stack[instr.C]]
elseif op == opList.GETTABLEKS then
local key = instr.K or ("key" .. tostring(instr.C))
table.insert(output, indent .. ("local %s = %s.%s"):format(
varName(proto, instr.A, pc),
varName(proto, instr.B, pc),
key
))
pc = pc + 1
pcall(function(...) -- error handling i guess
stack[instr.A] = stack[instr.B][instr.K]
end)
elseif op == opList.SETTABLEKS then
local key = instr.K or ("key" .. tostring(instr.C))
table.insert(output, indent .. ("%s.%s = %s"):format(
varName(proto, instr.A, pc),
key,
varName(proto, instr.B, pc)
))
pc = pc + 1
stack[instr.B][instr.K] = stack[instr.A]
elseif op == opList.GETTABLEN then
local key = instr.K or ("key" .. tostring(instr.C))
table.insert(output, indent..("local %s = %s.%s"):format(
varName(proto, instr.A, pc),
varName(proto, instr.B, pc),
key
))
stack[instr.A] = stack[instr.B][instr.C + 1]
pc = pc + 1
elseif op == opList.SETTABLEN then
local key = instr.K or ("key" .. tostring(instr.C))
table.insert(output, indent .. ("%s.%s = %s"):format(
varName(proto, instr.A, pc),
key,
varName(proto, instr.B, pc)
))
pc = pc + 1
stack[instr.B][instr.C + 1] = stack[instr.A]
elseif op == opList.NEWCLOSURE or op == opList.DUPCLOSURE then
if op == opList.DUPCLOSURE then
local subProto = mainproto.protoList[proto.protos[instr.K + 1]]
if subProto then
local args = {}
for i = 0, subProto.numparams - 1 do
table.insert(args, varName(subProto, i, 0))
end
local name = subProto.debugname or varName(proto, instr.A, pc)
table.insert(output, indent .. ("local %s = function(%s)"):format(
name,
table.concat(args, ", ")
))
local body = decompileBlock(subProto, indent .. "\t", 1, #subProto.code)
for _, line in ipairs(body) do
table.insert(output, line)
end
table.insert(output, indent .. "end")
else
table.insert(output, indent .. ("-- closure missing proto at pc=%d"):format(pc))
end
if proto.code[pc + 1] == opList.CAPTURE and proto.code[pc + 2] == opList.CAPTURE then
pc = pc + 4
else
pc = pc + 2
end
else
local subProto = mainproto.protoList[proto.protos[instr.D + 1]]
if subProto then
local args = {}
for i = 0, subProto.numparams - 1 do
table.insert(args, varName(subProto, i, 0))
end
local name = subProto.debugname or varName(proto, instr.A, pc)
table.insert(output, indent .. ("local %s = function(%s)"):format(
name,
table.concat(args, ", ")
))
local body = decompileBlock(subProto, indent .. "\t", 1, #subProto.code)
for _, line in ipairs(body) do
table.insert(output, line)
end
table.insert(output, indent .. "end")
else
table.insert(output, indent .. ("-- closure missing proto at pc=%d"):format(pc))
end
if proto.code[pc + 1] == opList.CAPTURE and proto.code[pc + 2] == opList.CAPTURE then
pc = pc + 4
else
pc = pc + 2
end
end
elseif op == opList.CAPTURE then
--handled by NEWCLOSURE and DUPCLOSURE
pc += 1
elseif op == 65 then
-- this opcods do nothing idk why i even added them
pc += 1
elseif op == opList.IDIV then
stack[instr.A] = stack[inst.B] // stack[inst.C]
table.insert(output, indent..varName(proto, inst.A, pc).." = "..varName(proto, inst.B, pc).." // "..varName(proto, inst.C, pc))
pc += 1
elseif op == opList.IDIVK then
stack[inst.A] = stack[inst.B] // inst.K
local name = inst.K
table.insert(output, indent..varName(proto, inst.A, pc).." = "..varName(proto, inst.B, pc).." // "..name)
elseif op == opList.NOP then
-- does no operation/nothing
pc += 1
else
print("PC: "..pc.."\nOPCODE:"..op.."\n\n")
pc += 1
end
end
env.script = old_env_script --important part to not edit the scripts enviroment
return output
end
-- two decompile functions idk why i added this tbh
local function decompile(proto, mainproto)
local success, lines = pcall(decompileBlock, proto, nil, nil, nil, mainproto)
if not success then
return nil
end
return "--DECOMPILED USING CELESTIAL V2.2 AS A FAST LUAU DECOMPILER\n--DECOMPILER MADE BY GLITCHED VOID\n\n"..table.concat(lines, "\n")
end
local function parseprotolist(protolist)
local out = {}
for i,v in protolist do
if i == #protolist then
break
end
out[i] = decompile(v)
end
out[#protolist] = decompile(protolist[#protolist])
return "--DECOMPILED USING CELESTIAL V2.2 AS A FAST LUAU DECOMPILER\n--DECOMPILER MADE BY GLITCHED VOID\n\n"..table.concat(out)
end
local function decompileLuau(proto)
local ast
if proto.mainProto and proto.protoList then
ast = decompile(proto.mainProto, proto)
--ast = parseBlock(proto.mainProto, proto.mainProto.code, 1, #proto.mainProto.code, proto.protoList)
else
return cerror("MAIN","Invalid bytecode structure. Expected mainProto.")
end
if ast == nil then
return cerror("MAIN", "Unknown error decompiling sorry please report this")
end
if not ast:find("--DECOMPILED") then
return cerror("MAIN", "Possible error:\n"..ast)
end
local lines = ast
return lines
end
tostring = old_tostring
-- // Environment changes in the VM are not supposed to alter the behaviour of the VM so we localise globals beforehand
local type = type
local pcall = pcall
local error = error
local tonumber = tonumber
local assert = assert
local setmetatable = setmetatable
local string_format = string.format
local table_move = table.move
local table_pack = table.pack
local table_unpack = table.unpack
local table_create = table.create
local table_insert = table.insert
local table_remove = table.remove
local coroutine_create = coroutine.create
local coroutine_yield = coroutine.yield
local coroutine_resume = coroutine.resume
local coroutine_close = coroutine.close
local buffer_fromstring = buffer.fromstring
local buffer_len = buffer.len
local buffer_readu8 = buffer.readu8
local buffer_readu32 = buffer.readu32
local buffer_readstring = buffer.readstring
local buffer_readf32 = buffer.readf32
local buffer_readf64 = buffer.readf64
local bit32_bor = bit32.bor
local bit32_band = bit32.band
local bit32_btest = bit32.btest
local bit32_rshift = bit32.rshift
local bit32_lshift = bit32.lshift
local bit32_extract = bit32.extract
-- // opList contains information about the instruction, each instruction is defined in this format:
-- // {OP_NAME, OP_MODE, K_MODE, HAS_AUX}
-- // OP_MODE specifies what type of registers the instruction uses if any
-- 0 = NONE
-- 1 = A
-- 2 = AB
-- 3 = ABC
-- 4 = AD
-- 5 = AE
-- // K_MODE specifies if the instruction has a register that holds a constant table index, which will be directly converted to the constant in the 2nd pass
-- 0 = NONE
-- 1 = AUX
-- 2 = C
-- 3 = D
-- 4 = AUX import
-- 5 = AUX boolean low 1 bit
-- 6 = AUX number low 24 bits
-- // HAS_AUX boolean specifies whether the instruction is followed up with an AUX word, which may be used to execute the instruction.
local opList = {
{ "NOP", 0, 0, false },
{ "BREAK", 0, 0, false },
{ "LOADNIL", 1, 0, false },
{ "LOADB", 3, 0, false },
{ "LOADN", 4, 0, false },
{ "LOADK", 4, 3, false },
{ "MOVE", 2, 0, false },
{ "GETGLOBAL", 1, 1, true },
{ "SETGLOBAL", 1, 1, true },
{ "GETUPVAL", 2, 0, false },
{ "SETUPVAL", 2, 0, false },
{ "CLOSEUPVALS", 1, 0, false },
{ "GETIMPORT", 4, 4, true },
{ "GETTABLE", 3, 0, false },
{ "SETTABLE", 3, 0, false },
{ "GETTABLEKS", 3, 1, true },
{ "SETTABLEKS", 3, 1, true },
{ "GETTABLEN", 3, 0, false },
{ "SETTABLEN", 3, 0, false },
{ "NEWCLOSURE", 4, 0, false },
{ "NAMECALL", 3, 1, true },
{ "CALL", 3, 0, false },
{ "RETURN", 2, 0, false },
{ "JUMP", 4, 0, false },
{ "JUMPBACK", 4, 0, false },
{ "JUMPIF", 4, 0, false },
{ "JUMPIFNOT", 4, 0, false },
{ "JUMPIFEQ", 4, 0, true },
{ "JUMPIFLE", 4, 0, true },
{ "JUMPIFLT", 4, 0, true },
{ "JUMPIFNOTEQ", 4, 0, true },
{ "JUMPIFNOTLE", 4, 0, true },
{ "JUMPIFNOTLT", 4, 0, true },
{ "ADD", 3, 0, false },
{ "SUB", 3, 0, false },
{ "MUL", 3, 0, false },
{ "DIV", 3, 0, false },
{ "MOD", 3, 0, false },
{ "POW", 3, 0, false },
{ "ADDK", 3, 2, false },
{ "SUBK", 3, 2, false },
{ "MULK", 3, 2, false },
{ "DIVK", 3, 2, false },
{ "MODK", 3, 2, false },
{ "POWK", 3, 2, false },
{ "AND", 3, 0, false },