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
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
//! String chunk parsers for ADT format.
//!
//! String chunks (MTEX, MMDX, MWMO) store null-terminated filenames consecutively.
//! Index chunks (MMID, MWID) store offset tables referencing string data.
//!
//! String data is stored as consecutive null-terminated ASCII/UTF-8 strings with
//! no padding between entries. Some files contain non-ASCII characters requiring
//! lossy UTF-8 conversion for compatibility.

use binrw::{BinRead, BinWrite};
use std::io::{Read, Seek, Write};

/// MTEX chunk - Texture filenames (Vanilla+)
///
/// Contains null-terminated texture filenames (e.g., "Tileset\\Terrain.blp\0").
/// Referenced by texture layer indices in MCLY chunks.
///
/// Filenames are stored consecutively without padding. Parse until end of chunk.
/// Use lossy UTF-8 conversion to handle non-ASCII characters in some files.
///
/// Reference: <https://wowdev.wiki/ADT/v18#MTEX_chunk>
///
/// ## Version Support
///
/// - **Vanilla (1.12.1)**: ✅ Introduced
/// - **TBC (2.4.3)**: ✅ Present
/// - **WotLK (3.3.5a)**: ✅ Present
/// - **Cataclysm (4.3.4)**: ✅ Present (moved to _tex0.adt in split files)
/// - **MoP (5.4.8)**: ✅ Present (in _tex0.adt split files)
#[derive(Debug, Clone, Default)]
pub struct MtexChunk {
    /// Texture filenames (null-terminated)
    pub filenames: Vec<String>,
}

impl BinRead for MtexChunk {
    type Args<'a> = ();

    fn read_options<R: Read + Seek>(
        reader: &mut R,
        _endian: binrw::Endian,
        _args: Self::Args<'_>,
    ) -> binrw::BinResult<Self> {
        let filenames = parse_null_terminated_strings(reader)?;
        Ok(Self { filenames })
    }
}

impl BinWrite for MtexChunk {
    type Args<'a> = ();

    fn write_options<W: Write + Seek>(
        &self,
        writer: &mut W,
        _endian: binrw::Endian,
        _args: Self::Args<'_>,
    ) -> binrw::BinResult<()> {
        for filename in &self.filenames {
            writer.write_all(filename.as_bytes())?;
            writer.write_all(&[0u8])?; // Null terminator
        }
        Ok(())
    }
}

/// MMDX chunk - M2 model filenames (Vanilla+)
///
/// Contains null-terminated M2 model filenames (e.g., "World\\Doodad\\Model.m2\0").
/// Referenced by MMID offset indices for model placement via MDDF chunks.
///
/// Reference: <https://wowdev.wiki/ADT/v18#MMDX_chunk>
///
/// ## Version Support
///
/// - **Vanilla (1.12.1)**: ✅ Introduced
/// - **TBC (2.4.3)**: ✅ Present
/// - **WotLK (3.3.5a)**: ✅ Present
/// - **Cataclysm (4.3.4)**: ✅ Present (moved to _obj0.adt in split files)
/// - **MoP (5.4.8)**: ✅ Present (in _obj0.adt split files)
#[derive(Debug, Clone, Default)]
pub struct MmdxChunk {
    /// M2 model filenames (null-terminated)
    pub filenames: Vec<String>,
}

impl BinRead for MmdxChunk {
    type Args<'a> = ();

    fn read_options<R: Read + Seek>(
        reader: &mut R,
        _endian: binrw::Endian,
        _args: Self::Args<'_>,
    ) -> binrw::BinResult<Self> {
        let filenames = parse_null_terminated_strings(reader)?;
        Ok(Self { filenames })
    }
}

impl BinWrite for MmdxChunk {
    type Args<'a> = ();

    fn write_options<W: Write + Seek>(
        &self,
        writer: &mut W,
        _endian: binrw::Endian,
        _args: Self::Args<'_>,
    ) -> binrw::BinResult<()> {
        for filename in &self.filenames {
            writer.write_all(filename.as_bytes())?;
            writer.write_all(&[0u8])?; // Null terminator
        }
        Ok(())
    }
}

/// MWMO chunk - WMO filenames (Vanilla+)
///
/// Contains null-terminated World Map Object (WMO) filenames.
/// Referenced by MWID offset indices for WMO placement.
///
/// Reference: <https://wowdev.wiki/ADT/v18#MWMO_chunk>
///
/// ## Version Support
///
/// - **Vanilla (1.12.1)**: ✅ Introduced
/// - **TBC (2.4.3)**: ✅ Present
/// - **WotLK (3.3.5a)**: ✅ Present
/// - **Cataclysm (4.3.4)**: ✅ Present (moved to _obj0.adt in split files)
/// - **MoP (5.4.8)**: ✅ Present (in _obj0.adt split files)
#[derive(Debug, Clone, Default)]
pub struct MwmoChunk {
    /// WMO filenames (null-terminated)
    pub filenames: Vec<String>,
}

impl BinRead for MwmoChunk {
    type Args<'a> = ();

    fn read_options<R: Read + Seek>(
        reader: &mut R,
        _endian: binrw::Endian,
        _args: Self::Args<'_>,
    ) -> binrw::BinResult<Self> {
        let filenames = parse_null_terminated_strings(reader)?;
        Ok(Self { filenames })
    }
}

impl BinWrite for MwmoChunk {
    type Args<'a> = ();

    fn write_options<W: Write + Seek>(
        &self,
        writer: &mut W,
        _endian: binrw::Endian,
        _args: Self::Args<'_>,
    ) -> binrw::BinResult<()> {
        for filename in &self.filenames {
            writer.write_all(filename.as_bytes())?;
            writer.write_all(&[0u8])?; // Null terminator
        }
        Ok(())
    }
}

/// MMID chunk - M2 model offset indices (Vanilla+)
///
/// Contains offset table into MMDX string data. Each u32 is a byte offset
/// pointing to a null-terminated filename in the MMDX chunk.
///
/// Used by MDDF (doodad placement) chunk to reference models.
///
/// Reference: <https://wowdev.wiki/ADT/v18#MMID_chunk>
///
/// ## Version Support
///
/// - **Vanilla (1.12.1)**: ✅ Introduced
/// - **TBC (2.4.3)**: ✅ Present
/// - **WotLK (3.3.5a)**: ✅ Present
/// - **Cataclysm (4.3.4)**: ✅ Present (moved to _obj0.adt in split files)
/// - **MoP (5.4.8)**: ✅ Present (in _obj0.adt split files)
#[derive(Debug, Clone, Default, BinRead, BinWrite)]
#[brw(little)]
pub struct MmidChunk {
    /// Byte offsets into MMDX chunk (one per model)
    #[br(parse_with = binrw::helpers::until_eof)]
    pub offsets: Vec<u32>,
}

impl MmidChunk {
    /// Validate that all offsets are within MMDX chunk bounds.
    ///
    /// # Arguments
    ///
    /// * `mmdx_size` - Total size of MMDX chunk data in bytes
    ///
    /// # Returns
    ///
    /// `true` if all offsets are valid, `false` if any offset is out of bounds
    pub fn validate_offsets(&self, mmdx_size: usize) -> bool {
        self.offsets
            .iter()
            .all(|&offset| (offset as usize) < mmdx_size)
    }

    /// Get filename index for a given offset by counting null terminators.
    ///
    /// # Arguments
    ///
    /// * `mmdx` - Reference to MMDX chunk containing filenames
    /// * `offset` - Byte offset into MMDX data
    ///
    /// # Returns
    ///
    /// Index into `mmdx.filenames` array, or None if offset is invalid
    pub fn get_filename_index(&self, mmdx: &MmdxChunk, offset: u32) -> Option<usize> {
        // Count null terminators before this offset to find filename index
        let mut current_offset = 0;
        for (idx, filename) in mmdx.filenames.iter().enumerate() {
            if current_offset == offset {
                return Some(idx);
            }
            current_offset += filename.len() as u32 + 1; // +1 for null terminator
        }
        None
    }
}

/// MWID chunk - WMO offset indices (Vanilla+)
///
/// Contains offset table into MWMO string data. Each u32 is a byte offset
/// pointing to a null-terminated filename in the MWMO chunk.
///
/// Used by MODF (WMO placement) chunk to reference World Map Objects.
///
/// Reference: <https://wowdev.wiki/ADT/v18#MWID_chunk>
///
/// ## Version Support
///
/// - **Vanilla (1.12.1)**: ✅ Introduced
/// - **TBC (2.4.3)**: ✅ Present
/// - **WotLK (3.3.5a)**: ✅ Present
/// - **Cataclysm (4.3.4)**: ✅ Present (moved to _obj0.adt in split files)
/// - **MoP (5.4.8)**: ✅ Present (in _obj0.adt split files)
#[derive(Debug, Clone, Default, BinRead, BinWrite)]
#[brw(little)]
pub struct MwidChunk {
    /// Byte offsets into MWMO chunk (one per WMO)
    #[br(parse_with = binrw::helpers::until_eof)]
    pub offsets: Vec<u32>,
}

impl MwidChunk {
    /// Validate that all offsets are within MWMO chunk bounds.
    ///
    /// # Arguments
    ///
    /// * `mwmo_size` - Total size of MWMO chunk data in bytes
    ///
    /// # Returns
    ///
    /// `true` if all offsets are valid, `false` if any offset is out of bounds
    pub fn validate_offsets(&self, mwmo_size: usize) -> bool {
        self.offsets
            .iter()
            .all(|&offset| (offset as usize) < mwmo_size)
    }

    /// Get filename index for a given offset by counting null terminators.
    ///
    /// # Arguments
    ///
    /// * `mwmo` - Reference to MWMO chunk containing filenames
    /// * `offset` - Byte offset into MWMO data
    ///
    /// # Returns
    ///
    /// Index into `mwmo.filenames` array, or None if offset is invalid
    pub fn get_filename_index(&self, mwmo: &MwmoChunk, offset: u32) -> Option<usize> {
        let mut current_offset = 0;
        for (idx, filename) in mwmo.filenames.iter().enumerate() {
            if current_offset == offset {
                return Some(idx);
            }
            current_offset += filename.len() as u32 + 1; // +1 for null terminator
        }
        None
    }
}

