espers 0.1.0

Library for reading ESP and ESM files
Documentation
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
pub mod aact;
pub mod achr;
pub mod acti;
pub mod addn;
pub mod alch;
pub mod ammo;
pub mod anio;
pub mod appa;
pub mod arma;
pub mod armo;
pub mod arto;
pub mod aspc;
pub mod astp;
pub mod avif;
pub mod book;
pub mod bptd;
pub mod cams;
pub mod cell;
pub mod clas;
pub mod clfm;
pub mod clmt;
pub mod cobj;
pub mod coll;
pub mod cont;
pub mod cpth;
pub mod csty;
pub mod debr;
pub mod dial;
pub mod dlbr;
pub mod dlvw;
pub mod dobj;
pub mod door;
pub mod dual;
pub mod eczn;
pub mod efsh;
pub mod ench;
pub mod equp;
pub mod expl;
pub mod eyes;
pub mod fact;
pub mod flor;
pub mod flst;
pub mod fstp;
pub mod fsts;
pub mod furn;
pub mod glob;
pub mod gmst;
pub mod gras;
pub mod grup;
pub mod hazd;
pub mod hdpt;
pub mod idle;
pub mod idlm;
pub mod imad;
pub mod imgs;
pub mod info;
pub mod ingr;
pub mod ipct;
pub mod ipds;
pub mod keym;
pub mod kywd;
pub mod land;
pub mod lcrt;
pub mod lctn;
pub mod lgtm;
pub mod ligh;
pub mod lscr;
pub mod ltex;
pub mod lvli;
pub mod lvln;
pub mod lvsp;
pub mod mato;
pub mod matt;
pub mod mesg;
pub mod mgef;
pub mod misc;
pub mod movt;
pub mod mstt;
pub mod musc;
pub mod must;
pub mod navi;
pub mod navm;
pub mod note;
pub mod npc_;
pub mod otft;
pub mod pack;
pub mod perk;
pub mod pgre;
pub mod phzd;
pub mod proj;
pub mod qust;
pub mod race;
pub mod refr;
pub mod regn;
pub mod rela;
pub mod revb;
pub mod rfct;
pub mod scen;
pub mod scrl;
pub mod shou;
pub mod slgm;
pub mod smbn;
pub mod smen;
pub mod smqn;
pub mod snct;
pub mod sndr;
pub mod sopm;
pub mod soun;
pub mod spel;
pub mod spgd;
pub mod stat;
pub mod tact;
pub mod tes4;
pub mod tree;
pub mod txst;
pub mod vtyp;
pub mod watr;
pub mod weap;
pub mod woop;
pub mod wrld;
pub mod wthr;

