wow_dbc 0.3.0

Library for parsing World of Warcraft DBC files for 1.12, 2.4.3 and 3.3.5.
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
use crate::{
    DbcTable, ExtendedLocalizedString, Indexable,
};
use crate::header::{
    DbcHeader, HEADER_SIZE, parse_header,
};
use crate::wrath_tables::area_group::AreaGroupKey;
use crate::wrath_tables::faction::FactionKey;
use crate::wrath_tables::power_display::PowerDisplayKey;
use crate::wrath_tables::spell_cast_times::SpellCastTimesKey;
use crate::wrath_tables::spell_category::SpellCategoryKey;
use crate::wrath_tables::spell_description_variables::SpellDescriptionVariablesKey;
use crate::wrath_tables::spell_dispel_type::SpellDispelTypeKey;
use crate::wrath_tables::spell_duration::SpellDurationKey;
use crate::wrath_tables::spell_focus_object::SpellFocusObjectKey;
use crate::wrath_tables::spell_icon::SpellIconKey;
use crate::wrath_tables::spell_mechanic::SpellMechanicKey;
use crate::wrath_tables::spell_missile::SpellMissileKey;
use crate::wrath_tables::spell_rune_cost::SpellRuneCostKey;
use std::io::Write;
use wow_world_base::wrath::AuraMod;

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct Spell {
    pub rows: Vec<SpellRow>,
}

impl DbcTable for Spell {
    type Row = SpellRow;

    const FILENAME: &'static str = "Spell.dbc";

    fn rows(&self) -> &[Self::Row] { &self.rows }
    fn rows_mut(&mut self) -> &mut [Self::Row] { &mut self.rows }

