forked from luau/UniversalSynSaveInstance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaveinstance.luau
2959 lines (2491 loc) · 101 KB
/
saveinstance.luau
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
--!native
--!optimize 2
--!divine-intellect
-- https://discord.gg/wx4ThpAsmw
local service = setmetatable({}, {
__index = function(self, index)
local Service = game:GetService(index) or settings():GetService(index) or UserSettings():GetService(index)
self[index] = Service
return Service
end,
})
local function string_find(s, pattern)
return string.find(s, pattern, nil, true)
end
local function ArrayToDictionary(t, hydridMode, valueOverride)
local tmp = {}
if hydridMode then
for some1, some2 in t do
if type(some1) == "number" then
tmp[some2] = valueOverride or true
elseif type(some2) == "table" then
tmp[some1] = ArrayToDictionary(some2, hydridMode) -- Some1 is Class, Some2 is Name
else
tmp[some1] = some2
end
end
else
for _, key in t do
if type(key) == "string" then
tmp[key] = true
end
end
end
return tmp
end
local ESCAPES_PATTERN = "[&<>\"'\0\1-\9\11-\12\14-\31\127-\255]" -- * The safe way is to escape all five characters in text. However, the three characters " ' and > needn't be escaped in text
-- %z (\0 aka NULL) might not be needed as Roblox automatically converts it to space everywhere it seems like
-- Characters from: https://create.roblox.com/docs/en-us/ui/rich-text#escape-forms
-- * EscapesPattern should be ordered from most common to least common characters for sake of speed
-- * Might wanna use their numerical codes instead of named codes for reduced file size (Could be an Option)
-- TODO Maybe we should invert the pattern to only allow certain characters (future-proof)
local ESCAPES = {
["&"] = "&", -- 38
["<"] = "<", -- 60
[">"] = ">", -- 62
['"'] = """, -- quot
["'"] = "'", -- apos
["\0"] = "",
}
for rangeStart, rangeEnd in string.gmatch(ESCAPES_PATTERN, "(.)%-(.)") do
for charCode = string.byte(rangeStart), string.byte(rangeEnd) do
ESCAPES[string.char(charCode)] = "&#" .. charCode .. ";"
end
end
local global_container
do
local filename = "UniversalMethodFinder"
local finder
finder, global_container = loadstring(
game:HttpGet("https://raw.githubusercontent.com/luau/SomeHub/main/" .. filename .. ".luau", true),
filename
)()
finder({
-- readbinarystring = 'string.find(...,"bin",nil,true)', -- ! Could match some unwanted stuff (getbinaryindex)
-- request = 'string.find(...,"request",nil,true) and not string.find(...,"internal",nil,true)',
base64encode = 'local a={...}local b=a[1]local function c(a,b)return string.find(a,b,nil,true)end;return c(b,"encode")and(c(b,"base64")or c(string.lower(tostring(a[2])),"base64"))',
decompile = '(string.find(...,"decomp",nil,true) and string.sub(...,#...) ~= "s") or string.find(...,"assembl",nil,true)',
gethiddenproperty = 'string.find(...,"get",nil,true) and string.find(...,"h",nil,true) and string.find(...,"prop",nil,true) and string.sub(...,#...) ~= "s"',
gethui = 'string.find(...,"get",nil,true) and string.find(...,"h",nil,true) and string.find(...,"ui",nil,true)',
getnilinstances = 'string.find(...,"nil",nil,true) and string.find(...,"get",nil,true) and string.sub(...,#...) == "s"', -- ! Could match some unwanted stuff
getscriptbytecode = 'string.find(...,"get",nil,true) and string.find(...,"bytecode",nil,true)', -- or string.find(...,"dump",nil,true) and string.find(...,"string",nil,true) due to Fluxus (dumpstring returns a function)
hash = 'local a={...}local b=a[1]local function c(a,b)return string.find(a,b,nil,true)end;return c(b,"hash")and c(string.lower(tostring(a[2])),"crypt")',
protectgui = 'string.find(...,"protect",nil,true) and string.find(...,"ui",nil,true) and not string.find(...,"un",nil,true)',
setthreadidentity = 'string.find(...,"identity",nil,true) and string.find(...,"set",nil,true)',
}, true, 10)
end
local identify_executor = identifyexecutor or getexecutorname or whatexecutor
local EXECUTOR_NAME = identify_executor and identify_executor() or ""
local gethiddenproperty = global_container.gethiddenproperty
-- These should be universal enough
local appendfile = appendfile
local readfile = readfile
local writefile = writefile
local getscriptbytecode = global_container.getscriptbytecode -- * A lot of assumptions are made based on whether this function is defined or not. So in certain edge cases, like if the executor defines "decompile" or "getscripthash" function yet doesn't define this function there might be loss of functionality of the saveinstance. Although that would be very rare and weird
local base64encode = global_container.base64encode
local sha384
local gethiddenproperty_fallback
do -- * Load Region of Déjà Vu
local UGCValidationService = service.UGCValidationService
gethiddenproperty_fallback = function(instance, propertyName)
return UGCValidationService:GetPropertyValue(instance, propertyName) -- TODO Sadly there's no way to tell whether value is actually nil or the function just couldn't read it
end
local o, r = pcall(gethiddenproperty, workspace.Terrain, "Decoration")
if not o or type(r) ~= "boolean" then -- * Tests if gethiddenproperty is fake
gethiddenproperty = nil
end
local function benchmark(f1, f2, ...)
local ranking = table.create(2)
for i, f in { f1, f2 } do
local start = os.clock()
for _ = 1, 50 do
f(...)
end
ranking[i] = { t = os.clock() - start, f = f }
end
table.sort(ranking, function(a, b)
return a.t < b.t
end)
return ranking[1].f
end
local test_str = string.rep("\1\0\0\0\1\2\3\4\5\6\7", 50)
do
if not bit32.byteswap or not pcall(bit32.byteswap, 1) then -- Because Fluxus is missing byteswap
bit32 = table.clone(bit32)
local function tobit(num)
num %= (bit32.bxor(num, 32))
if 0x80000000 < num then
num -= bit32.bxor(num, 32)
end
return num
end
bit32.byteswap = function(num)
local BYTE_SIZE = 8
local MAX_BYTE_VALUE = 255
num %= bit32.bxor(2, 32)
local a = bit32.band(num, MAX_BYTE_VALUE)
num = bit32.rshift(num, BYTE_SIZE)
local b = bit32.band(num, MAX_BYTE_VALUE)
num = bit32.rshift(num, BYTE_SIZE)
local c = bit32.band(num, MAX_BYTE_VALUE)
num = bit32.rshift(num, BYTE_SIZE)
local d = bit32.band(num, MAX_BYTE_VALUE)
num = tobit(bit32.lshift(bit32.lshift(bit32.lshift(a, BYTE_SIZE) + b, BYTE_SIZE) + c, BYTE_SIZE) + d)
return num
end
table.freeze(bit32)
end
-- Credits @Reselim
local reselim_base64encode
pcall(function()
local b64_enc_buf = loadstring(
game:HttpGet("https://raw.githubusercontent.com/Reselim/Base64/master/Base64.lua", true),
"Base64"
)().encode
reselim_base64encode = function(raw)
return raw == "" and raw or buffer.tostring(b64_enc_buf(buffer.fromstring(raw)))
end
end)
-- * Tests if base64encode exists and works properly then benchmark it
if base64encode and base64encode("\1\0\0\0\1") == "AQAAAAE=" then
if reselim_base64encode then
base64encode = benchmark(base64encode, reselim_base64encode, test_str)
end
else
base64encode = reselim_base64encode
end
assert(base64encode, "base64encode not found")
end
do
local hash = global_container.hash
if hash then
sha384 = function(data)
return hash(data, "sha384")
end
end
local filename = "RequireOnlineModule"
-- Credits @boatbomber
local hashlib_sha384
pcall(function()
hashlib_sha384 = loadstring(
game:HttpGet("https://raw.githubusercontent.com/luau/SomeHub/main/" .. filename .. ".luau", true),
filename
)()(4544052033).sha384
end)
-- * Tests if sha384 exists then benchmark it
if hashlib_sha384 then
if sha384 then
sha384 = benchmark(sha384, hashlib_sha384, test_str)
else
sha384 = hashlib_sha384
end
end
assert(sha384, "sha384 not found")
end
end
local custom_decompiler, load_decompiler -- TODO Temporary
if getscriptbytecode then
-- Credits @w-a-e
local decompiler_source
if
pcall(function()
decompiler_source =
game:HttpGet("https://raw.githubusercontent.com/w-a-e/Advanced-Decompiler-V3/main/init.lua", true)
end)
then
load_decompiler = function(decompTimeout)
local CONSTANTS = [[
local ENABLED_REMARKS = {
NATIVE_REMARK = true,
INLINE_REMARK = true
}
local DECOMPILER_TIMEOUT = ]] .. decompTimeout .. [[
local READER_FLOAT_PRECISION = 7 -- up to 99
local SHOW_INSTRUCTION_LINES = false
local SHOW_REFERENCES = true
local SHOW_OPERATION_NAMES = false
local SHOW_MISC_OPERATIONS = false
local LIST_USED_GLOBALS = true
local RETURN_ELAPSED_TIME = false
]]
local _ENV = (getgenv or getrenv or getfenv)()
local old = _ENV.decompile
local f = loadstring(
string.gsub(
string.gsub(
decompiler_source,
"return %(x %% 2^32%) // %(2^disp%)",
"return math.floor((x %% 2^32) / (2^disp))",
1
), -- TODO Temporary fix for macsploit (// operator)
";;CONSTANTS HERE;;",
CONSTANTS
),
"Advanced-Decompiler-V3"
)
if not f then
return
end
f()
custom_decompiler = _ENV.decompile
_ENV.decompile = old
end
end
end
local SharedStrings = {}
local SharedString_identifiers = setmetatable({
identifier = 1e15, -- 1 quadrillion, up to 9.(9) quadrillion, in theory this shouldn't ever run out and be enough for all sharedstrings ever imaginable
-- TODO: worst case, add fallback to str randomizer once numbers run out : )
}, {
__index = function(self, str)
local Identifier = base64encode(tostring(self.identifier)) -- tostring is only needed for built-in base64encode, Reselim's doesn't need it as buffers autoconvert
self.identifier += 1
self[str] = Identifier -- ? The value of the md5 attribute is a Base64-encoded key. <SharedString> type elements use this key to refer to the value of the string. The value is the text content, which is Base64-encoded. Historically, the key was the MD5 hash of the string value. However, this is not required; the key can be any value that will uniquely identify the shared string. Roblox currently uses BLAKE2b truncated to 16 bytes..
return Identifier
end,
})
local Type_IDs = {
string = 0x02,
boolean = 0x03,
-- float = 0x05,
number = 0x06,
UDim = 0x09,
UDim2 = 0x0A,
BrickColor = 0x0E,
Color3 = 0x0F,
Vector2 = 0x10,
Vector3 = 0x11,
CFrame = 0x14,
EnumItem = 0x15,
NumberSequence = 0x17,
ColorSequence = 0x19,
NumberRange = 0x1B,
Rect = 0x1C,
Font = 0x21,
}
local CFrame_Rotation_IDs = {
["\0\0\128\63\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\63\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\63"] = 0x02,
["\0\0\128\63\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\191\0\0\0\0\0\0\128\63\0\0\0\0"] = 0x03,
["\0\0\128\63\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\191\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\191"] = 0x05,
["\0\0\128\63\0\0\0\0\0\0\0\128\0\0\0\0\0\0\0\0\0\0\128\63\0\0\0\0\0\0\128\191\0\0\0\0"] = 0x06,
["\0\0\0\0\0\0\128\63\0\0\0\0\0\0\128\63\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\191"] = 0x07,
["\0\0\0\0\0\0\0\0\0\0\128\63\0\0\128\63\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\63\0\0\0\0"] = 0x09,
["\0\0\0\0\0\0\128\191\0\0\0\0\0\0\128\63\0\0\0\0\0\0\0\128\0\0\0\0\0\0\0\0\0\0\128\63"] = 0x0a,
["\0\0\0\0\0\0\0\0\0\0\128\191\0\0\128\63\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\191\0\0\0\0"] = 0x0c,
["\0\0\0\0\0\0\128\63\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\63\0\0\128\63\0\0\0\0\0\0\0\0"] = 0x0d,
["\0\0\0\0\0\0\0\0\0\0\128\191\0\0\0\0\0\0\128\63\0\0\0\0\0\0\128\63\0\0\0\0\0\0\0\0"] = 0x0e,
["\0\0\0\0\0\0\128\191\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\191\0\0\128\63\0\0\0\0\0\0\0\0"] = 0x10,
["\0\0\0\0\0\0\0\0\0\0\128\63\0\0\0\0\0\0\128\191\0\0\0\0\0\0\128\63\0\0\0\0\0\0\0\128"] = 0x11,
["\0\0\128\191\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\63\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\191"] = 0x14,
["\0\0\128\191\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\63\0\0\0\0\0\0\128\63\0\0\0\128"] = 0x15,
["\0\0\128\191\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\191\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\63"] = 0x17,
["\0\0\128\191\0\0\0\0\0\0\0\128\0\0\0\0\0\0\0\0\0\0\128\191\0\0\0\0\0\0\128\191\0\0\0\128"] = 0x18,
["\0\0\0\0\0\0\128\63\0\0\0\128\0\0\128\191\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\63"] = 0x19,
["\0\0\0\0\0\0\0\0\0\0\128\191\0\0\128\191\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\63\0\0\0\0"] = 0x1b,
["\0\0\0\0\0\0\128\191\0\0\0\128\0\0\128\191\0\0\0\0\0\0\0\128\0\0\0\0\0\0\0\0\0\0\128\191"] = 0x1c,
["\0\0\0\0\0\0\0\0\0\0\128\63\0\0\128\191\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\191\0\0\0\0"] = 0x1e,
["\0\0\0\0\0\0\128\63\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\191\0\0\128\191\0\0\0\0\0\0\0\0"] = 0x1f,
["\0\0\0\0\0\0\0\0\0\0\128\63\0\0\0\0\0\0\128\63\0\0\0\128\0\0\128\191\0\0\0\0\0\0\0\0"] = 0x20,
["\0\0\0\0\0\0\128\191\0\0\0\0\0\0\0\0\0\0\0\0\0\0\128\63\0\0\128\191\0\0\0\0\0\0\0\0"] = 0x22,
["\0\0\0\0\0\0\0\0\0\0\128\191\0\0\0\0\0\0\128\191\0\0\0\128\0\0\128\191\0\0\0\0\0\0\0\128"] = 0x23,
}
local Binary_Descriptors
Binary_Descriptors = {
__SEQUENCE = function(raw, valueFormatter, keypointSize, Envelope)
local Keypoints = raw.Keypoints
local Keypoints_n = #Keypoints
local len = 4 + (keypointSize or 12) * Keypoints_n
local b = buffer.create(len)
local offset = 0
buffer.writeu32(b, offset, Keypoints_n)
offset += 4
for _, keypoint in Keypoints do
buffer.writef32(b, offset, Envelope or keypoint.Envelope)
offset += 4
buffer.writef32(b, offset, keypoint.Time)
offset += 4
local Value = keypoint.Value
if valueFormatter then
offset += valueFormatter(Value, b, offset)
else
buffer.writef32(b, offset, Value)
offset += 4
end
end
return b, len
end,
--------------------------------------------------------------
--------------------------------------------------------------
--------------------------------------------------------------
["string"] = function(raw)
local raw_len = #raw
local len = 4 + raw_len
local b = buffer.create(len)
buffer.writeu32(b, 0, raw_len)
buffer.writestring(b, 4, raw)
return b, len
end,
["boolean"] = function(raw)
local b = buffer.create(1)
buffer.writeu8(b, 0, raw and 1 or 0)
return b, 1
end,
["number"] = function(raw) -- double
local b = buffer.create(8)
buffer.writef64(b, 0, raw)
return b, 8
end,
["UDim"] = function(raw)
local b = buffer.create(8)
buffer.writef32(b, 0, raw.Scale)
buffer.writei32(b, 4, raw.Offset)
return b, 8
end,
["UDim2"] = function(raw)
local b = buffer.create(16)
local Descriptors_UDim = Binary_Descriptors.UDim
local X = Descriptors_UDim(raw.X)
buffer.copy(b, 0, X)
local Y = Descriptors_UDim(raw.Y)
buffer.copy(b, 8, Y)
return b, 16
end,
["BrickColor"] = function(raw)
local b = buffer.create(4)
buffer.writeu32(b, 0, raw.Number)
return b, 4
end,
["Color3"] = function(raw)
local b = buffer.create(12)
buffer.writef32(b, 0, raw.R)
buffer.writef32(b, 4, raw.G)
buffer.writef32(b, 8, raw.B)
return b, 12
end,
["Vector2"] = function(raw)
local b = buffer.create(8)
buffer.writef32(b, 0, raw.X)
buffer.writef32(b, 4, raw.Y)
return b, 8
end,
["Vector3"] = function(raw)
local b = buffer.create(12)
buffer.writef32(b, 0, raw.X)
buffer.writef32(b, 4, raw.Y)
buffer.writef32(b, 8, raw.Z)
return b, 12
end,
["CFrame"] = function(raw)
local X, Y, Z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = raw:GetComponents()
local rotation_ID = CFrame_Rotation_IDs[string.pack("<fffffffff", R00, R01, R02, R10, R11, R12, R20, R21, R22)]
local len = rotation_ID and 13 or 49
local b = buffer.create(len)
-- ? TODO
-- local write_vector3 = Descriptors.Vector3
-- local pos = write_vector3(raw.Position)
-- buffer.copy(b, 0, pos)
buffer.writef32(b, 0, X)
buffer.writef32(b, 4, Y)
buffer.writef32(b, 8, Z)
if rotation_ID then
buffer.writeu8(b, 12, rotation_ID)
else
buffer.writeu8(b, 12, 0x0)
-- ? TODO
-- buffer.copy(b, 13, write_vector3(raw.XVector)) -- R00, R10, R20
-- buffer.copy(b, 13 + 12, write_vector3(raw.YVector)) -- R01, R11, R21
-- buffer.copy(b, 13 + 24, write_vector3(raw.ZVector)) -- R02, R12, R22
buffer.writef32(b, 13, R00)
buffer.writef32(b, 17, R01)
buffer.writef32(b, 21, R02)
buffer.writef32(b, 25, R10)
buffer.writef32(b, 29, R11)
buffer.writef32(b, 33, R12)
buffer.writef32(b, 37, R20)
buffer.writef32(b, 41, R21)
buffer.writef32(b, 45, R22)
end
return b, len
end,
["EnumItem"] = function(raw)
local b_Name, Name_size = Binary_Descriptors.string(tostring(raw.EnumType))
local len = Name_size + 4
local b = buffer.create(len)
buffer.copy(b, 0, b_Name)
buffer.writeu32(b, Name_size, raw.Value)
return b, len
end,
["NumberSequence"] = nil,
["ColorSequence"] = function(raw)
return Binary_Descriptors.__SEQUENCE(raw, function(color3, b, offset)
buffer.copy(b, offset, Binary_Descriptors.Color3(color3))
return 12
end, 20, 0)
end,
["NumberRange"] = function(raw)
local b = buffer.create(8)
buffer.writef32(b, 0, raw.Min)
buffer.writef32(b, 4, raw.Max)
return b, 8
end,
["Rect"] = function(raw)
local b = buffer.create(16)
local Descriptors_Vector2 = Binary_Descriptors.Vector2
local Min = Descriptors_Vector2(raw.Min)
buffer.copy(b, 0, Min)
local Max = Descriptors_Vector2(raw.Max)
buffer.copy(b, 8, Max)
return b, 16
end,
["Font"] = function(raw)
local Descriptors_string = Binary_Descriptors.string
local b_Family, Family_size = Descriptors_string(raw.Family)
local b_CachedFaceId, CachedFaceId_size = Descriptors_string("")
local len = 3 + Family_size + CachedFaceId_size
local b = buffer.create(len)
buffer.writeu16(b, 0, raw.Weight.Value)
buffer.writeu8(b, 2, raw.Style.Value)
buffer.copy(b, 3, b_Family)
buffer.copy(b, 3 + Family_size, b_CachedFaceId)
return b, len
end,
}
do
Binary_Descriptors.NumberSequence = Binary_Descriptors.__SEQUENCE
end
local XML_Descriptors
XML_Descriptors = {
__APIPRECISION = function(raw, precision)
if raw == 0 or raw % 1 == 0 then
return raw
end
local Extreme = XML_Descriptors.__EXTREME(raw)
if Extreme then
return Extreme
end
-- TODO: scientific notation formatting also takes place if value is a decimal (only counts if it starts with 0.) then values like 0.00008 will be formatted as 8.0000000000000006544e-05 ("%.19e"), it must have 5 or more consecutive (?) zeros for this, on other hand, if it doesn't start with 0. then e20+ format is applied in case it has 20 or more consecutive (?) zeros so 1e20 will be formatted as 1e+20 and upwards (1e+19 is not allowed, same as 1e-04 for decimals)
-- ? The good part is compression of value so less file size BUT at the potential cost of precision loss
return string.format("%." .. precision .. "f", raw)
end,
__BIT = function(...) -- * Credits to Friend (you know yourself)
local Value = 0
for i, bit in { ... } do
if bit then
Value += 2 ^ (i - 1)
end
end
return Value
end,
__CDATA = function(raw) -- ? Normally Roblox doesn't use CDATA unless the string has newline characters (\n); We rather CDATA everything for sake of speed
return "<![CDATA[" .. raw .. "]]>"
end,
__ENUM = function(raw)
return raw.Value, "token"
end,
__EXTREME = function(raw)
local Extreme
if raw ~= raw then
Extreme = "NAN"
elseif raw == math.huge then
Extreme = "INF"
elseif raw == -math.huge then
Extreme = "-INF"
end
return Extreme
end,
__EXTREME_RANGE = function(raw)
return raw ~= raw and "0" or raw -- Normally we should return "-nan(ind)" instead of "0" but this adds more compatibility
end,
__MINMAX = function(min, max, descriptor)
return "<min>" .. descriptor(min) .. "</min><max>" .. descriptor(max) .. "</max>"
end,
__PROTECTEDSTRING = function(raw) -- ? its purpose is to "protect" data from being treated as ordinary character data during processing;
return string_find(raw, "]]>") and string.gsub(raw, ESCAPES_PATTERN, ESCAPES) or XML_Descriptors.__CDATA(raw)
end,
__SEQUENCE = function(raw, valueFormatter) --The value is the text content, formatted as a space-separated list of floating point numbers.
-- tostring(raw) also works (but way slower rn)
local __EXTREME_RANGE = XML_Descriptors.__EXTREME_RANGE
local Converted = ""
for _, keypoint in raw.Keypoints do
local Value = keypoint.Value
Converted ..= keypoint.Time .. " " .. (valueFormatter and valueFormatter(Value) or __EXTREME_RANGE(Value) .. " " .. __EXTREME_RANGE(
keypoint.Envelope
) .. " ") -- ? Trailing whitespace is only needed for lune compatibility
end
return Converted
end,
__VECTOR = function(X, Y, Z) -- Each element is a <float>
local Value = "<X>" .. X .. "</X><Y>" .. Y .. "</Y>" -- There is no Vector without at least two Coordinates.. (Vector1, at least on Roblox)
if Z then
Value ..= "<Z>" .. Z .. "</Z>"
end
return Value
end,
--------------------------------------------------------------
--------------------------------------------------------------
--------------------------------------------------------------
Axes = function(raw)
--The text of this element is formatted as an integer between 0 and 7
return "<axes>" .. XML_Descriptors.__BIT(raw.X, raw.Y, raw.Z) .. "</axes>"
end,
-- ? Roblox uses CDATA only for these (try to prove this wrong): CollisionGroupData, SmoothGrid, MaterialColors, PhysicsGrid
-- ! Assuming all base64 encoded strings won't have newlines
-- ! 7/7/24
-- ! Fix for Electron v3
-- ! Electron v3 'gethiddenproperty' automatically base64 encodes BinaryString values
BinaryString = EXECUTOR_NAME == "Electron" and function(raw)
return raw
end or base64encode, -- TODO Issues may arise if NotScriptableFix or gethiddenproperty_fallback are able to read BinaryString where gethiddenproperty can't on Electron
BrickColor = function(raw)
return raw.Number -- * Roblox encodes the tags as "int", but this is not required for Roblox to properly decode the type. For better compatibility, it is preferred that third-party implementations encode and decode "BrickColor" tags instead. Could also use "int" or "Color3uint8"
end,
CFrame = function(raw)
local X, Y, Z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = raw:GetComponents()
return XML_Descriptors.__VECTOR(X, Y, Z)
.. "<R00>"
.. R00
.. "</R00><R01>"
.. R01
.. "</R01><R02>"
.. R02
.. "</R02><R10>"
.. R10
.. "</R10><R11>"
.. R11
.. "</R11><R12>"
.. R12
.. "</R12><R20>"
.. R20
.. "</R20><R21>"
.. R21
.. "</R21><R22>"
.. R22
.. "</R22>",
"CoordinateFrame"
end,
Color3 = function(raw) -- Each element is a <float>
return "<R>" .. raw.R .. "</R><G>" .. raw.G .. "</G><B>" .. raw.B .. "</B>" -- ? It is recommended that Color3 is encoded with elements instead of text.
end,
Color3uint8 = function(raw)
-- https://github.com/rojo-rbx/rbx-dom/blob/master/docs/xml.md#color3uint8
return 0xFF000000
+ (math.floor(raw.R * 255) * 0x10000)
+ (math.floor(raw.G * 255) * 0x100)
+ math.floor(raw.B * 255) -- ? It is recommended that Color3uint8 is encoded with text instead of elements.
-- return bit32.bor(
-- bit32.bor(bit32.bor(bit32.lshift(0xFF, 24), bit32.lshift(0xFF * raw.R, 16)), bit32.lshift(0xFF * raw.G, 8)),
-- 0xFF * raw.B
-- )
-- return tonumber(string.format("0xFF%02X%02X%02X",raw.R*255,raw.G*255,raw.B*255))
end,
ColorSequence = function(raw)
--The value is the text content, formatted as a space-separated list of FLOATing point numbers.
return XML_Descriptors.__SEQUENCE(raw, function(color3)
local __EXTREME_RANGE = XML_Descriptors.__EXTREME_RANGE
return __EXTREME_RANGE(color3.R)
.. " "
.. __EXTREME_RANGE(color3.G)
.. " "
.. __EXTREME_RANGE(color3.B)
.. " 0 "
end)
end,
Content = function(raw)
return raw == "" and "<null></null>" or "<url>" .. XML_Descriptors.string(raw, true) .. "</url>"
end,
CoordinateFrame = function(raw)
return "<CFrame>" .. XML_Descriptors.CFrame(raw) .. "</CFrame>"
end,
-- DateTime = function(raw) return raw.UnixTimestampMillis end, -- TODO
Faces = function(raw)
-- The text of this element is formatted as an integer between 0 and 63
return "<faces>"
.. XML_Descriptors.__BIT(raw.Right, raw.Top, raw.Back, raw.Left, raw.Bottom, raw.Front)
.. "</faces>"
end,
Font = function(raw)
local FontString = tostring(raw) -- TODO: Temporary fix
local EmptyWeight = string_find(FontString, "Weight = ,")
local EmptyStyle = string_find(FontString, "Style = }")
return "<Family>"
.. XML_Descriptors.Content(raw.Family)
.. "</Family><Weight>"
.. (EmptyWeight and "" or XML_Descriptors.__ENUM(raw.Weight))
.. "</Weight><Style>"
.. (EmptyStyle and "" or raw.Style.Name) -- Weird but this field accepts .Name of enum instead..
.. "</Style>" --TODO (OPTIONAL ELEMENT): Figure out how to determine (Content) <CachedFaceId><url>rbxasset://fonts/GothamSSm-Medium.otf</url></CachedFaceId>
end,
NumberRange = function(raw) -- tostring(raw) also works
--The value is the text content, formatted as a space-separated list of floating point numbers.
local __EXTREME_RANGE = XML_Descriptors.__EXTREME_RANGE
return __EXTREME_RANGE(raw.Min) .. " " .. __EXTREME_RANGE(raw.Max) --[[.. " "]] -- ! This might be required to bypass detections as thats how its formatted usually; __EXTREME_RANGE is not needed here but it fixes the issue where "nan 10" value would reset to "0 0"
end,
NumberSequence = nil,
-- NumberSequence = Descriptors.__SEQUENCE,
PhysicalProperties = function(raw)
--[[Contains at least one CustomPhysics element, which is interpreted according to the bool type. If this value is true, then the tag also contains an element for each component of the PhysicalProperties:
Density
Friction
Elasticity
FrictionWeight
ElasticityWeight
The value of each component is represented by the text content formatted as a 32-bit floating point number (see float).]]
local CustomPhysics
if raw then
CustomPhysics = true
else
CustomPhysics = false
end
CustomPhysics = "<CustomPhysics>" .. XML_Descriptors.bool(CustomPhysics) .. "</CustomPhysics>"
return raw
and CustomPhysics .. "<Density>" .. raw.Density .. "</Density><Friction>" .. raw.Friction .. "</Friction><Elasticity>" .. raw.Elasticity .. "</Elasticity><FrictionWeight>" .. raw.FrictionWeight .. "</FrictionWeight><ElasticityWeight>" .. raw.ElasticityWeight .. "</ElasticityWeight>"
or CustomPhysics
end,
-- ProtectedString = function(raw) return tostring(raw), "ProtectedString" end,
Ray = function(raw)
local vector3 = XML_Descriptors.Vector3
return "<origin>" .. vector3(raw.Origin) .. "</origin><direction>" .. vector3(raw.Direction) .. "</direction>"
end,
Rect = function(raw)
return XML_Descriptors.__MINMAX(raw.Min, raw.Max, XML_Descriptors.Vector2), "Rect2D"
end,
Region3 = function(raw) --? Not sure yet (https://github.com/ui0ppk/roblox-master/blob/main/Network/Replicator.cpp#L1306)
local Translation = raw.CFrame.Position
local HalfSize = raw.Size * 0.5
return XML_Descriptors.__MINMAX(
Translation - HalfSize, -- https://github.com/ui0ppk/roblox-master/blob/main/App/util/Region3.cpp#L38
Translation + HalfSize, -- https://github.com/ui0ppk/roblox-master/blob/main/App/util/Region3.cpp#L42
XML_Descriptors.Vector3
)
end,
Region3int16 = function(raw) --? Not sure yet (https://github.com/ui0ppk/roblox-master/blob/main/App/v8tree/EnumProperty.cpp#L346)
return XML_Descriptors.__MINMAX(raw.Min, raw.Max, XML_Descriptors.Vector3int16)
end,
SharedString = function(raw)
raw = base64encode(raw)
local Identifier = SharedString_identifiers[raw]
if SharedStrings[Identifier] == nil then
SharedStrings[Identifier] = raw
end
return Identifier
end,
-- SecurityCapabilities = function(raw) return raw end,
-- SystemAddress = function(raw) return raw end,
UDim = function(raw)
--[[
S: Represents the Scale component. Interpreted as a <float>.
O: Represents the Offset component. Interpreted as an <int>.
]]
return "<S>" .. raw.Scale .. "</S><O>" .. raw.Offset .. "</O>"
end,
UDim2 = function(raw)
--[[
XS: Represents the X.Scale component. Interpreted as a <float>.
XO: Represents the X.Offset component. Interpreted as an <int>.
YS: Represents the Y.Scale component. Interpreted as a <float>.
YO: Represents the Y.Offset component. Interpreted as an <int>.
]]
local X, Y = raw.X, raw.Y
return "<XS>"
.. X.Scale
.. "</XS><XO>"
.. X.Offset
.. "</XO><YS>"
.. Y.Scale
.. "</YS><YO>"
.. Y.Offset
.. "</YO>"
end,
-- UniqueId = function(raw)
--[[
UniqueId properties might be random everytime Studio saves a place file
and don't have a use right now outside of packages, which SSI doesn't
account for anyway. They generate diff noise, so we shouldn't serialize
them until we have to.
]]
-- https://github.com/MaximumADHD/Roblox-Client-Tracker/blob/roblox/LuaPackages/Packages/_Index/ApolloClientTesting/ApolloClientTesting/utilities/common/makeUniqueId.lua#L62
-- return "" -- ? No idea if this even needs a Descriptor
-- end,
Vector2 = function(raw)
--[[
X: Represents the X component. Interpreted as a <float>.
Y: Represents the Y component. Interpreted as a <float>.
]]
return XML_Descriptors.__VECTOR(raw.X, raw.Y)
end,
Vector2int16 = nil,
-- Vector2int16 = Descriptors.Vector2, -- except as <int>
Vector3 = function(raw)
--[[
X: Represents the X component. Interpreted as a <float>.
Y: Represents the Y component. Interpreted as a <float>.
Z: Represents the Z component. Interpreted as a <float>.
]]
return XML_Descriptors.__VECTOR(raw.X, raw.Y, raw.Z)
end,
Vector3int16 = nil,
-- Vector3int16 = Descriptors.Vector3, -- except as <int>\
bool = function(raw)
return raw and "true" or "false"
end,
double = function(raw) -- Float64
return XML_Descriptors.__APIPRECISION(raw, 17) --? A precision of at least 17 is required to properly represent a 64-bit floating point value, so this amount is recommended.
end, -- ? wouldn't float be better as an optimization
float = function(raw) -- Float32
return XML_Descriptors.__APIPRECISION(raw, 9) -- ? A precision of at least 9 is required to properly represent a 32-bit floating point value, so this amount is recommended.
end,
int = function(raw) -- Int32
return XML_Descriptors.__EXTREME(raw) or raw
end,
int64 = nil,
string = function(raw, skipEmptyCheck)
return not skipEmptyCheck and (raw == "" and raw or raw == nil and "")
or string_find(raw, "]]>") and string.gsub(raw, ESCAPES_PATTERN, ESCAPES)
or XML_Descriptors.__CDATA(string.gsub(raw, "\0", ""))
end,
}
for descriptorName, redirectName in
{
NumberSequence = "__SEQUENCE",
Vector2int16 = "Vector2",
Vector3int16 = "Vector3",
int64 = "int", -- Int64 (long)
}
do
XML_Descriptors[descriptorName] = XML_Descriptors[redirectName]
end
local ClassList
do
local ClassPropertyExceptions = { TriangleMeshPart = ArrayToDictionary({ "CollisionFidelity" }) }
local NotScriptableFixes =
{ -- For more info: https://github.com/luau/UniversalSynSaveInstance/blob/master/Tests/Potentially%20Missing%20Properties%20Tracker.luau
Instance = {
AttributesSerialize = function(instance)
-- * There are certain restrictions for names of attributes
-- https://create.roblox.com/docs/reference/engine/classes/Instance#SetAttribute
-- But it seems like even if those are present, Studio still opens the file just fine
-- So there is no need to check for them currently
-- TODO: merge sequence Descriptors and some other descriptors where possible (check xml descriptors)
-- ? Return early for empty tags (this proved equally as fast when done using counter/next)
local attrs = instance:GetAttributes()
local attrs_n = 0
local buffer_size = 4
local attrs_sorted = {}
local attrs_formatted = table.clone(attrs)
-- warn(instance)
for attr, val in attrs do
attrs_n += 1
attrs_sorted[attrs_n] = attr
local Type = typeof(val)
local Descriptor = Binary_Descriptors[Type]
local attr_size
attrs_formatted[attr], attr_size = Descriptor(val)
buffer_size += 5 + #attr + attr_size
end
-- print(instance, attrs_n) -- ? Looks like multiple buffer calls cause the this part to be skipped sometimes ?
if attrs_n == 0 then
return ""
end
table.sort(attrs_sorted)
local b = buffer.create(buffer_size)
local offset = 0
buffer.writeu32(b, offset, attrs_n)
offset += 4
local Descriptors_string = Binary_Descriptors.string
for _, attr in attrs_sorted do
local b_Name, Name_size = Descriptors_string(attr)
buffer.copy(b, offset, b_Name)
offset += Name_size
buffer.writeu8(b, offset, Type_IDs[typeof(attrs[attr])])
offset += 1
local bb = attrs_formatted[attr]
buffer.copy(b, offset, bb)
offset += buffer.len(bb)
end
return buffer.tostring(b)
end,
Tags = function(instance)
-- https://github.com/RobloxAPI/spec/blob/master/properties/Tags.md
local tags = instance:GetTags()
if #tags == 0 then
return ""
end
return table.concat(tags, "\0")
end,
},
-- DebuggerBreakpoint = {line="Line"}, -- ? This shouldn't appear in live games (try to prove this wrong)
BallSocketConstraint = { MaxFrictionTorqueXml = "MaxFrictionTorque" },
BasePart = {
Color3uint8 = "Color",
MaterialVariantSerialized = "MaterialVariant",
size = "Size",
},
-- CustomEvent = {PersistedCurrentValue=function(instance) -- * Class is Deprecated and :SetValue doesn't seem to affect GetCurrentValue anymore
-- local Receiver = instance:GetAttachedReceivers()[1]
-- if Receiver then
-- return Receiver:GetCurrentValue()
-- else
-- error("No Receiver", 2)
-- end
-- end},
Terrain = {
AcquisitionMethod = "LastUsedModificationMethod", -- ? Not sure
MaterialColors = function(instance) -- https://github.com/RobloxAPI/spec/blob/master/properties/MaterialColors.md
local TERRAIN_MATERIAL_COLORS =
{ --https://github.com/rojo-rbx/rbx-dom/blob/master/rbx_dom_lua/src/customProperties.lua#L5
Enum.Material.Grass,
Enum.Material.Slate,
Enum.Material.Concrete,
Enum.Material.Brick,
Enum.Material.Sand,
Enum.Material.WoodPlanks,
Enum.Material.Rock,
Enum.Material.Glacier,
Enum.Material.Snow,