wow-m2 0.6.2

Parser, validator, and converter for World of Warcraft M2 model files with animation support
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
use crate::io_ext::{ReadExt, WriteExt};
use bitflags::bitflags;
use std::io::{Read, Seek, Write};

use crate::common::M2Array;
use crate::error::{M2Error, Result};
use crate::version::M2Version;

/// Magic signature for legacy M2 files ("MD20")
pub const M2_MAGIC_LEGACY: [u8; 4] = *b"MD20";

/// Magic signature for chunked M2 files ("MD21")
pub const M2_MAGIC_CHUNKED: [u8; 4] = *b"MD21";

/// Legacy magic signature for compatibility ("MD20")
pub const M2_MAGIC: [u8; 4] = M2_MAGIC_LEGACY;

bitflags! {
    /// Model flags as defined in the M2 format
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct M2ModelFlags: u32 {
        /// Tilt on X axis
        const TILT_X = 0x0001;
        /// Tilt on Y axis
        const TILT_Y = 0x0002;
        /// Add a back-reference to the model
        const ADD_BACK_REFERENCE = 0x0004;
        /// Use texture combiners
        const USE_TEXTURE_COMBINERS = 0x0008;
        /// Is it a camera?
        const IS_CAMERA = 0x0010;
        /// Unused flag
        const UNUSED = 0x0020;
        /// No particle trails
        const NO_PARTICLE_TRAILS = 0x0040;
        /// Unknown
        const UNKNOWN_0x80 = 0x0080;
        /// Load phys data
        const LOAD_PHYS_DATA = 0x0100;
        /// Unknown
        const UNKNOWN_0x200 = 0x0200;
        /// Has bones
        const HAS_BONES = 0x0400;
        /// Unused 0x800
        const UNUSED_0x800 = 0x0800;
        /// Unknown
        const UNKNOWN_0x1000 = 0x1000;
        /// Use texture IDs
        const USE_TEXTURE_IDS = 0x2000;
        /// Camera can be modified
        const CAMERA_MODIFIABLE = 0x4000;
        /// New particle system
        const NEW_PARTICLE_SYSTEM = 0x8000;
        /// Unknown
        const UNKNOWN_0x10000 = 0x10000;
        /// Unknown
        const UNKNOWN_0x20000 = 0x20000;
        /// Unknown
        const UNKNOWN_0x40000 = 0x40000;
        /// Unknown
        const UNKNOWN_0x80000 = 0x80000;
        /// Unknown
        const UNKNOWN_0x100000 = 0x100000;
        /// Unknown
        const UNKNOWN_0x200000 = 0x200000;
        /// Unknown
        const UNKNOWN_0x400000 = 0x400000;
        /// Unknown
        const UNKNOWN_0x800000 = 0x800000;
        /// Unknown
        const UNKNOWN_0x1000000 = 0x1000000;
        /// Unknown
        const UNKNOWN_0x2000000 = 0x2000000;
        /// Unknown
        const UNKNOWN_0x4000000 = 0x4000000;
        /// Unknown
        const UNKNOWN_0x8000000 = 0x8000000;
        /// Unknown
        const UNKNOWN_0x10000000 = 0x10000000;
        /// Unknown
        const UNKNOWN_0x20000000 = 0x20000000;
        /// Unknown
        const UNKNOWN_0x40000000 = 0x40000000;
        /// Unknown
        const UNKNOWN_0x80000000 = 0x80000000;
    }
}

/// M2 model header structure
/// Based on: <https://wowdev.wiki/M2#Header>
#[derive(Debug, Clone)]
pub struct M2Header {
    /// Magic signature ("MD20")
    pub magic: [u8; 4],
    /// Version of the M2 file
    pub version: u32,
    /// Name of the model
    pub name: M2Array<u8>,
    /// Flags
    pub flags: M2ModelFlags,

    // Sequence-related fields
    /// Global sequences
    pub global_sequences: M2Array<u32>,
    /// Animations
    pub animations: M2Array<u32>,
    /// Animation lookups (C in Classic)
    pub animation_lookup: M2Array<u16>,
    /// Playable animation lookup - only present in versions <= 263
    pub playable_animation_lookup: Option<M2Array<u16>>,

    // Bone-related fields
    /// Bones
    pub bones: M2Array<u32>,
    /// Key bone lookup
    pub key_bone_lookup: M2Array<u16>,