    fn read(b: &mut impl std::io::Read) -> Result<Self, crate::DbcError> {
        let mut header = [0_u8; HEADER_SIZE];
        b.read_exact(&mut header)?;
        let header = parse_header(&header)?;

        if header.record_size != 936 {
            return Err(crate::DbcError::InvalidHeader(
                crate::InvalidHeaderError::RecordSize {
                    expected: 936,
                    actual: header.record_size,
                },
            ));
        }

        if header.field_count != 234 {
            return Err(crate::DbcError::InvalidHeader(
                crate::InvalidHeaderError::FieldCount {
                    expected: 234,
                    actual: header.field_count,
                },
            ));
        }

        let mut r = vec![0_u8; (header.record_count * header.record_size) as usize];
        b.read_exact(&mut r)?;
        let mut string_block = vec![0_u8; header.string_block_size as usize];
        b.read_exact(&mut string_block)?;

        let mut rows = Vec::with_capacity(header.record_count as usize);

        for mut chunk in r.chunks(header.record_size as usize) {
            let chunk = &mut chunk;

            // id: primary_key (Spell) int32
            let id = SpellKey::new(crate::util::read_i32_le(chunk)?);

            // category: foreign_key (SpellCategory) int32
            let category = SpellCategoryKey::new(crate::util::read_i32_le(chunk)?.into());

            // dispel_type: foreign_key (SpellDispelType) int32
            let dispel_type = SpellDispelTypeKey::new(crate::util::read_i32_le(chunk)?.into());

            // mechanic: foreign_key (SpellMechanic) int32
            let mechanic = SpellMechanicKey::new(crate::util::read_i32_le(chunk)?.into());

            // attributes: int32
            let attributes = crate::util::read_i32_le(chunk)?;

            // attributes_ex: int32
            let attributes_ex = crate::util::read_i32_le(chunk)?;

            // attributes_ex_b: int32
            let attributes_ex_b = crate::util::read_i32_le(chunk)?;

            // attributes_ex_c: int32
            let attributes_ex_c = crate::util::read_i32_le(chunk)?;

            // attributes_ex_d: int32
            let attributes_ex_d = crate::util::read_i32_le(chunk)?;

            // attributes_ex_e: int32
            let attributes_ex_e = crate::util::read_i32_le(chunk)?;

            // attributes_ex_f: int32
            let attributes_ex_f = crate::util::read_i32_le(chunk)?;

            // attributes_ex_g: int32
            let attributes_ex_g = crate::util::read_i32_le(chunk)?;

            // shapeshift_mask: int32[2]
            let shapeshift_mask = crate::util::read_array_i32::<2>(chunk)?;

            // shapeshift_exclude: int32[2]
            let shapeshift_exclude = crate::util::read_array_i32::<2>(chunk)?;

            // targets: int32
            let targets = crate::util::read_i32_le(chunk)?;

            // target_creature_type: int32
            let target_creature_type = crate::util::read_i32_le(chunk)?;

            // requires_spell_focus: foreign_key (SpellFocusObject) int32
            let requires_spell_focus = SpellFocusObjectKey::new(crate::util::read_i32_le(chunk)?.into());

            // facing_caster_flags: int32
            let facing_caster_flags = crate::util::read_i32_le(chunk)?;

            // caster_aura_state: int32
            let caster_aura_state = crate::util::read_i32_le(chunk)?;

            // target_aura_state: int32
            let target_aura_state = crate::util::read_i32_le(chunk)?;

            // exclude_caster_aura_state: int32
            let exclude_caster_aura_state = crate::util::read_i32_le(chunk)?;

            // exclude_target_aura_state: int32
            let exclude_target_aura_state = crate::util::read_i32_le(chunk)?;

            // caster_aura_spell: int32
            let caster_aura_spell = crate::util::read_i32_le(chunk)?;

            // target_aura_spell: int32
            let target_aura_spell = crate::util::read_i32_le(chunk)?;

            // exclude_caster_aura_spell: int32
            let exclude_caster_aura_spell = crate::util::read_i32_le(chunk)?;

            // exclude_target_aura_spell: int32
            let exclude_target_aura_spell = crate::util::read_i32_le(chunk)?;

            // casting_time_index: foreign_key (SpellCastTimes) int32
            let casting_time_index = SpellCastTimesKey::new(crate::util::read_i32_le(chunk)?.into());

            // recovery_time: int32
            let recovery_time = crate::util::read_i32_le(chunk)?;

            // category_recovery_time: int32
            let category_recovery_time = crate::util::read_i32_le(chunk)?;

            // interrupt_flags: int32
            let interrupt_flags = crate::util::read_i32_le(chunk)?;

            // aura_interrupt_flags: int32
            let aura_interrupt_flags = crate::util::read_i32_le(chunk)?;

            // channel_interrupt_flags: int32
            let channel_interrupt_flags = crate::util::read_i32_le(chunk)?;

            // proc_type_mask: int32
            let proc_type_mask = crate::util::read_i32_le(chunk)?;

            // proc_chance: int32
            let proc_chance = crate::util::read_i32_le(chunk)?;

            // proc_charges: int32
            let proc_charges = crate::util::read_i32_le(chunk)?;

            // max_level: int32
            let max_level = crate::util::read_i32_le(chunk)?;

            // base_level: int32
            let base_level = crate::util::read_i32_le(chunk)?;

            // spell_level: int32
            let spell_level = crate::util::read_i32_le(chunk)?;

            // duration_index: foreign_key (SpellDuration) int32
            let duration_index = SpellDurationKey::new(crate::util::read_i32_le(chunk)?.into());

            // power_type: int32
            let power_type = crate::util::read_i32_le(chunk)?;

            // mana_cost: int32
            let mana_cost = crate::util::read_i32_le(chunk)?;

            // mana_cost_per_level: int32
            let mana_cost_per_level = crate::util::read_i32_le(chunk)?;

            // mana_per_second: int32
            let mana_per_second = crate::util::read_i32_le(chunk)?;

            // mana_per_second_per_level: int32
            let mana_per_second_per_level = crate::util::read_i32_le(chunk)?;

            // range_index: int32
            let range_index = crate::util::read_i32_le(chunk)?;

            // speed: float
            let speed = crate::util::read_f32_le(chunk)?;

            // modal_next_spell: int32
            let modal_next_spell = crate::util::read_i32_le(chunk)?;

            // cumulative_aura: int32
            let cumulative_aura = crate::util::read_i32_le(chunk)?;

            // totem: int32[2]
            let totem = crate::util::read_array_i32::<2>(chunk)?;

            // reagent: int32[8]
            let reagent = crate::util::read_array_i32::<8>(chunk)?;

            // reagent_count: int32[8]
            let reagent_count = crate::util::read_array_i32::<8>(chunk)?;

            // equipped_item_class: int32
            let equipped_item_class = crate::util::read_i32_le(chunk)?;

            // equipped_item_subclass: int32
            let equipped_item_subclass = crate::util::read_i32_le(chunk)?;

            // equipped_item_inv_types: int32
            let equipped_item_inv_types = crate::util::read_i32_le(chunk)?;

            // effect: int32[3]
            let effect = crate::util::read_array_i32::<3>(chunk)?;

            // effect_die_sides: int32[3]
            let effect_die_sides = crate::util::read_array_i32::<3>(chunk)?;

            // effect_real_points_per_level: float[3]
            let effect_real_points_per_level = crate::util::read_array_f32::<3>(chunk)?;

            // effect_base_points: int32[3]
            let effect_base_points = crate::util::read_array_i32::<3>(chunk)?;

            // effect_mechanic: int32[3]
            let effect_mechanic = crate::util::read_array_i32::<3>(chunk)?;

            // implicit_target_a: int32[3]
            let implicit_target_a = crate::util::read_array_i32::<3>(chunk)?;

            // implicit_target_b: int32[3]
            let implicit_target_b = crate::util::read_array_i32::<3>(chunk)?;

            // effect_radius_index: int32[3]
            let effect_radius_index = crate::util::read_array_i32::<3>(chunk)?;

            // effect_aura: AuraMod[3]
            let effect_aura = {
                let mut arr = [AuraMod::default(); 3];
                for i in arr.iter_mut() {
                    *i = crate::util::read_i32_le(chunk)?.try_into()?;
                }

                arr
            };

            // effect_aura_period: int32[3]
            let effect_aura_period = crate::util::read_array_i32::<3>(chunk)?;

            // effect_amplitude: float[3]
            let effect_amplitude = crate::util::read_array_f32::<3>(chunk)?;

            // effect_chain_targets: int32[3]
            let effect_chain_targets = crate::util::read_array_i32::<3>(chunk)?;

            // effect_item_type: int32[3]
            let effect_item_type = crate::util::read_array_i32::<3>(chunk)?;

            // effect_misc_value: int32[3]
            let effect_misc_value = crate::util::read_array_i32::<3>(chunk)?;

            // effect_misc_value_b: int32[3]
            let effect_misc_value_b = crate::util::read_array_i32::<3>(chunk)?;

            // effect_trigger_spell: int32[3]
            let effect_trigger_spell = crate::util::read_array_i32::<3>(chunk)?;

            // effect_points_per_combo: float[3]
            let effect_points_per_combo = crate::util::read_array_f32::<3>(chunk)?;

            // effect_spell_class_mask_a: int32[3]
            let effect_spell_class_mask_a = crate::util::read_array_i32::<3>(chunk)?;

            // effect_spell_class_mask_b: int32[3]
            let effect_spell_class_mask_b = crate::util::read_array_i32::<3>(chunk)?;

            // effect_spell_class_mask_c: int32[3]
            let effect_spell_class_mask_c = crate::util::read_array_i32::<3>(chunk)?;

            // spell_visual_id: int32[2]
            let spell_visual_id = crate::util::read_array_i32::<2>(chunk)?;

            // spell_icon_id: foreign_key (SpellIcon) int32
            let spell_icon_id = SpellIconKey::new(crate::util::read_i32_le(chunk)?.into());

            // active_icon_id: foreign_key (SpellIcon) int32
            let active_icon_id = SpellIconKey::new(crate::util::read_i32_le(chunk)?.into());

            // spell_priority: int32
            let spell_priority = crate::util::read_i32_le(chunk)?;

            // name_lang: string_ref_loc (Extended)
            let name_lang = crate::util::read_extended_localized_string(chunk, &string_block)?;

            // name_subtext_lang: string_ref_loc (Extended)
            let name_subtext_lang = crate::util::read_extended_localized_string(chunk, &string_block)?;

            // description_lang: string_ref_loc (Extended)
            let description_lang = crate::util::read_extended_localized_string(chunk, &string_block)?;

            // aura_description_lang: string_ref_loc (Extended)
            let aura_description_lang = crate::util::read_extended_localized_string(chunk, &string_block)?;

            // mana_cost_pct: int32
            let mana_cost_pct = crate::util::read_i32_le(chunk)?;

            // start_recovery_category: int32
            let start_recovery_category = crate::util::read_i32_le(chunk)?;

            // start_recovery_time: int32
            let start_recovery_time = crate::util::read_i32_le(chunk)?;

            // max_target_level: int32
            let max_target_level = crate::util::read_i32_le(chunk)?;

            // spell_class_set: int32
            let spell_class_set = crate::util::read_i32_le(chunk)?;

            // spell_class_mask: int32[3]
            let spell_class_mask = crate::util::read_array_i32::<3>(chunk)?;

            // max_targets: int32
            let max_targets = crate::util::read_i32_le(chunk)?;

            // defense_type: int32
            let defense_type = crate::util::read_i32_le(chunk)?;

            // prevention_type: int32
            let prevention_type = crate::util::read_i32_le(chunk)?;

            // stance_bar_order: int32
            let stance_bar_order = crate::util::read_i32_le(chunk)?;

            // effect_chain_amplitude: float[3]
            let effect_chain_amplitude = crate::util::read_array_f32::<3>(chunk)?;

            // min_faction_id: foreign_key (Faction) int32
            let min_faction_id = FactionKey::new(crate::util::read_i32_le(chunk)?.into());

            // min_reputation: int32
            let min_reputation = crate::util::read_i32_le(chunk)?;

            // required_aura_vision: int32
            let required_aura_vision = crate::util::read_i32_le(chunk)?;

            // required_totem_category_id: int32[2]
            let required_totem_category_id = crate::util::read_array_i32::<2>(chunk)?;

            // required_areas_id: foreign_key (AreaGroup) int32
            let required_areas_id = AreaGroupKey::new(crate::util::read_i32_le(chunk)?.into());

            // school_mask: int32
            let school_mask = crate::util::read_i32_le(chunk)?;

            // rune_cost_id: foreign_key (SpellRuneCost) int32
            let rune_cost_id = SpellRuneCostKey::new(crate::util::read_i32_le(chunk)?.into());

            // spell_missile_id: foreign_key (SpellMissile) int32
            let spell_missile_id = SpellMissileKey::new(crate::util::read_i32_le(chunk)?.into());

            // power_display_id: foreign_key (PowerDisplay) int32
            let power_display_id = PowerDisplayKey::new(crate::util::read_i32_le(chunk)?.into());

            // effect_bonus_coefficient: float[3]
            let effect_bonus_coefficient = crate::util::read_array_f32::<3>(chunk)?;

            // description_variables_id: foreign_key (SpellDescriptionVariables) int32
            let description_variables_id = SpellDescriptionVariablesKey::new(crate::util::read_i32_le(chunk)?.into());

            // difficulty: int32
            let difficulty = crate::util::read_i32_le(chunk)?;


            rows.push(SpellRow {
                id,
                category,
                dispel_type,
                mechanic,
                attributes,
                attributes_ex,
                attributes_ex_b,
                attributes_ex_c,
                attributes_ex_d,
                attributes_ex_e,
                attributes_ex_f,
                attributes_ex_g,
                shapeshift_mask,
                shapeshift_exclude,
                targets,
                target_creature_type,
                requires_spell_focus,
                facing_caster_flags,
                caster_aura_state,
                target_aura_state,
                exclude_caster_aura_state,
                exclude_target_aura_state,
                caster_aura_spell,
                target_aura_spell,
                exclude_caster_aura_spell,
                exclude_target_aura_spell,
                casting_time_index,
                recovery_time,
                category_recovery_time,
                interrupt_flags,
                aura_interrupt_flags,
                channel_interrupt_flags,
                proc_type_mask,
                proc_chance,
                proc_charges,
                max_level,
                base_level,
                spell_level,
                duration_index,
                power_type,
                mana_cost,
                mana_cost_per_level,
                mana_per_second,
                mana_per_second_per_level,
                range_index,
                speed,
                modal_next_spell,
                cumulative_aura,
                totem,
                reagent,
                reagent_count,
                equipped_item_class,
                equipped_item_subclass,
                equipped_item_inv_types,
                effect,
                effect_die_sides,
                effect_real_points_per_level,
                effect_base_points,
                effect_mechanic,
                implicit_target_a,
                implicit_target_b,
                effect_radius_index,
                effect_aura,
                effect_aura_period,
                effect_amplitude,
                effect_chain_targets,
                effect_item_type,
                effect_misc_value,
                effect_misc_value_b,
                effect_trigger_spell,
                effect_points_per_combo,
                effect_spell_class_mask_a,
                effect_spell_class_mask_b,
                effect_spell_class_mask_c,
                spell_visual_id,
                spell_icon_id,
                active_icon_id,
                spell_priority,
                name_lang,
                name_subtext_lang,
                description_lang,
                aura_description_lang,
                mana_cost_pct,
                start_recovery_category,
                start_recovery_time,
                max_target_level,
                spell_class_set,
                spell_class_mask,
                max_targets,
                defense_type,
                prevention_type,
                stance_bar_order,
                effect_chain_amplitude,
                min_faction_id,
                min_reputation,
                required_aura_vision,
                required_totem_category_id,
                required_areas_id,
                school_mask,
                rune_cost_id,
                spell_missile_id,
                power_display_id,
                effect_bonus_coefficient,
                description_variables_id,
                difficulty,
            });
        }

        Ok(Spell { rows, })
    }

