wc3 0.2.0

Read, edit, and write Warcraft III models, BLP textures, and MPQ archives
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
//! Ordered rendering layers and material settings.
use super::layer::{
    EmissiveGain, EmissiveGainField, FresnelField, Layer, LayerFresnel, LayerShaderTypeField,
    LayerTextureSlot, LayerTextureSlots, LayerTextureSlotsField, NoEmissiveGain, NoFresnel,
    NoLayerShaderType, NoLayerTextureSlots, LAYER_TAG,
};
use super::{write_count, ShaderType};
use crate::model::conversion::ConversionContext;
use crate::model::{mdl, mdx};
use crate::model::{ConversionError, ConversionIssueKind};
use crate::model::{
    Cursor, Encoder, FixedText, KnownChunk, MaterialsChunk, Model, ModelVersion,
    SupportsMaterialShaderPath, Tag, ValueError, Version,
};
use bitfield::bitfield;
use mdl_codec::zero_priority;
use std::{borrow::Cow, fmt::Debug, marker::PhantomData};

bitfield! {
    /// Material rendering bits, preserving unrecognized bits.
    #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, mdx::Read, mdx::Write, mdl::Read, mdl::Write)]
    #[mdl(flags(
        ConstantColor = 1,
        TwoSided = 2,
        SortPrimsNearZ = 8,
        SortPrimsFarZ = 16,
        FullResolution = 32
    ))]
    #[mdl(hive_flags(SortPrimitives = 16), hive_skip_bits = 8)]
    pub struct MaterialRenderFlags(u32);
    /// Returns the exact stored bits.
    pub bits, _: 31, 0;
    /// Returns or changes the `CONSTANT_COLOR` bit.
    pub constant_color, set_constant_color: 0;
    /// Returns or changes the historical `TWO_SIDED` bit.
    pub two_sided, set_two_sided: 1;
    /// Returns or changes the historical `SORT_PRIMITIVES_NEAR_Z` bit.
    pub sort_primitives_near_z, set_sort_primitives_near_z: 3;
    /// Returns or changes the `SORT_PRIMITIVES_FAR_Z` bit.
    pub sort_primitives_far_z, set_sort_primitives_far_z: 4;
    /// Returns or changes the `FULL_RESOLUTION` bit.
    pub full_resolution, set_full_resolution: 5;
}

/// Surface appearance composed of ordered rendering layers.
#[derive(Clone, Debug, PartialEq, mdl::Read, mdl::Write)]
#[mdl(
    block = "Material",
    write_order(priority_plane, render_mode, unfogged, shader, layers),
    virtual_fields(
        #[mdl(flag = "Unfogged", default)]
        #[mdl(get = "Self::mdl_unfogged", set = "Self::set_mdl_unfogged")]
        unfogged: bool
    )
)]
pub struct Material<V: ModelVersion> {
    #[mdl(skip, default)]
    version: PhantomData<V>,
    #[mdl(property = "PriorityPlane", default, skip_if = "zero_priority")]
    /// Signed render-order priority.
    pub priority_plane: i32,
    #[mdl(flatten)]
    /// Rendering flags.
    pub render_mode: MaterialRenderFlags,
    #[mdl(property = "Shader", delegate)]
    shader: V::Shader,
    #[mdl(repeated = "Layer")]
    /// Rendering layers, in draw order.
    pub layers: Vec<Layer<V>>,
}

/// The fixed shader field selected by a model version.
pub trait ShaderField:
    Default
    + mdx::Read
    + mdx::Write
    + mdl::ReadProperty
    + mdl::WriteProperty
    + Clone
    + Debug
    + PartialEq
{
    fn text(&self) -> Option<Cow<'_, str>>;
    fn fixed_text(&self) -> Option<&FixedText<80>> {
        None
    }
    fn fixed_text_mut(&mut self) -> Option<&mut FixedText<80>> {
        None
    }
    fn set(&mut self, text: &str) -> Result<(), ValueError>;
}