    // Geometry data
    /// Vertices
    pub vertices: M2Array<u32>,
    /// Views (LOD levels) - M2Array for BC and earlier, count for later versions
    pub views: M2Array<u32>,
    /// Number of skin profiles for WotLK+ (when views becomes a count)
    pub num_skin_profiles: Option<u32>,

    // Color data
    /// Color animations
    pub color_animations: M2Array<u32>,

    // Texture-related fields
    /// Textures
    pub textures: M2Array<u32>,
    /// Transparency lookups
    pub transparency_lookup: M2Array<u16>,
    /// Texture flipbooks - only present in BC and earlier
    pub texture_flipbooks: Option<M2Array<u32>>,
    /// Texture animations
    pub texture_animations: M2Array<u32>,

    // Material data
    /// Color replacements
    pub color_replacements: M2Array<u32>,
    /// Render flags
    pub render_flags: M2Array<u32>,
    /// Bone lookup table
    pub bone_lookup_table: M2Array<u16>,
    /// Texture lookup table
    pub texture_lookup_table: M2Array<u16>,
    /// Texture units
    pub texture_units: M2Array<u16>,
    /// Transparency lookup table
    pub transparency_lookup_table: M2Array<u16>,
    /// Texture animation lookup table
    pub texture_animation_lookup: M2Array<u16>,

    // Bounding box data
    /// Bounding box min corner
    pub bounding_box_min: [f32; 3],
    /// Bounding box max corner
    pub bounding_box_max: [f32; 3],
    /// Bounding sphere radius
    pub bounding_sphere_radius: f32,
    /// Collision bounding box min corner
    pub collision_box_min: [f32; 3],
    /// Collision bounding box max corner
    pub collision_box_max: [f32; 3],
    /// Collision bounding sphere radius
    pub collision_sphere_radius: f32,

    // Additional geometry data
    /// Bounding triangles
    pub bounding_triangles: M2Array<u32>,
    /// Bounding vertices
    pub bounding_vertices: M2Array<u32>,
    /// Bounding normals
    pub bounding_normals: M2Array<u32>,

    // Attachments and events
    /// Attachments
    pub attachments: M2Array<u32>,
    /// Attachment lookup table
    pub attachment_lookup_table: M2Array<u16>,
    /// Events
    pub events: M2Array<u32>,
    /// Lights
    pub lights: M2Array<u32>,
    /// Cameras
    pub cameras: M2Array<u32>,
    /// Camera lookup table
    pub camera_lookup_table: M2Array<u16>,

    // Ribbon emitters
    /// Ribbon emitters
    pub ribbon_emitters: M2Array<u32>,

    // Particle systems
    /// Particle emitters
    pub particle_emitters: M2Array<u32>,

    // Additional fields for newer versions
    /// Blend map overrides (BC+ with specific flag)
    pub blend_map_overrides: Option<M2Array<u32>>,
    /// Texture combiner combos (added in Cataclysm)
    pub texture_combiner_combos: Option<M2Array<u32>>,

    // Fields added in Legion
    /// Texture transforms
    pub texture_transforms: Option<M2Array<u32>>,
}

impl M2Header {
    /// Parse the M2 header from a reader
    pub fn parse<R: Read + Seek>(reader: &mut R) -> Result<Self> {
        // Read and check magic
        let mut magic = [0u8; 4];
        reader.read_exact(&mut magic)?;

        if magic != M2_MAGIC_LEGACY {
            return Err(M2Error::InvalidMagic {
                expected: String::from_utf8_lossy(&M2_MAGIC_LEGACY).to_string(),
                actual: String::from_utf8_lossy(&magic).to_string(),
            });
        }

        // Read version
        let version = reader.read_u32_le()?;

        // Check if version is supported
        if M2Version::from_header_version(version).is_none() {
            return Err(M2Error::UnsupportedVersion(version.to_string()));
        }

        // Read the common fields present in all versions
        let name = M2Array::parse(reader)?;
        let flags = M2ModelFlags::from_bits_retain(reader.read_u32_le()?);

        let global_sequences = M2Array::parse(reader)?;
        let animations = M2Array::parse(reader)?;
        let animation_lookup = M2Array::parse(reader)?;

        // FIXED: Vanilla (256) DOES have playable animation lookup, contrary to previous assumption
        // This field exists in vanilla, BC, and was removed in Wrath (264+)
        let playable_animation_lookup = if (256..=263).contains(&version) {
            Some(M2Array::parse(reader)?)
        } else {
            None
        };

        let bones = M2Array::parse(reader)?;
        let key_bone_lookup = M2Array::parse(reader)?;

        let vertices = M2Array::parse(reader)?;

        // Views field changes between versions
        let (views, num_skin_profiles) = if version <= 263 {
            // BC and earlier: views is M2Array
            (M2Array::parse(reader)?, None)
        } else {
            // WotLK+: views becomes a count (num_skin_profiles)
            let count = reader.read_u32_le()?;
            (M2Array::new(0, 0), Some(count))
        };

        let color_animations = M2Array::parse(reader)?;

        let textures = M2Array::parse(reader)?;
        let transparency_lookup = M2Array::parse(reader)?;

        // Texture flipbooks only exist in BC and earlier
        let texture_flipbooks = if version <= 263 {
            Some(M2Array::parse(reader)?)
        } else {
            None
        };

        let texture_animations = M2Array::parse(reader)?;

        let color_replacements = M2Array::parse(reader)?;
        let render_flags = M2Array::parse(reader)?;
        let bone_lookup_table = M2Array::parse(reader)?;
        let texture_lookup_table = M2Array::parse(reader)?;
        let texture_units = M2Array::parse(reader)?;
        let transparency_lookup_table = M2Array::parse(reader)?;
        let mut texture_animation_lookup = M2Array::parse(reader)?;

        // Workaround: Some M2 files have corrupted texture_animation_lookup fields
        // This may be due to field alignment differences across versions or file corruption
        // If the count is extremely large, treat as empty to prevent crashes
        if texture_animation_lookup.count > 1_000_000 {
            texture_animation_lookup = M2Array::new(0, 0);
        }

        // Read bounding box
        let mut bounding_box_min = [0.0; 3];
        let mut bounding_box_max = [0.0; 3];

        for item in &mut bounding_box_min {
            *item = reader.read_f32_le()?;
        }

        for item in &mut bounding_box_max {
            *item = reader.read_f32_le()?;
        }

        let bounding_sphere_radius = reader.read_f32_le()?;

        // Read collision box
        let mut collision_box_min = [0.0; 3];
        let mut collision_box_max = [0.0; 3];

        for item in &mut collision_box_min {
            *item = reader.read_f32_le()?;
        }

        for item in &mut collision_box_max {
            *item = reader.read_f32_le()?;
        }

        let collision_sphere_radius = reader.read_f32_le()?;

        let bounding_triangles = M2Array::parse(reader)?;
        let bounding_vertices = M2Array::parse(reader)?;
        let bounding_normals = M2Array::parse(reader)?;

        let attachments = M2Array::parse(reader)?;
        let attachment_lookup_table = M2Array::parse(reader)?;
        let events = M2Array::parse(reader)?;
        let lights = M2Array::parse(reader)?;
        let cameras = M2Array::parse(reader)?;
        let camera_lookup_table = M2Array::parse(reader)?;

        let ribbon_emitters = M2Array::parse(reader)?;
        let particle_emitters = M2Array::parse(reader)?;

        // Version-specific fields
        let m2_version = M2Version::from_header_version(version).unwrap();

        // Blend map overrides (BC+ with specific flag)
        let blend_map_overrides = if version >= 260 && (flags.bits() & 0x8000000 != 0) {
            // USE_BLEND_MAP_OVERRIDES flag - using hex value as we don't have the flag defined
            Some(M2Array::parse(reader)?)
        } else {
            None
        };

        let texture_combiner_combos = if m2_version >= M2Version::Cataclysm {
            Some(M2Array::parse(reader)?)
        } else {
            None
        };

        let texture_transforms = if m2_version >= M2Version::Legion {
            Some(M2Array::parse(reader)?)
        } else {
            None
        };

        Ok(Self {
            magic,
            version,
            name,
            flags,
            global_sequences,
            animations,
            animation_lookup,
            playable_animation_lookup,
            bones,
            key_bone_lookup,
            vertices,
            views,
            num_skin_profiles,
            color_animations,
            textures,
            transparency_lookup,
            texture_flipbooks,
            texture_animations,
            color_replacements,
            render_flags,
            bone_lookup_table,
            texture_lookup_table,
            texture_units,
            transparency_lookup_table,
            texture_animation_lookup,
            bounding_box_min,
            bounding_box_max,
            bounding_sphere_radius,
            collision_box_min,
            collision_box_max,
            collision_sphere_radius,
            bounding_triangles,
            bounding_vertices,
            bounding_normals,
            attachments,
            attachment_lookup_table,
            events,
            lights,
            cameras,
            camera_lookup_table,
            ribbon_emitters,
            particle_emitters,
            blend_map_overrides,
            texture_combiner_combos,
            texture_transforms,
        })
    }

    /// Write the M2 header to a writer
    pub fn write<W: Write>(&self, writer: &mut W) -> Result<()> {
        // Write magic and version
        writer.write_all(&self.magic)?;
        writer.write_u32_le(self.version)?;

        // Write common fields
        self.name.write(writer)?;
        writer.write_u32_le(self.flags.bits())?;

        self.global_sequences.write(writer)?;
        self.animations.write(writer)?;
        self.animation_lookup.write(writer)?;

        // Vanilla/TBC versions have playable animation lookup
        if self.version <= 263
            && let Some(ref pal) = self.playable_animation_lookup
        {
            pal.write(writer)?;
        }

        self.bones.write(writer)?;
        self.key_bone_lookup.write(writer)?;

        self.vertices.write(writer)?;

        // Views field changes between versions
        if self.version <= 263 {
            // BC and earlier: views is M2Array
            self.views.write(writer)?;
        } else {
            // WotLK+: write num_skin_profiles as u32
            let count = self.num_skin_profiles.unwrap_or(0);
            writer.write_u32_le(count)?;
        }

        self.color_animations.write(writer)?;

        self.textures.write(writer)?;
        self.transparency_lookup.write(writer)?;

        // Texture flipbooks only exist in BC and earlier
        if self.version <= 263
            && let Some(ref flipbooks) = self.texture_flipbooks
        {
            flipbooks.write(writer)?;
        }

        self.texture_animations.write(writer)?;

        self.color_replacements.write(writer)?;
        self.render_flags.write(writer)?;
        self.bone_lookup_table.write(writer)?;
        self.texture_lookup_table.write(writer)?;
        self.texture_units.write(writer)?;
        self.transparency_lookup_table.write(writer)?;
        self.texture_animation_lookup.write(writer)?;

        // Write bounding box
        for &value in &self.bounding_box_min {
            writer.write_f32_le(value)?;
        }

        for &value in &self.bounding_box_max {
            writer.write_f32_le(value)?;
        }

        writer.write_f32_le(self.bounding_sphere_radius)?;

        // Write collision box
        for &value in &self.collision_box_min {
            writer.write_f32_le(value)?;
        }

        for &value in &self.collision_box_max {
            writer.write_f32_le(value)?;
        }

        writer.write_f32_le(self.collision_sphere_radius)?;

        self.bounding_triangles.write(writer)?;
        self.bounding_vertices.write(writer)?;
        self.bounding_normals.write(writer)?;

        self.attachments.write(writer)?;
        self.attachment_lookup_table.write(writer)?;
        self.events.write(writer)?;
        self.lights.write(writer)?;
        self.cameras.write(writer)?;
        self.camera_lookup_table.write(writer)?;

        self.ribbon_emitters.write(writer)?;
        self.particle_emitters.write(writer)?;

        // Version-specific fields
        if let Some(ref overrides) = self.blend_map_overrides {
            overrides.write(writer)?;
        }

        if let Some(ref combos) = self.texture_combiner_combos {
            combos.write(writer)?;
        }

        if let Some(ref transforms) = self.texture_transforms {
            transforms.write(writer)?;
        }

        Ok(())
    }

    /// Get the version of the M2 file
    pub fn version(&self) -> Option<M2Version> {
        M2Version::from_header_version(self.version)
    }

