-
Notifications
You must be signed in to change notification settings - Fork 0
/
skyrimstructs.py
1334 lines (1178 loc) · 49 KB
/
skyrimstructs.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
# -*- coding: utf-8 -*-
"""
SkyAlchemy
Copyright ©2016 Ronan Paixão
Licensed under the terms of the MIT License.
See LICENSE for details.
@author: Ronan Paixão
"""
from __future__ import unicode_literals, division
from io import BytesIO
import zlib
import ctypes
import math
#%% unpack and data
from skyrimtypes import _types, unpack, RefID
from skyrimdata import db
#%%
c_uint32 = ctypes.c_uint32
class MGEFflagbits(ctypes.LittleEndianStructure):
_fields_ = [
("Hostile", c_uint32, 1),
("Recover", c_uint32, 1),
("Detrimental", c_uint32, 1),
("SnaptoNavmesh", c_uint32, 1),
("NoHitEvent", c_uint32, 1),
("Unk1", c_uint32, 1),
("Unk2", c_uint32, 1),
("Unk3", c_uint32, 1),
("DispelEffects", c_uint32, 1),
("NoDuration", c_uint32, 1),
("NoMagnitude", c_uint32, 1),
("NoArea", c_uint32, 1),
("FXPersist", c_uint32, 1),
("Unk4", c_uint32, 1),
("GoryVisual", c_uint32, 1),
("HideinUI", c_uint32, 1),
("Unk5", c_uint32, 1),
("NoRecast", c_uint32, 1),
("Unk6", c_uint32, 1),
("Unk7", c_uint32, 1),
("Unk8", c_uint32, 1),
("PowerAffectsMagnitude", c_uint32, 1),
("PowerAffectsDuration", c_uint32, 1),
("Unk9", c_uint32, 1),
("Unk10", c_uint32, 1),
("Unk11", c_uint32, 1),
("Painless", c_uint32, 1),
("NoHitEffect", c_uint32, 1),
("NoDeathDispel", c_uint32, 1),
]
class MGEFflags(ctypes.Union):
_fields_ = [("b", MGEFflagbits), ("int", c_uint32)]
def __init__(self, uint32):
super(MGEFflags, self).__init__()
self.int = uint32
#%% Stat
_stat_categories = {
0: "General",
1: "Quest",
2: "Combat",
3: "Magic",
4: "Crafting",
5: "Crime",
6: "DLC Stats",
}
class Stat(object):
def __init__(self, f):
self.name = unpack("wstring", f)
self.category = unpack("uint8", f)
self.category_name = _stat_categories[self.category]
self.value = unpack("uint32", f)
def __repr__(self):
return "Stat<{}({}):{} = {}>".format(self.category_name,
self.category,
self.name,
self.value)
def read_miscStats(f):
count = unpack("uint32", f)
return [Stat(f) for i in range(count)]
_types["miscStat"] = read_miscStats
#%% Form ID
class FormID(object):
def __init__(self, f):
self.name = unpack("wstring", f)
self.category = unpack("uint8", f)
self.category_name = _stat_categories[self.category]
self.value = unpack("uint32", f)
def __repr__(self):
return "FormID<>".format()
#def read_formIDs(f):
# count = unpack("uint32", f)
# return [Stat(f) for i in range(count)]
_types["FormID"] = FormID
#%% Created objects
class EnchInfo(object):
def __init__(self, f):
self.magnitude = unpack("float", f)
self.duration = unpack("uint32", f)
self.area = unpack("uint32", f)
def __repr__(self):
return "EnchInfo<mag={}, dur={}, area={}>".format(self.magnitude,
self.duration,
self.area)
_types["EnchInfo"] = EnchInfo
class MagicEffect(object):
def __init__(self, f):
self.refID = unpack("RefID", f)
self.info = unpack("EnchInfo", f)
self.price = unpack("float", f)
def __repr__(self):
return "MagicEffect<{}:{} = {}>".format(self.refID,
self.info,
self.price)
_types["MagicEffect"] = MagicEffect
class Enchantment(object):
def __init__(self, f):
self.refID = unpack("RefID", f)
RefID.createdid[self.refID.value] = self
self.timesUsed = unpack("uint32", f)
count = unpack("vsval", f)
self.effects = [unpack("MagicEffect", f) for i in range(count)]
def __repr__(self):
return "Enchantment<{:08x} x{}: {}>".format(self.refID.value,
self.timesUsed,
self.effects)
_types["Enchantment"] = Enchantment
def read_CreatedObjects(f):
weaponCount = unpack("vsval", f)
weapons = [Enchantment(f) for i in range(weaponCount)]
armourCount = unpack("vsval", f)
armours = [Enchantment(f) for i in range(armourCount)]
potionCount = unpack("vsval", f)
potions = [Enchantment(f) for i in range(potionCount)]
# from IPython import embed; embed()
poisonCount = unpack("vsval", f)
poisons = [Enchantment(f) for i in range(poisonCount)]
return {"weapons": weapons, "armours": armours, "potions": potions,
"poisons": poisons}
_types["CreatedObjects"] = read_CreatedObjects
#%% Ingredient Shared
def read_IngredientsShared(f):
count = unpack("uint32", f)
return [(unpack("RefID", f), unpack("RefID", f)) for i in range(count)]
#%% Global Data
_gdata_type_names = {
0: ("Misc Stats", read_miscStats),
1: ("Player Location", lambda f: "Not implemented"),
2: ("TES", lambda f: "Not implemented"),
3: ("Global Variables", lambda f: "Not implemented"),
4: ("Created Objects", read_CreatedObjects),
5: ("Effects", lambda f: "Not implemented"),
6: ("Weather", lambda f: "Not implemented"),
7: ("Audio", lambda f: "Not implemented"),
8: ("SkyCells", lambda f: "Not implemented"),
100: ("Process Lists", lambda f: "Not implemented"),
101: ("Combat", lambda f: "Not implemented"),
102: ("Interface", lambda f: "Not implemented"),
103: ("Actor Causes", lambda f: "Not implemented"),
104: ("Unknown 104", lambda f: "Not implemented"),
105: ("Detection Manager", lambda f: "Not implemented"),
106: ("Location MetaData", lambda f: "Not implemented"),
107: ("Quest Static Data", lambda f: "Not implemented"),
108: ("StoryTeller", lambda f: "Not implemented"),
109: ("Magic Favorites", lambda f: "Not implemented"),
110: ("PlayerControls", lambda f: "Not implemented"),
111: ("Story Event Manager", lambda f: "Not implemented"),
112: ("Ingredient Shared", read_IngredientsShared),
113: ("MenuControls", lambda f: "Not implemented"),
114: ("MenuTopicManager", lambda f: "Not implemented"),
1000: ("Temp Effects", lambda f: "Not implemented"),
1001: ("Papyrus", lambda f: "Not implemented"),
1002: ("Anim Objects", lambda f: "Not implemented"),
1003: ("Timer", lambda f: "Not implemented"),
1004: ("Synchronized Animations", lambda f: "Not implemented"),
1005: ("Main", lambda f: "Not implemented"),
}
def read_globalData(f):
type_ = unpack("uint32", f)
type_name, type_decoder = _gdata_type_names[type_]
length = unpack("uint32", f)
return (type_, type_name, type_decoder(BytesIO(f.read(length))))
_types["globalData"] = read_globalData
#%% Change Form
_ChangeForm_flags = {
"CHANGE_FORM_FLAGS": 0x01,
"CHANGE_REFR_MOVE": 0x02,
"CHANGE_REFR_HAVOK_MOVE": 0x04,
"CHANGE_REFR_CELL_CHANGED": 0x08,
"CHANGE_REFR_SCALE": 0x10,
"CHANGE_REFR_INVENTORY": 0x20,
"CHANGE_REFR_EXTRA_OWNERSHIP": 0x40,
"CHANGE_REFR_BASEOBJECT": 0x80,
"CHANGE_REFR_PROMOTED": 0x2000000,
"CHANGE_REFR_EXTRA_ACTIVATING_CHILDREN": 0x4000000,
"CHANGE_REFR_LEVELED_INVENTORY": 0x8000000,
"CHANGE_REFR_ANIMATION": 0x10000000,
"CHANGE_REFR_EXTRA_ENCOUNTER_ZONE": 0x20000000,
"CHANGE_REFR_EXTRA_CREATED_ONLY": 0x40000000,
"CHANGE_REFR_EXTRA_GAME_ONLY": 0x80000000,
"CHANGE_OBJECT_EXTRA_ITEM_DATA": 0x400,
"CHANGE_OBJECT_EXTRA_AMMO": 0x800,
"CHANGE_OBJECT_EXTRA_LOCK": 0x1000,
"CHANGE_DOOR_EXTRA_TELEPORT": 0x20000,
"CHANGE_OBJECT_EMPTY": 0x200000,
"CHANGE_OBJECT_OPEN_DEFAULT_STATE": 0x400000,
"CHANGE_OBJECT_OPEN_STATE": 0x800000,
}
extra_data_flags = (_ChangeForm_flags['CHANGE_REFR_EXTRA_OWNERSHIP'] |
_ChangeForm_flags['CHANGE_OBJECT_EXTRA_LOCK'] |
_ChangeForm_flags['CHANGE_REFR_EXTRA_ENCOUNTER_ZONE'] |
_ChangeForm_flags['CHANGE_REFR_EXTRA_GAME_ONLY'] |
_ChangeForm_flags['CHANGE_OBJECT_EXTRA_AMMO'] |
_ChangeForm_flags['CHANGE_DOOR_EXTRA_TELEPORT'] |
_ChangeForm_flags['CHANGE_REFR_PROMOTED'] |
_ChangeForm_flags['CHANGE_REFR_EXTRA_ACTIVATING_CHILDREN'] |
_ChangeForm_flags['CHANGE_OBJECT_EXTRA_ITEM_DATA'])
class ChangeForm(object):
def __init__(self, f):
self.formid = unpack("RefID", f)
self.changeFlags = unpack("uint32", f)
type_ = unpack("uint8", f)
sizeFlag = {0: "uint8", 1: "uint16", 2: "uint32"}[type_ >> 6]
self.type = type_ & 0b111111
self.version = unpack("uint8", f)
length1 = unpack(sizeFlag, f)
length2 = unpack(sizeFlag, f)
data = f.read(length1)
if length2 != 0:
data = zlib.decompress(data, 0, length2)
self.data = data
self.d = {}
if self.type == 0: # REFR
sdata = BytesIO(data)
if self.formid.value >= 0xFF000000:
initialType = 5 # No hits
elif self.changeFlags & (_ChangeForm_flags['CHANGE_REFR_PROMOTED'] |
_ChangeForm_flags['CHANGE_REFR_CELL_CHANGED']):
initialType = 6
elif self.changeFlags & (_ChangeForm_flags['CHANGE_REFR_HAVOK_MOVE'] |
_ChangeForm_flags['CHANGE_REFR_MOVE']):
initialType = 4 # No hits
else:
initialType = 0
self.d['initialType'] = initialType
if not (self.changeFlags & (_ChangeForm_flags['CHANGE_REFR_INVENTORY'] |
_ChangeForm_flags['CHANGE_REFR_LEVELED_INVENTORY'])):
return # TODO: not really interested right now
sdata.read({5: 31, 6: 34, 4: 27, 0:0}[initialType])
if self.changeFlags & _ChangeForm_flags['CHANGE_REFR_HAVOK_MOVE']:
hmcount = unpack("vsval", sdata)
self.d['hmcount'] = hmcount
self.d['hmdata'] = sdata.read(hmcount)
if self.changeFlags & _ChangeForm_flags['CHANGE_FORM_FLAGS']:
self.d['flag'] = unpack("uint32", sdata)
self.d['flagdata'] = unpack("uint16", sdata)
if self.changeFlags & _ChangeForm_flags['CHANGE_REFR_BASEOBJECT']:
self.d['baseobject'] = unpack("RefID", sdata)
if self.changeFlags & _ChangeForm_flags['CHANGE_REFR_SCALE']:
self.d['scale'] = unpack("float", sdata)
if self.changeFlags & (_ChangeForm_flags['CHANGE_REFR_INVENTORY'] |
_ChangeForm_flags['CHANGE_REFR_LEVELED_INVENTORY']):
if self.changeFlags & extra_data_flags:
self.d['extraData'] = unpack("ExtraData", sdata)
invcount = unpack("vsval", sdata)
# cf.items = [unpack("InventoryItem", sdata) for i in range(invcount)]
self.d['inventory'] = []
for i in range(invcount):
inv_item = unpack("InventoryItem", sdata)
if inv_item.item.value == 0x000001F4:
continue # Skip "Unarmed" item, since it's not an item
self.d['inventory'].append(inv_item)
# Skip Animation
# Skip Explosion
elif self.type == 1 and self.formid.value == 0x14: # Player ACHR
sdata = BytesIO(self.data)
if self.formid.value >= 0xFF000000:
initialType = 5 # No hits
elif self.changeFlags & (_ChangeForm_flags['CHANGE_REFR_PROMOTED'] |
_ChangeForm_flags['CHANGE_REFR_CELL_CHANGED']):
initialType = 6
elif self.changeFlags & (_ChangeForm_flags['CHANGE_REFR_HAVOK_MOVE'] |
_ChangeForm_flags['CHANGE_REFR_MOVE']):
initialType = 4 # No hits
else:
initialType = 0
self.d['initialType'] = initialType
if not (self.changeFlags & (_ChangeForm_flags['CHANGE_REFR_INVENTORY'] |
_ChangeForm_flags['CHANGE_REFR_LEVELED_INVENTORY'])):
return # TODO: not really interested right now
sdata.read({5: 31, 6: 34, 4: 27, 0:0}[initialType])
if self.changeFlags & _ChangeForm_flags['CHANGE_REFR_HAVOK_MOVE']:
hmcount = unpack("vsval", sdata)
self.d['hmcount'] = hmcount
self.d['hmdata'] = sdata.read(hmcount)
if self.changeFlags & _ChangeForm_flags['CHANGE_FORM_FLAGS']:
self.d['flag'] = unpack("uint32", sdata)
self.d['flagdata'] = unpack("uint16", sdata)
if self.changeFlags & _ChangeForm_flags['CHANGE_REFR_BASEOBJECT']:
self.d['baseobject'] = unpack("RefID", sdata)
if self.changeFlags & _ChangeForm_flags['CHANGE_REFR_SCALE']:
self.d['scale'] = unpack("float", sdata)
if self.changeFlags & (_ChangeForm_flags['CHANGE_REFR_INVENTORY'] |
_ChangeForm_flags['CHANGE_REFR_LEVELED_INVENTORY']):
sdata.read(8) # Unknown
if self.changeFlags & extra_data_flags:
self.d['extraData'] = unpack("ExtraData", sdata)
invcount = unpack("vsval", sdata)
self.d['inventory'] = []
for i in range(invcount):
inv_item = unpack("InventoryItem", sdata)
if inv_item.item.value == 0x000001F4:
continue # Skip "Unarmed" item, since it's not an item
self.d['inventory'].append(inv_item)
elif self.type == 16: # INGR
self.d['ingr_data'] = unpack("uint32", data)
def __repr__(self):
return "ChangeForm<{}>".format(self.formid)
_types["ChangeForm"] = ChangeForm
#%% Inventory
_dataTypeNames = {
22: "Worn",
23: "WornLeft",
24: "PackageStartLocation",
25: "Package",
26: "TresPassPackage",
27: "RunOncePacks",
28: "ReferenceHandle",
29: "unknown29",
30: "LevCreaModifier",
31: "Ghost",
33: "Ownership",
34: "Global",
35: "Rank",
36: "Count",
37: "Health",
39: "TimeLeft",
40: "Charge",
42: "Lock",
43: "Teleport",
44: "MapMarker",
45: "LeveledCreature",
46: "LeveledItem",
47: "Scale",
49: "NonActorMagicCaster",
50: "NonActorMagicTarget",
52: "PlayerCrimeList",
56: "ItemDropper",
61: "CannotWear",
62: "ExtraPoison",
68: "FriendHits",
69: "HeadingTarget",
72: "StartingWorldOrCell",
73: "Hotkey",
76: "InfoGeneralTopic",
77: "HasNoRumors",
79: "TerminalState",
83: "unknown83",
84: "CanTalkToPlayer",
85: "ObjectHealth",
88: "ModelSwap",
89: "Radius",
91: "FactionChanges",
92: "DismemberedLimbs",
93: "ActorCause",
101: "CombatStyle",
104: "OpenCloseActivateRef",
106: "Ammo",
108: "PackageData",
111: "SayTopicInfoOnceADay",
112: "EncounterZone",
113: "SayToTopicInfo",
120: "GuardedRefData",
133: "AshPileRef",
135: "FollowerSwimBreadcrumbs",
136: "AliasInstanceArray",
140: "PromotedRef",
142: "OutfitItem",
146: "SceneData",
149: "FromAlias",
150: "ShouldWear",
152: "AttachedArrows3D",
153: "TextDisplayData",
155: "Enchantment",
156: "Soul",
157: "ForcedTarget",
159: "UniqueID",
160: "Flags",
161: "RefrPath",
164: "ForcedLandingMarker",
169: "Interaction",
174: "GroupConstraint",
175: "ScriptedAnimDependence",
176: "CachedScale",
}
class MagicTarget(object):
def __init__(self, f):
self.ref = unpack("RefID", f)
unpack("uint8", f)
unpack("vsval", f)
count = unpack("RefID", f)
self.data = [unpack("uint8", f) for i in range(count)]
def __repr__(self):
return "MagicTarget<{}>".format()
_types["MagicTarget"] = MagicTarget
class MagicCaster(object):
def __init__(self, f):
unpack("uint32", f)
self.dataref = unpack("RefID", f)
unpack("uint32", f)
unpack("uint32", f)
self.dataref2 = unpack("RefID", f)
unpack("float", f)
self.ref = unpack("RefID", f)
self.ref2 = unpack("RefID", f)
def __repr__(self):
return "MagicCaster<{}>".format()
_types["MagicCaster"] = MagicCaster
class AttachedArrows3DData(object):
def __init__(self, f):
self.ref = unpack("RefID", f)
if self.ref.value != 0:
self.unknU16 = unpack("uint16", f)
if self.unknU16 != 0xFFFF:
self.unk2 = unpack("uint32", f)
self.unks = [unpack("float", f) for i in range(8)]
def __repr__(self):
return "AttachedArrows3DData<>".format()
_types["AttachedArrows3DData"] = AttachedArrows3DData
class AttachedArrows3D(object):
def __init__(self, f):
count = unpack("vsval", f)
self.var = [unpack("AttachedArrows3DData", f) for i in range(count)]
unpack("uint16", f)
unpack("uint16", f)
def __repr__(self):
return "AttachedArrows3D<>".format()
_types["AttachedArrows3D"] = AttachedArrows3D
class ExtraDataType(object):
def __init__(self, f):
type_ = unpack("uint8", f)
self.type = type_
self.typeName = _dataTypeNames[type_]
if type_ == 22:
pass
elif type_ == 23:
pass
elif type_ == 24:
self.data = [unpack("RefID", f), unpack("float", f),
unpack("float", f), unpack("float", f),
unpack("float", f)]
elif type_ == 25:
self.data = [unpack("RefID", f), unpack("RefID", f),
unpack("uint32", f), unpack("uint8", f),
unpack("uint8", f), unpack("uint8", f)]
elif type_ == 26:
self.data = [unpack("RefID", f)]
if self.data[0].value != 0:
raise NotImplementedError("There's more unknown data")
elif type_ == 27:
count = unpack("vsval", f)
self.data = [(unpack("RefID", f), unpack("uint8", f)) for i in range(count)]
elif type_ == 28:
self.data = [unpack("RefID", f)]
elif type_ == 29:
pass
elif type_ == 30:
self.data = [unpack("uint32", f)]
elif type_ == 31:
self.data = [unpack("uint8", f)]
elif type_ == 32:
self.data = [unpack("RefID", f)]
elif type_ == 33:
self.data = [unpack("RefID", f)]
elif type_ == 34:
self.data = [unpack("RefID", f)]
elif type_ == 35:
self.data = [unpack("RefID", f)]
elif type_ == 36:
self.data = [unpack("uint16", f)]
elif type_ == 37:
self.data = [unpack("float", f)]
elif type_ == 39:
self.data = [unpack("uint32", f)]
elif type_ == 40:
self.data = [unpack("float", f)]
elif type_ == 42:
self.data = [unpack("uint8", f), unpack("uint8", f),
unpack("RefID", f), unpack("uint32", f),
unpack("uint32", f)]
elif type_ == 43:
self.data = [unpack("float", f) for i in range(6)]
self.data.extend([unpack("uint8", f), unpack("RefID", f)])
elif type_ == 44:
self.data = [unpack("uint8", f)]
elif type_ == 45:
raise NotImplementedError("Too big for now") # TODO: fix
elif type_ == 46:
self.data = [unpack("uint32", f), unpack("uint8", f)]
elif type_ == 47:
self.data = [unpack("float", f)]
elif type_ == 49:
self.data = [unpack("MagicCaster", f)]
elif type_ == 50:
self.data = [unpack("RefID", f)]
count = unpack("vsval", f)
self.data.extend([unpack("MagicTarget", f) for i in range(count)])
elif type_ == 52:
count = unpack("vsval", f)
self.data = [(unpack("uint32", f), unpack("uint32", f)) for i in
range(count)]
elif type_ == 56:
self.data = [unpack("RefID", f)]
elif type_ == 62:
self.data = [unpack("RefID", f), unpack("uint32", f)]
elif type_ == 68:
count = unpack("vsval", f)
self.data = [unpack("float", f) for i in range(count)]
elif type_ == 69:
self.data = [unpack("RefID", f)]
elif type_ == 72:
self.data = [unpack("RefID", f)]
elif type_ == 73:
self.data = [unpack("uint8", f)]
elif type_ == 76:
self.data = [unpack("wstring", f)]
self.data.extend([unpack("uint8", f) for i in range(5)])
self.data.extend([unpack("RefID", f) for i in range(4)])
elif type_ == 77:
self.data = [unpack("uint8", f)]
elif type_ == 79:
self.data = [unpack("uint8", f), unpack("uint8", f)]
elif type_ == 83:
self.data = [unpack("uint32", f)]
elif type_ == 84:
self.data = [unpack("uint8", f)]
elif type_ == 85:
self.data = [unpack("float", f)]
elif type_ == 88:
self.data = [unpack("RefID", f), unpack("uint32", f)]
elif type_ == 89:
self.data = [unpack("uint32", f)]
elif type_ == 91:
count = unpack("vsval", f)
self.data = [(unpack("RefID", f), unpack("int8", f)) for i in range(count)]
self.data.append(unpack("RefID", f))
self.data.append(unpack("int8", f))
elif type_ == 92:
raise NotImplementedError("Too big for now") # TODO: fix
elif type_ == 93:
self.data = [unpack("uint32", f)]
elif type_ == 101:
self.data = [unpack("RefID", f)]
elif type_ == 104:
self.data = [unpack("RefID", f)]
elif type_ == 106:
self.data = [unpack("RefID", f), unpack("uint32", f)]
elif type_ == 108:
self.data = [unpack("uint8", f)]
if self.data != -1:
raise NotImplementedError("There's more unknown data")
elif type_ == 111:
count = unpack("vsval", f)
self.data = [(unpack("RefID", f), unpack("uint32", f),
unpack("uint32", f)) for i in range(count)]
elif type_ == 112:
self.data = [unpack("RefID", f)]
elif type_ == 113:
raise NotImplementedError("Too big for now") # TODO: fix
elif type_ == 120:
count = unpack("vsval", f)
self.data = [(unpack("RefID", f), unpack("uint32", f),
unpack("uint8", f)) for i in range(count)]
elif type_ == 133:
self.data = [unpack("RefID", f)]
elif type_ == 135:
self.data = [unpack("float", f), unpack("float", f),
unpack("float", f), unpack("RefID", f),
unpack("uint32", f)]
count = unpack("vsval", f)
self.data.extend([(unpack("float", f), unpack("float", f),
unpack("float", f), unpack("RefID", f),
unpack("float", f), unpack("float", f),
unpack("float", f), unpack("RefID", f),
unpack("uint8", f)) for i in range(count)])
elif type_ == 136:
count = unpack("vsval", f)
self.data = [(unpack("RefID", f), unpack("uint32", f)) for i in range(count)]
elif type_ == 140:
count = unpack("vsval", f)
self.data = [unpack("RefID", f) for i in range(count)]
elif type_ == 142:
self.data = [unpack("RefID", f)]
elif type_ == 146:
self.data = [unpack("RefID", f)]
elif type_ == 149:
self.data = [unpack("RefID", f), unpack("uint32", f)]
elif type_ == 150:
self.data = [unpack("uint8", f)]
elif type_ == 152:
self.data = [unpack("AttachedArrows3D", f)]
elif type_ == 153:
self.data = [unpack("RefID", f), unpack("RefID", f),
unpack("int32", f)]
if (self.data[2] == -2 and self.data[0].value == 0 and
self.data[1].value == 0):
self.data.append(unpack("wstring", f))
elif type_ == 155:
self.data = [unpack("RefID", f), unpack("uint16", f)]
elif type_ == 156:
self.data = [unpack("uint8", f)]
elif type_ == 157:
self.data = [unpack("RefID", f)]
elif type_ == 159:
self.data = [unpack("uint32", f), unpack("uint16", f)]
elif type_ == 160:
self.data = [unpack("uint32", f)]
elif type_ == 161:
self.data = [unpack("float", f) for i in range(3*6)]
self.data.extend([unpack("uint32", f) for i in range(4)])
elif type_ == 164:
self.data = [unpack("RefID", f)]
elif type_ == 169:
self.data = [unpack("uint32", f), unpack("RefID", f),
unpack("RefID", f), unpack("uint8", f)]
elif type_ == 174:
raise NotImplementedError("Too big for now") # TODO: fix
elif type_ == 175:
self.data = [unpack("RefID", f)]
elif type_ == 176:
self.data = [unpack("RefID", f)]
else:
raise RuntimeError("Shouldn't get here! ExtraDataType = %d" % type_)
# self.changeFlags = unpack("uint32", f)
def __repr__(self):
return "ExtraDataType<{}:{}>".format(self.type, self.typeName)
_types["ExtraDataType"] = ExtraDataType
class ExtraData(object):
def __init__(self, f):
self.count = unpack("vsval", f)
self.data = [unpack("ExtraDataType", f) for i in range(self.count)]
def __repr__(self):
return "ExtraData<>".format()
_types["ExtraData"] = ExtraData
class InventoryItem(object):
def __init__(self, f):
self.item = unpack("RefID", f)
self.itemcount = unpack("int32", f)
extracount = unpack("vsval", f)
self.extraData = [unpack("ExtraData", f) for i in range(extracount)]
# print "Position", f.tell()
def __repr__(self):
return "InventoryItem<{}x {}>".format(self.itemcount,
self.item)
_types["InventoryItem"] = InventoryItem
#%% Field
class Field(object):
def __init__(self, f):
self.type = f.read(4).decode()
size = unpack("uint16", f)
self.data = f.read(size)
def __repr__(self):
return "Field<{}>".format(self.type)
_types["Field"] = Field
#%% Record
class Record(object):
def __init__(self, fd, type_):
self.type = type_
dataSize = unpack("uint32", fd)
# if type_ == "GRUP":
# self.label = fd.read(4).decode("cp1252")
# self.groupType = unpack("int32", fd)
# self.stamp = unpack("uint16", fd)
# unpack("uint16", fd) # Unknown
# self.version = unpack("uint16", fd)
# unpack("uint16", fd) # Unknown
## children = []
## group_end = fd.tell() + self.size - 24
## while fd.tell() < group_end:
## type_ = f.read(4).decode("cp1252")
## children.append(Record(fd, type_))
## self.children = children
## self.data = fd.read(self.size - 24)
# if self.label in _read_record_types:
# data = BytesIO(fd.read(self.size - 24))
# else: # skip
# fd.seek(fd.tell() + self.size - 24)
# return
self.flags = unpack("uint32", fd)
self.id = unpack("uint32", fd)
self.revision = unpack("uint32", fd)
self.version = unpack("uint16", fd)
unpack("uint16", fd) # Unknown
if self.flags & 0x00040000: # Data is compressed
decompSize = unpack("uint32", fd)
compData = fd.read(dataSize - 4)
data = zlib.decompress(compData, 0, decompSize)
dataSize = decompSize
else:
data = fd.read(dataSize)
data = BytesIO(data)
fields = []
while data.tell() < dataSize:
fields.append(Field(data))
self.fields = fields
def __repr__(self):
if self.type == "GRUP":
return "{}:{}".format(self.type, self.label)
return self.type
class Effect(object):
def __init__(self, id_):
self.EffectID = id_
self.Magnitude = 0
self.AreaOfEffect = 0
self.Duration = 0
@property
def MGEF(self):
return db['MGEF'][self.EffectID]
@property
def Value(self):
try:
return math.floor(self.MGEF.BaseCost *
((self.Magnitude if self.Magnitude > 1 else 1)
* (self.Duration if self.Duration != 0 else 10)
/ 10) ** 1.1)
except:
return -1
@property
def Description(self):
return self.MGEF.Description.format(mag=self.Magnitude, dur=self.Duration)
def __repr__(self):
return "Effect<{}, {}>".format(self.MGEF.FullName, self.Description)
_types["Effect"] = Effect
class INGR(Record):
def __init__(self, fd, type_="INGR"):
super(INGR, self).__init__(fd, type_)
self.effects = []
self.FullName = "Nameless"
for field in self.fields:
if field.type == "EDID":
self.EditorID = unpack("zstring", field.data)
elif field.type == "FULL":
self.FullName = unpack("lstring", field.data)
elif field.type == "DATA":
self.Value = unpack("uint32", field.data[:4])
self.Weight = unpack("float", field.data[4:])
elif field.type == "EFID":
self.effects.append(Effect(unpack("formid", field.data)))
elif field.type == "EFIT":
last_effect = self.effects[-1]
last_effect.Magnitude = unpack("float", field.data[:4])
last_effect.AreaOfEffect = unpack("uint32", field.data[4:8])
last_effect.Duration = unpack("uint32", field.data[8:])
if self.id == 0x3ad5d: import pdb; pdb.set_trace()
db['INGR'][self.id] = self
def __repr__(self):
return "INGR<{:08X}:{}>".format(self.id, self.FullName)
_types["INGR"] = INGR
class MGEF(Record):
"""Magic Effect.
Data about magic effects from spells, enchantments and potions.
Reference:
http://en.m.uesp.net/wiki/Tes5Mod:Mod_File_Format/MGEF
"""
def __init__(self, fd, type_="MGEF"):
super(MGEF, self).__init__(fd, type_)
self.FullName = "Unnamed"
for field in self.fields:
if field.type == "EDID":
self.EditorID = unpack("zstring", field.data)
elif field.type == "FULL":
if self.id < 0x01000000:
self.FullName = unpack("lstring", field.data)
else:
# TODO: find where DLC strings are
self.FullName = "DLC string: {}".format(unpack('formid', field.data))
elif field.type == "KSIZ":
ksiz = unpack("uint32", field.data)
elif field.type == "KWDA":
sdata = BytesIO(field.data)
self.KWDA = [unpack("formid", sdata) for i in range(ksiz)]
elif field.type == "DATA":
fdata = BytesIO(field.data)
self.Flags = unpack("uint32", fdata)
self.BaseCost = unpack("float", fdata)
self.RelatedID = unpack("formid", fdata)
self.Skill = unpack("int32", fdata)
self.ResistanceAV = unpack("uint32", fdata)
fdata.read(16)
self.SkillLevel = unpack("uint32", fdata)
self.Area = unpack("uint32", fdata)
self.CastingTime = unpack("float", fdata)
fdata.read(12)
self.EffectType = unpack("uint32", fdata)
self.PrimaryAV = unpack("int32", fdata)
elif field.type == "ESCE":
self.CounterEffects = unpack("formid", field.data)
elif field.type == "DNAM":
desc = unpack("lstring", field.data)
self.Description = desc.translate({ord(c): ord(t) for c, t in
[('<','{'), ('>', '}')]})
db['MGEF'][self.id] = self
@property
def MGEFflags(self):
return MGEFflags(self.Flags)
@property
def alch_type(self):
# 0x1 = Hostile, 0x4 = Detrimental
return {True: "poison", False: "potion"}[bool(self.Flags & (0x1 | 0x4))]
def __repr__(self):
return "MGEF<{:08X}:{}>".format(self.id, self.FullName)
_types["MGEF"] = MGEF
class ALCH(Record):
def __init__(self, fd, type_="ALCH"):
super(ALCH, self).__init__(fd, type_)
self.effects = []
self.FullName = "Unnamed"
for field in self.fields:
if field.type == "EDID":
self.EditorID = unpack("zstring", field.data)
elif field.type == "FULL":
self.FullName = unpack("lstring", field.data)
elif field.type == "DATA":
self.Weight = unpack("float", field.data)
elif field.type == "ENIT":
self.Value = unpack("uint32", field.data[:4])
flags = unpack("uint32", field.data[4:8])
flag_bits = {0x1: "ManualCalc", 0x2: "Food",
0x10000: "Medicine", 0x20000: "Poison"}
self.Flags = [v for k, v in flag_bits.items() if k & flags]
elif field.type == "EFID":
self.effects.append(Effect(unpack("formid", field.data)))
elif field.type == "EFIT":
last_effect = self.effects[-1]
last_effect.Magnitude = unpack("float", field.data[:4])
last_effect.AreaOfEffect = unpack("uint32", field.data[4:8])
last_effect.Duration = unpack("uint32", field.data[8:])
db['ALCH'][self.id] = self
def __repr__(self):
return "ALCH<{:08X}:{}>".format(self.id, self.FullName)
_types["ALCH"] = ALCH
class EnchantedItem(object):
def __init__(self, f):
self.Value = unpack("uint32", f)
flags = unpack("uint32", f)
flag_bits = {0x1: "ManualCalc", 0x4: "ExtendDurationOnRecast"}
self.flags = [v for k, v in flag_bits.items() if k & flags]
self.CastType = {0x00: "Constant Effect", 0x01: "Fire and Forget",
0x02: "Concentration"}[unpack("uint32", f)]
self.enchAmount = unpack("uint32", f)
self.delivery = {0x00: "Self", 0x01: "Touch", 0x02: "Aimed",
0x03: "Target Actor", 0x04: "Target Location"
}[unpack("uint32", f)]
self.EnchantType = {0x06: "Enchantment", 0x0C: "Staff Enchantment"
}[unpack("uint32", f)]
self.ChargeTime = unpack("float", f)
self.BaseEnchantment = unpack("formid", f)
def __repr__(self):
return "EnchantedItem<>".format()
_types["EnchantedItem"] = EnchantedItem
class ENCH(Record):
def __init__(self, fd, type_="ENCH"):
super(ENCH, self).__init__(fd, type_)
self.effects = []
self.FullName = "Unnamed"
for field in self.fields:
if field.type == "EDID":
self.EditorID = unpack("zstring", field.data)
elif field.type == "FULL":
self.FullName = unpack("lstring", field.data)
elif field.type == "ENIT":
self.ArmorRating = unpack("EnchantedItem", BytesIO(field.data))
elif field.type == "EFID":
self.effects.append(Effect(unpack("formid", field.data)))
elif field.type == "EFIT":
last_effect = self.effects[-1]
last_effect.Magnitude = unpack("float", field.data[:4])
last_effect.AreaOfEffect = unpack("uint32", field.data[4:8])
last_effect.Duration = unpack("uint32", field.data[8:])
db['ENCH'][self.id] = self
@property
def Value(self):
return sum([ef.Value for ef in self.effects])
def __repr__(self):
return "ENCH<{:08X}:{}>".format(self.id, self.FullName)
_types["ENCH"] = ENCH
class ARMO(Record):
def __init__(self, fd, type_="ARMO"):
super(ARMO, self).__init__(fd, type_)
self.FullName = "Unnamed"
self.enchantment_id = 0
for field in self.fields:
if field.type == "EDID":
self.EditorID = unpack("zstring", field.data)
elif field.type == "FULL":
self.FullName = unpack("lstring", field.data)
elif field.type == "EITM":
self.enchantment_id = unpack("formid", field.data)