    fn write(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
        let header = DbcHeader {
            record_count: self.rows.len() as u32,
            field_count: 234,
            record_size: 936,
            string_block_size: self.string_block_size(),
        };

        b.write_all(&header.write_header())?;

        let mut string_index = 1;
        for row in &self.rows {
            // id: primary_key (Spell) int32
            b.write_all(&row.id.id.to_le_bytes())?;

            // category: foreign_key (SpellCategory) int32
            b.write_all(&(row.category.id as i32).to_le_bytes())?;

            // dispel_type: foreign_key (SpellDispelType) int32
            b.write_all(&(row.dispel_type.id as i32).to_le_bytes())?;

            // mechanic: foreign_key (SpellMechanic) int32
            b.write_all(&(row.mechanic.id as i32).to_le_bytes())?;

            // attributes: int32
            b.write_all(&row.attributes.to_le_bytes())?;

            // attributes_ex: int32
            b.write_all(&row.attributes_ex.to_le_bytes())?;

            // attributes_ex_b: int32
            b.write_all(&row.attributes_ex_b.to_le_bytes())?;

            // attributes_ex_c: int32
            b.write_all(&row.attributes_ex_c.to_le_bytes())?;

            // attributes_ex_d: int32
            b.write_all(&row.attributes_ex_d.to_le_bytes())?;

            // attributes_ex_e: int32
            b.write_all(&row.attributes_ex_e.to_le_bytes())?;

            // attributes_ex_f: int32
            b.write_all(&row.attributes_ex_f.to_le_bytes())?;

            // attributes_ex_g: int32
            b.write_all(&row.attributes_ex_g.to_le_bytes())?;

            // shapeshift_mask: int32[2]
            for i in row.shapeshift_mask {
                b.write_all(&i.to_le_bytes())?;
            }


            // shapeshift_exclude: int32[2]
            for i in row.shapeshift_exclude {
                b.write_all(&i.to_le_bytes())?;
            }


            // targets: int32
            b.write_all(&row.targets.to_le_bytes())?;

            // target_creature_type: int32
            b.write_all(&row.target_creature_type.to_le_bytes())?;

            // requires_spell_focus: foreign_key (SpellFocusObject) int32
            b.write_all(&(row.requires_spell_focus.id as i32).to_le_bytes())?;

            // facing_caster_flags: int32
            b.write_all(&row.facing_caster_flags.to_le_bytes())?;

            // caster_aura_state: int32
            b.write_all(&row.caster_aura_state.to_le_bytes())?;

            // target_aura_state: int32
            b.write_all(&row.target_aura_state.to_le_bytes())?;

            // exclude_caster_aura_state: int32
            b.write_all(&row.exclude_caster_aura_state.to_le_bytes())?;

            // exclude_target_aura_state: int32
            b.write_all(&row.exclude_target_aura_state.to_le_bytes())?;

            // caster_aura_spell: int32
            b.write_all(&row.caster_aura_spell.to_le_bytes())?;

            // target_aura_spell: int32
            b.write_all(&row.target_aura_spell.to_le_bytes())?;

            // exclude_caster_aura_spell: int32
            b.write_all(&row.exclude_caster_aura_spell.to_le_bytes())?;

            // exclude_target_aura_spell: int32
            b.write_all(&row.exclude_target_aura_spell.to_le_bytes())?;

            // casting_time_index: foreign_key (SpellCastTimes) int32
            b.write_all(&(row.casting_time_index.id as i32).to_le_bytes())?;

            // recovery_time: int32
            b.write_all(&row.recovery_time.to_le_bytes())?;

            // category_recovery_time: int32
            b.write_all(&row.category_recovery_time.to_le_bytes())?;

            // interrupt_flags: int32
            b.write_all(&row.interrupt_flags.to_le_bytes())?;

            // aura_interrupt_flags: int32
            b.write_all(&row.aura_interrupt_flags.to_le_bytes())?;

            // channel_interrupt_flags: int32
            b.write_all(&row.channel_interrupt_flags.to_le_bytes())?;

            // proc_type_mask: int32
            b.write_all(&row.proc_type_mask.to_le_bytes())?;

            // proc_chance: int32
            b.write_all(&row.proc_chance.to_le_bytes())?;

            // proc_charges: int32
            b.write_all(&row.proc_charges.to_le_bytes())?;

            // max_level: int32
            b.write_all(&row.max_level.to_le_bytes())?;

            // base_level: int32
            b.write_all(&row.base_level.to_le_bytes())?;

            // spell_level: int32
            b.write_all(&row.spell_level.to_le_bytes())?;

            // duration_index: foreign_key (SpellDuration) int32
            b.write_all(&(row.duration_index.id as i32).to_le_bytes())?;

            // power_type: int32
            b.write_all(&row.power_type.to_le_bytes())?;

            // mana_cost: int32
            b.write_all(&row.mana_cost.to_le_bytes())?;

            // mana_cost_per_level: int32
            b.write_all(&row.mana_cost_per_level.to_le_bytes())?;

            // mana_per_second: int32
            b.write_all(&row.mana_per_second.to_le_bytes())?;

            // mana_per_second_per_level: int32
            b.write_all(&row.mana_per_second_per_level.to_le_bytes())?;

            // range_index: int32
            b.write_all(&row.range_index.to_le_bytes())?;

            // speed: float
            b.write_all(&row.speed.to_le_bytes())?;

            // modal_next_spell: int32
            b.write_all(&row.modal_next_spell.to_le_bytes())?;

            // cumulative_aura: int32
            b.write_all(&row.cumulative_aura.to_le_bytes())?;

            // totem: int32[2]
            for i in row.totem {
                b.write_all(&i.to_le_bytes())?;
            }


            // reagent: int32[8]
            for i in row.reagent {
                b.write_all(&i.to_le_bytes())?;
            }


            // reagent_count: int32[8]
            for i in row.reagent_count {
                b.write_all(&i.to_le_bytes())?;
            }


            // equipped_item_class: int32
            b.write_all(&row.equipped_item_class.to_le_bytes())?;

            // equipped_item_subclass: int32
            b.write_all(&row.equipped_item_subclass.to_le_bytes())?;

            // equipped_item_inv_types: int32
            b.write_all(&row.equipped_item_inv_types.to_le_bytes())?;

            // effect: int32[3]
            for i in row.effect {
                b.write_all(&i.to_le_bytes())?;
            }


            // effect_die_sides: int32[3]
            for i in row.effect_die_sides {
                b.write_all(&i.to_le_bytes())?;
            }


            // effect_real_points_per_level: float[3]
            for i in row.effect_real_points_per_level {
                b.write_all(&i.to_le_bytes())?;
            }


            // effect_base_points: int32[3]
            for i in row.effect_base_points {
                b.write_all(&i.to_le_bytes())?;
            }


            // effect_mechanic: int32[3]
            for i in row.effect_mechanic {
                b.write_all(&i.to_le_bytes())?;
            }


            // implicit_target_a: int32[3]
            for i in row.implicit_target_a {
                b.write_all(&i.to_le_bytes())?;
            }


            // implicit_target_b: int32[3]
            for i in row.implicit_target_b {
                b.write_all(&i.to_le_bytes())?;
            }


            // effect_radius_index: int32[3]
            for i in row.effect_radius_index {
                b.write_all(&i.to_le_bytes())?;
            }


            // effect_aura: AuraMod[3]
            for i in row.effect_aura {
                b.write_all(&(i.as_int() as i32).to_le_bytes())?;
            }


            // effect_aura_period: int32[3]
            for i in row.effect_aura_period {
                b.write_all(&i.to_le_bytes())?;
            }


            // effect_amplitude: float[3]
            for i in row.effect_amplitude {
                b.write_all(&i.to_le_bytes())?;
            }


            // effect_chain_targets: int32[3]
            for i in row.effect_chain_targets {
                b.write_all(&i.to_le_bytes())?;
            }


            // effect_item_type: int32[3]
            for i in row.effect_item_type {
                b.write_all(&i.to_le_bytes())?;
            }


            // effect_misc_value: int32[3]
            for i in row.effect_misc_value {
                b.write_all(&i.to_le_bytes())?;
            }


            // effect_misc_value_b: int32[3]
            for i in row.effect_misc_value_b {
                b.write_all(&i.to_le_bytes())?;
            }


            // effect_trigger_spell: int32[3]
            for i in row.effect_trigger_spell {
                b.write_all(&i.to_le_bytes())?;
            }


            // effect_points_per_combo: float[3]
            for i in row.effect_points_per_combo {
                b.write_all(&i.to_le_bytes())?;
            }


            // effect_spell_class_mask_a: int32[3]
            for i in row.effect_spell_class_mask_a {
                b.write_all(&i.to_le_bytes())?;
            }


            // effect_spell_class_mask_b: int32[3]
            for i in row.effect_spell_class_mask_b {
                b.write_all(&i.to_le_bytes())?;
            }


            // effect_spell_class_mask_c: int32[3]
            for i in row.effect_spell_class_mask_c {
                b.write_all(&i.to_le_bytes())?;
            }


            // spell_visual_id: int32[2]
            for i in row.spell_visual_id {
                b.write_all(&i.to_le_bytes())?;
            }


            // spell_icon_id: foreign_key (SpellIcon) int32
            b.write_all(&(row.spell_icon_id.id as i32).to_le_bytes())?;

            // active_icon_id: foreign_key (SpellIcon) int32
            b.write_all(&(row.active_icon_id.id as i32).to_le_bytes())?;

            // spell_priority: int32
            b.write_all(&row.spell_priority.to_le_bytes())?;

            // name_lang: string_ref_loc (Extended)
            b.write_all(&row.name_lang.string_indices_as_array(&mut string_index))?;

            // name_subtext_lang: string_ref_loc (Extended)
            b.write_all(&row.name_subtext_lang.string_indices_as_array(&mut string_index))?;

            // description_lang: string_ref_loc (Extended)
            b.write_all(&row.description_lang.string_indices_as_array(&mut string_index))?;

            // aura_description_lang: string_ref_loc (Extended)
            b.write_all(&row.aura_description_lang.string_indices_as_array(&mut string_index))?;

            // mana_cost_pct: int32
            b.write_all(&row.mana_cost_pct.to_le_bytes())?;

            // start_recovery_category: int32
            b.write_all(&row.start_recovery_category.to_le_bytes())?;

            // start_recovery_time: int32
            b.write_all(&row.start_recovery_time.to_le_bytes())?;

            // max_target_level: int32
            b.write_all(&row.max_target_level.to_le_bytes())?;

            // spell_class_set: int32
            b.write_all(&row.spell_class_set.to_le_bytes())?;

            // spell_class_mask: int32[3]
            for i in row.spell_class_mask {
                b.write_all(&i.to_le_bytes())?;
            }


            // max_targets: int32
            b.write_all(&row.max_targets.to_le_bytes())?;

            // defense_type: int32
            b.write_all(&row.defense_type.to_le_bytes())?;

            // prevention_type: int32
            b.write_all(&row.prevention_type.to_le_bytes())?;

            // stance_bar_order: int32
            b.write_all(&row.stance_bar_order.to_le_bytes())?;

            // effect_chain_amplitude: float[3]
            for i in row.effect_chain_amplitude {
                b.write_all(&i.to_le_bytes())?;
            }


            // min_faction_id: foreign_key (Faction) int32
            b.write_all(&(row.min_faction_id.id as i32).to_le_bytes())?;

            // min_reputation: int32
            b.write_all(&row.min_reputation.to_le_bytes())?;

            // required_aura_vision: int32
            b.write_all(&row.required_aura_vision.to_le_bytes())?;

            // required_totem_category_id: int32[2]
            for i in row.required_totem_category_id {
                b.write_all(&i.to_le_bytes())?;
            }


            // required_areas_id: foreign_key (AreaGroup) int32
            b.write_all(&(row.required_areas_id.id as i32).to_le_bytes())?;

            // school_mask: int32
            b.write_all(&row.school_mask.to_le_bytes())?;

            // rune_cost_id: foreign_key (SpellRuneCost) int32
            b.write_all(&(row.rune_cost_id.id as i32).to_le_bytes())?;

            // spell_missile_id: foreign_key (SpellMissile) int32
            b.write_all(&(row.spell_missile_id.id as i32).to_le_bytes())?;

            // power_display_id: foreign_key (PowerDisplay) int32
            b.write_all(&(row.power_display_id.id as i32).to_le_bytes())?;

            // effect_bonus_coefficient: float[3]
            for i in row.effect_bonus_coefficient {
                b.write_all(&i.to_le_bytes())?;
            }


            // description_variables_id: foreign_key (SpellDescriptionVariables) int32
            b.write_all(&(row.description_variables_id.id as i32).to_le_bytes())?;

            // difficulty: int32
            b.write_all(&row.difficulty.to_le_bytes())?;

        }

        self.write_string_block(b)?;

        Ok(())
    }

}

