-
Notifications
You must be signed in to change notification settings - Fork 16
/
BitstreamPropertyTypes.go
1332 lines (1200 loc) · 33.6 KB
/
BitstreamPropertyTypes.go
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
package peer
import (
"errors"
"fmt"
"math"
"github.com/Gskartwii/roblox-dissector/datamodel"
"github.com/robloxapi/rbxfile"
)
// PhysicsMotor is an alias type for rbxfile.ValueCFrames. They are used to
// describe motors in physics packets
type PhysicsMotor rbxfile.ValueCFrame
// Returns the stringified version of the motor
func (m PhysicsMotor) String() string {
return rbxfile.ValueCFrame(m).String()
}
func (b *extendedReader) readUDim() (rbxfile.ValueUDim, error) {
var err error
val := rbxfile.ValueUDim{}
val.Scale, err = b.readFloat32BE()
if err != nil {
return val, err
}
off, err := b.readUint32BE()
val.Offset = int32(off)
return val, err
}
func (b *extendedReader) readUDim2() (rbxfile.ValueUDim2, error) {
var err error
val := rbxfile.ValueUDim2{}
val.X.Scale, err = b.readFloat32BE()
if err != nil {
return val, err
}
offx, err := b.readUint32BE()
val.X.Offset = int32(offx)
if err != nil {
return val, err
}
val.Y.Scale, err = b.readFloat32BE()
if err != nil {
return val, err
}
offy, err := b.readUint32BE()
val.Y.Offset = int32(offy)
return val, err
}
func (b *extendedReader) readRay() (rbxfile.ValueRay, error) {
var err error
val := rbxfile.ValueRay{}
val.Origin, err = b.readVector3Simple()
if err != nil {
return val, err
}
val.Direction, err = b.readVector3Simple()
return val, err
}
func (b *extendedReader) readRegion3() (datamodel.ValueRegion3, error) {
var err error
val := datamodel.ValueRegion3{}
val.Start, err = b.readVector3Simple()
if err != nil {
return val, err
}
val.End, err = b.readVector3Simple()
return val, err
}
func (b *extendedReader) readRegion3int16() (datamodel.ValueRegion3int16, error) {
var err error
val := datamodel.ValueRegion3int16{}
val.Start, err = b.readVector3int16()
if err != nil {
return val, err
}
val.End, err = b.readVector3int16()
return val, err
}
func (b *extendedReader) readColor3() (rbxfile.ValueColor3, error) {
var err error
val := rbxfile.ValueColor3{}
val.R, err = b.readFloat32BE()
if err != nil {
return val, err
}
val.G, err = b.readFloat32BE()
if err != nil {
return val, err
}
val.B, err = b.readFloat32BE()
return val, err
}
func (b *extendedReader) readColor3uint8() (rbxfile.ValueColor3uint8, error) {
var err error
val := rbxfile.ValueColor3uint8{}
val.R, err = b.readUint8()
if err != nil {
return val, err
}
val.G, err = b.readUint8()
if err != nil {
return val, err
}
val.B, err = b.readUint8()
return val, err
}
func (b *extendedReader) readVector2() (rbxfile.ValueVector2, error) {
var err error
val := rbxfile.ValueVector2{}
val.X, err = b.readFloat32BE()
if err != nil {
return val, err
}
val.Y, err = b.readFloat32BE()
return val, err
}
// reads a offline Vector3 value (f32 X, f32 Y, f32 Z)
func (b *extendedReader) readVector3Simple() (rbxfile.ValueVector3, error) {
var err error
val := rbxfile.ValueVector3{}
val.X, err = b.readFloat32BE()
if err != nil {
return val, err
}
val.Y, err = b.readFloat32BE()
if err != nil {
return val, err
}
val.Z, err = b.readFloat32BE()
return val, err
}
// reads a complicated Vector3 value
func (b *extendedReader) readVector3() (rbxfile.ValueVector3, error) {
val := rbxfile.ValueVector3{}
/*isInteger, err := b.readBool()
if err != nil {
return rbxfile.ValueVector3{}, err
}
if !isInteger {
return b.readVector3Simple()
}
x, err := b.bits(11)
if err != nil {
return val, err
}
xShort := uint16((x >> 3) | ((x & 7) << 8))
if xShort&0x400 != 0 {
xShort |= 0xFC00
}
y, err := b.bits(11)
if err != nil {
return val, err
}
yShort := uint16((y >> 3) | ((y & 7) << 8))
z, err := b.bits(11)
if err != nil {
return val, err
}
zShort := uint16((z >> 3) | ((z & 7) << 8))
if zShort&0x400 != 0 {
zShort |= 0xFC00
}
val.X = float32(int16(xShort)) * 0.5
val.Y = float32(yShort) * 0.1
val.Z = float32(int16(zShort)) * 0.5*/
return val, errors.New("v3comp not implemented")
}
func (b *extendedReader) readVector2int16() (rbxfile.ValueVector2int16, error) {
var err error
val := rbxfile.ValueVector2int16{}
valX, err := b.readUint16BE()
if err != nil {
return val, err
}
valY, err := b.readUint16BE()
val.X = int16(valX)
val.Y = int16(valY)
return val, err
}
func (b *extendedReader) readVector3int16() (rbxfile.ValueVector3int16, error) {
var err error
var val rbxfile.ValueVector3int16
valX, err := b.readUint16BE()
if err != nil {
return val, err
}
valY, err := b.readUint16BE()
if err != nil {
return val, err
}
valZ, err := b.readUint16BE()
val = rbxfile.ValueVector3int16{X: int16(valX), Y: int16(valY), Z: int16(valZ)}
return val, err
}
func (b *extendedReader) readPBool() (rbxfile.ValueBool, error) {
val, err := b.readBoolByte()
return rbxfile.ValueBool(val), err
}
// reads a signed integer
func (b *extendedReader) readPSInt() (rbxfile.ValueInt, error) {
val, err := b.readUint32BE()
return rbxfile.ValueInt(val), err
}
// reads a single-precision float
func (b *extendedReader) readPFloat() (rbxfile.ValueFloat, error) {
val, err := b.readFloat32BE()
return rbxfile.ValueFloat(val), err
}
// reads a double-precision float
func (b *extendedReader) readPDouble() (rbxfile.ValueDouble, error) {
val, err := b.readFloat64BE()
return rbxfile.ValueDouble(val), err
}
func (b *extendedReader) readAxes() (rbxfile.ValueAxes, error) {
val, err := b.readUint32BE()
axesVal := rbxfile.ValueAxes{
X: val&4 != 0,
Y: val&2 != 0,
Z: val&1 != 0,
}
return axesVal, err
}
func (b *extendedReader) readFaces() (rbxfile.ValueFaces, error) {
val, err := b.readUint32BE()
facesVal := rbxfile.ValueFaces{
Right: val&32 != 0,
Top: val&16 != 0,
Back: val&8 != 0,
Left: val&4 != 0,
Bottom: val&2 != 0,
Front: val&1 != 0,
}
return facesVal, err
}
func (b *extendedReader) readBrickColor() (rbxfile.ValueBrickColor, error) {
val, err := b.readUint16BE()
return rbxfile.ValueBrickColor(val), err
}
func (b *extendedReader) readObject(context *CommunicationContext, caches *Caches) (datamodel.Reference, error) {
if context.ServerPeerID != 0 {
return b.readObjectPeerID(context)
}
scope, err := b.readCachedScope(caches)
if err != nil && err != ErrCacheReadOOB { // TODO: hack! physics packets may have problems with caches
return datamodel.Reference{}, err
}
reference := datamodel.Reference{Scope: scope}
if scope != "NULL" {
reference.Id, err = b.readUint32LE()
} else {
reference.IsNull = true
}
return reference, err
}
func (b *extendedReader) readCFrameSimple() (rbxfile.ValueCFrame, error) {
var err error
val := rbxfile.ValueCFrame{}
val.Position, err = b.readVector3Simple()
if err != nil {
return val, err
}
// FIXME: There's something wrong with CFrame parsing? Or maybe writing them?
special, err := b.readUint8()
if err != nil {
return val, err
}
if special > 0 {
if err != nil {
return val, err
}
if special > 36 {
println("oob, special", special)
return val, errors.New("special rotmatrix oob")
}
val.Rotation = lookupRotMatrix(uint64(special - 1))
} else {
for i := 0; i < 9; i++ {
val.Rotation[i], err = b.readFloat32BE()
if err != nil {
return val, err
}
}
}
return val, nil
}
func quaternionToRotMatrix(q [4]float32) [9]float32 {
X := q[0]
Y := q[1]
Z := q[2]
W := q[3]
XS := X * 2
YS := Y * 2
ZS := Z * 2
WX := W * XS
WY := W * YS
WZ := W * ZS
XX := XS * X
XY := YS * X
XZ := ZS * X
YY := YS * Y
YZ := ZS * Y
ZZ := ZS * Z
return [9]float32{
1 - (YY + ZZ),
XY - WZ,
XZ + WY,
XY + WZ,
1 - (XX + ZZ),
YZ - WX,
XZ - WY,
YZ + WX,
1 - (XX + YY),
}
}
func transformQuaternionToMatrix(q [4]float32) [9]float32 {
midresult := quaternionToRotMatrix(q)
xScaleFactor := float32(1.0 / math.Sqrt(float64(midresult[0]*midresult[0]+midresult[3]*midresult[3]+midresult[6]*midresult[6])))
result := [9]float32{
xScaleFactor * midresult[0], 0, 0,
xScaleFactor * midresult[3], 0, 0,
xScaleFactor * midresult[6], 0, 0,
} // X has been normalized
sXYSum := result[0]*midresult[1] + result[3]*midresult[4] + result[6]*midresult[7]
trueR01 := midresult[1] - result[0]*sXYSum
trueR11 := midresult[4] - result[3]*sXYSum
trueR21 := midresult[7] - result[6]*sXYSum
yScaleFactor := float32(1.0 / math.Sqrt(float64(trueR01*trueR01+trueR11*trueR11+trueR21*trueR21)))
result[1] = trueR01 * yScaleFactor
result[4] = trueR11 * yScaleFactor
result[7] = trueR21 * yScaleFactor
sYZSum := result[1]*midresult[2] + result[4]*midresult[5] + result[7]*midresult[8]
sXZSum := result[0]*midresult[2] + result[3]*midresult[5] + result[6]*midresult[8]
trueR02 := midresult[2] - result[0]*sXZSum - result[1]*sYZSum
trueR12 := midresult[5] - result[3]*sXZSum - result[4]*sYZSum
trueR22 := midresult[8] - result[6]*sXZSum - result[7]*sYZSum
zScaleFactor := float32(1.0 / math.Sqrt(float64(trueR02*trueR02+trueR12*trueR12+trueR22*trueR22)))
result[2] = trueR02 * zScaleFactor
result[5] = trueR12 * zScaleFactor
result[8] = trueR22 * zScaleFactor
return result
}
var specialRows = [6][3]float32{
[3]float32{1, 0, 0},
[3]float32{0, 1, 0},
[3]float32{0, 0, 1},
[3]float32{-1, 0, 0},
[3]float32{0, -1, 0},
[3]float32{0, 0, -1},
}
func lookupRotMatrix(special uint64) [9]float32 {
specialRowDiv6 := specialRows[special/6]
specialRowMod6 := specialRows[special%6]
ret := [9]float32{
0, 0, 0,
specialRowMod6[0], specialRowMod6[1], specialRowMod6[2],
specialRowDiv6[0], specialRowDiv6[1], specialRowDiv6[2],
}
ret[0] = ret[2*3+1]*ret[1*3+2] - ret[2*3+2]*ret[1*3+1]
ret[1] = ret[1*3+0]*ret[2*3+2] - ret[2*3+0]*ret[1*3+2]
ret[2] = ret[2*3+0]*ret[1*3+1] - ret[1*3+0]*ret[2*3+1]
trueRet := [9]float32{
ret[6], ret[7], ret[8],
ret[3], ret[4], ret[5],
ret[0], ret[1], ret[2],
}
return trueRet
}
func (b *extendedReader) readCFrame() (rbxfile.ValueCFrame, error) {
var err error
val := rbxfile.ValueCFrame{}
val.Position, err = b.readVector3Simple()
if err != nil {
return val, err
}
// FIXME: There's something wrong with CFrame parsing? Or maybe writing them?
special, err := b.readUint8()
if err != nil {
return val, err
}
if special > 0 {
if err != nil {
return val, err
}
if special > 36 {
println("oob, special", special)
return val, errors.New("special rotmatrix oob")
}
val.Rotation = lookupRotMatrix(uint64(special - 1))
} else {
val.Rotation, err = b.readPhysicsMatrix()
}
return val, err
}
func (b *joinSerializeReader) readContent() (rbxfile.ValueContent, error) {
var result string
stringLen, err := b.readUint32BE()
if err != nil {
return rbxfile.ValueContent(result), err
}
result, err = b.readASCII(int(stringLen))
return rbxfile.ValueContent(result), err
}
func (b *extendedReader) readContent(caches *Caches) (rbxfile.ValueContent, error) {
val, err := b.readCachedContent(caches)
return rbxfile.ValueContent(val), err
}
// TODO: Make this function uniform with other cache functions
func (b *extendedReader) readSystemAddress(caches *Caches) (datamodel.ValueSystemAddress, error) {
cache := &caches.SystemAddress
thisAddress := datamodel.ValueSystemAddress{IP: make([]byte, 4)}
var err error
var cacheIndex uint8
cacheIndex, err = b.readUint8()
if err != nil {
return thisAddress, err
}
if cacheIndex == 0x00 {
return thisAddress, err
}
if cacheIndex < 0x80 {
result, ok := cache.Get(cacheIndex)
if !ok {
return thisAddress, nil
}
return result.(datamodel.ValueSystemAddress), nil
}
err = b.bytes(thisAddress.IP, 4)
if err != nil {
return thisAddress, err
}
thisAddress.IP[0], thisAddress.IP[3] = thisAddress.IP[3], thisAddress.IP[0]
thisAddress.IP[1], thisAddress.IP[2] = thisAddress.IP[2], thisAddress.IP[1]
port, err := b.readUint16BE()
thisAddress.Port = int(port)
if err != nil {
return thisAddress, err
}
cache.Put(thisAddress, cacheIndex-0x80)
return datamodel.ValueSystemAddress(thisAddress), nil
}
func (b *joinSerializeReader) readSystemAddress() (datamodel.ValueSystemAddress, error) {
var err error
thisAddress := datamodel.ValueSystemAddress{IP: make([]byte, 4)}
err = b.bytes(thisAddress.IP, 4)
if err != nil {
return thisAddress, err
}
port, err := b.readUint16BE()
thisAddress.Port = int(port)
return thisAddress, err
}
func (b *extendedReader) readUintUTF8() (uint32, error) {
var res uint32
thisByte, err := b.ReadByte()
var shiftIndex uint32
for err == nil {
res |= uint32(thisByte&0x7F) << shiftIndex
shiftIndex += 7
if thisByte&0x80 == 0 {
break
}
thisByte, err = b.ReadByte()
}
return res, err
}
func (b *extendedReader) readSintUTF8() (int32, error) {
res, err := b.readUintUTF8()
return int32((res >> 1) ^ -(res & 1)), err
}
func (b *extendedReader) readVarint64() (uint64, error) {
var res uint64
thisByte, err := b.ReadByte()
var shiftIndex uint32
for err == nil {
res |= uint64(thisByte&0x7F) << shiftIndex
shiftIndex += 7
if thisByte&0x80 == 0 {
break
}
thisByte, err = b.ReadByte()
}
return res, err
}
func (b *extendedReader) readVarsint64() (int64, error) {
res, err := b.readVarint64()
return int64((res >> 1) ^ -(res & 1)), err
}
func (b *extendedReader) readVarLengthString() (string, error) {
stringLen, err := b.readUintUTF8()
if err != nil {
return "", err
}
return b.readASCII(int(stringLen))
}
func (b *extendedReader) readLuauProtectedStringRaw() (rbxfile.ValueProtectedString, error) {
stringLen, err := b.readUintUTF8()
if err != nil {
return rbxfile.ValueProtectedString(nil), err
}
val, err := b.readString(int(stringLen))
if err != nil {
return rbxfile.ValueProtectedString(nil), err
}
extraBytesLen, err := b.readUintUTF8()
if err != nil {
return rbxfile.ValueProtectedString(nil), err
}
extraBytes, err := b.readString(int(extraBytesLen))
if err != nil {
return rbxfile.ValueProtectedString(nil), err
}
fmt.Printf("extrabytes: %X\n", extraBytes)
return rbxfile.ValueProtectedString(val), nil
}
func (b *joinSerializeReader) readNewPString() (rbxfile.ValueString, error) {
val, err := b.readVarLengthString()
return rbxfile.ValueString(val), err
}
func (b *extendedReader) readNewPString(caches *Caches) (rbxfile.ValueString, error) {
val, err := b.readCached(caches)
return rbxfile.ValueString(val), err
}
func (b *joinSerializeReader) readNewProtectedString() (rbxfile.ValueProtectedString, error) {
res, err := b.readNewPString()
return rbxfile.ValueProtectedString(res), err
}
func (b *extendedReader) readNewProtectedString(caches *Caches) (rbxfile.ValueProtectedString, error) {
res, err := b.readNewCachedProtectedString(caches)
return rbxfile.ValueProtectedString(res), err
}
func (b *joinSerializeReader) readLuauProtectedString() (rbxfile.ValueProtectedString, error) {
return b.readLuauProtectedStringRaw()
}
func (b *extendedReader) readLuauProtectedString(caches *Caches) (rbxfile.ValueProtectedString, error) {
res, err := b.readLuauCachedProtectedString(caches)
return rbxfile.ValueProtectedString(res), err
}
func (b *joinSerializeReader) readNewContent() (rbxfile.ValueContent, error) {
res, err := b.readNewPString()
return rbxfile.ValueContent(res), err
}
func (b *extendedReader) readNewContent(caches *Caches) (rbxfile.ValueContent, error) {
res, err := b.readCachedContent(caches)
return rbxfile.ValueContent(res), err
}
func (b *extendedReader) readNewBinaryString() (rbxfile.ValueBinaryString, error) {
res, err := b.readVarLengthString()
return rbxfile.ValueBinaryString(res), err
}
// TODO: Remove context argument dependency
func (b *extendedReader) readNewEnumValue(enumID uint16, context *CommunicationContext) (datamodel.ValueToken, error) {
val, err := b.readUintUTF8()
token := datamodel.ValueToken{Value: val, ID: enumID}
return token, err
}
func (b *extendedReader) readNewPSint() (rbxfile.ValueInt, error) {
val, err := b.readSintUTF8()
return rbxfile.ValueInt(val), err
}
func getEnumName(context *CommunicationContext, id uint16) string {
return context.NetworkSchema.Enums[id].Name
}
// readNewTypeAndValue is never used by join data!
func (b *extendedReader) readNewTypeAndValue(reader PacketReader, deferred deferredStrings) (rbxfile.Value, error) {
var val rbxfile.Value
thisType, err := b.readUint8()
if err != nil {
return val, err
}
var enumID uint16
if thisType == PropertyTypeEnum {
enumID, err = b.readUint16BE()
if err != nil {
return val, err
}
}
val, err = b.ReadSerializedValue(reader, thisType, enumID, deferred)
return val, err
}
func (b *extendedReader) readNewTuple(reader PacketReader, deferred deferredStrings) (datamodel.ValueTuple, error) {
var tuple datamodel.ValueTuple
tupleLen, err := b.readUintUTF8()
if err != nil {
return tuple, err
}
if tupleLen > 0x10000 {
return tuple, errors.New("sanity check: exceeded maximum tuple len")
}
tuple = make(datamodel.ValueTuple, tupleLen)
for i := 0; i < int(tupleLen); i++ {
val, err := b.readNewTypeAndValue(reader, deferred)
if err != nil {
return tuple, err
}
tuple[i] = val
}
return tuple, nil
}
func (b *extendedReader) readNewArray(reader PacketReader, deferred deferredStrings) (datamodel.ValueArray, error) {
array, err := b.readNewTuple(reader, deferred)
return datamodel.ValueArray(array), err
}
func (b *extendedReader) readNewDictionary(reader PacketReader, deferred deferredStrings) (datamodel.ValueDictionary, error) {
var dictionary datamodel.ValueDictionary
dictionaryLen, err := b.readUintUTF8()
if err != nil {
return dictionary, err
}
if dictionaryLen > 0x10000 {
return dictionary, errors.New("sanity check: exceeded maximum dictionary len")
}
dictionary = make(datamodel.ValueDictionary, dictionaryLen)
for i := 0; i < int(dictionaryLen); i++ {
keyLen, err := b.readUintUTF8()
if err != nil {
return dictionary, err
}
key, err := b.readASCII(int(keyLen))
if err != nil {
return dictionary, err
}
dictionary[key], err = b.readNewTypeAndValue(reader, deferred)
if err != nil {
return dictionary, err
}
}
return dictionary, nil
}
func (b *extendedReader) readNewMap(reader PacketReader, deferred deferredStrings) (datamodel.ValueMap, error) {
thisMap, err := b.readNewDictionary(reader, deferred)
return datamodel.ValueMap(thisMap), err
}
func (b *extendedReader) readNumberSequenceKeypoint() (datamodel.ValueNumberSequenceKeypoint, error) {
var err error
thisKeypoint := datamodel.ValueNumberSequenceKeypoint{}
thisKeypoint.Time, err = b.readFloat32BE()
if err != nil {
return thisKeypoint, err
}
thisKeypoint.Value, err = b.readFloat32BE()
if err != nil {
return thisKeypoint, err
}
thisKeypoint.Envelope, err = b.readFloat32BE()
return thisKeypoint, err
}
func (b *extendedReader) readNumberSequence() (datamodel.ValueNumberSequence, error) {
var err error
numKeypoints, err := b.readUint32BE()
if err != nil {
return nil, err
}
if numKeypoints > 0x10000 {
return nil, errors.New("sanity check: exceeded maximum numberseq len")
}
thisSequence := make(datamodel.ValueNumberSequence, numKeypoints)
for i := 0; i < int(numKeypoints); i++ {
thisSequence[i], err = b.readNumberSequenceKeypoint()
if err != nil {
return thisSequence, err
}
}
return thisSequence, nil
}
func (b *extendedReader) readNumberRange() (rbxfile.ValueNumberRange, error) {
thisRange := rbxfile.ValueNumberRange{}
var err error
thisRange.Min, err = b.readFloat32BE()
if err != nil {
return thisRange, err
}
thisRange.Max, err = b.readFloat32BE()
return thisRange, err
}
func (b *extendedReader) readColorSequenceKeypoint() (datamodel.ValueColorSequenceKeypoint, error) {
var err error
thisKeypoint := datamodel.ValueColorSequenceKeypoint{}
thisKeypoint.Time, err = b.readFloat32BE()
if err != nil {
return thisKeypoint, err
}
thisKeypoint.Value, err = b.readColor3()
if err != nil {
return thisKeypoint, err
}
thisKeypoint.Envelope, err = b.readFloat32BE()
return thisKeypoint, err
}
func (b *extendedReader) readColorSequence() (datamodel.ValueColorSequence, error) {
var err error
numKeypoints, err := b.readUint32BE()
if err != nil {
return nil, err
}
if numKeypoints > 0x10000 {
return nil, errors.New("sanity check: exceeded maximum colorseq len")
}
thisSequence := make(datamodel.ValueColorSequence, numKeypoints)
for i := 0; i < int(numKeypoints); i++ {
thisSequence[i], err = b.readColorSequenceKeypoint()
if err != nil {
return thisSequence, err
}
}
return thisSequence, nil
}
func (b *extendedReader) readRect2D() (rbxfile.ValueRect2D, error) {
var err error
thisRect := rbxfile.ValueRect2D{}
thisRect.Min.X, err = b.readFloat32BE()
if err != nil {
return thisRect, err
}
thisRect.Min.Y, err = b.readFloat32BE()
if err != nil {
return thisRect, err
}
thisRect.Max.X, err = b.readFloat32BE()
if err != nil {
return thisRect, err
}
thisRect.Max.Y, err = b.readFloat32BE()
return thisRect, err
}
func (b *extendedReader) readPhysicalProperties() (rbxfile.ValuePhysicalProperties, error) {
var err error
props := rbxfile.ValuePhysicalProperties{}
props.CustomPhysics, err = b.readBoolByte()
if props.CustomPhysics {
if err != nil {
return props, err
}
props.Density, err = b.readFloat32BE()
if err != nil {
return props, err
}
props.Friction, err = b.readFloat32BE()
if err != nil {
return props, err
}
props.Elasticity, err = b.readFloat32BE()
if err != nil {
return props, err
}
props.FrictionWeight, err = b.readFloat32BE()
if err != nil {
return props, err
}
props.ElasticityWeight, err = b.readFloat32BE()
}
return props, err
}
func (b *extendedReader) readCoordsMode0() (rbxfile.ValueVector3, error) {
return b.readVector3Simple()
}
func (b *extendedReader) readCoordsMode1() (rbxfile.ValueVector3, error) {
value := rbxfile.ValueVector3{}
cRange, err := b.readFloat32BE()
if err != nil {
return value, err
}
if cRange <= 0.0000099999997 { // Has to be precise
return rbxfile.ValueVector3{}, nil
}
x, err := b.readUint16BE()
if err != nil {
return value, err
}
y, err := b.readUint16BE()
if err != nil {
return value, err
}
z, err := b.readUint16BE()
if err != nil {
return value, err
}
value.X = (float32(x)/32767.0 - 1.0) * cRange
value.Y = (float32(y)/32767.0 - 1.0) * cRange
value.Z = (float32(z)/32767.0 - 1.0) * cRange
return value, nil
}
func (b *extendedReader) readCoordsMode2() (rbxfile.ValueVector3, error) {
val := rbxfile.ValueVector3{}
/*x, err := b.bits(15)
if err != nil {
return val, err
}
xShort := uint16((x >> 7) | ((x & 0x7F) << 8))
y, err := b.bits(14)
if err != nil {
return val, err
}
yShort := uint16((y >> 6) | ((y & 0x3F) >> 8))
z, err := b.bits(15)
if err != nil {
return val, err
}
zShort := uint16((z >> 7) | ((z & 0x7F) << 8))
val.X = float32(xShort)*0.0625 - 2048.0
val.Y = float32(yShort)*0.0625 - 2048.0
val.Z = float32(zShort)*0.0625 - 2048.0*/
return val, errors.New("coordmode 2 not implemented")
}
func (b *extendedReader) readPhysicsCoords() (rbxfile.ValueVector3, error) {
var val rbxfile.ValueVector3
flags, err := b.readUint8()
if err != nil {
return val, err
}
exp := flags >> 3 // void signs
scale := float32(math.Exp2(float64(exp)))
// nice bitpacking!
if exp > 4 {
if exp > 10 { // when scale is > 0x400
// minimum precision is 2^11/2^21 = 2^-10
// 21 bits per value
val1, err := b.readUint32BE()
if err != nil {
return val, err
}
val2, err := b.readUint32BE()
if err != nil {
return val, err
}
xSign := uint32((flags >> 2) & 1)
ySign := uint32((flags >> 1) & 1)
zSign := uint32((flags >> 0) & 1)
v22 := (xSign << 21) | (val1 >> 11)
v25 := int32(v22<<10) >> 10
v26 := int32(((val2>>21)|(((ySign<<11)|val1&0x7FF)<<10))<<10) >> 10
v27 := val2&0x1FFFFF | (zSign << 31 >> 10)
val.X = float32(float32(v25)*0.00000047683739) * scale
val.Y = float32(float32(v26)*0.00000047683739) * scale
val.Y = float32(float32(v27)*0.00000047683739) * scale
} else { // when scale is in range 0x10 < scale <= 0x400
// 16 bits per value (and sign), precision ranges from 1/4096 to 1/128
// 2^16 = 65536, so values range from -16~16 to -512~512
x, err := b.readUint16BE()
if err != nil {
return val, err
}
y, err := b.readUint16BE()
if err != nil {
return val, err
}
z, err := b.readUint16BE()
if err != nil {
return val, err
}
xSign := uint32((flags >> 2) & 1)
ySign := uint32((flags >> 1) & 1)
zSign := uint32((flags >> 0) & 1)
rx := uint32(x)
ry := uint32(y)
rz := uint32(z)
v20 := int32((ry|(ySign<<16))<<15) >> 15
v21 := int32(((rz)|(zSign<<16))<<15) >> 15 // for some reason IDA doesn't show this as signed, despite that fact that it's created by a sar instruction!
val.X = float32(float32(int32((rx|(xSign<<16))<<15)>>15) * 0.000015259022 * scale)
val.Y = float32(float32(v20) * 0.000015259022 * scale)
val.Z = float32(float32(v21) * 0.000015259022 * scale)
}
} else { // when scale is in range 0 <= scale <= 0x10
val1, err := b.readUint32BE()
if err != nil {
return val, err
}
xSign := uint32((flags >> 2) & 1)
ySign := uint32((flags >> 1) & 1)
zSign := uint32((flags >> 0) & 1)
res := (int32(ySign<<31) >> 21) | int32((val1>>10)&0x3FF)
v17 := int32(val1&0x3FF) | (int32(zSign<<31) >> 21)
val.X = float32(float32(int32(((xSign<<10)|(val1>>20))<<21)>>21)*0.00097751711) * scale
val.Y = float32(float32(res)*0.00097751711) * scale
val.Z = float32(float32(v17)*0.00097751711) * scale
}
return val, err
}
func (b *extendedReader) readMatrixMode0() ([9]float32, error) {
var val [9]float32
var err error
q := [4]float32{}
q[3], err = b.readFloat32BE()
if err != nil {
return val, err
}
q[0], err = b.readFloat32BE()
if err != nil {
return val, err
}
q[1], err = b.readFloat32BE()
if err != nil {
return val, err
}
q[2], err = b.readFloat32BE()
if err != nil {
return val, err
}
return quaternionToRotMatrix(q), nil
}