#[derive(Clone, Debug, Default, PartialEq, mdx::Read, mdx::Write)]
pub struct NoShader;

impl ShaderField for NoShader {
    fn text(&self) -> Option<Cow<'_, str>> {
        None
    }
    fn set(&mut self, _: &str) -> Result<(), ValueError> {
        Err(ValueError::UnavailableField {
            tag: *b"MTLS",
            field: "shader",
        })
    }
}

#[derive(Clone, Debug, Default, PartialEq, mdx::Read, mdx::Write)]
pub struct ShaderText(FixedText<80>);

impl ShaderField for ShaderText {
    fn fixed_text(&self) -> Option<&FixedText<80>> {
        Some(&self.0)
    }
    fn fixed_text_mut(&mut self) -> Option<&mut FixedText<80>> {
        Some(&mut self.0)
    }
    fn text(&self) -> Option<Cow<'_, str>> {
        Some(self.0.text())
    }
    fn set(&mut self, text: &str) -> Result<(), ValueError> {
        self.0.set_text(text)
    }
}

/// Chooses the material and layer fields for a version.
pub trait MaterialLayout {
    /// Storage for the material shader path.
    type Shader: ShaderField;
    /// Storage for layer emissive gain.
    type EmissiveGain: EmissiveGainField;
    /// Storage for layer Fresnel controls.
    type Fresnel: FresnelField;
    /// Storage for the layer shader-type identifier.
    type ShaderType: LayerShaderTypeField;
    /// Storage for layer texture-role slots.
    type TextureSlots: LayerTextureSlotsField;
}
use crate::model::{V1000, V1100, V1200, V1300, V1400, V1600, V1800, V800, V900};

impl MaterialLayout for V800 {
    type Shader = NoShader;
    type EmissiveGain = NoEmissiveGain;
    type Fresnel = NoFresnel;
    type ShaderType = NoLayerShaderType;
    type TextureSlots = NoLayerTextureSlots;
}
impl MaterialLayout for V900 {
    type Shader = ShaderText;
    type EmissiveGain = EmissiveGain;
    type Fresnel = NoFresnel;
    type ShaderType = NoLayerShaderType;
    type TextureSlots = NoLayerTextureSlots;
}
impl MaterialLayout for V1000 {
    type Shader = ShaderText;
    type EmissiveGain = EmissiveGain;
    type Fresnel = LayerFresnel;
    type ShaderType = NoLayerShaderType;
    type TextureSlots = NoLayerTextureSlots;
}
impl MaterialLayout for V1100 {
    type Shader = NoShader;
    type EmissiveGain = EmissiveGain;
    type Fresnel = LayerFresnel;
    type ShaderType = ShaderType;
    type TextureSlots = LayerTextureSlots;
}
impl MaterialLayout for V1200 {
    type Shader = NoShader;
    type EmissiveGain = EmissiveGain;
    type Fresnel = LayerFresnel;
    type ShaderType = ShaderType;
    type TextureSlots = LayerTextureSlots;
}
impl MaterialLayout for V1300 {
    type Shader = NoShader;
    type EmissiveGain = EmissiveGain;
    type Fresnel = LayerFresnel;
    type ShaderType = ShaderType;
    type TextureSlots = LayerTextureSlots;
}
impl MaterialLayout for V1400 {
    type Shader = NoShader;
    type EmissiveGain = EmissiveGain;
    type Fresnel = LayerFresnel;
    type ShaderType = ShaderType;
    type TextureSlots = LayerTextureSlots;
}
impl MaterialLayout for V1600 {
    type Shader = NoShader;
    type EmissiveGain = EmissiveGain;
    type Fresnel = LayerFresnel;
    type ShaderType = ShaderType;
    type TextureSlots = LayerTextureSlots;
}
impl MaterialLayout for V1800 {
    type Shader = NoShader;
    type EmissiveGain = EmissiveGain;
    type Fresnel = LayerFresnel;
    type ShaderType = ShaderType;
    type TextureSlots = LayerTextureSlots;
}