/// Parse null-terminated strings until end of stream.
///
/// Reads consecutive null-terminated strings from the reader until EOF.
/// Uses lossy UTF-8 conversion to handle potential non-ASCII characters.
fn parse_null_terminated_strings<R: Read>(reader: &mut R) -> binrw::BinResult<Vec<String>> {
    let mut strings = Vec::new();

    loop {
        let mut string_bytes = Vec::new();

        // Read bytes until null terminator or EOF
        loop {
            let mut byte = [0u8; 1];
            match reader.read_exact(&mut byte) {
                Ok(()) => {
                    if byte[0] == 0 {
                        // Null terminator found
                        break;
                    }
                    string_bytes.push(byte[0]);
                }
                Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
                    // EOF reached - if we have bytes, create final string
                    if !string_bytes.is_empty() {
                        let string = String::from_utf8_lossy(&string_bytes).into_owned();
                        strings.push(string);
                    }
                    return Ok(strings);
                }
                Err(e) => {
                    return Err(binrw::Error::Io(e));
                }
            }
        }

        // Create string from collected bytes (lossy UTF-8)
        if !string_bytes.is_empty() {
            let string = String::from_utf8_lossy(&string_bytes).into_owned();
            strings.push(string);
        }
    }
}

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

    #[test]
    fn test_mtex_chunk_parse() {
        // Two filenames: "test.blp\0" and "world.blp\0"
        let data = b"test.blp\0world.blp\0";
        let mut cursor = Cursor::new(data.to_vec());

        let mtex = MtexChunk::read_le(&mut cursor).unwrap();
        assert_eq!(mtex.filenames.len(), 2);
        assert_eq!(mtex.filenames[0], "test.blp");
        assert_eq!(mtex.filenames[1], "world.blp");
    }

    #[test]
    fn test_mtex_chunk_single_file() {
        let data = b"terrain.blp\0";
        let mut cursor = Cursor::new(data.to_vec());

        let mtex = MtexChunk::read_le(&mut cursor).unwrap();
        assert_eq!(mtex.filenames.len(), 1);
        assert_eq!(mtex.filenames[0], "terrain.blp");
    }

    #[test]
    fn test_mtex_chunk_empty() {
        let data = b"";
        let mut cursor = Cursor::new(data.to_vec());

        let mtex = MtexChunk::read_le(&mut cursor).unwrap();
        assert_eq!(mtex.filenames.len(), 0);
    }

    #[test]
    fn test_mtex_chunk_utf8_lossy() {
        // Test lossy UTF-8 conversion with invalid byte
        let data = b"test\xFF.blp\0"; // 0xFF is invalid UTF-8
        let mut cursor = Cursor::new(data.to_vec());

        let mtex = MtexChunk::read_le(&mut cursor).unwrap();
        assert_eq!(mtex.filenames.len(), 1);
        // Should handle invalid UTF-8 gracefully with replacement character
        assert!(mtex.filenames[0].contains("test"));
        assert!(mtex.filenames[0].contains(".blp"));
    }

    #[test]
    fn test_mtex_chunk_backslash_path() {
        // Real WoW texture path with backslashes
        let data = b"Tileset\\Generic\\CityTile.blp\0";
        let mut cursor = Cursor::new(data.to_vec());

        let mtex = MtexChunk::read_le(&mut cursor).unwrap();
        assert_eq!(mtex.filenames.len(), 1);
        assert_eq!(mtex.filenames[0], "Tileset\\Generic\\CityTile.blp");
    }

    #[test]
    fn test_mtex_chunk_multiple_paths() {
        let data = b"Tileset\\Ground.blp\0Tileset\\Detail.blp\0Tileset\\Alpha.blp\0";
        let mut cursor = Cursor::new(data.to_vec());

        let mtex = MtexChunk::read_le(&mut cursor).unwrap();
        assert_eq!(mtex.filenames.len(), 3);
        assert_eq!(mtex.filenames[0], "Tileset\\Ground.blp");
        assert_eq!(mtex.filenames[1], "Tileset\\Detail.blp");
        assert_eq!(mtex.filenames[2], "Tileset\\Alpha.blp");
    }

    #[test]
    fn test_mtex_chunk_round_trip() {
        let original = MtexChunk {
            filenames: vec![
                "texture1.blp".to_string(),
                "texture2.blp".to_string(),
                "texture3.blp".to_string(),
            ],
        };

        let mut buffer = Cursor::new(Vec::new());
        original.write_le(&mut buffer).unwrap();

        let data = buffer.into_inner();
        let mut cursor = Cursor::new(data);
        let parsed = MtexChunk::read_le(&mut cursor).unwrap();

        assert_eq!(original.filenames.len(), parsed.filenames.len());
        assert_eq!(original.filenames, parsed.filenames);
    }

    #[test]
    fn test_mtex_chunk_round_trip_with_paths() {
        let original = MtexChunk {
            filenames: vec![
                "Tileset\\Ground\\Grass.blp".to_string(),
                "Tileset\\Detail\\Rock.blp".to_string(),
            ],
        };

        let mut buffer = Cursor::new(Vec::new());
        original.write_le(&mut buffer).unwrap();

        let data = buffer.into_inner();
        let mut cursor = Cursor::new(data);
        let parsed = MtexChunk::read_le(&mut cursor).unwrap();

        assert_eq!(original.filenames, parsed.filenames);
    }

    #[test]
    fn test_mtex_chunk_default() {
        let mtex = MtexChunk::default();
        assert_eq!(mtex.filenames.len(), 0);
    }

    #[test]
    fn test_mtex_chunk_no_trailing_null() {
        // Edge case: file ends without final null terminator
        let data = b"test.blp\0world.blp";
        let mut cursor = Cursor::new(data.to_vec());

        let mtex = MtexChunk::read_le(&mut cursor).unwrap();
        assert_eq!(mtex.filenames.len(), 2);
        assert_eq!(mtex.filenames[0], "test.blp");
        assert_eq!(mtex.filenames[1], "world.blp");
    }

    #[test]
    fn test_mwmo_chunk() {
        let data = b"building.wmo\0castle.wmo\0";
        let mut cursor = Cursor::new(data.to_vec());

        let mwmo = MwmoChunk::read_le(&mut cursor).unwrap();
        assert_eq!(mwmo.filenames.len(), 2);
        assert_eq!(mwmo.filenames[0], "building.wmo");
        assert_eq!(mwmo.filenames[1], "castle.wmo");
    }

    #[test]
    fn test_mwmo_chunk_single_file() {
        let data = b"dungeon.wmo\0";
        let mut cursor = Cursor::new(data.to_vec());

        let mwmo = MwmoChunk::read_le(&mut cursor).unwrap();
        assert_eq!(mwmo.filenames.len(), 1);
        assert_eq!(mwmo.filenames[0], "dungeon.wmo");
    }

    #[test]
    fn test_mwmo_chunk_empty() {
        let data = b"";
        let mut cursor = Cursor::new(data.to_vec());

        let mwmo = MwmoChunk::read_le(&mut cursor).unwrap();
        assert_eq!(mwmo.filenames.len(), 0);
    }

    #[test]
    fn test_mwmo_chunk_with_path() {
        let data = b"World\\wmo\\Dungeon\\Dungeon.wmo\0";
        let mut cursor = Cursor::new(data.to_vec());

        let mwmo = MwmoChunk::read_le(&mut cursor).unwrap();
        assert_eq!(mwmo.filenames.len(), 1);
        assert_eq!(mwmo.filenames[0], "World\\wmo\\Dungeon\\Dungeon.wmo");
    }

    #[test]
    fn test_mwmo_chunk_round_trip() {
        let original = MwmoChunk {
            filenames: vec!["building.wmo".to_string(), "castle.wmo".to_string()],
        };

        let mut buffer = Cursor::new(Vec::new());
        original.write_le(&mut buffer).unwrap();

        let data = buffer.into_inner();
        let mut cursor = Cursor::new(data);
        let parsed = MwmoChunk::read_le(&mut cursor).unwrap();

        assert_eq!(original.filenames, parsed.filenames);
    }

    #[test]
    fn test_mmdx_chunk() {
        let data = b"model1.m2\0model2.m2\0";
        let mut cursor = Cursor::new(data.to_vec());

        let mmdx = MmdxChunk::read_le(&mut cursor).unwrap();
        assert_eq!(mmdx.filenames.len(), 2);
        assert_eq!(mmdx.filenames[0], "model1.m2");
        assert_eq!(mmdx.filenames[1], "model2.m2");
    }

    #[test]
    fn test_mmdx_chunk_with_path() {
        let data = b"World\\Doodad\\Tree.m2\0";
        let mut cursor = Cursor::new(data.to_vec());

        let mmdx = MmdxChunk::read_le(&mut cursor).unwrap();
        assert_eq!(mmdx.filenames.len(), 1);
        assert_eq!(mmdx.filenames[0], "World\\Doodad\\Tree.m2");
    }

    #[test]
    fn test_mmdx_chunk_round_trip() {
        let original = MmdxChunk {
            filenames: vec!["model1.m2".to_string(), "model2.m2".to_string()],
        };

        let mut buffer = Cursor::new(Vec::new());
        original.write_le(&mut buffer).unwrap();

        let data = buffer.into_inner();
        let mut cursor = Cursor::new(data);
        let parsed = MmdxChunk::read_le(&mut cursor).unwrap();

        assert_eq!(original.filenames, parsed.filenames);
    }

    #[test]
    fn test_mmid_chunk() {
        // 3 offsets: 0, 10, 20
        let data = vec![
            0x00, 0x00, 0x00, 0x00, // offset[0] = 0
            0x0A, 0x00, 0x00, 0x00, // offset[1] = 10
            0x14, 0x00, 0x00, 0x00, // offset[2] = 20
        ];
        let mut cursor = Cursor::new(data);
        let mmid = MmidChunk::read_le(&mut cursor).unwrap();

        assert_eq!(mmid.offsets.len(), 3);
        assert_eq!(mmid.offsets[0], 0);
        assert_eq!(mmid.offsets[1], 10);
        assert_eq!(mmid.offsets[2], 20);
    }

    #[test]
    fn test_mmid_validate_offsets() {
        let mmid = MmidChunk {
            offsets: vec![0, 10, 20],
        };

        // All offsets valid with MMDX size of 30
        assert!(mmid.validate_offsets(30));

        // Offset 20 invalid with MMDX size of 20
        assert!(!mmid.validate_offsets(20));
    }

    #[test]
    fn test_mmid_get_filename_index() {
        let mmdx = MmdxChunk {
            filenames: vec![
                "model1.m2".to_string(), // offset 0
                "model2.m2".to_string(), // offset 10 (9 + 1)
                "model3.m2".to_string(), // offset 20 (10 + 10)
            ],
        };

        let mmid = MmidChunk {
            offsets: vec![0, 10, 20],
        };

        assert_eq!(mmid.get_filename_index(&mmdx, 0), Some(0));
        assert_eq!(mmid.get_filename_index(&mmdx, 10), Some(1));
        assert_eq!(mmid.get_filename_index(&mmdx, 20), Some(2));
        assert_eq!(mmid.get_filename_index(&mmdx, 999), None);
    }
    #[test]
    fn test_mwid_chunk() {
        // 2 offsets: 0, 15
        let data = vec![
            0x00, 0x00, 0x00, 0x00, // offset[0] = 0
            0x0F, 0x00, 0x00, 0x00, // offset[1] = 15
        ];
        let mut cursor = Cursor::new(data);
        let mwid = MwidChunk::read_le(&mut cursor).unwrap();

        assert_eq!(mwid.offsets.len(), 2);
        assert_eq!(mwid.offsets[0], 0);
        assert_eq!(mwid.offsets[1], 15);
    }

    #[test]
    fn test_mwid_validate_offsets() {
        let mwid = MwidChunk {
            offsets: vec![0, 15, 30],
        };

        // All offsets valid with MWMO size of 40
        assert!(mwid.validate_offsets(40));

        // Offset 30 invalid with MWMO size of 30
        assert!(!mwid.validate_offsets(30));
    }

    #[test]
    fn test_mwid_get_filename_index() {
        let mwmo = MwmoChunk {
            filenames: vec![
                "building.wmo".to_string(), // offset 0
                "castle.wmo".to_string(),   // offset 13 (12 + 1)
            ],
        };

        let mwid = MwidChunk {
            offsets: vec![0, 13],
        };

        assert_eq!(mwid.get_filename_index(&mwmo, 0), Some(0));
        assert_eq!(mwid.get_filename_index(&mwmo, 13), Some(1));
        assert_eq!(mwid.get_filename_index(&mwmo, 999), None);
    }
}