    /// Create a new M2 header for a specific version
    pub fn new(version: M2Version) -> Self {
        let version_num = version.to_header_version();

        let texture_combiner_combos = if version >= M2Version::Cataclysm {
            Some(M2Array::new(0, 0))
        } else {
            None
        };

        let texture_transforms = if version >= M2Version::Legion {
            Some(M2Array::new(0, 0))
        } else {
            None
        };

        // Version-specific fields
        let playable_animation_lookup = if (260..=263).contains(&version_num) {
            Some(M2Array::new(0, 0))
        } else {
            None
        };

        let texture_flipbooks = if version_num <= 263 {
            Some(M2Array::new(0, 0))
        } else {
            None
        };

        let num_skin_profiles = if version_num > 263 { Some(0) } else { None };

        Self {
            magic: M2_MAGIC_LEGACY,
            version: version_num,
            name: M2Array::new(0, 0),
            flags: M2ModelFlags::empty(),
            global_sequences: M2Array::new(0, 0),
            animations: M2Array::new(0, 0),
            animation_lookup: M2Array::new(0, 0),
            playable_animation_lookup,
            bones: M2Array::new(0, 0),
            key_bone_lookup: M2Array::new(0, 0),
            vertices: M2Array::new(0, 0),
            views: M2Array::new(0, 0),
            num_skin_profiles,
            color_animations: M2Array::new(0, 0),
            textures: M2Array::new(0, 0),
            transparency_lookup: M2Array::new(0, 0),
            texture_flipbooks,
            texture_animations: M2Array::new(0, 0),
            color_replacements: M2Array::new(0, 0),
            render_flags: M2Array::new(0, 0),
            bone_lookup_table: M2Array::new(0, 0),
            texture_lookup_table: M2Array::new(0, 0),
            texture_units: M2Array::new(0, 0),
            transparency_lookup_table: M2Array::new(0, 0),
            texture_animation_lookup: M2Array::new(0, 0),
            bounding_box_min: [0.0, 0.0, 0.0],
            bounding_box_max: [0.0, 0.0, 0.0],
            bounding_sphere_radius: 0.0,
            collision_box_min: [0.0, 0.0, 0.0],
            collision_box_max: [0.0, 0.0, 0.0],
            collision_sphere_radius: 0.0,
            bounding_triangles: M2Array::new(0, 0),
            bounding_vertices: M2Array::new(0, 0),
            bounding_normals: M2Array::new(0, 0),
            attachments: M2Array::new(0, 0),
            attachment_lookup_table: M2Array::new(0, 0),
            events: M2Array::new(0, 0),
            lights: M2Array::new(0, 0),
            cameras: M2Array::new(0, 0),
            camera_lookup_table: M2Array::new(0, 0),
            ribbon_emitters: M2Array::new(0, 0),
            particle_emitters: M2Array::new(0, 0),
            blend_map_overrides: None,
            texture_combiner_combos,
            texture_transforms,
        }
    }