fn expect_tag(
    cursor: &mut Cursor<'_>,
    expected: Tag,
    record_tag: Tag,
) -> Result<(), mdx::ReadError> {
    let offset = cursor.absolute_position();
    let actual = cursor.read()?;
    if actual == expected {
        Ok(())
    } else {
        Err(mdx::ReadError::new(
            offset,
            mdx::ReadErrorKind::UnexpectedTag { expected, actual },
        )
        .with_tag(record_tag))
    }
}

impl<V: ModelVersion> Material<V> {
    /// Creates an empty material using `V`'s fields.
    pub fn new() -> Self {
        Self {
            version: PhantomData,
            priority_plane: 0,
            render_mode: MaterialRenderFlags::default(),
            shader: V::Shader::default(),
            layers: Vec::new(),
        }
    }

    /// Returns the MDX version used for this material.
    pub fn version(&self) -> Version {
        V::NUMBER
    }

    /// Returns the shader path in versions 900 through 1099.
    pub fn try_shader(&self) -> Result<Cow<'_, str>, ValueError> {
        if !(matches!(V::NUMBER, 900 | 1000)) {
            return Err(ValueError::UnsupportedField {
                tag: *b"MTLS",
                field: "shader",
                actual: V::NUMBER,
            });
        }
        Ok(self.shader.text().expect("supported version"))
    }
    /// Changes the shader path and clears unused bytes.
    pub fn try_set_shader(&mut self, shader: &str) -> Result<(), ValueError> {
        if !matches!(V::NUMBER, 900 | 1000) {
            return Err(ValueError::UnsupportedField {
                tag: *b"MTLS",
                field: "shader",
                actual: V::NUMBER,
            });
        }
        self.shader.set(shader)
    }
}

impl<V: ModelVersion> Default for Material<V> {
    fn default() -> Self {
        Self::new()
    }
}

impl<V: ModelVersion> Model<V> {
    /// Returns owned copies of `MTLS` records in file order.
    pub fn materials(&self) -> Vec<Material<V>> {
        self.collect_chunk_records::<MaterialsChunk<V>>()
    }

    /// Replaces all material records with one `MTLS` chunk, removing any duplicate chunks.
    pub fn set_materials(&mut self, materials: &[Material<V>]) {
        self.replace_chunk(MaterialsChunk::new(materials.to_vec()));
    }
}

impl<V: ModelVersion> mdx::Read for Material<V> {
    fn read_mdx(source: &mut Cursor<'_>) -> Result<Self, mdx::ReadError> {
        let mut cursor = source.subcursor_u32_sized()?;
        let value = {
            let priority_plane = cursor.read()?;
            let render_mode = cursor.read()?;
            let shader = cursor.read::<V::Shader>()?;
            expect_tag(&mut cursor, LAYER_TAG, MaterialsChunk::<V>::TAG)?;
            let count = cursor.read::<u32>()? as usize;
            let mut layers = Vec::new();
            for _ in 0..count {
                let layer = cursor.read()?;
                layers.push(layer);
            }
            Ok(Self {
                version: PhantomData,
                priority_plane,
                render_mode,
                shader,
                layers,
            })
        }?;
        cursor.finish()?;
        Ok(value)
    }
}

impl<V: ModelVersion> mdx::Write for Material<V> {
    fn write_mdx(&self, bytes: &mut Encoder<'_>) -> Result<(), mdx::WriteError> {
        let marker = bytes.begin_sized();
        bytes.write(&(self.priority_plane))?;
        bytes.write(&(self.render_mode))?;
        bytes.write(&self.shader)?;
        bytes.write_bytes(b"LAYS");
        write_count(bytes, self.layers.len(), MaterialsChunk::<V>::TAG)?;
        for layer in &self.layers {
            bytes.write(layer)?;
        }
        bytes.finish_sized(marker, MaterialsChunk::<V>::TAG)?;
        Ok(())
    }
}

