-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ed8pkg2glb.py
3497 lines (3326 loc) · 188 KB
/
ed8pkg2glb.py
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
import os
import gc
import sys
import io
import struct
import array
try:
import zstandard
except:
pass
def read_null_ending_string(f):
import itertools
import functools
toeof = iter(functools.partial(f.read, 1), b'')
return sys.intern(b''.join(itertools.takewhile(b'\x00'.__ne__, toeof)).decode('ASCII'))
def bytearray_byteswap(p, itemsize):
if itemsize == 1:
pass
elif itemsize == 2:
for i in range(0, len(p), itemsize):
p0 = p[i + 0]
p[i + 0] = p[i + 1]
p[i + 1] = p0
elif itemsize == 4:
for i in range(0, len(p), itemsize):
p0 = p[i + 0]
p1 = p[i + 1]
p[i + 0] = p[i + 3]
p[i + 1] = p[i + 2]
p[i + 2] = p1
p[i + 3] = p0
elif itemsize == 8:
for i in range(0, len(p), itemsize):
p0 = p[i + 0]
p1 = p[i + 1]
p2 = p[i + 2]
p3 = p[i + 3]
p[i + 0] = p[i + 7]
p[i + 1] = p[i + 6]
p[i + 2] = p[i + 5]
p[i + 3] = p[i + 4]
p[i + 4] = p3
p[i + 5] = p2
p[i + 6] = p1
p[i + 7] = p0
else:
raise Exception("don't know how to byteswap this array type")
def cast_memoryview(mv, t):
return mv.cast(t)
def read_integer(f, size, unsigned, endian='<'):
typemap = {1: 'b', 2: 'h', 4: 'i', 8: 'q'}
inttype = typemap[size]
if unsigned == True:
inttype = inttype.upper()
ba = bytearray(f.read(size))
if endian == '>':
bytearray_byteswap(ba, size)
return cast_memoryview(memoryview(ba), inttype)[0]
def imageUntilePS4(buffer, width, height, bpb, pitch=0):
Tile = (0, 1, 8, 9, 2, 3, 10, 11, 16, 17, 24, 25, 18, 19, 26, 27, 4, 5, 12, 13, 6, 7, 14, 15, 20, 21, 28, 29, 22, 23, 30, 31, 32, 33, 40, 41, 34, 35, 42, 43, 48, 49, 56, 57, 50, 51, 58, 59, 36, 37, 44, 45, 38, 39, 46, 47, 52, 53, 60, 61, 54, 55, 62, 63)
tileWidth = 8
tileHeight = 8
out = bytearray(len(buffer))
usingPitch = False
if pitch > 0 and pitch != width:
width_bak = width
width = pitch
usingPitch = True
if width % tileWidth or height % tileHeight:
width_show = width
height_show = height
width = width_real = (width + (tileWidth - 1)) // tileWidth * tileWidth
height = height_real = (height + (tileHeight - 1)) // tileHeight * tileHeight
else:
width_show = width_real = width
height_show = height_real = height
for InY in range(height):
for InX in range(width):
Z = InY * width + InX
globalX = Z // (tileWidth * tileHeight) * tileWidth
globalY = globalX // width * tileHeight
globalX %= width
inTileX = Z % tileWidth
inTileY = Z // tileWidth % tileHeight
inTile = inTileY * tileHeight + inTileX
inTile = Tile[inTile]
inTileX = inTile % tileWidth
inTileY = inTile // tileHeight
OutX = globalX + inTileX
OutY = globalY + inTileY
OffsetIn = InX * bpb + InY * bpb * width
OffsetOut = OutX * bpb + OutY * bpb * width
out[OffsetOut:OffsetOut + bpb] = buffer[OffsetIn:OffsetIn + bpb]
if usingPitch:
width_show = width_bak
if width_show != width_real or height_show != height_real:
crop = bytearray(width_show * height_show * bpb)
for Y in range(height_show):
OffsetIn = Y * width_real * bpb
OffsetOut = Y * width_show * bpb
crop[OffsetOut:OffsetOut + width_show * bpb] = out[OffsetIn:OffsetIn + width_show * bpb]
out = crop
return out
def imageUntileMorton(buffer, width, height, bpb, pitch=0):
Tile = (0, 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)
tileWidth = 8
tileHeight = 8
out = bytearray(len(buffer))
usingPitch = False
if pitch > 0 and pitch != width:
width_bak = width
width = pitch
usingPitch = True
if width % tileWidth or height % tileHeight:
width_show = width
height_show = height
width = width_real = (width + (tileWidth - 1)) // tileWidth * tileWidth
height = height_real = (height + (tileHeight - 1)) // tileHeight * tileHeight
else:
width_show = width_real = width
height_show = height_real = height
for InY in range(height):
for InX in range(width):
Z = InY * width + InX
globalX = Z // (tileWidth * tileHeight) * tileWidth
globalY = globalX // width * tileHeight
globalX %= width
inTileX = Z % tileWidth
inTileY = Z // tileWidth % tileHeight
inTile = inTileY * tileHeight + inTileX
inTile = Tile[inTile]
inTileX = inTile % tileWidth
inTileY = inTile // tileHeight
OutX = globalX + inTileX
OutY = globalY + inTileY
OffsetIn = InX * bpb + InY * bpb * width
OffsetOut = OutX * bpb + OutY * bpb * width
out[OffsetOut:OffsetOut + bpb] = buffer[OffsetIn:OffsetIn + bpb]
if usingPitch:
width_show = width_bak
if width_show != width_real or height_show != height_real:
crop = bytearray(width_show * height_show * bpb)
for Y in range(height_show):
OffsetIn = Y * width_real * bpb
OffsetOut = Y * width_show * bpb
crop[OffsetOut:OffsetOut + width_show * bpb] = out[OffsetIn:OffsetIn + width_show * bpb]
out = crop
return out
def Compact1By1(x):
x &= 1431655765
x = (x ^ x >> 1) & 858993459
x = (x ^ x >> 2) & 252645135
x = (x ^ x >> 4) & 16711935
x = (x ^ x >> 8) & 65535
return x
def DecodeMorton2X(code):
return Compact1By1(code >> 0)
def DecodeMorton2Y(code):
return Compact1By1(code >> 1)
def imageUntileVita(buffer, width, height, bpb, pitch=0):
import math
tileWidth = 8
tileHeight = 8
out = bytearray(len(buffer))
usingPitch = False
if pitch > 0 and pitch != width:
width_bak = width
width = pitch
usingPitch = True
if width % tileWidth or height % tileHeight:
width_show = width
height_show = height
width = width_real = (width + (tileWidth - 1)) // tileWidth * tileWidth
height = height_real = (height + (tileHeight - 1)) // tileHeight * tileHeight
else:
width_show = width_real = width
height_show = height_real = height
for InY in range(height):
for InX in range(width):
Z = InY * width + InX
mmin = width if width < height else height
k = int(math.log(mmin, 2))
if height < width:
j = Z >> 2 * k << 2 * k | (DecodeMorton2Y(Z) & mmin - 1) << k | (DecodeMorton2X(Z) & mmin - 1) << 0
OutX = j // height
OutY = j % height
else:
j = Z >> 2 * k << 2 * k | (DecodeMorton2X(Z) & mmin - 1) << k | (DecodeMorton2Y(Z) & mmin - 1) << 0
OutX = j % width
OutY = j // width
OffsetIn = InX * bpb + InY * bpb * width
OffsetOut = OutX * bpb + OutY * bpb * width
out[OffsetOut:OffsetOut + bpb] = buffer[OffsetIn:OffsetIn + bpb]
if usingPitch:
width_show = width_bak
if width_show != width_real or height_show != height_real:
crop = bytearray(width_show * height_show * bpb)
for Y in range(height_show):
OffsetIn = Y * width_real * bpb
OffsetOut = Y * width_show * bpb
crop[OffsetOut:OffsetOut + width_show * bpb] = out[OffsetIn:OffsetIn + width_show * bpb]
out = crop
return out
def Unswizzle(data, width, height, imgFmt, cb, pitch=0):
TexParams = (('DXT1', 1, 8), ('DXT3', 1, 16), ('DXT5', 1, 16), ('BC5', 1, 16), ('BC7', 1, 16), ('RGBA8', 0, 4), ('ARGB8', 0, 4), ('L8', 0, 1), ('A8', 0, 1), ('LA88', 0, 2), ('RGBA16F', 0, 8), ('ARGB1555', 0, 2), ('ARGB4444', 0, 2), ('RGB565', 0, 2), ('ARGB8_SRGB', 0, 4))
TexParams = tuple((tuple((TexParams[j][i] for j in range(len(TexParams)))) for i in range(len(TexParams[0]))))
IsBlockCompressed = TexParams[1][TexParams[0].index(imgFmt)]
BytesPerBlock = TexParams[2][TexParams[0].index(imgFmt)]
if IsBlockCompressed:
width >>= 2
height >>= 2
pitch >>= 2
if imgFmt == 'DXT5' and cb == imageUntileVita:
BytesPerBlock = 8
data = cb(data, width, height, BytesPerBlock, pitch)
return data
def GetInfo(val, sh1, sh2):
val &= 4294967295
val <<= 31 - sh1
val &= 4294967295
val >>= 31 - sh1 + sh2
val &= 4294967295
return val
def decode_bc7_block(src):
def get_bits(src, bit, count):
v = 0
x = 0
by = bit >> 3
bit &= 7
if count == 0:
return 0
if bit + count <= 8:
v = src[by] >> bit & (1 << count) - 1
else:
x = src[by] | src[by + 1] << 8
v = x >> bit & (1 << count) - 1
return v & 255
bc7_modes = [[3, 4, 0, 0, 4, 0, 1, 0, 3, 0], [2, 6, 0, 0, 6, 0, 0, 1, 3, 0], [3, 6, 0, 0, 5, 0, 0, 0, 2, 0], [2, 6, 0, 0, 7, 0, 1, 0, 2, 0], [1, 0, 2, 1, 5, 6, 0, 0, 2, 3], [1, 0, 2, 0, 7, 8, 0, 0, 2, 2], [1, 0, 0, 0, 7, 7, 1, 0, 4, 0], [2, 6, 0, 0, 5, 5, 1, 0, 2, 0]]
def bc7_mode_to_dict(mode_arr):
return {'ns': mode_arr[0], 'pb': mode_arr[1], 'rb': mode_arr[2], 'isb': mode_arr[3], 'cb': mode_arr[4], 'ab': mode_arr[5], 'epb': mode_arr[6], 'spb': mode_arr[7], 'ib': mode_arr[8], 'ib2': mode_arr[9]}
bc7_modes[:] = [bc7_mode_to_dict(mode) for mode in bc7_modes]
bc7_si2 = [52428, 34952, 61166, 60616, 51328, 65260, 65224, 60544, 51200, 65516, 65152, 59392, 65512, 65280, 65520, 61440, 63248, 142, 28928, 2254, 140, 29456, 12544, 36046, 2188, 12560, 26214, 13932, 6120, 4080, 29070, 14748, 43690, 61680, 23130, 13260, 15420, 21930, 38550, 42330, 29646, 5064, 12876, 15324, 27030, 49980, 39270, 1632, 626, 1252, 20032, 10016, 51510, 37740, 14790, 25500, 37686, 40134, 33150, 59160, 52464, 4044, 30532, 60962]
bc7_si3 = [2858963024, 1784303680, 1515864576, 1414570152, 2779054080, 2694860880, 1431675040, 1515868240, 2857697280, 2857719040, 2863289600, 2425393296, 2492765332, 2762253476, 2846200912, 705315408, 2777960512, 172118100, 2779096320, 1436590240, 2829603924, 1785348160, 2762231808, 437912832, 5285028, 2862977168, 342452500, 1768494080, 2693105056, 2860651540, 1352967248, 1784283648, 2846195712, 1351655592, 2829094992, 606348324, 11162880, 613566756, 608801316, 1352993360, 1342874960, 2863285316, 1717960704, 2778768800, 1352683680, 1764256040, 1152035396, 1717986816, 2856600644, 1420317864, 2508232064, 2526451200, 2824098984, 2157286784, 2853442580, 2526412800, 2863272980, 2689618080, 2695210400, 2516582400, 1082146944, 2846402984, 2863311428, 709513812]
bc7_ai0 = [15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 2, 8, 2, 2, 8, 8, 15, 2, 8, 2, 2, 8, 8, 2, 2, 15, 15, 6, 8, 2, 8, 15, 15, 2, 8, 2, 2, 2, 15, 15, 6, 6, 2, 6, 8, 15, 15, 2, 2, 15, 15, 15, 15, 15, 2, 2, 15]
bc7_ai1 = [3, 3, 15, 15, 8, 3, 15, 15, 8, 8, 6, 6, 6, 5, 3, 3, 3, 3, 8, 15, 3, 3, 6, 10, 5, 8, 8, 6, 8, 5, 15, 15, 8, 15, 3, 5, 6, 10, 8, 15, 15, 3, 15, 5, 15, 15, 15, 15, 3, 15, 5, 5, 5, 8, 5, 10, 5, 10, 8, 13, 15, 12, 3, 3]
bc7_ai2 = [15, 8, 8, 3, 15, 15, 3, 8, 15, 15, 15, 15, 15, 15, 15, 8, 15, 8, 15, 3, 15, 8, 15, 8, 3, 15, 6, 10, 15, 15, 10, 8, 15, 3, 15, 10, 10, 8, 9, 10, 6, 15, 8, 15, 3, 6, 6, 8, 15, 3, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 3, 15, 15, 8]
bc7_weights2 = [0, 21, 43, 64]
bc7_weights3 = [0, 9, 18, 27, 37, 46, 55, 64]
bc7_weights4 = [0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64]
def bc7_get_weights(n):
if n == 2:
return bc7_weights2
if n == 3:
return bc7_weights3
return bc7_weights4
def bc7_get_subset(ns, partition, n):
if ns == 2:
return 1 & bc7_si2[partition] >> n
if ns == 3:
return 3 & bc7_si3[partition] >> 2 * n
return 0
def expand_quantized(v, bits):
v = v << 8 - bits
return (v | v >> bits) & 255
def bc7_lerp(dst, dst_offset, e, e_offset, s0, s1):
t0 = 64 - s0
t1 = 64 - s1
dst_write_offset = dst_offset * 4
e_read_offset_0 = (e_offset + 0) * 4
e_read_offset_1 = (e_offset + 1) * 4
dst[dst_write_offset + 0] = t0 * e[e_read_offset_0 + 0] + s0 * e[e_read_offset_1 + 0] + 32 >> 6 & 255
dst[dst_write_offset + 1] = t0 * e[e_read_offset_0 + 1] + s0 * e[e_read_offset_1 + 1] + 32 >> 6 & 255
dst[dst_write_offset + 2] = t0 * e[e_read_offset_0 + 2] + s0 * e[e_read_offset_1 + 2] + 32 >> 6 & 255
dst[dst_write_offset + 3] = t1 * e[e_read_offset_0 + 3] + s1 * e[e_read_offset_1 + 3] + 32 >> 6 & 255
col = bytearray(4 * 4 * 4)
endpoints = bytearray(6 * 4)
bit = 0
mode = src[0]
if mode == 0:
for i in range(16):
col[i * 4 + 0] = 0
col[i * 4 + 1] = 0
col[i * 4 + 2] = 0
col[i * 4 + 3] = 255
return col
while True:
cond = mode & 1 << bit != 0
bit += 1
if cond:
break
mode = bit - 1
info = bc7_modes[mode]
cb = info['cb']
ab = info['ab']
cw = bc7_get_weights(info['ib'])
aw = bc7_get_weights(info['ib2'] if ab != 0 and info['ib2'] != 0 else info['ib'])
partition = get_bits(src, bit, info['pb'])
bit += info['pb']
rotation = get_bits(src, bit, info['rb'])
bit += info['rb']
index_sel = get_bits(src, bit, info['isb'])
bit += info['isb']
numep = info['ns'] << 1
for i in range(numep):
val = get_bits(src, bit, cb)
bit += cb
endpoints[i * 4 + 0] = val
for i in range(numep):
val = get_bits(src, bit, cb)
bit += cb
endpoints[i * 4 + 1] = val
for i in range(numep):
val = get_bits(src, bit, cb)
bit += cb
endpoints[i * 4 + 2] = val
for i in range(numep):
val = 255
if ab != 0:
val = get_bits(src, bit, ab)
bit += ab
endpoints[i * 4 + 3] = val
if info['epb'] != 0:
cb += 1
if ab != 0:
ab += 1
for i in range(numep):
endpoint_write_offset = i * 4
val = get_bits(src, bit, 1)
bit += 1
endpoints[endpoint_write_offset + 0] = endpoints[endpoint_write_offset + 0] << 1 | val
endpoints[endpoint_write_offset + 1] = endpoints[endpoint_write_offset + 1] << 1 | val
endpoints[endpoint_write_offset + 2] = endpoints[endpoint_write_offset + 2] << 1 | val
if ab != 0:
endpoints[endpoint_write_offset + 3] = endpoints[endpoint_write_offset + 3] << 1 | val
if info['spb'] != 0:
cb += 1
if ab != 0:
ab += 1
for i in range(0, numep, 2):
val = get_bits(src, bit, 1)
bit += 1
for j in range(2):
endpoint_write_offset = (i + j) * 4
endpoints[endpoint_write_offset + 0] = endpoints[endpoint_write_offset + 0] << 1 | val
endpoints[endpoint_write_offset + 1] = endpoints[endpoint_write_offset + 1] << 1 | val
endpoints[endpoint_write_offset + 2] = endpoints[endpoint_write_offset + 2] << 1 | val
if ab != 0:
endpoints[endpoint_write_offset + 3] = endpoints[endpoint_write_offset + 3] << 1 | val
for i in range(numep):
endpoint_write_offset = i * 4
endpoints[endpoint_write_offset + 0] = expand_quantized(endpoints[endpoint_write_offset + 0], cb)
endpoints[endpoint_write_offset + 1] = expand_quantized(endpoints[endpoint_write_offset + 1], cb)
endpoints[endpoint_write_offset + 2] = expand_quantized(endpoints[endpoint_write_offset + 2], cb)
if ab != 0:
endpoints[endpoint_write_offset + 3] = expand_quantized(endpoints[endpoint_write_offset + 3], ab)
cibit = bit
aibit = cibit + 16 * info['ib'] - info['ns']
for i in range(16):
s = bc7_get_subset(info['ns'], partition, i) << 1
ib = info['ib']
if i == 0:
ib -= 1
elif info['ns'] == 2:
if i == bc7_ai0[partition]:
ib -= 1
elif info['ns'] == 3:
if i == bc7_ai1[partition]:
ib -= 1
elif i == bc7_ai2[partition]:
ib -= 1
i0 = get_bits(src, cibit, ib)
cibit += ib
if ab != 0 and info['ib2'] != 0:
ib2 = info['ib2']
if ib2 != 0 and i == 0:
ib2 -= 1
i1 = get_bits(src, aibit, ib2)
aibit += ib2
if index_sel != 0:
bc7_lerp(col, i, endpoints, s, aw[i1], cw[i0])
else:
bc7_lerp(col, i, endpoints, s, cw[i0], aw[i1])
else:
bc7_lerp(col, i, endpoints, s, cw[i0], cw[i0])
if rotation == 1:
val = col[i * 4 + 0]
col[i * 4 + 0] = col[i * 4 + 3]
col[i * 4 + 3] = val
elif rotation == 2:
val = col[i * 4 + 1]
col[i * 4 + 1] = col[i * 4 + 3]
col[i * 4 + 3] = val
elif rotation == 3:
val = col[i * 4 + 2]
col[i * 4 + 2] = col[i * 4 + 3]
col[i * 4 + 3] = val
return col
def decode_bc5(data):
def decode_bc3_alpha(dst, dst_offset, src, src_offset, stride, o, sign):
a0 = 0
a1 = 0
a = bytearray(8)
lut1 = 0
lut2 = 0
if sign == 1:
raise Exception('Signed bc5 not implemented!')
else:
a0 = src[src_offset + 0]
a1 = src[src_offset + 1]
src_lut_offset = src_offset + 2
lut1 = src[src_lut_offset + 0] | src[src_lut_offset + 1] << 8 | src[src_lut_offset + 2] << 16
lut2 = src[src_lut_offset + 3] | src[src_lut_offset + 4] << 8 | src[src_lut_offset + 5] << 16
a[0] = a0 & 255
a[1] = a1 & 255
if a0 > a1:
a[2] = (6 * a0 + 1 * a1) // 7
a[3] = (5 * a0 + 2 * a1) // 7
a[4] = (4 * a0 + 3 * a1) // 7
a[5] = (3 * a0 + 4 * a1) // 7
a[6] = (2 * a0 + 5 * a1) // 7
a[7] = (1 * a0 + 6 * a1) // 7
else:
a[2] = (4 * a0 + 1 * a1) // 5
a[3] = (3 * a0 + 2 * a1) // 5
a[4] = (2 * a0 + 3 * a1) // 5
a[5] = (1 * a0 + 4 * a1) // 5
a[6] = 0
a[7] = 255
for n in range(8):
aw = 7 & lut1 >> 3 * n
dst[dst_offset + (stride * n + o)] = a[aw]
for n in range(8):
aw = 7 & lut2 >> 3 * n
dst[dst_offset + (stride * (8 + n) + o)] = a[aw]
finalColor = bytearray(4 * 4 * 4)
block = data[:16]
decode_bc3_alpha(finalColor, 0, block, 0, 4, 0, 0)
decode_bc3_alpha(finalColor, 0, block, 8, 4, 1, 0)
return finalColor
import struct
def decode_dxt1(data):
finalColor = bytearray(4 * 4 * 4)
(color0, color1, bits) = struct.unpack('<HHI', data[:8])
r0 = (color0 >> 11 & 31) << 3
g0 = (color0 >> 5 & 63) << 2
b0 = (color0 & 31) << 3
r1 = (color1 >> 11 & 31) << 3
g1 = (color1 >> 5 & 63) << 2
b1 = (color1 & 31) << 3
for j in range(4):
j_offset = j * 4 * 4
for i in range(4):
i_offset = i * 4
control = bits & 3
bits = bits >> 2
if control == 0:
finalColor[j_offset + i_offset + 0] = r0
finalColor[j_offset + i_offset + 1] = g0
finalColor[j_offset + i_offset + 2] = b0
finalColor[j_offset + i_offset + 3] = 255
elif control == 1:
finalColor[j_offset + i_offset + 0] = r1
finalColor[j_offset + i_offset + 1] = g1
finalColor[j_offset + i_offset + 2] = b1
finalColor[j_offset + i_offset + 3] = 255
elif control == 2:
if color0 > color1:
finalColor[j_offset + i_offset + 0] = (2 * r0 + r1) // 3
finalColor[j_offset + i_offset + 1] = (2 * g0 + g1) // 3
finalColor[j_offset + i_offset + 2] = (2 * b0 + b1) // 3
finalColor[j_offset + i_offset + 3] = 255
else:
finalColor[j_offset + i_offset + 0] = (r0 + r1) // 2
finalColor[j_offset + i_offset + 1] = (g0 + g1) // 2
finalColor[j_offset + i_offset + 2] = (b0 + b1) // 2
finalColor[j_offset + i_offset + 3] = 255
elif control == 3:
if color0 > color1:
finalColor[j_offset + i_offset + 0] = (2 * r1 + r0) // 3
finalColor[j_offset + i_offset + 1] = (2 * g1 + g0) // 3
finalColor[j_offset + i_offset + 2] = (2 * b1 + b0) // 3
finalColor[j_offset + i_offset + 3] = 255
else:
finalColor[j_offset + i_offset + 0] = 0
finalColor[j_offset + i_offset + 1] = 0
finalColor[j_offset + i_offset + 2] = 0
finalColor[j_offset + i_offset + 3] = 0
return bytes(finalColor)
def decode_dxt3(data):
finalColor = bytearray(4 * 4 * 4)
block = data[:16]
bits = struct.unpack(b'<8B', block[:8])
(color0, color1) = struct.unpack(b'<HH', block[8:12])
(code,) = struct.unpack(b'<I', block[12:])
r0 = (color0 >> 11 & 31) << 3
g0 = (color0 >> 5 & 63) << 2
b0 = (color0 & 31) << 3
r1 = (color1 >> 11 & 31) << 3
g1 = (color1 >> 5 & 63) << 2
b1 = (color1 & 31) << 3
for j in range(4):
j_offset = j * 4 * 4
high = False
for i in range(4):
i_offset = i * 4
if high:
high = False
else:
high = True
alphaCodeIndex = (4 * j + i) // 2
finalAlpha = bits[alphaCodeIndex]
if high:
finalAlpha &= 15
else:
finalAlpha >>= 4
finalAlpha *= 17
colorCode = code >> 2 * (4 * j + i) & 3
if colorCode == 0:
finalColor[j_offset + i_offset + 0] = r0
finalColor[j_offset + i_offset + 1] = g0
finalColor[j_offset + i_offset + 2] = b0
elif colorCode == 1:
finalColor[j_offset + i_offset + 0] = r1
finalColor[j_offset + i_offset + 1] = g1
finalColor[j_offset + i_offset + 2] = b1
elif colorCode == 2:
finalColor[j_offset + i_offset + 0] = (2 * r0 + r1) // 3
finalColor[j_offset + i_offset + 1] = (2 * g0 + g1) // 3
finalColor[j_offset + i_offset + 2] = (2 * b0 + b1) // 3
elif colorCode == 3:
finalColor[j_offset + i_offset + 0] = (r0 + 2 * r1) // 3
finalColor[j_offset + i_offset + 1] = (g0 + 2 * g1) // 3
finalColor[j_offset + i_offset + 2] = (b0 + 2 * b1) // 3
finalColor[j_offset + i_offset + 3] = finalAlpha
return bytes(finalColor)
def decode_dxt5(data):
finalColor = bytearray(4 * 4 * 4)
block = data[:16]
(alpha0, alpha1) = struct.unpack(b'<BB', block[:2])
bits = struct.unpack(b'<6B', block[2:8])
alphaCode1 = bits[2] | bits[3] << 8 | bits[4] << 16 | bits[5] << 24
alphaCode2 = bits[0] | bits[1] << 8
(color0, color1) = struct.unpack(b'<HH', block[8:12])
(code,) = struct.unpack(b'<I', block[12:])
r0 = (color0 >> 11 & 31) << 3
g0 = (color0 >> 5 & 63) << 2
b0 = (color0 & 31) << 3
r1 = (color1 >> 11 & 31) << 3
g1 = (color1 >> 5 & 63) << 2
b1 = (color1 & 31) << 3
for j in range(4):
j_offset = j * 4 * 4
for i in range(4):
i_offset = i * 4
alphaCodeIndex = 3 * (4 * j + i)
if alphaCodeIndex <= 12:
alphaCode = alphaCode2 >> alphaCodeIndex & 7
elif alphaCodeIndex == 15:
alphaCode = alphaCode2 >> 15 | alphaCode1 << 1 & 6
else:
alphaCode = alphaCode1 >> alphaCodeIndex - 16 & 7
if alphaCode == 0:
finalAlpha = alpha0
elif alphaCode == 1:
finalAlpha = alpha1
elif alpha0 > alpha1:
finalAlpha = ((8 - alphaCode) * alpha0 + (alphaCode - 1) * alpha1) // 7
elif alphaCode == 6:
finalAlpha = 0
elif alphaCode == 7:
finalAlpha = 255
else:
finalAlpha = ((6 - alphaCode) * alpha0 + (alphaCode - 1) * alpha1) // 5
colorCode = code >> 2 * (4 * j + i) & 3
if colorCode == 0:
finalColor[j_offset + i_offset + 0] = r0
finalColor[j_offset + i_offset + 1] = g0
finalColor[j_offset + i_offset + 2] = b0
elif colorCode == 1:
finalColor[j_offset + i_offset + 0] = r1
finalColor[j_offset + i_offset + 1] = g1
finalColor[j_offset + i_offset + 2] = b1
elif colorCode == 2:
finalColor[j_offset + i_offset + 0] = (2 * r0 + r1) // 3
finalColor[j_offset + i_offset + 1] = (2 * g0 + g1) // 3
finalColor[j_offset + i_offset + 2] = (2 * b0 + b1) // 3
elif colorCode == 3:
finalColor[j_offset + i_offset + 0] = (r0 + 2 * r1) // 3
finalColor[j_offset + i_offset + 1] = (g0 + 2 * g1) // 3
finalColor[j_offset + i_offset + 2] = (b0 + 2 * b1) // 3
finalColor[j_offset + i_offset + 3] = finalAlpha
return bytes(finalColor)
def decode_block_into_abgr8(f, dwWidth, dwHeight, dxgiFormat):
rounded_height = (dwHeight + 3) // 4 * 4
rounded_width = (dwWidth + 3) // 4 * 4
block_size = 8 if dxgiFormat == 71 else 16
blocks_height = rounded_height // 4
blocks_width = rounded_width // 4
line_pitch = blocks_width * block_size
size_in_bytes = line_pitch * blocks_height
in_data = f.read(size_in_bytes)
if len(in_data) != size_in_bytes:
raise Exception('Data read incomplete')
decode_callback = None
if dxgiFormat == 71:
decode_callback = decode_dxt1
elif dxgiFormat == 74:
decode_callback = decode_dxt3
elif dxgiFormat == 77:
decode_callback = decode_dxt5
elif dxgiFormat == 83:
decode_callback = decode_bc5
elif dxgiFormat == 98:
decode_callback = decode_bc7_block
else:
raise Exception('Not supported format ' + str(dxgiFormat))
pixel_size_in_bytes = 4
block_width_size_in_bytes = 4 * pixel_size_in_bytes
single_row_size_in_bytes = blocks_width * block_width_size_in_bytes
out_data = bytearray(single_row_size_in_bytes * rounded_height)
for row in range(0, rounded_height, 4):
offs = row // 4 * line_pitch
block_line_data = in_data[offs:offs + line_pitch]
blocks = len(block_line_data) // block_size
blocks_line_width = blocks * block_size
for block_offset in range(0, blocks_line_width, block_size):
block = block_line_data[block_offset:block_offset + block_size]
decoded = decode_callback(block)
for i in range(4):
start_write_offset = block_offset // block_size * block_width_size_in_bytes + (row + i) * single_row_size_in_bytes
start_read_offset = 4 * 4 * i
out_data[start_write_offset:start_write_offset + block_width_size_in_bytes] = decoded[start_read_offset:start_read_offset + block_width_size_in_bytes]
if rounded_height != dwHeight or rounded_width != dwWidth:
single_row_size_in_bytes_cropped = pixel_size_in_bytes * dwWidth
out_data_cropped = bytearray(single_row_size_in_bytes_cropped * dwHeight)
for row in range(dwHeight):
out_data_cropped[single_row_size_in_bytes_cropped * row:single_row_size_in_bytes_cropped * (row + 1)] = out_data[single_row_size_in_bytes * row:single_row_size_in_bytes * (row + 1)]
out_data = out_data_cropped
return bytes(out_data)
def decode_l8_into_abgr8(f, dwWidth, dwHeight, dxgiFormat):
size_in_bytes = dwWidth * dwHeight * 1
in_data = f.read(size_in_bytes)
if len(in_data) != size_in_bytes:
raise Exception('Data read incomplete')
out_data = bytearray(dwWidth * dwHeight * 4)
for row in range(dwHeight):
for col in range(dwWidth):
out_offset = (row * dwWidth + col) * 4
in_offset = (row * dwWidth + col) * 1
(color,) = struct.unpack(b'<B', in_data[in_offset:in_offset + 1])
out_data[out_offset + 0] = color & 255
out_data[out_offset + 1] = color & 255
out_data[out_offset + 2] = color & 255
out_data[out_offset + 3] = 255
return bytes(out_data)
def decode_la8_into_abgr8(f, dwWidth, dwHeight, dxgiFormat):
size_in_bytes = dwWidth * dwHeight * 2
in_data = f.read(size_in_bytes)
if len(in_data) != size_in_bytes:
raise Exception('Data read incomplete')
out_data = bytearray(dwWidth * dwHeight * 4)
for row in range(dwHeight):
for col in range(dwWidth):
out_offset = (row * dwWidth + col) * 4
in_offset = (row * dwWidth + col) * 2
(color,) = struct.unpack(b'<H', in_data[in_offset:in_offset + 2])
out_data[out_offset + 0] = color >> 8 & 255
out_data[out_offset + 1] = color >> 8 & 255
out_data[out_offset + 2] = color >> 8 & 255
out_data[out_offset + 3] = color >> 0 & 255
return bytes(out_data)
def decode_rgb565_into_abgr8(f, dwWidth, dwHeight, dxgiFormat):
size_in_bytes = dwWidth * dwHeight * 2
in_data = f.read(size_in_bytes)
if len(in_data) != size_in_bytes:
raise Exception('Data read incomplete')
out_data = bytearray(dwWidth * dwHeight * 4)
for row in range(dwHeight):
for col in range(dwWidth):
out_offset = (row * dwWidth + col) * 4
in_offset = (row * dwWidth + col) * 2
(color,) = struct.unpack(b'<H', in_data[in_offset:in_offset + 2])
out_data[out_offset + 0] = (color >> 11 & 31) << 3
out_data[out_offset + 1] = (color >> 5 & 63) << 2
out_data[out_offset + 2] = (color & 31) << 3
out_data[out_offset + 3] = 255
return bytes(out_data)
def decode_argb4444_into_abgr8(f, dwWidth, dwHeight, dxgiFormat):
size_in_bytes = dwWidth * dwHeight * 2
in_data = f.read(size_in_bytes)
if len(in_data) != size_in_bytes:
raise Exception('Data read incomplete')
out_data = bytearray(dwWidth * dwHeight * 4)
for row in range(dwHeight):
for col in range(dwWidth):
out_offset = (row * dwWidth + col) * 4
in_offset = (row * dwWidth + col) * 2
(color,) = struct.unpack(b'<H', in_data[in_offset:in_offset + 2])
out_data[out_offset + 0] = (color >> 8 & 15) * 17
out_data[out_offset + 1] = (color >> 4 & 15) * 17
out_data[out_offset + 2] = (color >> 0 & 15) * 17
out_data[out_offset + 3] = (color >> 12 & 15) * 17
return bytes(out_data)
def decode_rgba8_into_abgr8(f, dwWidth, dwHeight, dxgiFormat):
size_in_bytes = dwWidth * dwHeight * 4
in_data = f.read(size_in_bytes)
if len(in_data) != size_in_bytes:
raise Exception('Data read incomplete')
out_data = bytearray(dwWidth * dwHeight * 4)
for row in range(dwHeight):
for col in range(dwWidth):
out_offset = (row * dwWidth + col) * 4
in_offset = out_offset
(color,) = struct.unpack(b'<I', in_data[in_offset:in_offset + 4])
out_data[out_offset + 0] = color >> 0 & 255
out_data[out_offset + 1] = color >> 8 & 255
out_data[out_offset + 2] = color >> 16 & 255
out_data[out_offset + 3] = color >> 24 & 255
return bytes(out_data)
def decode_argb8_into_agbr8(f, dwWidth, dwHeight, dxgiFormat):
size_in_bytes = dwWidth * dwHeight * 4
in_data = f.read(size_in_bytes)
if len(in_data) != size_in_bytes:
raise Exception('Data read incomplete')
out_data = bytearray(dwWidth * dwHeight * 4)
for row in range(dwHeight):
for col in range(dwWidth):
out_offset = (row * dwWidth + col) * 4
in_offset = out_offset
(color,) = struct.unpack(b'<I', in_data[in_offset:in_offset + 4])
out_data[out_offset + 0] = color >> 16 & 255
out_data[out_offset + 1] = color >> 8 & 255
out_data[out_offset + 2] = color >> 0 & 255
out_data[out_offset + 3] = color >> 24 & 255
return bytes(out_data)
def get_dds_header(fmt, width, height, mipmap_levels, is_cube_map):
dwMagic = b'DDS '
dwSize = 124
dwFlags = 1 | 2 | 4 | 4096
dwHeight = height
dwWidth = width
dwPitchOrLinearSize = 0
dwDepth = 0
dwMipMapCount = 0
ddspf_dwSize = 32
ddspf_dwFlags = 0
ddspf_dwFourCC = b''
ddspf_dwRGBBitCount = 0
ddspf_dwRBitMask = 0
ddspf_dwGBitMask = 0
ddspf_dwBBitMask = 0
ddspf_dwABitMask = 0
dwCaps = 4096
dwCaps2 = 0
dwCaps3 = 0
dwCaps4 = 0
dxgiFormat = 0
resourceDimension = 3
miscFlag = 0
arraySize = 1
miscFlags2 = 0
if True:
if fmt == 'LA8':
(ddspf_dwRBitMask, ddspf_dwGBitMask, ddspf_dwBBitMask, ddspf_dwABitMask) = (255, 255, 255, 65280)
dwFlags |= 8
elif fmt == 'L8':
(ddspf_dwRBitMask, ddspf_dwGBitMask, ddspf_dwBBitMask, ddspf_dwABitMask) = (255, 255, 255, 0)
dwFlags |= 8
elif fmt == 'ARGB8' or fmt == 'ARGB8_SRGB':
(ddspf_dwRBitMask, ddspf_dwGBitMask, ddspf_dwBBitMask, ddspf_dwABitMask) = (65280, 16711680, 4278190080, 255)
dwFlags |= 8
elif fmt == 'RGBA8':
(ddspf_dwRBitMask, ddspf_dwGBitMask, ddspf_dwBBitMask, ddspf_dwABitMask) = (255, 65280, 16711680, 4278190080)
dwFlags |= 8
elif fmt == 'RGB565':
(ddspf_dwRBitMask, ddspf_dwGBitMask, ddspf_dwBBitMask, ddspf_dwABitMask) = (63488, 2016, 31, 0)
dwFlags |= 8
elif fmt == 'ARGB4444':
(ddspf_dwRBitMask, ddspf_dwGBitMask, ddspf_dwBBitMask, ddspf_dwABitMask) = (3840, 240, 15, 61440)
dwFlags |= 8
elif fmt == 'BC5':
dwFlags |= 524288
dxgiFormat = 83
elif fmt == 'BC7':
dwFlags |= 524288
dxgiFormat = 98
elif fmt == 'DXT1':
dwFlags |= 524288
elif fmt == 'DXT3':
dwFlags |= 524288
elif fmt == 'DXT5':
dwFlags |= 524288
if dwFlags & 8 != 0:
ddspf_dwFlags = 64
if ddspf_dwABitMask != 0:
ddspf_dwFlags |= 1
all_bit_mask = ddspf_dwRBitMask | ddspf_dwGBitMask | ddspf_dwBBitMask | ddspf_dwABitMask
if all_bit_mask & 4278190080 != 0:
ddspf_dwRGBBitCount = 32
elif all_bit_mask & 16711680 != 0:
ddspf_dwRGBBitCount = 24
elif all_bit_mask & 65280 != 0:
ddspf_dwRGBBitCount = 16
elif all_bit_mask & 255 != 0:
ddspf_dwRGBBitCount = 8
dwPitchOrLinearSize = (width * ddspf_dwRGBBitCount + 7) // 8
if dwFlags & 524288 != 0:
ddspf_dwFlags = 4
if dxgiFormat != 0:
ddspf_dwFourCC = b'DX10'
else:
ddspf_dwFourCC = fmt.encode('ASCII')
dwPitchOrLinearSize = (width + 3) // 4 * (8 if fmt == 'DXT1' else 16)
if mipmap_levels != None:
dwFlags |= 131072
dwMipMapCount = mipmap_levels
dwCaps |= 4194304
if mipmap_levels != None or is_cube_map:
dwCaps |= 8
if is_cube_map:
dwCaps2 = 512 | 1024 | 2048 | 4096 | 8192 | 16384 | 32768
if ddspf_dwFourCC == b'DX10':
return struct.pack('<4s20I4s10I5I', dwMagic, dwSize, dwFlags, dwHeight, dwWidth, dwPitchOrLinearSize, dwDepth, dwMipMapCount, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ddspf_dwSize, ddspf_dwFlags, ddspf_dwFourCC, ddspf_dwRGBBitCount, ddspf_dwRBitMask, ddspf_dwGBitMask, ddspf_dwBBitMask, ddspf_dwABitMask, dwCaps, dwCaps2, dwCaps3, dwCaps4, 0, dxgiFormat, resourceDimension, miscFlag, arraySize, miscFlags2)
else:
return struct.pack('<4s20I4s10I', dwMagic, dwSize, dwFlags, dwHeight, dwWidth, dwPitchOrLinearSize, dwDepth, dwMipMapCount, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ddspf_dwSize, ddspf_dwFlags, ddspf_dwFourCC, ddspf_dwRGBBitCount, ddspf_dwRBitMask, ddspf_dwGBitMask, ddspf_dwBBitMask, ddspf_dwABitMask, dwCaps, dwCaps2, dwCaps3, dwCaps4, 0)
def uncompress_nislzss(src, decompressed_size, compressed_size):
des = int.from_bytes(src.read(4), byteorder='little')
if des != decompressed_size:
des = des if des > decompressed_size else decompressed_size
cms = int.from_bytes(src.read(4), byteorder='little')
if cms != compressed_size and compressed_size - cms != 4:
raise Exception("compression size in header and stream don't match")
num3 = int.from_bytes(src.read(4), byteorder='little')
fin = src.tell() + cms - 13
cd = bytearray(des)
num4 = 0
while src.tell() <= fin:
b = src.read(1)[0]
if b == num3:
b2 = src.read(1)[0]
if b2 != num3:
if b2 >= num3:
b2 -= 1
b3 = src.read(1)[0]
if b2 < b3:
for _ in range(b3):
cd[num4] = cd[num4 - b2]
num4 += 1
else:
sliding_window_pos = num4 - b2
cd[num4:num4 + b3] = cd[sliding_window_pos:sliding_window_pos + b3]
num4 += b3
else:
cd[num4] = b2
num4 += 1
else:
cd[num4] = b
num4 += 1
return cd
def uncompress_lz4(src, decompressed_size, compressed_size):
dst = bytearray(decompressed_size)
min_match_len = 4
num4 = 0
fin = src.tell() + compressed_size
def get_length(src, length):
if length != 15:
return length
while True:
read_buf = src.read(1)
if len(read_buf) != 1:
raise Exception('EOF at length read')
len_part = read_buf[0]
length += len_part
if len_part != 255:
break
return length
while src.tell() <= fin:
read_buf = src.read(1)
if not read_buf:
raise Exception('EOF at reading literal-len')
token = read_buf[0]
literal_len = get_length(src, token >> 4 & 15)
read_buf = src.read(literal_len)
if len(read_buf) != literal_len:
raise Exception('not literal data')
dst[num4:num4 + literal_len] = read_buf[:literal_len]
num4 += literal_len
read_buf = src.read(2)
if not read_buf or src.tell() > fin:
if token & 15 != 0:
raise Exception('EOF, but match-len > 0: %u' % (token % 15,))
break
if len(read_buf) != 2:
raise Exception('premature EOF')
offset = read_buf[0] | read_buf[1] << 8
if offset == 0:
raise Exception("offset can't be 0")
match_len = get_length(src, token >> 0 & 15)
match_len += min_match_len
if offset < match_len:
for _ in range(match_len):
dst[num4] = dst[num4 - offset]
num4 += 1
else:
sliding_window_pos = num4 - offset
dst[num4:num4 + match_len] = dst[sliding_window_pos:sliding_window_pos + match_len]
num4 += match_len
return dst
def uncompress_zstd(src, decompressed_size, compressed_size):
dctx = zstandard.ZstdDecompressor()
uncompressed = dctx.decompress(src.read(compressed_size), max_output_size=decompressed_size)
return uncompressed
NOEPY_HEADER_BE = 1381582928
NOEPY_HEADER_LE = 1346918738
def get_type(id_, type_strings, class_descriptors):
total_types = len(type_strings) + 1
if id_ < total_types:
return type_strings[id_]
else:
id_ -= total_types
return class_descriptors[id_].name
def get_class_from_type(id_, type_strings):
total_types = len(type_strings) + 1
if id_ < total_types:
return None
else:
return id_ - total_types + 1
def get_reference_from_class_descriptor_index(cluster_type_info, class_name, index):
if class_name in cluster_type_info.list_for_class_descriptors and len(cluster_type_info.list_for_class_descriptors[class_name]) > index:
return cluster_type_info.list_for_class_descriptors[class_name][index].split('#', 1)
return None
def get_class_name(cluster_type_info, id_):
return cluster_type_info.class_descriptors[id_ - 1].name
def get_class_size(cluster_type_info, id_):
return cluster_type_info.class_descriptors[id_ - 1].get_size_in_bytes()
def get_member_id_to_pointer_fixup_list(g, class_descriptor, cluster_type_info, cluster_list_fixup_info, pointer_fixup_count, class_element):
member_id_to_pointer_fixup_list = {}
for m in range(class_descriptor.class_data_member_count):
member_id = class_descriptor.member_offset + m
data_member = cluster_type_info.data_members[member_id]
value_offset = data_member.value_offset
member_id_to_pointer_fixup_list[member_id] = []
for b in range(pointer_fixup_count):
pointer_fixup = cluster_list_fixup_info.pointer_fixups[b + cluster_list_fixup_info.pointer_fixup_offset]
if pointer_fixup.source_object_id == class_element:
if (pointer_fixup.som == value_offset + 4 or pointer_fixup.som + 4 == value_offset or pointer_fixup.som == value_offset) and (not pointer_fixup.is_class_data_member()) or (pointer_fixup.som == member_id and pointer_fixup.is_class_data_member()):
member_id_to_pointer_fixup_list[member_id].append(pointer_fixup)
return member_id_to_pointer_fixup_list
def map_object_member_from_value_offset_recursive(cluster_type_info, class_id, offset_from_parent, object_value_offset_to_member_id, object_member_to_fixup_map):
class_descriptor = cluster_type_info.class_descriptors[class_id - 1]
for m in range(class_descriptor.class_data_member_count):
member_id = class_descriptor.member_offset + m
data_member = cluster_type_info.data_members[member_id]
value_offset = offset_from_parent + data_member.value_offset
object_value_offset_to_member_id[value_offset] = member_id
for i in object_member_to_fixup_map:
if member_id not in object_member_to_fixup_map[i]: