wow-adt 0.6.4

Parser for World of Warcraft ADT terrain files with heightmap and texture layer 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
// validator.rs - Comprehensive ADT validation

use crate::Adt;
use crate::error::{AdtError, Result};
use crate::split_adt::SplitAdtType;
use crate::version::AdtVersion;
use std::collections::HashSet;
use std::path::Path;

/// Validation levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ValidationLevel {
    /// Basic structure validation
    Basic,
    /// Standard validation with consistency checks
    #[default]
    Standard,
    /// Strict validation with all checks enabled
    Strict,
}

/// Validate an ADT file
pub fn validate_adt(adt: &Adt, level: ValidationLevel) -> Result<ValidationReport> {
    validate_adt_with_context(adt, level, None::<&Path>)
}

/// Validate an ADT file with file context
pub fn validate_adt_with_context<P: AsRef<Path>>(
    adt: &Adt,
    level: ValidationLevel,
    file_path: Option<P>,
) -> Result<ValidationReport> {
    let mut report = ValidationReport::new();

    // Determine file type if path is provided
    let file_type = file_path
        .as_ref()
        .map(|p| SplitAdtType::from_filename(&p.as_ref().to_string_lossy()));

    // Basic structure validation (always performed)
    basic_validation(adt, &mut report, file_type)?;

    // Return early if only basic validation is requested
    if level == ValidationLevel::Basic {
        return Ok(report);
    }

    // Cross-reference validation
    xref_validation(adt, &mut report)?;

    // Strict validation checks
    if level == ValidationLevel::Strict {
        strict_validation(adt, &mut report)?;
    }

    Ok(report)
}

/// Basic structure validation
fn basic_validation(
    adt: &Adt,
    report: &mut ValidationReport,
    file_type: Option<SplitAdtType>,
) -> Result<()> {
    // For split ADT files, different validation rules apply
    match file_type {
        Some(SplitAdtType::Obj0) | Some(SplitAdtType::Obj1) => {
            // Object files don't require MHDR or MCNK chunks
            // They contain object placement data (MMDX, MMID, MWMO, MWID, MDDF, MODF)
            return Ok(());
        }
        Some(SplitAdtType::Tex0) | Some(SplitAdtType::Tex1) => {
            // Texture files don't require MHDR or MCNK chunks
            // They contain texture data (MTEX and texture-related MCNK subchunks)
            return Ok(());
        }
        Some(SplitAdtType::Lod) => {
            // LOD files have different requirements
            return Ok(());
        }
        _ => {
            // Root ADT file - apply normal validation
        }
    }

    // Check for required chunks in root ADT files
    if adt.mhdr.is_none() {
        report.add_error("Missing MHDR chunk".to_string());
        return Err(AdtError::MissingChunk("MHDR".to_string()));
    }

    // Check for MCNK chunks - should be 256 (16x16) for a complete map tile
    if adt.mcnk_chunks.is_empty() {
        report.add_error("No MCNK chunks found".to_string());
        return Err(AdtError::ValidationError(
            "No MCNK chunks found".to_string(),
        ));
    }

    if adt.mcnk_chunks.len() != 256 {
        report.add_warning(format!(
            "Expected 256 MCNK chunks for a complete map tile, found {}",
            adt.mcnk_chunks.len()
        ));
    }

    // Version-specific validation
    match adt.version() {
        AdtVersion::TBC | AdtVersion::WotLK | AdtVersion::Cataclysm => {
            // Validate that MHDR has appropriate offsets for the version
            if let Some(ref mhdr) = adt.mhdr {
                if adt.version() >= AdtVersion::TBC && mhdr.mfbo_offset.is_none() {
                    report.add_warning("TBC+ ADT should have MFBO offset in MHDR".to_string());
                }

                if adt.version() >= AdtVersion::WotLK && mhdr.mh2o_offset.is_none() {
                    report.add_warning("WotLK+ ADT should have MH2O offset in MHDR".to_string());
                }

                if adt.version() >= AdtVersion::Cataclysm && mhdr.mtfx_offset.is_none() {
                    report
                        .add_warning("Cataclysm+ ADT should have MTFX offset in MHDR".to_string());
                }
            }
        }
        _ => {}
    }

    // Validate MHDR offsets vs actual data
    if let Some(ref mhdr) = adt.mhdr {
        if mhdr.mcin_offset > 0 && adt.mcin.is_none() {
            report.add_warning("MHDR has MCIN offset but no MCIN chunk found".to_string());
        }

        if mhdr.mtex_offset > 0 && adt.mtex.is_none() {
            report.add_warning("MHDR has MTEX offset but no MTEX chunk found".to_string());
        }

        if mhdr.mmdx_offset > 0 && adt.mmdx.is_none() {
            report.add_warning("MHDR has MMDX offset but no MMDX chunk found".to_string());
        }
    }

    Ok(())
}