impl Indexable for Spell {
    type PrimaryKey = SpellKey;
    fn get(&self, key: impl TryInto<Self::PrimaryKey>) -> Option<&Self::Row> {
        let key = key.try_into().ok()?;
        self.rows.iter().find(|a| a.id.id == key.id)
    }

    fn get_mut(&mut self, key: impl TryInto<Self::PrimaryKey>) -> Option<&mut Self::Row> {
        let key = key.try_into().ok()?;
        self.rows.iter_mut().find(|a| a.id.id == key.id)
    }
}

impl Spell {
    fn write_string_block(&self, b: &mut impl Write) -> Result<(), std::io::Error> {
        b.write_all(&[0])?;

        for row in &self.rows {
            row.name_lang.string_block_as_array(b)?;
            row.name_subtext_lang.string_block_as_array(b)?;
            row.description_lang.string_block_as_array(b)?;
            row.aura_description_lang.string_block_as_array(b)?;
        }

        Ok(())
    }

    fn string_block_size(&self) -> u32 {
        let mut sum = 1;
        for row in &self.rows {
            sum += row.name_lang.string_block_size();
            sum += row.name_subtext_lang.string_block_size();
            sum += row.description_lang.string_block_size();
            sum += row.aura_description_lang.string_block_size();
        }

        sum as u32
    }

}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
pub struct SpellKey {
    pub id: i32
}