impl<V: SupportsMaterialShaderPath> Material<V> {
    /// Returns the shader path, decoding invalid UTF-8 lossily.
    pub fn shader(&self) -> Cow<'_, str> {
        self.shader.text().expect("supported version")
    }
    /// Sets the shader path; rejects embedded NUL and paths beyond the fixed capacity.
    pub fn set_shader(&mut self, shader: &str) -> Result<(), ValueError> {
        self.try_set_shader(shader)
    }
}

impl<V: ModelVersion> Material<V> {
    pub(crate) fn convert_with<T: ModelVersion>(
        &self,
        context: &mut ConversionContext<'_>,
        path: &str,
    ) -> Result<Material<T>, ConversionError> {
        let mut target = Material::<T>::new();
        target.priority_plane = self.priority_plane;
        target.render_mode = self.render_mode;
        if matches!(V::NUMBER, 900 | 1000) && T::NUMBER >= 1100 {
            let shader = self
                .shader
                .fixed_text()
                .and_then(|name| ShaderType::from_name(&name.text()));
            if let Some(shader) = shader {
                let hd = shader == ShaderType::HD_DEFAULT_UNIT || shader == ShaderType::HD_CRYSTAL;
                if hd && self.layers.len() != 6 {
                    context.drop(
                        &format!("{path}.shader"),
                        "HD shader upgrade requires six texture-role layers",
                    )?;
                } else {
                    if hd {
                        for (index, layer) in self.layers.iter().enumerate().skip(1) {
                            if layer.alpha.track().is_some()
                                || layer
                                    .try_emissive_gain()
                                    .is_ok_and(|value| value.track().is_some())
                                || layer.try_fresnel().is_ok_and(|value| {
                                    value.color.track().is_some()
                                        || value.opacity.track().is_some()
                                        || value.team_color.track().is_some()
                                })
                                || layer.coordinate_id != self.layers[0].coordinate_id
                                || layer.texture_animation_id != self.layers[0].texture_animation_id
                            {
                                return Err(context.error(&format!("{path}.layers[{index}]"),
                                    "HD texture-role layer has independent animation or UV settings"));
                            }
                        }
                        let mut slots = Vec::new();
                        for (index, layer) in self.layers.iter().enumerate() {
                            let converted = layer
                                .convert_with::<T>(context, &format!("{path}.layers[{index}]"))?;
                            let mut slot: LayerTextureSlot =
                                converted.try_texture_slots().expect("target slots")[0].clone();
                            slot.texture_type = index as u32;
                            slots.push(slot);
                            if index == 0 {
                                target.layers.push(converted);
                            }
                        }
                        let layer = &mut target.layers[0];
                        layer.try_set_texture_slots(&slots).expect("target slots");
                        layer.try_set_shader_type(shader).expect("target shader");
                        if self.render_mode.two_sided() {
                            layer.shading_flags.set_two_sided(true);
                        }
                    } else {
                        for (index, layer) in self.layers.iter().enumerate() {
                            let mut layer = layer
                                .convert_with::<T>(context, &format!("{path}.layers[{index}]"))?;
                            layer.try_set_shader_type(shader).expect("target shader");
                            target.layers.push(layer);
                        }
                    }
                    context.issue(&format!("{path}.shader"), ConversionIssueKind::Normalized,
                        if hd { "upgraded six texture-role layers into one HD layer using the first layer's rendering properties" }
                        else { "moved material shader assignment into layer shader fields" });
                    return Ok(target);
                }
            }
        }
        context.field(
            self.shader.fixed_text().copied(),
            target.shader.fixed_text_mut(),
            FixedText::default(),
            &format!("{path}.shader"),
        )?;
        target.layers = self
            .layers
            .iter()
            .enumerate()
            .map(|(i, layer)| layer.convert_with::<T>(context, &format!("{path}.layers[{i}]")))
            .collect::<Result<_, _>>()?;
        Ok(target)
    }
}

mod mdl_codec;