/// Cross-reference validation
fn xref_validation(adt: &Adt, report: &mut ValidationReport) -> Result<()> {
    // Validate MCNK indices
    for (i, chunk) in adt.mcnk_chunks.iter().enumerate() {
        let expected_x = (i % 16) as u32;
        let expected_y = (i / 16) as u32;

        if chunk.ix != expected_x || chunk.iy != expected_y {
            report.add_warning(format!(
                "MCNK chunk at index {} has incorrect indices [{}, {}], expected [{}, {}]",
                i, chunk.ix, chunk.iy, expected_x, expected_y
            ));
        }
    }

    // Validate MCIN indices point to valid MCNK chunks
    if let Some(ref mcin) = adt.mcin {
        for (i, entry) in mcin.entries.iter().enumerate() {
            if entry.offset == 0 && entry.size == 0 {
                // Empty entry
                continue;
            }

            // In a file, the offset would be relative to the file start
            // But we don't know the actual offset here, so we can only check
            // if the size seems reasonable
            if entry.size < 8 || entry.size > 1024 * 1024 {
                report.add_warning(format!(
                    "MCIN entry {} has suspicious size: {}",
                    i, entry.size
                ));
            }
        }
    }

    // Validate MMID references valid indices in MMDX
    if let Some(ref _mmdx) = adt.mmdx {
        if let Some(ref mmid) = adt.mmid {
            for (i, &offset) in mmid.offsets.iter().enumerate() {
                let found = false;

                // Ideally, we would check if each offset points to a valid position
                // in the MMDX chunk, but we don't have the raw data here

                if !found {
                    report.add_info(format!("MMID entry {i} references offset {offset} in MMDX"));
                }
            }
        }
    }

    // Validate MWID references valid indices in MWMO
    if let Some(ref _mwmo) = adt.mwmo {
        if let Some(ref mwid) = adt.mwid {
            for (i, &offset) in mwid.offsets.iter().enumerate() {
                let found = false;

                // Similar to MMID, we would need the raw data to properly validate

                if !found {
                    report.add_info(format!("MWID entry {i} references offset {offset} in MWMO"));
                }
            }
        }
    }

    // Validate MDDF references valid MMID indices
    if let Some(ref mddf) = adt.mddf {
        if let Some(ref mmid) = adt.mmid {
            for (i, doodad) in mddf.doodads.iter().enumerate() {
                if doodad.name_id as usize >= mmid.offsets.len() {
                    report.add_error(format!(
                        "MDDF entry {} references invalid MMID index: {}",
                        i, doodad.name_id
                    ));
                }
            }
        } else if !mddf.doodads.is_empty() {
            report.add_error("MDDF references doodads but no MMID chunk found".to_string());
        }
    }

    // Validate MODF references valid MWID indices
    if let Some(ref modf) = adt.modf {
        if let Some(ref mwid) = adt.mwid {
            for (i, model) in modf.models.iter().enumerate() {
                if model.name_id as usize >= mwid.offsets.len() {
                    report.add_error(format!(
                        "MODF entry {} references invalid MWID index: {}",
                        i, model.name_id
                    ));
                }
            }
        } else if !modf.models.is_empty() {
            report.add_error("MODF references WMOs but no MWID chunk found".to_string());
        }
    }

    // Validate texture references
    if let Some(ref mtex) = adt.mtex {
        let texture_count = mtex.filenames.len();

        for (i, chunk) in adt.mcnk_chunks.iter().enumerate() {
            for (j, layer) in chunk.texture_layers.iter().enumerate() {
                if layer.texture_id as usize >= texture_count {
                    report.add_error(format!(
                        "MCNK chunk {}, layer {} references invalid texture ID: {}",
                        i, j, layer.texture_id
                    ));
                }
            }
        }
    }

    // Validate MCNK doodad references
    for (i, chunk) in adt.mcnk_chunks.iter().enumerate() {
        if !chunk.doodad_refs.is_empty() {
            if let Some(ref mmid) = adt.mmid {
                for (j, &doodad_ref) in chunk.doodad_refs.iter().enumerate() {
                    if doodad_ref as usize >= mmid.offsets.len() {
                        report.add_error(format!(
                            "MCNK chunk {i}, doodad ref {j} references invalid MMID index: {doodad_ref}"
                        ));
                    }
                }
            } else {
                report.add_error(format!(
                    "MCNK chunk {i} references doodads but no MMID chunk found"
                ));
            }
        }

        if !chunk.map_obj_refs.is_empty() {
            if let Some(ref mwid) = adt.mwid {
                for (j, &map_obj_ref) in chunk.map_obj_refs.iter().enumerate() {
                    if map_obj_ref as usize >= mwid.offsets.len() {
                        report.add_error(format!(
                            "MCNK chunk {i}, map object ref {j} references invalid MWID index: {map_obj_ref}"
                        ));
                    }
                }
            } else {
                report.add_error(format!(
                    "MCNK chunk {i} references map objects but no MWID chunk found"
                ));
            }
        }
    }

    Ok(())
}