impl SpellKey {
    pub const fn new(id: i32) -> Self {
        Self { id }
    }

}

impl From<u8> for SpellKey {
    fn from(v: u8) -> Self {
        Self::new(v.into())
    }
}

impl From<u16> for SpellKey {
    fn from(v: u16) -> Self {
        Self::new(v.into())
    }
}

impl From<i8> for SpellKey {
    fn from(v: i8) -> Self {
        Self::new(v.into())
    }
}

impl From<i16> for SpellKey {
    fn from(v: i16) -> Self {
        Self::new(v.into())
    }
}

impl From<i32> for SpellKey {
    fn from(v: i32) -> Self {
        Self::new(v)
    }
}

impl TryFrom<u32> for SpellKey {
    type Error = u32;
    fn try_from(v: u32) -> Result<Self, Self::Error> {
        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
    }
}

impl TryFrom<usize> for SpellKey {
    type Error = usize;
    fn try_from(v: usize) -> Result<Self, Self::Error> {
        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
    }
}

impl TryFrom<u64> for SpellKey {
    type Error = u64;
    fn try_from(v: u64) -> Result<Self, Self::Error> {
        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
    }
}

impl TryFrom<i64> for SpellKey {
    type Error = i64;
    fn try_from(v: i64) -> Result<Self, Self::Error> {
        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
    }
}

