-
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathsaveinstance.lua
More file actions
4066 lines (3447 loc) · 135 KB
/
saveinstance.lua
File metadata and controls
4066 lines (3447 loc) · 135 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
--!native
--!optimize 2
--!divine-intellect
-- https://discord.gg/wx4ThpAsmw
local function string_find(s, pattern)
return string.find(s, pattern, nil, true)
end
local function ArrayToDict(t, hydridMode, valueOverride, typeStrict)
local tmp = {}
if hydridMode then
for any1, any2 in next, t do
if type(any1) == "number" then
tmp[any2] = valueOverride or true
elseif type(any2) == "table" then
tmp[any1] = ArrayToDict(any2, hydridMode) -- any1 is Class, any2 is Name
else
tmp[any1] = any2
end
end
else
for _, key in next, t do
if not typeStrict or typeStrict and type(key) == typeStrict then
tmp[key] = true
end
end
end
return tmp
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"))',
-- cloneref = 'string.find(...,"clone",nil,true) and string.find(...,"ref",nil,true)',
-- decompile = '(string.find(...,"decomp",nil,true) and string.sub(...,#...) ~= "s")',
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)',
-- getcon = 'string.find(...,"get",nil,true) and (string.find(...,"conn",nil,true) or string.find(...,"sig",nil,true)) and string.sub(...,#(...))=="s"',
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 cloneref = global_container.cloneref
local gethiddenproperty = global_container.gethiddenproperty
-- These should be universal enough
local appendfile = appendfile
local isfile = isfile
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 service = setmetatable({}, {
__index = function(self, serviceName)
local o, s = pcall(Instance.new, serviceName)
local Service = o and s
or game:GetService(serviceName)
or settings():GetService(serviceName)
or UserSettings():GetService(serviceName)
-- if cloneref then
-- Service = cloneref(Service)
-- end
if Service then
self[serviceName] = Service
end
return Service
end,
})
local gethiddenproperty_fallback
do -- * Load Region of Déjà Vu
local UGCValidationService -- = service.UGCValidationService
gethiddenproperty_fallback = function(instance, propertyName)
if not UGCValidationService then
UGCValidationService = service.UGCValidationService
end
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 (always returns nil for "Class" category properties)
-- TODO `category ~= "Class"` causes WeldConstraint Part1Internal to be read as nil and not get unfiltered. Currently, there are no properties of category "Class" that match the following: NotScriptable, can be read with gethiddenproperty_fallback accurately (it always outputs nil for "Class" category, making that check useless anyway) & don't have a NotScriptableFix.
end
if gethiddenproperty then
local o, r = pcall(gethiddenproperty, workspace, "StreamOutBehavior")
if not o or r ~= nil and typeof(r) ~= "EnumItem" then -- * Tests if gethiddenproperty is broken
gethiddenproperty = nil
else
o, r = pcall(gethiddenproperty, Instance.new("AnimationRigData", Instance.new("Folder")), "parent") -- * Tests how it reacts to property overlap (shadowing) due to AnimationRigData.parent; expected BinaryString
if o and r ~= nil and type(r) ~= "string" then
gethiddenproperty = nil
end
end
end
local function benchmark(funcs, ...)
local ranking = table.create(2)
for i, f in next, funcs 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 = num % (bit32.bxor(num, 32))
if 0x80000000 < num then
num = num - bit32.bxor(num, 32)
end
return num
end
bit32.byteswap = function(num)
local BYTE_SIZE = 8
local MAX_BYTE_VALUE = 255
num = 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 @daily3014 & @XoifaiI
local rbxcrypt_base64encode
pcall(function()
local b64_enc_buf = loadstring(
game:HttpGet(
"https://raw.githubusercontent.com/daily3014/rbx-cryptography/refs/heads/main/src/Utilities/Base64.luau",
true
),
"Base64"
)().Encode
rbxcrypt_base64encode = function(raw)
return buffer.tostring(b64_enc_buf(buffer.fromstring(raw)))
end
end)
local EncodingService = game:GetService("EncodingService")
local EncodingService_base64encode = function(raw)
return buffer.tostring(EncodingService:Base64Encode(buffer.fromstring(raw)))
end
-- * Tests if base64encode exists and works properly then benchmark it
if base64encode and base64encode("\1\0\0\0\1") == "AQAAAAE=" then
if rbxcrypt_base64encode then
base64encode =
benchmark({ base64encode, rbxcrypt_base64encode, EncodingService_base64encode }, test_str)
end
else
base64encode = rbxcrypt_base64encode
end
assert(base64encode, "base64encode not found")
end
end
local SharedString_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 : )
local SharedStrings = setmetatable({}, {
__index = function(self, str)
local identifier = base64encode(tostring(SharedString_identifier)) -- tostring is only needed for built-in base64encode, Luau base64 implementations don't need it as buffers autoconvert
SharedString_identifier = SharedString_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 inherited_properties = {}
local default_instances = {}
local referents, ref_size = {}, 0 -- ? Roblox encodes all <Item> elements with a referent attribute. Each value is generated by starting with the prefix RBX, followed by a UUID version 4, with - characters removed, and all characters converted to uppercase.
local function GetRef(instance)
local ref = referents[instance]
if not ref then
ref = ref_size
referents[instance] = ref
ref_size = ref_size + 1
end
return ref
end
local function index(self, index_name)
return self[index_name]
end
local version = version
if not version then
version = function()
return "UNKNOWN"
end
end
local CLIENT_VERSION = tonumber(string.split(version(), ".")[2]) or 9e9 -- Velocity temp fix
local attr_Type_IDs = {
string = 0x02,
boolean = 0x03,
int32 = 0x04,
-- float = 0x05, -- float32
number = 0x06, -- float64 (double)
-- Array = 0x07,
-- Dictionary = 0x08,
UDim = 0x09,
UDim2 = 0x0A,
Ray = 0x0B,
Faces = 0x0C,
Axes = 0x0D,
BrickColor = 0x0E,
Color3 = 0x0F,
Vector2 = 0x10,
Vector3 = 0x11,
Vector2int16 = 0x12,
Vector3int16 = 0x13,
CFrame = 0x14,
EnumItem = 0x15,
NumberSequence = 0x17,
NumberSequenceKeypoint = 0x18,
ColorSequence = 0x19,
ColorSequenceKeypoint = 0x1A,
NumberRange = 0x1B,
Rect = 0x1C,
PhysicalProperties = 0x1D,
Region3 = 0x1F,
Region3int16 = 0x20,
Font = 0x21,
SecurityCapabilities = 0x22,
Path2DControlPoint = 0x23,
}
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 BASE_CAPABILITIES
pcall(function()
BASE_CAPABILITIES = SecurityCapabilities.new()
end)
local CAPABILITY_BITS = {
Plugin = 2 ^ 0, ---------------- 0
LocalUser = 2 ^ 1, ------------- 1
WritePlayer = 2 ^ 2, ----------- 2
RobloxScript = 2 ^ 3, ---------- 3
RobloxEngine = 2 ^ 4, ---------- 4
NotAccessible = 2 ^ 5, --------- 5
RunClientScript = 2 ^ 8, ------- 8
RunServerScript = 2 ^ 9, ------- 9
Unknown = 2 ^ 10, -------------- 10 (0xa)
AccessOutsideWrite = 2 ^ 11, --- 11 (0xb)
Unassigned = 2 ^ 15, ----------- 15 (0xf)
AssetRequire = 2 ^ 16, --------- 16 (0x10)
LoadString = 2 ^ 17, ----------- 17 (0x11)
ScriptGlobals = 2 ^ 18, -------- 18 (0x12)
CreateInstances = 2 ^ 19, ------ 19 (0x13)
Basic = 2 ^ 20, ---------------- 20 (0x14)
Audio = 2 ^ 21, ---------------- 21 (0x15)
DataStore = 2 ^ 22, ------------ 22 (0x16)
Network = 2 ^ 23, -------------- 23 (0x17)
Physics = 2 ^ 24, -------------- 24 (0x18)
UI = 2 ^ 25, ------------------- 25 (0x19)
CSG = 2 ^ 26, ------------------ 26 (0x1a)
Chat = 2 ^ 27, ----------------- 27 (0x1b)
Animation = 2 ^ 28, ------------ 28 (0x1c)
Avatar = 2 ^ 29, --------------- 29 (0x1d)
Input = 2 ^ 30, ---------------- 30 (0x1e)
Environment = 2 ^ 31, ---------- 31 (0x1f)
RemoteEvent = 2 ^ 32, ---------- 32 (0x20)
LegacySound = 2 ^ 33, ---------- 33 (0x21)
Players = 2 ^ 34, -------------- 34 (0x22)
CapabilityControl = 2 ^ 35, ---- 35 (0x23)
RemoteCommand = 2 ^ 59, -------- 59 (0x3b)
InternalTest = 2 ^ 60, --------- 60 (0x3c), Related to TestingGameScript
PluginOrOpenCloud = 2 ^ 61, ---- 61 (0x3d)
Assistant = 2 ^ 62, ------------ 62 (0x3e)
-- Restricted = 2 ^ 63, -------- for negative (highest bit for i64)
}
local function __COUNT_CAPABILITY_BITS(raw)
-- TODO tostring & string.split aren't ideal but this is the only way until the feature is out of the experimental phase (SecurityCapabilities.Contains exists but the Enums that it accepts lacks some hidden bits) - NotAccessible, Unknown, Restricted
-- ! Seems like both tostring & .Contains ignore high / internal bits (anything above CapabilityControl): RemoteCommand, InternalTest, PluginOrOpenCloud, Assistant. They're present when created & saved by Studio but can't be read through current means
local result = 0
for _, flag in next, string.split(tostring(raw), " | ") do
local bit = CAPABILITY_BITS[flag]
if bit then
result = result + bit
end
end
return result
end
local function __COUNT_BITS(...) -- * Credits to Friend (you know yourself)
local Value = 0
for i, bit in next, { ... } do
if bit then
Value = Value + 2 ^ (i - 1)
end
end
return Value
end
local Binary_Descriptors
Binary_Descriptors = {
__PACK_MULTIPLE = function(descriptor, value1, value2, value3)
local buf1, size1 = descriptor(value1)
local buf2, size2 = descriptor(value2)
local len = size1 + size2
local buf3, size3
if value3 ~= nil then
buf3, size3 = descriptor(value3)
len = len + size3
end
local b = buffer.create(len)
buffer.copy(b, 0, buf1)
buffer.copy(b, size1, buf2)
if value3 ~= nil then
buffer.copy(b, size1 + size2, buf3)
end
return b, len
end,
__construct_Sequence = function(keypoint_handler, keypointSize)
return function(raw)
local Keypoints = raw.Keypoints
local Keypoints_n = #Keypoints
local len = 4 + keypointSize * Keypoints_n
local b = buffer.create(len)
buffer.writeu32(b, 0, Keypoints_n)
local offset = 4
for _, keypoint in next, Keypoints do
keypoint_handler(keypoint, b, offset)
offset = offset + keypointSize
end
return b, len
end
end,
__writei64 = function(b, offset, raw)
local low = bit32.band(raw, 0xFFFFFFFF)
local high = (raw - low) / 0x100000000
buffer.writei32(b, offset, low)
buffer.writei32(b, offset + 4, high)
end,
__PACK_F32 = nil,
__PACK_I16 = nil,
__construct__PACKER = function(float)
local writeFunc = float and buffer.writef32 or buffer.writei16
local elementSize = float and 4 or 2
-- local zbuf, nozbuf = buffer.create(elementSize * 3), buffer.create(elementSize * 2)
return function(X, Y, Z)
local len = Z and (elementSize * 3) or (elementSize * 2)
local b = buffer.create(len)
writeFunc(b, 0, X)
writeFunc(b, elementSize, Y)
if Z then
writeFunc(b, elementSize * 2, Z)
end
return b, len
end
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)
return Binary_Descriptors.__PACK_MULTIPLE(Binary_Descriptors["UDim"], raw.X, raw.Y)
end,
["Ray"] = function(raw)
return Binary_Descriptors.__PACK_MULTIPLE(Binary_Descriptors["Vector3"], raw.Origin, raw.Direction)
end,
["Faces"] = function(raw)
local b = buffer.create(4)
buffer.writeu32(b, 0, __COUNT_BITS(raw.Right, raw.Top, raw.Back, raw.Left, raw.Bottom, raw.Front))
return b, 4
end,
["Axes"] = function(raw)
local b = buffer.create(4)
buffer.writeu32(b, 0, __COUNT_BITS(raw.X, raw.Y, raw.Z))
return b, 4
end,
["BrickColor"] = function(raw)
local b = buffer.create(4)
buffer.writeu32(b, 0, raw.Number)
return b, 4
end,
["Color3"] = function(raw)
return Binary_Descriptors.__PACK_F32(raw.R, raw.G, raw.B)
end,
["Vector2"] = function(raw)
return Binary_Descriptors.__PACK_F32(raw.X, raw.Y)
end,
["Vector3"] = function(raw)
return Binary_Descriptors.__PACK_F32(raw.X, raw.Y, raw.Z)
end,
["Vector2int16"] = function(raw)
return Binary_Descriptors.__PACK_I16(raw.X, raw.Y)
end,
["Vector3int16"] = function(raw)
return Binary_Descriptors.__PACK_I16(raw.X, raw.Y, raw.Z)
end,
["CFrame"] = function(raw)
local X, Y, Z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = raw:GetComponents()
local rotation_ID
do
local b = buffer.create(36)
buffer.writef32(b, 0, R00)
buffer.writef32(b, 4, R01)
buffer.writef32(b, 8, R02)
buffer.writef32(b, 12, R10)
buffer.writef32(b, 16, R11)
buffer.writef32(b, 20, R12)
buffer.writef32(b, 24, R20)
buffer.writef32(b, 28, R21)
buffer.writef32(b, 32, R22)
rotation_ID = CFrame_Rotation_IDs[buffer.tostring(b)]
end
local len = rotation_ID and 13 or 49
local b = buffer.create(len)
local __PACK_F32 = Binary_Descriptors.__PACK_F32
local position = __PACK_F32(X, Y, Z)
buffer.copy(b, 0, position)
-- 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)
local xBasis = __PACK_F32(R00, R01, R02)
buffer.copy(b, 13, xBasis)
local yBasis = __PACK_F32(R10, R11, R12)
buffer.copy(b, 13 + 12, yBasis)
local zBasis = __PACK_F32(R20, R21, R22)
buffer.copy(b, 13 + 24, zBasis)
-- 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,
["NumberSequenceKeypoint"] = function(keypoint, b, offset)
if not b then
return Binary_Descriptors.__PACK_F32(keypoint.Envelope, keypoint.Time, keypoint.Value)
end
buffer.writef32(b, offset, keypoint.Envelope)
offset = offset + 4
buffer.writef32(b, offset, keypoint.Time)
offset = offset + 4
buffer.writef32(b, offset, keypoint.Value)
end,
["ColorSequence"] = nil,
["ColorSequenceKeypoint"] = function(keypoint, b, offset)
local Value = Binary_Descriptors["Color3"](keypoint.Value)
if not b then
b = buffer.create(20)
offset = 0
end
buffer.writef32(b, offset, 0)
offset = offset + 4
buffer.writef32(b, offset, keypoint.Time)
offset = offset + 4
buffer.copy(b, offset, Value)
return b, 20
end,
["NumberRange"] = function(raw)
return Binary_Descriptors.__PACK_F32(raw.Min, raw.Max)
end,
["Rect"] = function(raw)
return Binary_Descriptors.__PACK_MULTIPLE(Binary_Descriptors["Vector2"], raw.Min, raw.Max)
end,
["PhysicalProperties"] = function(raw) -- ? Not sure yet (https://github.com/RobloxAPI/spec/blob/master/properties/drafts/AttributesSerializeFull.md#physicalproperties)
local len = 1
if raw then
len = len + 24
end
local b = buffer.create(len)
buffer.writeu8(b, 0, raw and 3 or 0) -- 3 means it has set CustomPhysicalProperties & has set AcousticAbsorption
if raw then
buffer.writef32(b, 1, raw.Density)
buffer.writef32(b, 5, raw.Friction)
buffer.writef32(b, 9, raw.Elasticity)
buffer.writef32(b, 13, raw.FrictionWeight)
buffer.writef32(b, 17, raw.ElasticityWeight)
buffer.writef32(b, 21, raw.AcousticAbsorption)
end
return b, len
end,
["Region3"] = function(raw)
local Translation = raw.CFrame.Position
local HalfSize = raw.Size * 0.5
return Binary_Descriptors.__PACK_MULTIPLE(
Binary_Descriptors["Vector3"],
Translation - HalfSize, -- /App/util/Region3.cpp#L38
Translation + HalfSize -- /App/util/Region3.cpp#L42
)
end,
["Region3int16"] = function(raw)
return Binary_Descriptors.__PACK_MULTIPLE(Binary_Descriptors["Vector3int16"], raw.Min, raw.Max)
end,
["Font"] = 636 < CLIENT_VERSION and function(raw)
local string__descriptor = Binary_Descriptors["string"]
local b_Family, Family_size = string__descriptor(raw.Family)
local b_CachedFaceId, CachedFaceId_size = string__descriptor("")
local len = 3 + Family_size + CachedFaceId_size
local b = buffer.create(len)
local ok_w, weight = pcall(index, raw, "Weight")
local ok_s, style = pcall(index, raw, "Style")
buffer.writeu16(b, 0, ok_w and weight.Value or 0)
buffer.writeu8(b, 2, ok_s and style.Value or 0)
buffer.copy(b, 3, b_Family)
buffer.copy(b, 3 + Family_size, b_CachedFaceId)
return b, len
end or function(raw)
local string__descriptor = Binary_Descriptors["string"]
local b_Family, Family_size = string__descriptor(raw.Family)
local b_CachedFaceId, CachedFaceId_size = string__descriptor("")
local len = 3 + Family_size + CachedFaceId_size
local b = buffer.create(len)
local FontString = tostring(raw)
local EmptyWeight = string_find(FontString, "Weight = ,")
local EmptyStyle = string_find(FontString, "Style = }")
buffer.writeu16(b, 0, EmptyWeight and 0 or raw.Weight.Value)
buffer.writeu8(b, 2, EmptyStyle and 0 or raw.Style.Value)
buffer.copy(b, 3, b_Family)
buffer.copy(b, 3 + Family_size, b_CachedFaceId)
return b, len
end,
["SecurityCapabilities"] = function(raw)
local b = buffer.create(8)
if raw == BASE_CAPABILITIES then
return b, 8
end
Binary_Descriptors.__writei64(b, 0, __COUNT_CAPABILITY_BITS(raw))
return b, 8
end,
["Path2DControlPoint"] = function(raw)
return Binary_Descriptors.__PACK_MULTIPLE(
Binary_Descriptors["UDim2"],
raw.Position,
raw.LeftTangent,
raw.RightTangent
)
end,
}
do -- Sequences
Binary_Descriptors["NumberSequence"] =
Binary_Descriptors.__construct_Sequence(Binary_Descriptors["NumberSequenceKeypoint"], 12)
Binary_Descriptors["ColorSequence"] =
Binary_Descriptors.__construct_Sequence(Binary_Descriptors["ColorSequenceKeypoint"], 20)
end
do -- Vectors
Binary_Descriptors.__PACK_F32 = Binary_Descriptors.__construct__PACKER(true)
Binary_Descriptors.__PACK_I16 = Binary_Descriptors.__construct__PACKER()
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 XML_Descriptors
XML_Descriptors = {
__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,
__NORMALIZE_NUMBER = function(raw)
if raw ~= raw then
return "NAN"
elseif raw == math.huge then
return "INF"
elseif raw == -math.huge then
return "-INF"
end
return raw
end,
__NORMALIZE_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,
__construct_Sequence = function(keypoint_handler)
-- The value is the text content, formatted as a space-separated list of floating point numbers.
-- tostring(raw) also works (but way slower rn)
-- ? Trailing whitespace after Envelope is needed for lune compatibility
return function(raw)
local sequence = ""
for _, keypoint in next, raw.Keypoints do
sequence = sequence .. keypoint_handler(keypoint)
end
return sequence
end
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 = 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>" .. __COUNT_BITS(raw.X, raw.Y, raw.Z) .. "</axes>"
end,
-- ! Assuming all base64 encoded strings won't have newlines
BinaryString = function(raw) -- ! only add raw == nil if such edge-case exists (note it)
return raw == "" and "" or base64encode(raw)
end,
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,
-- CFrameQuat = function(raw) -- ? This will probably never release as it's not even used anywhere naturally, but there are hints it does exist as a DataType
-- local X, Y, Z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = raw:GetComponents()
-- local trace = R00 + R11 + R22
-- local S, QW, QX, QY, QZ
-- if trace > 0 then
-- S = math.sqrt(1 + trace) * 2
-- QW = 0.25 * S
-- QX = (R21 - R12) / S
-- QY = (R02 - R20) / S
-- QZ = (R10 - R01) / S
-- elseif (R00 > R11) and (R00 > R22) then
-- S = math.sqrt(1 + R00 - R11 - R22) * 2
-- QW = (R21 - R12) / S
-- QX = 0.25 * S
-- QY = (R01 + R10) / S
-- QZ = (R02 + R20) / S
-- elseif R11 > R22 then
-- S = math.sqrt(1 + R11 - R00 - R22) * 2
-- QW = (R02 - R20) / S
-- QX = (R01 + R10) / S
-- QY = 0.25 * S
-- QZ = (R12 + R21) / S
-- else
-- S = math.sqrt(1 + R22 - R00 - R11) * 2
-- QW = (R10 - R01) / S
-- QX = (R02 + R20) / S
-- QY = (R12 + R21) / S
-- QZ = 0.25 * S
-- end
-- return XML_Descriptors.__VECTOR(X, Y, Z)
-- .. "<QX>"
-- .. QX
-- .. "</QX><QY>"
-- .. QY
-- .. "</QY><QZ>"
-- .. QZ
-- .. "</QZ><QW>"
-- .. QW
-- .. "</QW>"
-- 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
-- ? It is recommended that Color3uint8 is encoded with text instead of elements.
return 0xFF000000
+ (math.floor(raw.R * 255) * 0x10000)
+ (math.floor(raw.G * 255) * 0x100)
+ math.floor(raw.B * 255)
-- 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 = nil,
ColorSequenceKeypoint = function(keypoint)
local __NORMALIZE_RANGE = XML_Descriptors.__NORMALIZE_RANGE
local color3 = keypoint.Value
return __NORMALIZE_RANGE(keypoint.Time)
.. " "
.. __NORMALIZE_RANGE(color3.R)
.. " "
.. __NORMALIZE_RANGE(color3.G)
.. " "
.. __NORMALIZE_RANGE(color3.B)
.. " 0 "
end,
Content = function(raw) -- TODO Not sure about Object & Opaque, run tests when possible
local SourceType = raw.SourceType
return SourceType == Enum.ContentSourceType.None and "<null></null>"
or SourceType == Enum.ContentSourceType.Uri and "<uri>" .. XML_Descriptors.string(raw.Uri) .. "</uri>"
or SourceType == Enum.ContentSourceType.Object and "<Ref>" .. GetRef(raw.Object) .. "</Ref>"
or SourceType == Enum.ContentSourceType.Opaque and "<Ref>" .. GetRef(raw.Opaque) .. "</Ref>"
end,
ContentId = function(raw) -- ! only add raw == nil if such edge-case exists (note it)
return raw == "" and "<null></null>" or "<url>" .. XML_Descriptors.string(raw) .. "</url>", "Content" -- TODO Remove "Content" str once Roblox fully releases Content DataType
end,
CoordinateFrame = function(raw)
return "<CFrame>" .. XML_Descriptors.CFrame(raw) .. "</CFrame>"
end,
-- DateTime = function(raw) return raw.UnixTimestampMillis end, -- ? Not sure
EnumItem = function(raw)
return raw.Value, "token"
end,
Faces = function(raw)
-- The text of this element is formatted as an integer between 0 and 63
return "<faces>" .. __COUNT_BITS(raw.Right, raw.Top, raw.Back, raw.Left, raw.Bottom, raw.Front) .. "</faces>"
end,
Font = 636 < CLIENT_VERSION
and function(raw)
-- TODO (OPTIONAL ELEMENT): Figure out how to determine (ContentId) <CachedFaceId><url>rbxasset://fonts/GothamSSm-Medium.otf</url></CachedFaceId>
--[[
? game:GetService("TextService"):GetFontMemoryData()
? rbxasset://fonts/families/{Enum.Font.BuilderSans.Name}.json
]]
local ok_w, weight = pcall(index, raw, "Weight")
local ok_s, style = pcall(index, raw, "Style")
return "<Family>"
.. XML_Descriptors.ContentId(raw.Family)
.. "</Family><Weight>"
.. (ok_w and XML_Descriptors.EnumItem(weight) or "")
.. "</Weight><Style>"
.. (ok_s and style.Name or "") -- Weird but this field accepts .Name of enum instead..
.. "</Style>"
end
or function(raw)
local FontString = tostring(raw)
local EmptyWeight = string_find(FontString, "Weight = ,")
local EmptyStyle = string_find(FontString, "Style = }")
return "<Family>"
.. XML_Descriptors.ContentId(raw.Family)
.. "</Family><Weight>"
.. (EmptyWeight and "" or XML_Descriptors.EnumItem(raw.Weight))
.. "</Weight><Style>"
.. (EmptyStyle and "" or raw.Style.Name) -- Weird but this field accepts .Name of enum instead..
.. "</Style>"
end,
NetAssetRef = nil,
NumberRange = function(raw) -- tostring(raw) also works
-- The value is the text content, formatted as a space-separated list of floating point numbers.
local __NORMALIZE_RANGE = XML_Descriptors.__NORMALIZE_RANGE
return __NORMALIZE_RANGE(raw.Min) .. " " .. __NORMALIZE_RANGE(raw.Max) --[[.. " "]] -- ! This might be required for compatibility; __NORMALIZE_RANGE is not needed here but it fixes the issue where "nan 10" value would reset to "0 0"
end,
NumberSequence = nil,
NumberSequenceKeypoint = function(keypoint)
local __NORMALIZE_RANGE = XML_Descriptors.__NORMALIZE_RANGE
return __NORMALIZE_RANGE(keypoint.Time)
.. " "
.. __NORMALIZE_RANGE(keypoint.Value)
.. " "
.. __NORMALIZE_RANGE(keypoint.Envelope)
.. " "
end,
-- Path2DControlPoint = function(raw) -- ? Not sure
-- local udim2 = XML_Descriptors.UDim2
-- return "<Position>"
-- .. udim2(raw.Position)
-- .. "</Position>"
-- .. "<LeftTangent>"
-- .. udim2(raw.LeftTangent)
-- .. "</LeftTangent>"
-- .. "<RightTangent>"
-- .. udim2(raw.RightTangent)
-- .. "</RightTangent>"