wow_m2/chunks/
texture.rs

1use crate::io_ext::{ReadExt, WriteExt};
2use std::io::{Read, Write};
3
4use crate::common::M2Array;
5use crate::error::Result;
6use crate::version::M2Version;
7
8/// Texture type enum as defined in the M2 format
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum M2TextureType {
11    /// Regular texture
12    Hardcoded = 0,
13    /// Body + clothes
14    Body = 1,
15    /// Item, capes
16    Item = 2,
17    /// Weapon, armor (armorless)
18    WeaponArmorBasic = 3,
19    /// Weapon blade
20    WeaponBlade = 4,
21    /// Weapon handle
22    WeaponHandle = 5,
23    /// Environment
24    Environment = 6,
25    /// Hair, beard
26    Hair = 7,
27    /// Accessories
28    Accessories = 8,
29    /// Custom type, not used
30    Custom1 = 9,
31    /// Custom type, not used
32    Custom2 = 10,
33    /// Custom type, not used
34    Custom3 = 11,
35}
36
37impl M2TextureType {
38    /// Parse from integer value
39    pub fn from_u32(value: u32) -> Option<Self> {
40        match value {
41            0 => Some(Self::Hardcoded),
42            1 => Some(Self::Body),
43            2 => Some(Self::Item),
44            3 => Some(Self::WeaponArmorBasic),
45            4 => Some(Self::WeaponBlade),
46            5 => Some(Self::WeaponHandle),
47            6 => Some(Self::Environment),
48            7 => Some(Self::Hair),
49            8 => Some(Self::Accessories),
50            9 => Some(Self::Custom1),
51            10 => Some(Self::Custom2),
52            11 => Some(Self::Custom3),
53            _ => None,
54        }
55    }
56}
57
58bitflags::bitflags! {
59    /// Texture flags as defined in the M2 format
60    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
61    pub struct M2TextureFlags: u32 {
62        /// Texture is wrapped horizontally
63        const WRAP_X = 0x01;
64        /// Texture is wrapped vertically
65        const WRAP_Y = 0x02;
66        /// Texture will not be replaced by other textures
67        /// (character customization texture replacement)
68        const NOT_REPLACEABLE = 0x04;
69    }
70}
71
72/// Represents a texture in an M2 model
73#[derive(Debug, Clone)]
74pub struct M2Texture {
75    /// Type of the texture
76    pub texture_type: M2TextureType,
77    /// Flags for this texture
78    pub flags: M2TextureFlags,
79    /// Filename of the texture
80    pub filename: M2Array<u8>,
81}
82
83impl M2Texture {
84    /// Parse a texture from a reader based on the M2 version
85    pub fn parse<R: Read>(reader: &mut R, _version: u32) -> Result<Self> {
86        let texture_type_raw = reader.read_u32_le()?;
87        let texture_type =
88            M2TextureType::from_u32(texture_type_raw).unwrap_or(M2TextureType::Hardcoded);
89
90        let flags = M2TextureFlags::from_bits_retain(reader.read_u32_le()?);
91        let filename = M2Array::parse(reader)?;
92
93        Ok(Self {
94            texture_type,
95            flags,
96            filename,
97        })
98    }
99
100    /// Write a texture to a writer
101    pub fn write<W: Write>(&self, writer: &mut W) -> Result<()> {
102        writer.write_u32_le(self.texture_type as u32)?;
103        writer.write_u32_le(self.flags.bits())?;
104        self.filename.write(writer)?;
105
106        Ok(())
107    }
108
109    /// Convert this texture to a different version (no version differences for textures)
110    pub fn convert(&self, _target_version: M2Version) -> Self {
111        self.clone()
112    }
113
114    /// Create a new texture with the given type and filename offset
115    pub fn new(texture_type: M2TextureType, filename_offset: u32, filename_len: u32) -> Self {
116        Self {
117            texture_type,
118            flags: M2TextureFlags::empty(),
119            filename: M2Array::new(filename_len, filename_offset),
120        }
121    }
122}
123
124#[cfg(test)]
125mod tests {
126    use super::*;
127    use std::io::Cursor;
128
129    #[test]
130    fn test_texture_parse() {
131        let mut data = Vec::new();
132
133        // Texture type (Body)
134        data.extend_from_slice(&1u32.to_le_bytes());
135
136        // Flags (WRAP_X | WRAP_Y)
137        data.extend_from_slice(&3u32.to_le_bytes());
138
139        // Filename
140        data.extend_from_slice(&10u32.to_le_bytes()); // count = 10
141        data.extend_from_slice(&0x100u32.to_le_bytes()); // offset = 0x100
142
143        let mut cursor = Cursor::new(data);
144        let texture =
145            M2Texture::parse(&mut cursor, M2Version::Classic.to_header_version()).unwrap();
146
147        assert_eq!(texture.texture_type, M2TextureType::Body);
148        assert_eq!(
149            texture.flags,
150            M2TextureFlags::WRAP_X | M2TextureFlags::WRAP_Y
151        );
152        assert_eq!(texture.filename.count, 10);
153        assert_eq!(texture.filename.offset, 0x100);
154    }
155
156    #[test]
157    fn test_texture_write() {
158        let texture = M2Texture {
159            texture_type: M2TextureType::Body,
160            flags: M2TextureFlags::WRAP_X | M2TextureFlags::WRAP_Y,
161            filename: M2Array::new(10, 0x100),
162        };
163
164        let mut data = Vec::new();
165        texture.write(&mut data).unwrap();
166
167        assert_eq!(
168            data,
169            [
170                // Texture type (Body)
171                1, 0, 0, 0, // Flags (WRAP_X | WRAP_Y)
172                3, 0, 0, 0, // Filename
173                10, 0, 0, 0, // count = 10
174                0, 1, 0, 0, // offset = 0x100
175            ]
176        );
177    }
178
179    #[test]
180    fn test_texture_conversion() {
181        let texture = M2Texture {
182            texture_type: M2TextureType::Body,
183            flags: M2TextureFlags::WRAP_X | M2TextureFlags::WRAP_Y,
184            filename: M2Array::new(10, 0x100),
185        };
186
187        // Convert to Cataclysm (should be identical since there are no version differences)
188        let converted = texture.convert(M2Version::Cataclysm);
189
190        assert_eq!(converted.texture_type, texture.texture_type);
191        assert_eq!(converted.flags, texture.flags);
192        assert_eq!(converted.filename.count, texture.filename.count);
193        assert_eq!(converted.filename.offset, texture.filename.offset);
194    }
195}