/// Strict validation with detailed checks
fn strict_validation(adt: &Adt, report: &mut ValidationReport) -> Result<()> {
    // Check for unique IDs in doodad placements
    if let Some(ref mddf) = adt.mddf {
        let mut doodad_ids = HashSet::new();

        for (i, doodad) in mddf.doodads.iter().enumerate() {
            if !doodad_ids.insert(doodad.unique_id) {
                report.add_warning(format!(
                    "MDDF entry {} has duplicate unique ID: {}",
                    i, doodad.unique_id
                ));
            }
        }
    }

    // Check for unique IDs in model placements
    if let Some(ref modf) = adt.modf {
        let mut model_ids = HashSet::new();

        for (i, model) in modf.models.iter().enumerate() {
            if !model_ids.insert(model.unique_id) {
                report.add_warning(format!(
                    "MODF entry {} has duplicate unique ID: {}",
                    i, model.unique_id
                ));
            }
        }
    }

    // Check for holes consistency
    for (i, chunk) in adt.mcnk_chunks.iter().enumerate() {
        if chunk.holes != 0 {
            // Holes are stored as a bit field where each bit represents
            // a triangle in the terrain mesh.
            // We could do more detailed validation here.
            report.add_info(format!("MCNK chunk {} has holes: {:#06x}", i, chunk.holes));
        }
    }

    // Version-specific strict validation
    match adt.version() {
        AdtVersion::WotLK | AdtVersion::Cataclysm => {
            // Validate MH2O consistency
            if let Some(ref mh2o) = adt.mh2o {
                if mh2o.chunks.len() != 256 {
                    report.add_error(format!(
                        "MH2O has {} chunks, expected 256",
                        mh2o.chunks.len()
                    ));
                }

                // More detailed MH2O validation could be done here
            }
        }
        _ => {}
    }

    Ok(())
}

/// Report of validation results
#[derive(Debug, Clone)]
pub struct ValidationReport {
    /// Errors (fatal issues)
    pub errors: Vec<String>,
    /// Warnings (potential issues)
    pub warnings: Vec<String>,
    /// Informational messages
    pub info: Vec<String>,
}

impl Default for ValidationReport {
    fn default() -> Self {
        Self::new()
    }
}

impl ValidationReport {
    /// Create a new validation report
    pub fn new() -> Self {
        Self {
            errors: Vec::new(),
            warnings: Vec::new(),
            info: Vec::new(),
        }
    }

    /// Add an error message
    pub fn add_error(&mut self, message: String) {
        self.errors.push(message);
    }

    /// Add a warning message
    pub fn add_warning(&mut self, message: String) {
        self.warnings.push(message);
    }

    /// Add an info message
    pub fn add_info(&mut self, message: String) {
        self.info.push(message);
    }

    /// Check if the validation passed (no errors)
    pub fn is_valid(&self) -> bool {
        self.errors.is_empty()
    }

    /// Check if the validation passed without warnings
    pub fn is_clean(&self) -> bool {
        self.errors.is_empty() && self.warnings.is_empty()
    }

    /// Format the report as a string
    pub fn format(&self) -> String {
        let mut result = String::new();

        if self.is_valid() {
            result.push_str("Validation passed");
            if !self.warnings.is_empty() {
                result.push_str(" with warnings");
            }
            result.push_str(".\n\n");
        } else {
            result.push_str(&format!(
                "Validation failed with {} errors.\n\n",
                self.errors.len()
            ));
        }

        if !self.errors.is_empty() {
            result.push_str("Errors:\n");
            for (i, error) in self.errors.iter().enumerate() {
                result.push_str(&format!("  {}. {}\n", i + 1, error));
            }
            result.push('\n');
        }

        if !self.warnings.is_empty() {
            result.push_str("Warnings:\n");
            for (i, warning) in self.warnings.iter().enumerate() {
                result.push_str(&format!("  {}. {}\n", i + 1, warning));
            }
            result.push('\n');
        }

        if !self.info.is_empty() {
            result.push_str("Info:\n");
            for (i, info) in self.info.iter().enumerate() {
                result.push_str(&format!("  {}. {}\n", i + 1, info));
            }
        }

        result
    }
}