pub use aact::{Action, AACT};
pub use achr::{ActorRef, ACHR};
pub use acti::{Activator, ACTI};
pub use addn::{AddonNode, ADDN};
pub use alch::{Alchemy, ALCH};
pub use ammo::{Ammo, AMMO};
pub use anio::{AnimatedObjectInfo, ANIO};
pub use appa::{Apparatus, APPA};
pub use arma::{ArmorAddon, ARMA};
pub use armo::{Armor, ARMO};
pub use arto::{ArtObject, ARTO};
pub use aspc::{AcousticSpace, ASPC};
pub use astp::{AssociationType, ASTP};
pub use avif::{ActorValue, AVIF};
pub use book::{Book, BOOK};
pub use bptd::{BodyPartData, BPTD};
pub use cams::{CameraShot, CAMS};
pub use cell::{Cell, CELL};
pub use clas::{Class, CLAS};
pub use clfm::{Color, CLFM};
pub use clmt::{Climate, CLMT};
pub use cobj::{ConstructibleObj, COBJ};
pub use coll::{CollisionLayer, COLL};
pub use cont::{Container, CONT};
pub use cpth::{CameraPath, CPTH};
pub use csty::{CombatStyle, CSTY};
pub use debr::{Debris, DEBR};
pub use dial::{DialogueTopic, DIAL};
pub use dlbr::{DialogueBranch, DLBR};
pub use dlvw::{DialogueView, DLVW};
pub use dobj::{DefaultObjectManager, DOBJ};
pub use door::{Door, DOOR};
pub use dual::{DualCastData, DUAL};
pub use eczn::{EncounterZone, ECZN};
pub use efsh::{EffectShader, EFSH};
pub use ench::{Enchantment, ENCH};
pub use equp::{EquipSlot, EQUP};
pub use expl::{Explosion, EXPL};
pub use eyes::{Eyes, EYES};
pub use fact::{Faction, FACT};
pub use flor::{Flora, FLOR};
pub use flst::{FormList, FLST};
pub use fstp::{Footstep, FSTP};
pub use fsts::{FootstepSet, FSTS};
pub use furn::{Furniture, FURN};
pub use glob::{GlobalVariable, GLOB};
pub use gmst::{GameSetting, GMST};
pub use gras::{Grass, GRAS};
pub use grup::{Group, GRUP};
pub use hazd::{Hazard, HAZD};
pub use hdpt::{HeadPart, HDPT};
pub use idle::{IdleAnimation, IDLE};
pub use idlm::{IdleMarker, IDLM};
pub use imad::{ImageSpaceModifier, IMAD};
pub use imgs::{ImageSpace, IMGS};
pub use info::{DialogueTopicInfo, INFO};
pub use ingr::{Ingredient, INGR};
pub use ipct::{ImpactData, IPCT};
pub use ipds::{ImpactDataSet, IPDS};
pub use keym::{Key, KEYM};
pub use kywd::{Keyword, KYWD};
pub use land::{Landscape, LAND};
pub use lcrt::{LocationRef, LCRT};
pub use lctn::{Location, LCTN};
pub use lgtm::{LightingTemplate, LGTM};
pub use ligh::{Light, LIGH};
pub use lscr::{LoadScreen, LSCR};
pub use ltex::{LandTexture, LTEX};
pub use lvli::{LeveledItem, LVLI};
pub use lvln::{LeveledActor, LVLN};
pub use lvsp::{LeveledSpell, LVSP};
pub use mato::{MaterialObject, MATO};
pub use matt::{MaterialType, MATT};
pub use mesg::{Message, MESG};
pub use mgef::{MagicEffect, MGEF};
pub use misc::{MiscItem, MISC};
pub use movt::{MovementType, MOVT};
pub use mstt::{MovableStatic, MSTT};
pub use musc::{MusicType, MUSC};
pub use must::{MusicTrack, MUST};
pub use navi::{Navigation, NAVI};
pub use navm::{NavMesh, NAVM};
pub use note::{Note, NOTE};
pub use npc_::{NPC, NPC_};
pub use otft::{Outfit, OTFT};
pub use pack::{AIPackage, PACK};
pub use perk::{Perk, PERK};
pub use pgre::{Placedgrenade, PGRE};
pub use phzd::{Placedhazard, PHZD};
pub use proj::{Projectile, PROJ};
pub use qust::{Quest, QUST};
pub use race::{Race, RACE};
pub use refr::{ObjectRef, REFR};
pub use regn::{Region, REGN};
pub use rela::{Relationship, RELA};
pub use revb::{ReverbParameters, REVB};
pub use rfct::{VisualEffect, RFCT};
pub use scen::{Scene, SCEN};
pub use scrl::{Scroll, SCRL};
pub use shou::{Shout, SHOU};
pub use slgm::{SoulGem, SLGM};
pub use smbn::{StoryManagerBranchNode, SMBN};
pub use smen::{StoryManagerEventNode, SMEN};
pub use smqn::{StoryManagerQuestNode, SMQN};
pub use snct::{SoundCategory, SNCT};
pub use sndr::{SoundReference, SNDR};
pub use sopm::{SoundOutputModel, SOPM};
pub use soun::{Sound, SOUN};
pub use spel::{Spell, SPEL};
pub use spgd::{ShaderParticleGeometry, SPGD};
pub use stat::{Static, STAT};
pub use tact::{TalkingActivator, TACT};
pub use tes4::{Header, TES4Flags, TES4};
pub use tree::{Tree, TREE};
pub use txst::{TextureSet, TXST};
pub use vtyp::{VoiceType, VTYP};
pub use watr::{WaterType, WATR};
pub use weap::{Weapon, WEAP};
pub use woop::{WordOfPower, WOOP};
pub use wrld::{World, WRLD};
pub use wthr::{Weather, WTHR};

use crate::error::Error;
use crate::string_table::StringTables;

use binrw::binrw;
use bitflags::bitflags;
use flate2::read::ZlibDecoder;
use serde_derive::{Deserialize, Serialize};
use std::fmt;
use std::io::{Cursor, Read};

bitflags! {
    #[binrw]
    #[derive(Serialize, Deserialize)]
    pub struct Flags: u32 {
        const MASTER = 0x00000001;
        const DELETED_GROUP = 0x00000010;
        const DELETED_RECORD = 0x00000010;
        const LOCALIZED = 0x00000080;
        const LIGHT_MASTER = 0x00000200;
        const COMPRESSED = 0x00040000;
    }
}

