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
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
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
use crate::io_ext::{ReadExt, WriteExt};
use std::io::{Read, Write};

use crate::common::{C2Vector, C3Vector};
use crate::error::Result;
use crate::version::M2Version;

/// Validation mode for vertex data parsing
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValidationMode {
    /// Apply all fixes aggressively - forces bone weights on vertices with zero weights
    /// This is the legacy behavior that ensures all vertices are animated
    Strict,
    /// Only fix clearly corrupted data - preserves intentional zero weights for static geometry
    /// Fixes out-of-bounds bone indices but preserves valid zero-weight vertices
    Permissive,
    /// No automatic fixes - preserves all original data
    /// Use with caution as this may result in invalid rendering
    None,
}

impl Default for ValidationMode {
    /// Default to Permissive mode - balances corruption fixes with data preservation
    fn default() -> Self {
        Self::Permissive
    }
}

bitflags::bitflags! {
    /// Vertex flags as defined in the M2 format
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct M2VertexFlags: u8 {
        /// Transform using bone 0
        const TRANSFORM_BONE_0 = 0x01;
        /// Transform using bone 1
        const TRANSFORM_BONE_1 = 0x02;
        /// Transform using bone 2
        const TRANSFORM_BONE_2 = 0x04;
        /// Transform using bone 3
        const TRANSFORM_BONE_3 = 0x08;
        /// Normal compressed
        const NORMAL_COMPRESSED = 0x10;
        /// Unknown 0x20
        const UNKNOWN_0x20 = 0x20;
        /// Unknown 0x40
        const UNKNOWN_0x40 = 0x40;
        /// Unknown 0x80
        const UNKNOWN_0x80 = 0x80;
    }
}

/// Represents a vertex in an M2 model
#[derive(Debug, Clone)]
pub struct M2Vertex {
    /// Position of the vertex
    pub position: C3Vector,
    /// Bone weights (0-255)
    pub bone_weights: [u8; 4],
    /// Bone indices
    pub bone_indices: [u8; 4],
    /// Normal vector
    pub normal: C3Vector,
    /// Primary texture coordinates
    pub tex_coords: C2Vector,
    /// Secondary texture coordinates (added in Cataclysm)
    pub tex_coords2: Option<C2Vector>,
}

impl M2Vertex {
    /// Parse a vertex from a reader based on the M2 version
    /// Uses default validation mode (Permissive)
    pub fn parse<R: Read>(reader: &mut R, version: u32) -> Result<Self> {
        Self::parse_with_validation(reader, version, None, ValidationMode::default())
    }

    /// Parse a vertex from a reader with bone index validation
    ///
    /// # Arguments
    ///
    /// * `reader` - The reader to parse from
    /// * `version` - The M2 version
    /// * `bone_count` - Optional bone count for validation (if None, no validation is performed)
    /// * `validation_mode` - Controls how aggressive the validation fixes are
    ///
    /// # Returns
    ///
    /// Returns a parsed vertex with validated bone indices
    pub fn parse_with_validation<R: Read>(
        reader: &mut R,
        _version: u32,
        bone_count: Option<u32>,
        validation_mode: ValidationMode,
    ) -> Result<Self> {
        // Position
        let position = C3Vector::parse(reader)?;

        // Bone weights
        let bone_weights = [
            reader.read_u8()?,
            reader.read_u8()?,
            reader.read_u8()?,
            reader.read_u8()?,
        ];

        // Bone indices
        let bone_indices = [
            reader.read_u8()?,
            reader.read_u8()?,
            reader.read_u8()?,
            reader.read_u8()?,
        ];

        // Normal
        let normal = C3Vector::parse(reader)?;

        // Texture coordinates
        let tex_coords = C2Vector::parse(reader)?;

        // Secondary texture coordinates (present in ALL versions, contrary to previous belief)
        // WMVx reference shows unknown1/unknown2 floats exist in vanilla
        let tex_coords2 = Some(C2Vector::parse(reader)?);

        // CRITICAL FIX: Validate bone indices and weights against actual bone count
        let (validated_bone_indices, validated_bone_weights) = if let Some(max_bones) = bone_count {
            Self::validate_bone_data(bone_indices, bone_weights, max_bones, validation_mode)
        } else {
            (bone_indices, bone_weights)
        };

        Ok(Self {
            position,
            bone_weights: validated_bone_weights,
            bone_indices: validated_bone_indices,
            normal,
            tex_coords,
            tex_coords2,
        })
    }