impl TryFrom<isize> for SpellKey {
    type Error = isize;
    fn try_from(v: isize) -> Result<Self, Self::Error> {
        Ok(TryInto::<i32>::try_into(v).ok().ok_or(v)?.into())
    }
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct SpellRow {
    pub id: SpellKey,
    pub category: SpellCategoryKey,
    pub dispel_type: SpellDispelTypeKey,
    pub mechanic: SpellMechanicKey,
    pub attributes: i32,
    pub attributes_ex: i32,
    pub attributes_ex_b: i32,
    pub attributes_ex_c: i32,
    pub attributes_ex_d: i32,
    pub attributes_ex_e: i32,
    pub attributes_ex_f: i32,
    pub attributes_ex_g: i32,
    pub shapeshift_mask: [i32; 2],
    pub shapeshift_exclude: [i32; 2],
    pub targets: i32,
    pub target_creature_type: i32,
    pub requires_spell_focus: SpellFocusObjectKey,
    pub facing_caster_flags: i32,
    pub caster_aura_state: i32,
    pub target_aura_state: i32,
    pub exclude_caster_aura_state: i32,
    pub exclude_target_aura_state: i32,
    pub caster_aura_spell: i32,
    pub target_aura_spell: i32,
    pub exclude_caster_aura_spell: i32,
    pub exclude_target_aura_spell: i32,
    pub casting_time_index: SpellCastTimesKey,
    pub recovery_time: i32,
    pub category_recovery_time: i32,
    pub interrupt_flags: i32,
    pub aura_interrupt_flags: i32,
    pub channel_interrupt_flags: i32,
    pub proc_type_mask: i32,
    pub proc_chance: i32,
    pub proc_charges: i32,
    pub max_level: i32,
    pub base_level: i32,
    pub spell_level: i32,
    pub duration_index: SpellDurationKey,
    pub power_type: i32,
    pub mana_cost: i32,
    pub mana_cost_per_level: i32,
    pub mana_per_second: i32,
    pub mana_per_second_per_level: i32,
    pub range_index: i32,
    pub speed: f32,
    pub modal_next_spell: i32,
    pub cumulative_aura: i32,
    pub totem: [i32; 2],
    pub reagent: [i32; 8],
    pub reagent_count: [i32; 8],
    pub equipped_item_class: i32,
    pub equipped_item_subclass: i32,
    pub equipped_item_inv_types: i32,
    pub effect: [i32; 3],
    pub effect_die_sides: [i32; 3],
    pub effect_real_points_per_level: [f32; 3],
    pub effect_base_points: [i32; 3],
    pub effect_mechanic: [i32; 3],
    pub implicit_target_a: [i32; 3],
    pub implicit_target_b: [i32; 3],
    pub effect_radius_index: [i32; 3],
    pub effect_aura: [AuraMod; 3],
    pub effect_aura_period: [i32; 3],
    pub effect_amplitude: [f32; 3],
    pub effect_chain_targets: [i32; 3],
    pub effect_item_type: [i32; 3],
    pub effect_misc_value: [i32; 3],
    pub effect_misc_value_b: [i32; 3],
    pub effect_trigger_spell: [i32; 3],
    pub effect_points_per_combo: [f32; 3],
    pub effect_spell_class_mask_a: [i32; 3],
    pub effect_spell_class_mask_b: [i32; 3],
    pub effect_spell_class_mask_c: [i32; 3],
    pub spell_visual_id: [i32; 2],
    pub spell_icon_id: SpellIconKey,
    pub active_icon_id: SpellIconKey,
    pub spell_priority: i32,
    pub name_lang: ExtendedLocalizedString,
    pub name_subtext_lang: ExtendedLocalizedString,
    pub description_lang: ExtendedLocalizedString,
    pub aura_description_lang: ExtendedLocalizedString,
    pub mana_cost_pct: i32,
    pub start_recovery_category: i32,
    pub start_recovery_time: i32,
    pub max_target_level: i32,
    pub spell_class_set: i32,
    pub spell_class_mask: [i32; 3],
    pub max_targets: i32,
    pub defense_type: i32,
    pub prevention_type: i32,
    pub stance_bar_order: i32,
    pub effect_chain_amplitude: [f32; 3],
    pub min_faction_id: FactionKey,
    pub min_reputation: i32,
    pub required_aura_vision: i32,
    pub required_totem_category_id: [i32; 2],
    pub required_areas_id: AreaGroupKey,
    pub school_mask: i32,
    pub rune_cost_id: SpellRuneCostKey,
    pub spell_missile_id: SpellMissileKey,
    pub power_display_id: PowerDisplayKey,
    pub effect_bonus_coefficient: [f32; 3],
    pub description_variables_id: SpellDescriptionVariablesKey,
    pub difficulty: i32,
}