    /// Convert this header to a different version
    pub fn convert(&self, target_version: M2Version) -> Result<Self> {
        let source_version = self.version().ok_or(M2Error::ConversionError {
            from: self.version,
            to: target_version.to_header_version(),
            reason: "Unknown source version".to_string(),
        })?;

        if source_version == target_version {
            return Ok(self.clone());
        }

        let mut new_header = self.clone();
        new_header.version = target_version.to_header_version();

        // Handle views <-> num_skin_profiles transition at WotLK boundary
        // Pre-WotLK (version <= 263): views is M2Array containing embedded skin data
        // WotLK+ (version >= 264): num_skin_profiles is a u32 count of external .skin files
        if target_version >= M2Version::WotLK && source_version < M2Version::WotLK {
            // Upgrading to WotLK+: convert views M2Array count to num_skin_profiles
            // Use the views count as the number of skin profiles (or 1 if views was empty)
            let count = if self.views.count > 0 {
                self.views.count
            } else {
                1 // Default to 1 skin profile
            };
            new_header.num_skin_profiles = Some(count);
            new_header.views = M2Array::new(0, 0); // Clear views array
        } else if target_version < M2Version::WotLK && source_version >= M2Version::WotLK {
            // Downgrading from WotLK+: convert num_skin_profiles to views M2Array
            // The views M2Array would normally point to embedded skin data, but we can't
            // recreate that, so we set it to empty (skin data would need separate handling)
            new_header.num_skin_profiles = None;
            new_header.views = M2Array::new(0, 0);
        }

        // Handle playable_animation_lookup (removed in WotLK+)
        if target_version >= M2Version::WotLK && source_version < M2Version::WotLK {
            // Remove playable_animation_lookup when upgrading to WotLK+
            new_header.playable_animation_lookup = None;
        } else if target_version < M2Version::WotLK && source_version >= M2Version::WotLK {
            // Add empty playable_animation_lookup when downgrading to pre-WotLK
            new_header.playable_animation_lookup = Some(M2Array::new(0, 0));
        }

        // Handle texture_flipbooks (removed in WotLK+)
        if target_version >= M2Version::WotLK && source_version < M2Version::WotLK {
            // Remove texture_flipbooks when upgrading to WotLK+
            new_header.texture_flipbooks = None;
        } else if target_version < M2Version::WotLK && source_version >= M2Version::WotLK {
            // Add empty texture_flipbooks when downgrading to pre-WotLK
            new_header.texture_flipbooks = Some(M2Array::new(0, 0));
        }

        // Handle texture_combiner_combos (added in Cataclysm)
        if target_version >= M2Version::Cataclysm && source_version < M2Version::Cataclysm {
            new_header.texture_combiner_combos = Some(M2Array::new(0, 0));
        } else if target_version < M2Version::Cataclysm && source_version >= M2Version::Cataclysm {
            new_header.texture_combiner_combos = None;
        }

        // Handle texture_transforms (added in Legion)
        if target_version >= M2Version::Legion && source_version < M2Version::Legion {
            new_header.texture_transforms = Some(M2Array::new(0, 0));
        } else if target_version < M2Version::Legion && source_version >= M2Version::Legion {
            new_header.texture_transforms = None;
        }

        Ok(new_header)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Cursor;

    // Helper function to create a basic test header
    fn create_test_header(version: M2Version) -> Vec<u8> {
        let mut data = Vec::new();

        // Magic "MD20"
        data.extend_from_slice(&M2_MAGIC_LEGACY);

        // Version
        data.extend_from_slice(&version.to_header_version().to_le_bytes());

        // Name
        data.extend_from_slice(&0u32.to_le_bytes()); // count = 0
        data.extend_from_slice(&0u32.to_le_bytes()); // offset = 0

        // Flags
        data.extend_from_slice(&0u32.to_le_bytes());

        // Global sequences
        data.extend_from_slice(&0u32.to_le_bytes()); // count = 0
        data.extend_from_slice(&0u32.to_le_bytes()); // offset = 0

        // ... continue for all required fields
        // This is simplified for brevity - in a real test, we'd populate all fields

        // For brevity, let's just add enough bytes to cover the base header
        for _ in 0..100 {
            data.extend_from_slice(&0u32.to_le_bytes());
        }

        data
    }

    #[test]
    fn test_header_parse_classic() {
        let data = create_test_header(M2Version::Vanilla);
        let mut cursor = Cursor::new(data);

        let header = M2Header::parse(&mut cursor).unwrap();

        assert_eq!(header.magic, M2_MAGIC_LEGACY);
        assert_eq!(header.version, M2Version::Vanilla.to_header_version());
        assert_eq!(header.texture_combiner_combos, None);
        assert_eq!(header.texture_transforms, None);
    }

    #[test]
    fn test_header_parse_cataclysm() {
        let data = create_test_header(M2Version::Cataclysm);
        let mut cursor = Cursor::new(data);

        let header = M2Header::parse(&mut cursor).unwrap();

        assert_eq!(header.magic, M2_MAGIC_LEGACY);
        assert_eq!(header.version, M2Version::Cataclysm.to_header_version());
        assert!(header.texture_combiner_combos.is_some());
        assert_eq!(header.texture_transforms, None);
    }

    #[test]
    fn test_header_parse_legion() {
        let data = create_test_header(M2Version::Legion);
        let mut cursor = Cursor::new(data);

        let header = M2Header::parse(&mut cursor).unwrap();

        assert_eq!(header.magic, M2_MAGIC_LEGACY);
        assert_eq!(header.version, M2Version::Legion.to_header_version());
        assert!(header.texture_combiner_combos.is_some());
        assert!(header.texture_transforms.is_some());
    }

    #[test]
    fn test_header_conversion() {
        let classic_header = M2Header::new(M2Version::Vanilla);

        // Convert Classic to Cataclysm
        let cataclysm_header = classic_header.convert(M2Version::Cataclysm).unwrap();
        assert_eq!(
            cataclysm_header.version,
            M2Version::Cataclysm.to_header_version()
        );
        assert!(cataclysm_header.texture_combiner_combos.is_some());
        assert_eq!(cataclysm_header.texture_transforms, None);

        // Convert Cataclysm to Legion
        let legion_header = cataclysm_header.convert(M2Version::Legion).unwrap();
        assert_eq!(legion_header.version, M2Version::Legion.to_header_version());
        assert!(legion_header.texture_combiner_combos.is_some());
        assert!(legion_header.texture_transforms.is_some());

        // Convert Legion back to Classic
        let classic_header_2 = legion_header.convert(M2Version::Vanilla).unwrap();
        assert_eq!(
            classic_header_2.version,
            M2Version::Vanilla.to_header_version()
        );
        assert_eq!(classic_header_2.texture_combiner_combos, None);
        assert_eq!(classic_header_2.texture_transforms, None);
    }
}