    /// Validate and fix bone indices and weights to ensure data integrity
    ///
    /// This fixes the critical issues where vertices in vanilla/TBC models have:
    /// 1. Bone indices that exceed the actual number of bones in the model
    /// 2. Zero bone weights for all bones (making vertices unmovable)
    /// 3. Invalid weight distributions
    ///
    /// The behavior depends on the validation mode:
    /// - `Strict`: Applies all fixes aggressively, forces weights on zero-weight vertices
    /// - `Permissive`: Fixes out-of-bounds indices, preserves valid zero weights for static geometry
    /// - `None`: Preserves original data without any fixes
    ///
    /// # Arguments
    ///
    /// * `bone_indices` - The raw bone indices from the vertex data
    /// * `bone_weights` - The raw bone weights from the vertex data
    /// * `bone_count` - The actual number of bones available in the model
    /// * `validation_mode` - Controls how aggressive the validation fixes are
    ///
    /// # Returns
    ///
    /// Returns (validated_indices, validated_weights) with fixes applied based on validation mode
    fn validate_bone_data(
        mut bone_indices: [u8; 4],
        mut bone_weights: [u8; 4],
        bone_count: u32,
        validation_mode: ValidationMode,
    ) -> ([u8; 4], [u8; 4]) {
        // Skip all validation if mode is None
        if validation_mode == ValidationMode::None {
            return (bone_indices, bone_weights);
        }

        // Track if we had invalid indices before fixing them (for Permissive mode corruption detection)
        let had_invalid_indices = bone_indices.iter().any(|&idx| (idx as u32) >= bone_count);

        // Fix invalid bone indices (both Strict and Permissive modes)
        // CRITICAL BUG FIX: Don't cast bone_count to u8 - this causes wraparound for bone_count > 255!
        // Instead, cast bone_index to u32 for comparison
        for bone_index in &mut bone_indices {
            if (*bone_index as u32) >= bone_count {
                // Invalid bone index found - clamp to valid range
                // Using 0 as a safe fallback (root bone)
                *bone_index = 0;
            }
        }

        // Fix zero bone weights issue - behavior depends on validation mode
        let total_weight: u32 = bone_weights.iter().map(|&w| w as u32).sum();
        if total_weight == 0 {
            match validation_mode {
                ValidationMode::Strict => {
                    // Strict mode: Force bone weight assignment for zero-weight vertices
                    // This ensures all vertices are animated but may break static geometry
                    bone_weights[0] = 255;
                    bone_indices[0] = 0; // Ensure it's the root bone
                }
                ValidationMode::Permissive => {
                    // Permissive mode: Preserve zero weights for static geometry
                    // Only fix if this appears to be corruption (had invalid bone indices with zero weights)
                    if had_invalid_indices {
                        // This looks like corruption - fix it
                        bone_weights[0] = 255;
                        bone_indices[0] = 0;
                    }
                    // Otherwise, preserve the zero weights as they may be intentional for static geometry
                }
                ValidationMode::None => {
                    // Already handled above - no changes
                }
            }
        }

        (bone_indices, bone_weights)
    }

    /// Write a vertex to a writer based on the M2 version
    ///
    /// Vertex size is always 48 bytes for all versions:
    /// position (12) + bone_weights (4) + bone_indices (4) + normal (12) + tex_coords (8) + tex_coords2 (8)
    ///
    /// Note: Secondary texture coordinates exist in ALL M2 versions (verified against vanilla files).
    /// They may be unused/zero in pre-Cataclysm models but the storage space is still present.
    pub fn write<W: Write>(&self, writer: &mut W, _version: u32) -> Result<()> {
        // Position (12 bytes)
        self.position.write(writer)?;

        // Bone weights (4 bytes)
        for &weight in &self.bone_weights {
            writer.write_u8(weight)?;
        }

        // Bone indices (4 bytes)
        for &index in &self.bone_indices {
            writer.write_u8(index)?;
        }

        // Normal (12 bytes)
        self.normal.write(writer)?;

        // Primary texture coordinates (8 bytes)
        self.tex_coords.write(writer)?;

        // Secondary texture coordinates (8 bytes) - present in all versions
        if let Some(tex_coords2) = self.tex_coords2 {
            tex_coords2.write(writer)?;
        } else {
            // Write default values
            C2Vector { x: 0.0, y: 0.0 }.write(writer)?;
        }

        Ok(())
    }

    /// Convert this vertex to a different version
    pub fn convert(&self, _target_version: M2Version) -> Self {
        // All versions now have the same structure, so no conversion needed
        self.clone()
    }