#[binrw]
#[brw(little)]
#[br(import(localized: bool))]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum RawRecord {
    AACT(AACT),
    ACHR(ACHR),
    ACTI(ACTI),
    ADDN(ADDN),
    ALCH(ALCH),
    AMMO(AMMO),
    ANIO(ANIO),
    APPA(APPA),
    ARMA(ARMA),
    ARMO(ARMO),
    ARTO(ARTO),
    ASPC(ASPC),
    ASTP(ASTP),
    AVIF(AVIF),
    BOOK(#[br(args(localized))] BOOK),
    BPTD(BPTD),
    CAMS(CAMS),
    CELL(CELL),
    CLAS(CLAS),
    CLFM(CLFM),
    CLMT(CLMT),
    COBJ(COBJ),
    COLL(COLL),
    CONT(CONT),
    CPTH(CPTH),
    CSTY(CSTY),
    DEBR(DEBR),
    DIAL(DIAL),
    DLBR(DLBR),
    DLVW(DLVW),
    DOBJ(DOBJ),
    DOOR(DOOR),
    DUAL(DUAL),
    ECZN(ECZN),
    EFSH(EFSH),
    ENCH(ENCH),
    EQUP(EQUP),
    EXPL(EXPL),
    EYES(EYES),
    FACT(FACT),
    FLOR(FLOR),
    FLST(FLST),
    FSTP(FSTP),
    FSTS(FSTS),
    FURN(FURN),
    GLOB(GLOB),
    GMST(GMST),
    GRAS(GRAS),
    GRUP(#[br(args(localized))] GRUP),
    HAZD(HAZD),
    HDPT(HDPT),
    IDLE(IDLE),
    IDLM(IDLM),
    IMAD(IMAD),
    IMGS(IMGS),
    INFO(INFO),
    INGR(INGR),
    IPCT(IPCT),
    IPDS(IPDS),
    KEYM(KEYM),
    KYWD(KYWD),
    LAND(LAND),
    LCRT(LCRT),
    LCTN(LCTN),
    LGTM(LGTM),
    LIGH(LIGH),
    LSCR(LSCR),
    LTEX(LTEX),
    LVLI(LVLI),
    LVLN(LVLN),
    LVSP(LVSP),
    MATO(MATO),
    MATT(MATT),
    MESG(MESG),
    MGEF(MGEF),
    MISC(MISC),
    MOVT(MOVT),
    MSTT(MSTT),
    MUSC(MUSC),
    MUST(MUST),
    NAVI(NAVI),
    NAVM(NAVM),
    NOTE(NOTE),
    NPC_(NPC_),
    OTFT(OTFT),
    PACK(PACK),
    PERK(PERK),
    PGRE(PGRE),
    PHZD(PHZD),
    PROJ(PROJ),
    QUST(QUST),
    RACE(RACE),
    REFR(REFR),
    REGN(REGN),
    RELA(RELA),
    REVB(REVB),
    RFCT(RFCT),
    SCEN(SCEN),
    SCRL(SCRL),
    SHOU(SHOU),
    SLGM(SLGM),
    SMBN(SMBN),
    SMEN(SMEN),
    SMQN(SMQN),
    SNCT(SNCT),
    SNDR(SNDR),
    SOPM(SOPM),
    SOUN(SOUN),
    SPEL(SPEL),
    SPGD(SPGD),
    STAT(STAT),
    TACT(TACT),
    TREE(TREE),
    TXST(TXST),
    VTYP(VTYP),
    WATR(WATR),
    WEAP(WEAP),
    WOOP(WOOP),
    WRLD(WRLD),
    WTHR(WTHR),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Record {
    AIPackage(AIPackage),
    AcousticSpace(AcousticSpace),
    Action(Action),
    Activator(Activator),
    ActorRef(ActorRef),
    ActorValue(ActorValue),
    AddonNode(AddonNode),
    Alchemy(Alchemy),
    Ammo(Ammo),
    AnimatedObjectInfo(AnimatedObjectInfo),
    Apparatus(Apparatus),
    Armor(Armor),
    ArmorAddon(ArmorAddon),
    ArtObject(ArtObject),
    AssociationType(AssociationType),
    BodyPartData(BodyPartData),
    Book(Book),
    CameraPath(CameraPath),
    CameraShot(CameraShot),
    Cell(Cell),
    Class(Class),
    Climate(Climate),
    CollisionLayer(CollisionLayer),
    Color(Color),
    CombatStyle(CombatStyle),
    ConstructibleObj(ConstructibleObj),
    Container(Container),
    Debris(Debris),
    DefaultObjectManager(DefaultObjectManager),
    DialogueBranch(DialogueBranch),
    DialogueTopic(DialogueTopic),
    DialogueTopicInfo(DialogueTopicInfo),
    DialogueView(DialogueView),
    Door(Door),
    DualCastData(DualCastData),
    EffectShader(EffectShader),
    Enchantment(Enchantment),
    EncounterZone(EncounterZone),
    EquipSlot(EquipSlot),
    Explosion(Explosion),
    Eyes(Eyes),
    Faction(Faction),
    Flora(Flora),
    Footstep(Footstep),
    FootstepSet(FootstepSet),
    FormList(FormList),
    Furniture(Furniture),
    GameSetting(GameSetting),
    GlobalVariable(GlobalVariable),
    Grass(Grass),
    Group(Group),
    Hazard(Hazard),
    HeadPart(HeadPart),
    IdleAnimation(IdleAnimation),
    IdleMarker(IdleMarker),
    ImageSpace(ImageSpace),
    ImageSpaceModifier(ImageSpaceModifier),
    ImpactData(ImpactData),
    ImpactDataSet(ImpactDataSet),
    Ingredient(Ingredient),
    Key(Key),
    Keyword(Keyword),
    LandTexture(LandTexture),
    Landscape(Landscape),
    LeveledActor(LeveledActor),
    LeveledItem(LeveledItem),
    LeveledSpell(LeveledSpell),
    Light(Light),
    LightingTemplate(LightingTemplate),
    LoadScreen(LoadScreen),
    Location(Location),
    LocationRef(LocationRef),
    MagicEffect(MagicEffect),
    MaterialObject(MaterialObject),
    MaterialType(MaterialType),
    Message(Message),
    MiscItem(MiscItem),
    MovableStatic(MovableStatic),
    MovementType(MovementType),
    MusicTrack(MusicTrack),
    MusicType(MusicType),
    NPC(NPC),
    NavMesh(NavMesh),
    Navigation(Navigation),
    Note(Note),
    ObjectRef(ObjectRef),
    Outfit(Outfit),
    Perk(Perk),
    Placedgrenade(Placedgrenade),
    Placedhazard(Placedhazard),
    Projectile(Projectile),
    Quest(Quest),
    Race(Race),
    Region(Region),
    Relationship(Relationship),
    ReverbParameters(ReverbParameters),
    Scene(Scene),
    Scroll(Scroll),
    ShaderParticleGeometry(ShaderParticleGeometry),
    Shout(Shout),
    SoulGem(SoulGem),
    Sound(Sound),
    SoundCategory(SoundCategory),
    SoundOutputModel(SoundOutputModel),
    SoundReference(SoundReference),
    Spell(Spell),
    Static(Static),
    StoryManagerBranchNode(StoryManagerBranchNode),
    StoryManagerEventNode(StoryManagerEventNode),
    StoryManagerQuestNode(StoryManagerQuestNode),
    TalkingActivator(TalkingActivator),
    TextureSet(TextureSet),
    Tree(Tree),
    VisualEffect(VisualEffect),
    VoiceType(VoiceType),
    WaterType(WaterType),
    Weapon(Weapon),
    Weather(Weather),
    WordOfPower(WordOfPower),
    World(World),
}

impl TryFrom<RawRecord> for Record {
    type Error = Error;

    fn try_from(r: RawRecord) -> Result<Self, Self::Error> {
        match r {
            RawRecord::AACT(x) => Ok(Record::Action(Action::try_from(x)?)),
            RawRecord::ACHR(x) => Ok(Record::ActorRef(ActorRef::try_from(x)?)),
            RawRecord::ACTI(x) => Ok(Record::Activator(Activator::try_from(x)?)),
            RawRecord::ADDN(x) => Ok(Record::AddonNode(AddonNode::try_from(x)?)),
            RawRecord::ALCH(x) => Ok(Record::Alchemy(Alchemy::try_from(x)?)),
            RawRecord::AMMO(x) => Ok(Record::Ammo(Ammo::try_from(x)?)),
            RawRecord::ANIO(x) => Ok(Record::AnimatedObjectInfo(AnimatedObjectInfo::try_from(x)?)),
            RawRecord::APPA(x) => Ok(Record::Apparatus(Apparatus::try_from(x)?)),
            RawRecord::ARMA(x) => Ok(Record::ArmorAddon(ArmorAddon::try_from(x)?)),
            RawRecord::ARMO(x) => Ok(Record::Armor(Armor::try_from(x)?)),
            RawRecord::ARTO(x) => Ok(Record::ArtObject(ArtObject::try_from(x)?)),
            RawRecord::ASPC(x) => Ok(Record::AcousticSpace(AcousticSpace::try_from(x)?)),
            RawRecord::ASTP(x) => Ok(Record::AssociationType(AssociationType::try_from(x)?)),
            RawRecord::AVIF(x) => Ok(Record::ActorValue(ActorValue::try_from(x)?)),
            RawRecord::BOOK(x) => Ok(Record::Book(Book::try_from(x)?)),
            RawRecord::BPTD(x) => Ok(Record::BodyPartData(BodyPartData::try_from(x)?)),
            RawRecord::CAMS(x) => Ok(Record::CameraShot(CameraShot::try_from(x)?)),
            RawRecord::CELL(x) => Ok(Record::Cell(Cell::try_from(x)?)),
            RawRecord::CLAS(x) => Ok(Record::Class(Class::try_from(x)?)),
            RawRecord::CLFM(x) => Ok(Record::Color(Color::try_from(x)?)),
            RawRecord::CLMT(x) => Ok(Record::Climate(Climate::try_from(x)?)),
            RawRecord::COBJ(x) => Ok(Record::ConstructibleObj(ConstructibleObj::try_from(x)?)),
            RawRecord::COLL(x) => Ok(Record::CollisionLayer(CollisionLayer::try_from(x)?)),
            RawRecord::CONT(x) => Ok(Record::Container(Container::try_from(x)?)),
            RawRecord::CPTH(x) => Ok(Record::CameraPath(CameraPath::try_from(x)?)),
            RawRecord::CSTY(x) => Ok(Record::CombatStyle(CombatStyle::try_from(x)?)),
            RawRecord::DEBR(x) => Ok(Record::Debris(Debris::try_from(x)?)),
            RawRecord::DIAL(x) => Ok(Record::DialogueTopic(DialogueTopic::try_from(x)?)),
            RawRecord::DLBR(x) => Ok(Record::DialogueBranch(DialogueBranch::try_from(x)?)),
            RawRecord::DLVW(x) => Ok(Record::DialogueView(DialogueView::try_from(x)?)),
            RawRecord::DOBJ(x) => Ok(Record::DefaultObjectManager(
                DefaultObjectManager::try_from(x)?,
            )),
            RawRecord::DOOR(x) => Ok(Record::Door(Door::try_from(x)?)),
            RawRecord::DUAL(x) => Ok(Record::DualCastData(DualCastData::try_from(x)?)),
            RawRecord::ECZN(x) => Ok(Record::EncounterZone(EncounterZone::try_from(x)?)),
            RawRecord::EFSH(x) => Ok(Record::EffectShader(EffectShader::try_from(x)?)),
            RawRecord::ENCH(x) => Ok(Record::Enchantment(Enchantment::try_from(x)?)),
            RawRecord::EQUP(x) => Ok(Record::EquipSlot(EquipSlot::try_from(x)?)),
            RawRecord::EXPL(x) => Ok(Record::Explosion(Explosion::try_from(x)?)),
            RawRecord::EYES(x) => Ok(Record::Eyes(Eyes::try_from(x)?)),
            RawRecord::FACT(x) => Ok(Record::Faction(Faction::try_from(x)?)),
            RawRecord::FLOR(x) => Ok(Record::Flora(Flora::try_from(x)?)),
            RawRecord::FLST(x) => Ok(Record::FormList(FormList::try_from(x)?)),
            RawRecord::FSTP(x) => Ok(Record::Footstep(Footstep::try_from(x)?)),
            RawRecord::FSTS(x) => Ok(Record::FootstepSet(FootstepSet::try_from(x)?)),
            RawRecord::FURN(x) => Ok(Record::Furniture(Furniture::try_from(x)?)),
            RawRecord::GLOB(x) => Ok(Record::GlobalVariable(GlobalVariable::try_from(x)?)),
            RawRecord::GMST(x) => Ok(Record::GameSetting(GameSetting::try_from(x)?)),
            RawRecord::GRAS(x) => Ok(Record::Grass(Grass::try_from(x)?)),
            RawRecord::GRUP(x) => Ok(Record::Group(Group::try_from(x)?)),
            RawRecord::HAZD(x) => Ok(Record::Hazard(Hazard::try_from(x)?)),
            RawRecord::HDPT(x) => Ok(Record::HeadPart(HeadPart::try_from(x)?)),
            RawRecord::IDLE(x) => Ok(Record::IdleAnimation(IdleAnimation::try_from(x)?)),
            RawRecord::IDLM(x) => Ok(Record::IdleMarker(IdleMarker::try_from(x)?)),
            RawRecord::IMAD(x) => Ok(Record::ImageSpaceModifier(ImageSpaceModifier::try_from(x)?)),
            RawRecord::IMGS(x) => Ok(Record::ImageSpace(ImageSpace::try_from(x)?)),
            RawRecord::INFO(x) => Ok(Record::DialogueTopicInfo(DialogueTopicInfo::try_from(x)?)),
            RawRecord::INGR(x) => Ok(Record::Ingredient(Ingredient::try_from(x)?)),
            RawRecord::IPCT(x) => Ok(Record::ImpactData(ImpactData::try_from(x)?)),
            RawRecord::IPDS(x) => Ok(Record::ImpactDataSet(ImpactDataSet::try_from(x)?)),
            RawRecord::KEYM(x) => Ok(Record::Key(Key::try_from(x)?)),
            RawRecord::KYWD(x) => Ok(Record::Keyword(Keyword::try_from(x)?)),
            RawRecord::LAND(x) => Ok(Record::Landscape(Landscape::try_from(x)?)),
            RawRecord::LCRT(x) => Ok(Record::LocationRef(LocationRef::try_from(x)?)),
            RawRecord::LCTN(x) => Ok(Record::Location(Location::try_from(x)?)),
            RawRecord::LGTM(x) => Ok(Record::LightingTemplate(LightingTemplate::try_from(x)?)),
            RawRecord::LIGH(x) => Ok(Record::Light(Light::try_from(x)?)),
            RawRecord::LSCR(x) => Ok(Record::LoadScreen(LoadScreen::try_from(x)?)),
            RawRecord::LTEX(x) => Ok(Record::LandTexture(LandTexture::try_from(x)?)),
            RawRecord::LVLI(x) => Ok(Record::LeveledItem(LeveledItem::try_from(x)?)),
            RawRecord::LVLN(x) => Ok(Record::LeveledActor(LeveledActor::try_from(x)?)),
            RawRecord::LVSP(x) => Ok(Record::LeveledSpell(LeveledSpell::try_from(x)?)),
            RawRecord::MATO(x) => Ok(Record::MaterialObject(MaterialObject::try_from(x)?)),
            RawRecord::MATT(x) => Ok(Record::MaterialType(MaterialType::try_from(x)?)),
            RawRecord::MESG(x) => Ok(Record::Message(Message::try_from(x)?)),
            RawRecord::MGEF(x) => Ok(Record::MagicEffect(MagicEffect::try_from(x)?)),
            RawRecord::MISC(x) => Ok(Record::MiscItem(MiscItem::try_from(x)?)),
            RawRecord::MOVT(x) => Ok(Record::MovementType(MovementType::try_from(x)?)),
            RawRecord::MSTT(x) => Ok(Record::MovableStatic(MovableStatic::try_from(x)?)),
            RawRecord::MUSC(x) => Ok(Record::MusicType(MusicType::try_from(x)?)),
            RawRecord::MUST(x) => Ok(Record::MusicTrack(MusicTrack::try_from(x)?)),
            RawRecord::NAVI(x) => Ok(Record::Navigation(Navigation::try_from(x)?)),
            RawRecord::NAVM(x) => Ok(Record::NavMesh(NavMesh::try_from(x)?)),
            RawRecord::NOTE(x) => Ok(Record::Note(Note::try_from(x)?)),
            RawRecord::NPC_(x) => Ok(Record::NPC(NPC::try_from(x)?)),
            RawRecord::OTFT(x) => Ok(Record::Outfit(Outfit::try_from(x)?)),
            RawRecord::PACK(x) => Ok(Record::AIPackage(AIPackage::try_from(x)?)),
            RawRecord::PERK(x) => Ok(Record::Perk(Perk::try_from(x)?)),
            RawRecord::PGRE(x) => Ok(Record::Placedgrenade(Placedgrenade::try_from(x)?)),
            RawRecord::PHZD(x) => Ok(Record::Placedhazard(Placedhazard::try_from(x)?)),
            RawRecord::PROJ(x) => Ok(Record::Projectile(Projectile::try_from(x)?)),
            RawRecord::QUST(x) => Ok(Record::Quest(Quest::try_from(x)?)),
            RawRecord::RACE(x) => Ok(Record::Race(Race::try_from(x)?)),
            RawRecord::REFR(x) => Ok(Record::ObjectRef(ObjectRef::try_from(x)?)),
            RawRecord::REGN(x) => Ok(Record::Region(Region::try_from(x)?)),
            RawRecord::RELA(x) => Ok(Record::Relationship(Relationship::try_from(x)?)),
            RawRecord::REVB(x) => Ok(Record::ReverbParameters(ReverbParameters::try_from(x)?)),
            RawRecord::RFCT(x) => Ok(Record::VisualEffect(VisualEffect::try_from(x)?)),
            RawRecord::SCEN(x) => Ok(Record::Scene(Scene::try_from(x)?)),
            RawRecord::SCRL(x) => Ok(Record::Scroll(Scroll::try_from(x)?)),
            RawRecord::SHOU(x) => Ok(Record::Shout(Shout::try_from(x)?)),
            RawRecord::SLGM(x) => Ok(Record::SoulGem(SoulGem::try_from(x)?)),
            RawRecord::SMBN(x) => Ok(Record::StoryManagerBranchNode(
                StoryManagerBranchNode::try_from(x)?,
            )),
            RawRecord::SMEN(x) => Ok(Record::StoryManagerEventNode(
                StoryManagerEventNode::try_from(x)?,
            )),
            RawRecord::SMQN(x) => Ok(Record::StoryManagerQuestNode(
                StoryManagerQuestNode::try_from(x)?,
            )),
            RawRecord::SNCT(x) => Ok(Record::SoundCategory(SoundCategory::try_from(x)?)),
            RawRecord::SNDR(x) => Ok(Record::SoundReference(SoundReference::try_from(x)?)),
            RawRecord::SOPM(x) => Ok(Record::SoundOutputModel(SoundOutputModel::try_from(x)?)),
            RawRecord::SOUN(x) => Ok(Record::Sound(Sound::try_from(x)?)),
            RawRecord::SPEL(x) => Ok(Record::Spell(Spell::try_from(x)?)),
            RawRecord::SPGD(x) => Ok(Record::ShaderParticleGeometry(
                ShaderParticleGeometry::try_from(x)?,
            )),
            RawRecord::STAT(x) => Ok(Record::Static(Static::try_from(x)?)),
            RawRecord::TACT(x) => Ok(Record::TalkingActivator(TalkingActivator::try_from(x)?)),
            RawRecord::TREE(x) => Ok(Record::Tree(Tree::try_from(x)?)),
            RawRecord::TXST(x) => Ok(Record::TextureSet(TextureSet::try_from(x)?)),
            RawRecord::VTYP(x) => Ok(Record::VoiceType(VoiceType::try_from(x)?)),
            RawRecord::WATR(x) => Ok(Record::WaterType(WaterType::try_from(x)?)),
            RawRecord::WEAP(x) => Ok(Record::Weapon(Weapon::try_from(x)?)),
            RawRecord::WOOP(x) => Ok(Record::WordOfPower(WordOfPower::try_from(x)?)),
            RawRecord::WRLD(x) => Ok(Record::World(World::try_from(x)?)),
            RawRecord::WTHR(x) => Ok(Record::Weather(Weather::try_from(x)?)),
        }
    }
}

impl fmt::Display for Record {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self {
            Record::AIPackage(x) => write!(f, "{}", x),
            Record::AcousticSpace(x) => write!(f, "{}", x),
            Record::Action(x) => write!(f, "{}", x),
            Record::Activator(x) => write!(f, "{}", x),
            Record::ActorRef(x) => write!(f, "{}", x),
            Record::ActorValue(x) => write!(f, "{}", x),
            Record::AddonNode(x) => write!(f, "{}", x),
            Record::Alchemy(x) => write!(f, "{}", x),
            Record::Ammo(x) => write!(f, "{}", x),
            Record::AnimatedObjectInfo(x) => write!(f, "{}", x),
            Record::Apparatus(x) => write!(f, "{}", x),
            Record::Armor(x) => write!(f, "{}", x),
            Record::ArmorAddon(x) => write!(f, "{}", x),
            Record::ArtObject(x) => write!(f, "{}", x),
            Record::AssociationType(x) => write!(f, "{}", x),
            Record::BodyPartData(x) => write!(f, "{}", x),
            Record::Book(x) => write!(f, "{}", x),
            Record::CameraPath(x) => write!(f, "{}", x),
            Record::CameraShot(x) => write!(f, "{}", x),
            Record::Cell(x) => write!(f, "{}", x),
            Record::Class(x) => write!(f, "{}", x),
            Record::Climate(x) => write!(f, "{}", x),
            Record::CollisionLayer(x) => write!(f, "{}", x),
            Record::Color(x) => write!(f, "{}", x),
            Record::CombatStyle(x) => write!(f, "{}", x),
            Record::ConstructibleObj(x) => write!(f, "{}", x),
            Record::Container(x) => write!(f, "{}", x),
            Record::Debris(x) => write!(f, "{}", x),
            Record::DefaultObjectManager(x) => write!(f, "{}", x),
            Record::DialogueBranch(x) => write!(f, "{}", x),
            Record::DialogueTopic(x) => write!(f, "{}", x),
            Record::DialogueTopicInfo(x) => write!(f, "{}", x),
            Record::DialogueView(x) => write!(f, "{}", x),
            Record::Door(x) => write!(f, "{}", x),
            Record::DualCastData(x) => write!(f, "{}", x),
            Record::EffectShader(x) => write!(f, "{}", x),
            Record::Enchantment(x) => write!(f, "{}", x),
            Record::EncounterZone(x) => write!(f, "{}", x),
            Record::EquipSlot(x) => write!(f, "{}", x),
            Record::Explosion(x) => write!(f, "{}", x),
            Record::Eyes(x) => write!(f, "{}", x),
            Record::Faction(x) => write!(f, "{}", x),
            Record::Flora(x) => write!(f, "{}", x),
            Record::Footstep(x) => write!(f, "{}", x),
            Record::FootstepSet(x) => write!(f, "{}", x),
            Record::FormList(x) => write!(f, "{}", x),
            Record::Furniture(x) => write!(f, "{}", x),
            Record::GameSetting(x) => write!(f, "{}", x),
            Record::GlobalVariable(x) => write!(f, "{}", x),
            Record::Grass(x) => write!(f, "{}", x),
            Record::Group(x) => write!(f, "{}", x),
            Record::Hazard(x) => write!(f, "{}", x),
            Record::HeadPart(x) => write!(f, "{}", x),
            Record::IdleAnimation(x) => write!(f, "{}", x),
            Record::IdleMarker(x) => write!(f, "{}", x),
            Record::ImageSpace(x) => write!(f, "{}", x),
            Record::ImageSpaceModifier(x) => write!(f, "{}", x),
            Record::ImpactData(x) => write!(f, "{}", x),
            Record::ImpactDataSet(x) => write!(f, "{}", x),
            Record::Ingredient(x) => write!(f, "{}", x),
            Record::Key(x) => write!(f, "{}", x),
            Record::Keyword(x) => write!(f, "{}", x),
            Record::LandTexture(x) => write!(f, "{}", x),
            Record::Landscape(x) => write!(f, "{}", x),
            Record::LeveledActor(x) => write!(f, "{}", x),
            Record::LeveledItem(x) => write!(f, "{}", x),
            Record::LeveledSpell(x) => write!(f, "{}", x),
            Record::Light(x) => write!(f, "{}", x),
            Record::LightingTemplate(x) => write!(f, "{}", x),
            Record::LoadScreen(x) => write!(f, "{}", x),
            Record::Location(x) => write!(f, "{}", x),
            Record::LocationRef(x) => write!(f, "{}", x),
            Record::MagicEffect(x) => write!(f, "{}", x),
            Record::MaterialObject(x) => write!(f, "{}", x),
            Record::MaterialType(x) => write!(f, "{}", x),
            Record::Message(x) => write!(f, "{}", x),
            Record::MiscItem(x) => write!(f, "{}", x),
            Record::MovableStatic(x) => write!(f, "{}", x),
            Record::MovementType(x) => write!(f, "{}", x),
            Record::MusicTrack(x) => write!(f, "{}", x),
            Record::MusicType(x) => write!(f, "{}", x),
            Record::NPC(x) => write!(f, "{}", x),
            Record::NavMesh(x) => write!(f, "{}", x),
            Record::Navigation(x) => write!(f, "{}", x),
            Record::Note(x) => write!(f, "{}", x),
            Record::ObjectRef(x) => write!(f, "{}", x),
            Record::Outfit(x) => write!(f, "{}", x),
            Record::Perk(x) => write!(f, "{}", x),
            Record::Placedgrenade(x) => write!(f, "{}", x),
            Record::Placedhazard(x) => write!(f, "{}", x),
            Record::Projectile(x) => write!(f, "{}", x),
            Record::Quest(x) => write!(f, "{}", x),
            Record::Race(x) => write!(f, "{}", x),
            Record::Region(x) => write!(f, "{}", x),
            Record::Relationship(x) => write!(f, "{}", x),
            Record::ReverbParameters(x) => write!(f, "{}", x),
            Record::Scene(x) => write!(f, "{}", x),
            Record::Scroll(x) => write!(f, "{}", x),
            Record::ShaderParticleGeometry(x) => write!(f, "{}", x),
            Record::Shout(x) => write!(f, "{}", x),
            Record::SoulGem(x) => write!(f, "{}", x),
            Record::Sound(x) => write!(f, "{}", x),
            Record::SoundCategory(x) => write!(f, "{}", x),
            Record::SoundOutputModel(x) => write!(f, "{}", x),
            Record::SoundReference(x) => write!(f, "{}", x),
            Record::Spell(x) => write!(f, "{}", x),
            Record::Static(x) => write!(f, "{}", x),
            Record::StoryManagerBranchNode(x) => write!(f, "{}", x),
            Record::StoryManagerEventNode(x) => write!(f, "{}", x),
            Record::StoryManagerQuestNode(x) => write!(f, "{}", x),
            Record::TalkingActivator(x) => write!(f, "{}", x),
            Record::TextureSet(x) => write!(f, "{}", x),
            Record::Tree(x) => write!(f, "{}", x),
            Record::VisualEffect(x) => write!(f, "{}", x),
            Record::VoiceType(x) => write!(f, "{}", x),
            Record::WaterType(x) => write!(f, "{}", x),
            Record::Weapon(x) => write!(f, "{}", x),
            Record::Weather(x) => write!(f, "{}", x),
            Record::WordOfPower(x) => write!(f, "{}", x),
            Record::World(x) => write!(f, "{}", x),
        }
    }
}

impl Record {
    pub fn localize(&mut self, string_table: &StringTables) {
        match self {
            Record::Book(x) => x.localize(string_table),
            Record::Group(x) => x.localize(string_table),
            _ => {}
        }
    }
}

fn get_cursor(data: &[u8], compressed: bool) -> Vec<u8> {
    if compressed {
        let mut cursor = Cursor::new(&data[4..]);
        let mut d = ZlibDecoder::new(&mut cursor);
        let mut decomp = Vec::new();
        d.read_to_end(&mut decomp).unwrap();
        decomp
    } else {
        data.into()
    }
}