    /// Get the effective bone count used by this vertex
    pub fn effective_bone_count(&self) -> u32 {
        let mut count = 0;

        for i in 0..4 {
            if self.bone_weights[i] > 0 {
                count += 1;
            }
        }

        count
    }

    /// Calculate the size of this vertex in bytes for a specific version
    pub fn size_in_bytes(_version: M2Version) -> usize {
        // ALL versions have the same vertex structure size (48 bytes)
        // Position (3 floats) + bone weights (4 bytes) + bone indices (4 bytes) +
        // Normal (3 floats) + texture coords (2 floats) + secondary tex coords (2 floats)
        3 * 4 + 4 + 4 + 3 * 4 + 2 * 4 + 2 * 4 // = 48 bytes
    }
}

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

    #[test]
    fn test_vertex_parse_classic() {
        let mut data = Vec::new();

        // Position
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&2.0f32.to_le_bytes());
        data.extend_from_slice(&3.0f32.to_le_bytes());

        // Bone weights
        data.push(255);
        data.push(128);
        data.push(64);
        data.push(0);

        // Bone indices
        data.push(0);
        data.push(1);
        data.push(2);
        data.push(3);

        // Normal
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&std::f32::consts::FRAC_1_SQRT_2.to_le_bytes());

        // Texture coordinates
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&1.0f32.to_le_bytes());

        // Secondary texture coordinates (now required for ALL versions)
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.5f32.to_le_bytes());

        let mut cursor = Cursor::new(data);
        let vertex = M2Vertex::parse(&mut cursor, M2Version::Vanilla.to_header_version()).unwrap();

        assert_eq!(vertex.position.x, 1.0);
        assert_eq!(vertex.position.y, 2.0);
        assert_eq!(vertex.position.z, 3.0);

        assert_eq!(vertex.bone_weights, [255, 128, 64, 0]);
        assert_eq!(vertex.bone_indices, [0, 1, 2, 3]);

        assert_eq!(vertex.normal.x, 0.5);
        assert_eq!(vertex.normal.y, 0.5);
        assert!((vertex.normal.z - std::f32::consts::FRAC_1_SQRT_2).abs() < 0.0001);

        assert_eq!(vertex.tex_coords.x, 0.0);
        assert_eq!(vertex.tex_coords.y, 1.0);

        // Secondary texture coordinates should now be present in Vanilla
        assert!(vertex.tex_coords2.is_some());
        let tex_coords2 = vertex.tex_coords2.unwrap();
        assert_eq!(tex_coords2.x, 0.5);
        assert_eq!(tex_coords2.y, 0.5);
    }

    #[test]
    fn test_vertex_parse_cataclysm() {
        let mut data = Vec::new();

        // Position
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&2.0f32.to_le_bytes());
        data.extend_from_slice(&3.0f32.to_le_bytes());

        // Bone weights
        data.push(255);
        data.push(128);
        data.push(64);
        data.push(0);

        // Bone indices
        data.push(0);
        data.push(1);
        data.push(2);
        data.push(3);

        // Normal
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&std::f32::consts::FRAC_1_SQRT_2.to_le_bytes());

        // Texture coordinates
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&1.0f32.to_le_bytes());

        // Secondary texture coordinates (added in Cataclysm)
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.5f32.to_le_bytes());

        let mut cursor = Cursor::new(data);
        let vertex =
            M2Vertex::parse(&mut cursor, M2Version::Cataclysm.to_header_version()).unwrap();

        assert_eq!(vertex.position.x, 1.0);
        assert_eq!(vertex.position.y, 2.0);
        assert_eq!(vertex.position.z, 3.0);

        assert_eq!(vertex.bone_weights, [255, 128, 64, 0]);
        assert_eq!(vertex.bone_indices, [0, 1, 2, 3]);

        assert_eq!(vertex.normal.x, 0.5);
        assert_eq!(vertex.normal.y, 0.5);
        assert!((vertex.normal.z - std::f32::consts::FRAC_1_SQRT_2).abs() < 0.0001);

        assert_eq!(vertex.tex_coords.x, 0.0);
        assert_eq!(vertex.tex_coords.y, 1.0);

        assert!(vertex.tex_coords2.is_some());
        let tex_coords2 = vertex.tex_coords2.unwrap();
        assert_eq!(tex_coords2.x, 0.5);
        assert_eq!(tex_coords2.y, 0.5);
    }

    #[test]
    fn test_vertex_write() {
        let vertex = M2Vertex {
            position: C3Vector {
                x: 1.0,
                y: 2.0,
                z: 3.0,
            },
            bone_weights: [255, 128, 64, 0],
            bone_indices: [0, 1, 2, 3],
            normal: C3Vector {
                x: 0.5,
                y: 0.5,
                z: std::f32::consts::FRAC_1_SQRT_2,
            },
            tex_coords: C2Vector { x: 0.0, y: 1.0 },
            tex_coords2: Some(C2Vector { x: 0.5, y: 0.5 }),
        };

        // Test writing in Vanilla format
        let mut classic_data = Vec::new();
        vertex
            .write(&mut classic_data, M2Version::Vanilla.to_header_version())
            .unwrap();

        // All versions have 48-byte vertices (including secondary tex coords)
        assert_eq!(classic_data.len(), 48);

        // Test writing in Cataclysm format
        let mut cata_data = Vec::new();
        vertex
            .write(&mut cata_data, M2Version::Cataclysm.to_header_version())
            .unwrap();

        // Same size (48 bytes) for all versions
        assert_eq!(cata_data.len(), 48);
    }

    #[test]
    fn test_vertex_conversion() {
        // Create a Classic vertex
        let classic_vertex = M2Vertex {
            position: C3Vector {
                x: 1.0,
                y: 2.0,
                z: 3.0,
            },
            bone_weights: [255, 128, 64, 0],
            bone_indices: [0, 1, 2, 3],
            normal: C3Vector {
                x: 0.5,
                y: 0.5,
                z: std::f32::consts::FRAC_1_SQRT_2,
            },
            tex_coords: C2Vector { x: 0.0, y: 1.0 },
            tex_coords2: Some(C2Vector { x: 0.5, y: 0.5 }),
        };

        // Convert to Cataclysm (no change expected)
        let cata_vertex = classic_vertex.convert(M2Version::Cataclysm);

        // Should be identical (all versions have same structure now)
        assert!(cata_vertex.tex_coords2.is_some());
        let tex_coords2 = cata_vertex.tex_coords2.unwrap();
        assert_eq!(tex_coords2.x, 0.5);
        assert_eq!(tex_coords2.y, 0.5);

        // Convert back to Classic (no change expected)
        let classic_vertex2 = cata_vertex.convert(M2Version::Vanilla);

        // Should still have secondary texture coordinates
        assert!(classic_vertex2.tex_coords2.is_some());
    }

    #[test]
    fn test_bone_index_validation() {
        // Test the critical bug fix for out-of-bounds bone indices
        let mut data = Vec::new();

        // Position
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&2.0f32.to_le_bytes());
        data.extend_from_slice(&3.0f32.to_le_bytes());

        // Bone weights
        data.push(255);
        data.push(128);
        data.push(64);
        data.push(0);

        // PROBLEMATIC bone indices that exceed available bones
        // This simulates the vanilla/TBC issue where vertices reference bones 196, 141, etc.
        data.push(196); // Invalid - should be clamped to 0
        data.push(141); // Invalid - should be clamped to 0
        data.push(100); // Invalid (>=96) - should be clamped to 0  
        data.push(255); // Invalid - should be clamped to 0

        // Normal
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&std::f32::consts::FRAC_1_SQRT_2.to_le_bytes());

        // Texture coordinates
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&1.0f32.to_le_bytes());

        // Secondary texture coordinates (required for all versions)
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.5f32.to_le_bytes());

        let mut cursor = Cursor::new(data);

        // Parse with validation for a model with only 96 bones (vanilla scenario)
        let vertex = M2Vertex::parse_with_validation(
            &mut cursor,
            M2Version::Vanilla.to_header_version(),
            Some(96),
            ValidationMode::default(),
        )
        .unwrap();

        // All invalid bone indices should be clamped to 0
        assert_eq!(vertex.bone_indices, [0, 0, 0, 0]);

        // Other data should be preserved
        assert_eq!(vertex.position.x, 1.0);
        assert_eq!(vertex.bone_weights, [255, 128, 64, 0]);
    }

    #[test]
    fn test_bone_index_validation_valid_indices() {
        // Test that valid bone indices are preserved
        let mut data = Vec::new();

        // Position
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&2.0f32.to_le_bytes());
        data.extend_from_slice(&3.0f32.to_le_bytes());

        // Bone weights
        data.push(255);
        data.push(128);
        data.push(64);
        data.push(0);

        // Valid bone indices within range
        data.push(0);
        data.push(10);
        data.push(50);
        data.push(95);

        // Normal
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&std::f32::consts::FRAC_1_SQRT_2.to_le_bytes());

        // Texture coordinates
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&1.0f32.to_le_bytes());

        // Secondary texture coordinates (required for all versions)
        data.extend_from_slice(&0.25f32.to_le_bytes());
        data.extend_from_slice(&0.75f32.to_le_bytes());

        let mut cursor = Cursor::new(data);

        // Parse with validation for a model with 96 bones
        let vertex = M2Vertex::parse_with_validation(
            &mut cursor,
            M2Version::Vanilla.to_header_version(),
            Some(96),
            ValidationMode::default(),
        )
        .unwrap();

        // Valid bone indices should be preserved
        assert_eq!(vertex.bone_indices, [0, 10, 50, 95]);
        // Valid bone weights should be preserved
        assert_eq!(vertex.bone_weights, [255, 128, 64, 0]);
    }

    #[test]
    fn test_zero_bone_weights_fix() {
        // Test the fix for vertices with zero bone weights (17% of vertices in issue)
        let mut data = Vec::new();

        // Position
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&2.0f32.to_le_bytes());
        data.extend_from_slice(&3.0f32.to_le_bytes());

        // ALL ZERO bone weights (this is the issue)
        data.push(0);
        data.push(0);
        data.push(0);
        data.push(0);

        // Some bone indices (could be invalid too)
        data.push(50); // Invalid - will be clamped
        data.push(100); // Invalid - will be clamped
        data.push(200); // Invalid - will be clamped
        data.push(255); // Invalid - will be clamped

        // Normal
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&0.0f32.to_le_bytes());

        // Texture coordinates
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&1.0f32.to_le_bytes());

        let mut cursor = Cursor::new(data);

        // Parse with validation for a model with 34 bones (Rabbit.m2 scenario)
        let vertex = M2Vertex::parse_with_validation(
            &mut cursor,
            M2Version::Vanilla.to_header_version(),
            Some(34),
            ValidationMode::Strict, // Use Strict mode to test the old behavior
        )
        .unwrap();

        // Zero weights should be fixed - first bone should get full weight
        assert_eq!(vertex.bone_weights[0], 255);
        assert_eq!(vertex.bone_weights[1], 0);
        assert_eq!(vertex.bone_weights[2], 0);
        assert_eq!(vertex.bone_weights[3], 0);

        // First bone index should be set to 0 (root bone) for safety
        assert_eq!(vertex.bone_indices[0], 0);

        // Total weight should no longer be zero
        let total_weight: u32 = vertex.bone_weights.iter().map(|&w| w as u32).sum();
        assert_eq!(total_weight, 255);
        assert!(
            total_weight > 0,
            "Vertex should no longer have zero total weight"
        );
    }

    #[test]
    fn test_critical_bug_bone_count_wraparound() {
        // Test the critical bug where bone_count > 255 causes u8 wraparound
        // This was causing validation to fail for models with many bones
        let mut data = Vec::new();

        // Position
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&2.0f32.to_le_bytes());
        data.extend_from_slice(&3.0f32.to_le_bytes());

        // Bone weights
        data.push(255);
        data.push(0);
        data.push(0);
        data.push(0);

        // Bone indices - these should be VALID for a model with 300 bones
        data.push(100);
        data.push(200);
        data.push(250);
        data.push(0);

        // Normal
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&0.0f32.to_le_bytes());

        // Texture coordinates
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.5f32.to_le_bytes());

        // Secondary texture coordinates
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&0.0f32.to_le_bytes());

        let mut cursor = std::io::Cursor::new(data);

        // Test with 300 bones - before the fix, bone_count as u8 would wrap to 44
        // This would incorrectly mark bone indices 100, 200, 250 as invalid
        let vertex = M2Vertex::parse_with_validation(
            &mut cursor,
            M2Version::Vanilla.to_header_version(),
            Some(300), // Large bone count that causes u8 wraparound
            ValidationMode::default(),
        )
        .unwrap();

        // These bone indices should be PRESERVED, not clamped to 0
        assert_eq!(
            vertex.bone_indices,
            [100, 200, 250, 0],
            "Bone indices should be preserved for models with > 255 bones, got: {:?}",
            vertex.bone_indices
        );

        // Weights should be unchanged
        assert_eq!(vertex.bone_weights, [255, 0, 0, 0]);
    }

    #[test]
    fn test_bone_count_255_boundary() {
        // Test the boundary case at exactly 255 bones
        let mut data = Vec::new();

        // Position
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&2.0f32.to_le_bytes());
        data.extend_from_slice(&3.0f32.to_le_bytes());

        // Bone weights
        data.push(255);
        data.push(0);
        data.push(0);
        data.push(0);

        // Test edge cases: bone indices 254 (valid) and 255 (invalid)
        data.push(254); // Should be valid for 255 bones (0-254)
        data.push(255); // Should be INVALID for 255 bones
        data.push(0);
        data.push(0);

        // Normal
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&0.0f32.to_le_bytes());

        // Texture coordinates
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.5f32.to_le_bytes());

        // Secondary texture coordinates
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&0.0f32.to_le_bytes());

        let mut cursor = std::io::Cursor::new(data);

        let vertex = M2Vertex::parse_with_validation(
            &mut cursor,
            M2Version::Vanilla.to_header_version(),
            Some(255),
            ValidationMode::default(),
        )
        .unwrap();

        // Bone index 254 should be preserved, 255 should be clamped to 0
        assert_eq!(
            vertex.bone_indices,
            [254, 0, 0, 0],
            "Bone index 254 should be valid, 255 should be clamped to 0, got: {:?}",
            vertex.bone_indices
        );
    }

    #[test]
    fn test_comprehensive_validation_all_issues() {
        // Test that all three critical issues are fixed simultaneously:
        // 1. Invalid bone indices: [51, 196, 141, 62] when only 34 bones exist
        // 2. Zero bone weights: All weights are 0
        // 3. Both issues combined (realistic corruption scenario)

        let mut data = Vec::new();

        // Position
        data.extend_from_slice(&(-2.5f32).to_le_bytes());
        data.extend_from_slice(&1.8f32.to_le_bytes());
        data.extend_from_slice(&0.3f32.to_le_bytes());

        // ZERO bone weights (Issue #3: Missing bone weights)
        data.push(0);
        data.push(0);
        data.push(0);
        data.push(0);

        // INVALID bone indices from the exact issue report (Issue #1)
        data.push(51); // > 34 bones
        data.push(196); // > 34 bones
        data.push(141); // > 34 bones
        data.push(62); // > 34 bones

        // Normal
        data.extend_from_slice(&std::f32::consts::FRAC_1_SQRT_2.to_le_bytes());
        data.extend_from_slice(&std::f32::consts::FRAC_1_SQRT_2.to_le_bytes());
        data.extend_from_slice(&0.0f32.to_le_bytes());

        // Texture coordinates
        data.extend_from_slice(&0.25f32.to_le_bytes());
        data.extend_from_slice(&0.75f32.to_le_bytes());
        data.extend_from_slice(&0.1f32.to_le_bytes());
        data.extend_from_slice(&0.9f32.to_le_bytes());

        let mut cursor = Cursor::new(data);

        // Parse with validation for Rabbit.m2 scenario (34 bones as reported)
        let vertex = M2Vertex::parse_with_validation(
            &mut cursor,
            M2Version::Vanilla.to_header_version(),
            Some(34),               // Exact bone count from issue report
            ValidationMode::Strict, // Use Strict mode to test comprehensive fixes
        )
        .unwrap();

        // Verify all fixes applied:

        // Fix #1: Invalid bone indices should be corrected
        assert!(
            vertex.bone_indices.iter().all(|&idx| (idx as u32) < 34),
            "All bone indices should be < 34, got: {:?}",
            vertex.bone_indices
        );

        // Fix #3: Zero bone weights should be corrected
        let total_weight: u32 = vertex.bone_weights.iter().map(|&w| w as u32).sum();
        assert!(
            total_weight > 0,
            "Total bone weight should not be zero, got: {}",
            total_weight
        );
        assert_eq!(
            vertex.bone_weights[0], 255,
            "First bone should get full weight when all were zero"
        );

        // Safety: First bone should be root (0) for the weight assignment
        assert_eq!(
            vertex.bone_indices[0], 0,
            "First bone should be root bone for safety"
        );

        // Other data should be preserved
        assert_eq!(vertex.position.x, -2.5);
        assert_eq!(vertex.position.y, 1.8);
        assert_eq!(vertex.position.z, 0.3);

        println!("✅ All critical vertex issues fixed:");
        println!(
            "   Original bone indices: [51, 196, 141, 62] -> {:?}",
            vertex.bone_indices
        );
        println!(
            "   Original bone weights: [0, 0, 0, 0] -> {:?}",
            vertex.bone_weights
        );
        println!("   Total weight: 0 -> {}", total_weight);
    }

    #[test]
    fn test_validation_mode_strict() {
        // Test ValidationMode::Strict - should fix all zero weights
        let mut data = Vec::new();

        // Position
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&2.0f32.to_le_bytes());
        data.extend_from_slice(&3.0f32.to_le_bytes());

        // Zero bone weights (static geometry)
        data.push(0);
        data.push(0);
        data.push(0);
        data.push(0);

        // Valid bone indices
        data.push(0);
        data.push(1);
        data.push(2);
        data.push(3);

        // Normal
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&0.0f32.to_le_bytes());

        // Texture coordinates
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&1.0f32.to_le_bytes());

        let mut cursor = Cursor::new(data);

        let vertex = M2Vertex::parse_with_validation(
            &mut cursor,
            M2Version::Vanilla.to_header_version(),
            Some(10),
            ValidationMode::Strict,
        )
        .unwrap();

        // Strict mode should force weight on first bone
        assert_eq!(vertex.bone_weights[0], 255);
        assert_eq!(vertex.bone_weights[1], 0);
        assert_eq!(vertex.bone_weights[2], 0);
        assert_eq!(vertex.bone_weights[3], 0);
        assert_eq!(vertex.bone_indices[0], 0); // Should be root bone
    }

    #[test]
    fn test_validation_mode_permissive() {
        // Test ValidationMode::Permissive - should preserve valid zero weights
        let mut data = Vec::new();

        // Position
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&2.0f32.to_le_bytes());
        data.extend_from_slice(&3.0f32.to_le_bytes());

        // Zero bone weights (static geometry)
        data.push(0);
        data.push(0);
        data.push(0);
        data.push(0);

        // Valid bone indices - all within range
        data.push(0);
        data.push(1);
        data.push(2);
        data.push(3);

        // Normal
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&0.0f32.to_le_bytes());

        // Texture coordinates
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&1.0f32.to_le_bytes());

        let mut cursor = Cursor::new(data);

        let vertex = M2Vertex::parse_with_validation(
            &mut cursor,
            M2Version::Vanilla.to_header_version(),
            Some(10),
            ValidationMode::Permissive,
        )
        .unwrap();

        // Permissive mode should preserve zero weights when indices are valid
        assert_eq!(vertex.bone_weights, [0, 0, 0, 0]);
        assert_eq!(vertex.bone_indices, [0, 1, 2, 3]);

        let total_weight: u32 = vertex.bone_weights.iter().map(|&w| w as u32).sum();
        assert_eq!(
            total_weight, 0,
            "Static geometry should preserve zero weights"
        );
    }

    #[test]
    fn test_validation_mode_permissive_with_corruption() {
        // Test ValidationMode::Permissive with corrupted data - should fix it
        let mut data = Vec::new();

        // Position
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&2.0f32.to_le_bytes());
        data.extend_from_slice(&3.0f32.to_le_bytes());

        // Zero bone weights (static geometry)
        data.push(0);
        data.push(0);
        data.push(0);
        data.push(0);

        // Invalid bone indices - suggests corruption
        data.push(200); // Invalid
        data.push(150); // Invalid
        data.push(100); // Invalid
        data.push(255); // Invalid

        // Normal
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&0.0f32.to_le_bytes());

        // Texture coordinates
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&1.0f32.to_le_bytes());

        let mut cursor = Cursor::new(data);

        let vertex = M2Vertex::parse_with_validation(
            &mut cursor,
            M2Version::Vanilla.to_header_version(),
            Some(10), // Only 10 bones, so all indices are invalid
            ValidationMode::Permissive,
        )
        .unwrap();

        // Permissive mode should detect this as corruption and fix it
        assert_eq!(
            vertex.bone_weights[0], 255,
            "Should fix zero weights when corruption detected"
        );
        assert_eq!(
            vertex.bone_indices[0], 0,
            "Should use root bone when fixing corruption"
        );

        let total_weight: u32 = vertex.bone_weights.iter().map(|&w| w as u32).sum();
        assert!(total_weight > 0, "Should fix corrupted zero weights");
    }

    #[test]
    fn test_validation_mode_none() {
        // Test ValidationMode::None - should preserve all original data
        let mut data = Vec::new();

        // Position
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&2.0f32.to_le_bytes());
        data.extend_from_slice(&3.0f32.to_le_bytes());

        // Zero bone weights
        data.push(0);
        data.push(0);
        data.push(0);
        data.push(0);

        // Invalid bone indices
        data.push(200);
        data.push(150);
        data.push(100);
        data.push(255);

        // Normal
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&1.0f32.to_le_bytes());
        data.extend_from_slice(&0.0f32.to_le_bytes());

        // Texture coordinates
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.5f32.to_le_bytes());
        data.extend_from_slice(&0.0f32.to_le_bytes());
        data.extend_from_slice(&1.0f32.to_le_bytes());

        let mut cursor = Cursor::new(data);

        let vertex = M2Vertex::parse_with_validation(
            &mut cursor,
            M2Version::Vanilla.to_header_version(),
            Some(10),
            ValidationMode::None,
        )
        .unwrap();

        // None mode should preserve original data exactly as-is
        assert_eq!(
            vertex.bone_weights,
            [0, 0, 0, 0],
            "Should preserve original weights"
        );
        assert_eq!(
            vertex.bone_indices,
            [200, 150, 100, 255],
            "Should preserve original indices"
        );

        let total_weight: u32 = vertex.bone_weights.iter().map(|&w| w as u32).sum();
        assert_eq!(total_weight, 0, "Should preserve zero total weight");
    }

    #[test]
    fn test_validation_mode_comparison() {
        // Compare all three modes with the same corrupted data
        let create_test_data = || -> Vec<u8> {
            let mut data = Vec::new();

            // Position
            data.extend_from_slice(&1.0f32.to_le_bytes());
            data.extend_from_slice(&2.0f32.to_le_bytes());
            data.extend_from_slice(&3.0f32.to_le_bytes());

            // Zero bone weights
            data.push(0);
            data.push(0);
            data.push(0);
            data.push(0);

            // Mixed valid/invalid bone indices
            data.push(0); // Valid
            data.push(200); // Invalid
            data.push(5); // Valid
            data.push(150); // Invalid

            // Normal
            data.extend_from_slice(&0.0f32.to_le_bytes());
            data.extend_from_slice(&1.0f32.to_le_bytes());
            data.extend_from_slice(&0.0f32.to_le_bytes());

            // Texture coordinates
            data.extend_from_slice(&0.5f32.to_le_bytes());
            data.extend_from_slice(&0.5f32.to_le_bytes());
            data.extend_from_slice(&0.0f32.to_le_bytes());
            data.extend_from_slice(&1.0f32.to_le_bytes());

            data
        };

        let bone_count = 10;

        // Test Strict mode
        let mut cursor_strict = Cursor::new(create_test_data());
        let vertex_strict = M2Vertex::parse_with_validation(
            &mut cursor_strict,
            M2Version::Vanilla.to_header_version(),
            Some(bone_count),
            ValidationMode::Strict,
        )
        .unwrap();

        // Test Permissive mode
        let mut cursor_permissive = Cursor::new(create_test_data());
        let vertex_permissive = M2Vertex::parse_with_validation(
            &mut cursor_permissive,
            M2Version::Vanilla.to_header_version(),
            Some(bone_count),
            ValidationMode::Permissive,
        )
        .unwrap();

        // Test None mode
        let mut cursor_none = Cursor::new(create_test_data());
        let vertex_none = M2Vertex::parse_with_validation(
            &mut cursor_none,
            M2Version::Vanilla.to_header_version(),
            Some(bone_count),
            ValidationMode::None,
        )
        .unwrap();

        // Verify different behaviors
        println!(
            "Strict mode: indices={:?}, weights={:?}",
            vertex_strict.bone_indices, vertex_strict.bone_weights
        );
        println!(
            "Permissive mode: indices={:?}, weights={:?}",
            vertex_permissive.bone_indices, vertex_permissive.bone_weights
        );
        println!(
            "None mode: indices={:?}, weights={:?}",
            vertex_none.bone_indices, vertex_none.bone_weights
        );

        // Strict: should fix both indices and weights
        assert_eq!(
            vertex_strict.bone_weights[0], 255,
            "Strict should force weights"
        );
        assert_eq!(
            vertex_strict.bone_indices,
            [0, 0, 5, 0],
            "Strict should fix invalid indices, preserve valid ones"
        );

        // Permissive: should fix indices but also detect corruption and fix weights due to invalid indices
        assert_eq!(
            vertex_permissive.bone_weights[0], 255,
            "Permissive should fix weights when corruption detected"
        );
        assert_eq!(
            vertex_permissive.bone_indices,
            [0, 0, 5, 0],
            "Permissive should fix invalid indices"
        );

        // None: should preserve everything
        assert_eq!(
            vertex_none.bone_weights,
            [0, 0, 0, 0],
            "None should preserve weights"
        );
        assert_eq!(
            vertex_none.bone_indices,
            [0, 200, 5, 150],
            "None should preserve indices"
        );
    }
}