wowsunpack 0.31.0

Utility for interacting with World of Warships game assets
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
use rootcause::Report;
use thiserror::Error;
use winnow::Parser;
use winnow::binary::le_i64;
use winnow::binary::le_u8;
use winnow::binary::le_u16;
use winnow::binary::le_u32;
use winnow::combinator::repeat;
use winnow::token::take;

use crate::data::parser_utils::WResult;
use crate::data::parser_utils::parse_packed_string;
use crate::data::parser_utils::resolve_relptr;

const ENCD_MAGIC: u32 = 0x44434E45;

/// Errors that can occur during `.geometry` file parsing.
#[derive(Debug, Error)]
pub enum GeometryError {
    #[error("invalid vertex stride: {0}")]
    InvalidStride(usize),
    #[error("unsupported index size: {0}")]
    UnsupportedIndexSize(u16),
    #[error("data extends beyond file at 0x{offset:X}")]
    OutOfBounds { offset: usize },
    #[error("meshopt decode error: {0}")]
    DecodeError(String),
    #[error("parse error: {0}")]
    ParseError(String),
}

#[derive(Debug)]
pub struct MergedGeometry<'a> {
    pub vertices_mapping: Vec<MappingEntry>,
    pub indices_mapping: Vec<MappingEntry>,
    pub merged_vertices: Vec<VerticesPrototype<'a>>,
    pub merged_indices: Vec<IndicesPrototype<'a>>,
    pub collision_models: Vec<ModelPrototype<'a>>,
    pub armor_models: Vec<ArmorModel>,
}

/// A single triangle in an armor model.
#[derive(Debug, Clone, Copy)]
pub struct ArmorTriangle {
    pub vertices: [[f32; 3]; 3],
    pub normals: [[f32; 3]; 3],
    /// Collision material ID from the BVH node header (byte 0 of the u32 first_dword).
    /// Maps to a collision material name (e.g. "Cit_Belt", "Bow_Bottom").
    pub material_id: u8,
    /// 1-based layer index from the BVH node header (byte 2 of the u32 first_dword).
    /// The u32 encodes `(layer_index << 16) | material_id`.
    /// Multi-layer armor materials have separate BVH node groups per layer,
    /// each covering different spatial regions. This maps directly to the
    /// `model_index` in GameParams armor keys: `(model_index << 16) | material_id`.
    pub layer_index: u8,
}

/// A parsed armor model containing triangle geometry for hit detection.
#[derive(Debug)]
pub struct ArmorModel {
    pub name: String,
    pub triangles: Vec<ArmorTriangle>,
}

/// A named axis-aligned bounding box from a `.splash` file.
/// Used to classify armor triangles into hit-location zones.
#[derive(Debug, Clone)]
pub struct SplashBox {
    pub name: String,
    pub min: [f32; 3],
    pub max: [f32; 3],
}

#[derive(Debug, Clone)]
pub struct MappingEntry {
    pub mapping_id: u32,
    pub merged_buffer_index: u16,
    pub packed_texel_density: u16,
    pub items_offset: u32,
    pub items_count: u32,
}

#[derive(Debug)]
pub struct VerticesPrototype<'a> {
    pub data: VertexData<'a>,
    pub format_name: String,
    pub size_in_bytes: u32,
    pub stride_in_bytes: u16,
    pub is_skinned: bool,
    pub is_bumped: bool,
}

#[derive(Debug)]
pub struct IndicesPrototype<'a> {
    pub data: IndexData<'a>,
    pub size_in_bytes: u32,
    pub index_size: u16,
}

#[derive(Debug)]
pub struct ModelPrototype<'a> {
    pub data: &'a [u8],
    pub name: String,
    pub size_in_bytes: u32,
}

#[derive(Debug)]
pub enum VertexData<'a> {
    Encoded { element_count: u32, payload: &'a [u8], stride: u16 },
    Raw(&'a [u8]),
}

#[derive(Debug)]
pub enum IndexData<'a> {
    Encoded { element_count: u32, payload: &'a [u8], index_size: u16 },
    Raw(&'a [u8]),
}

/// Decode a meshoptimizer-encoded vertex buffer with a runtime-known stride.
///
/// meshopt_rs requires `size_of::<Vertex>() == stride`, but our stride is only known at
/// runtime. We dispatch to a monomorphized call for each supported stride value.
fn decode_vertex_buffer_dynamic(count: usize, stride: usize, encoded: &[u8]) -> Result<Vec<u8>, Report<GeometryError>> {
    if stride == 0 || stride > 256 || !stride.is_multiple_of(4) {
        return Err(Report::new(GeometryError::InvalidStride(stride)));
    }

    let total_bytes = count * stride;
    let mut output = vec![0u8; total_bytes];

    macro_rules! decode_with_stride {
        ($stride:literal, $count:expr, $encoded:expr, $output:expr) => {{
            #[repr(C, align(4))]
            #[derive(Copy, Clone)]
            struct Vertex([u8; $stride]);
            // Safety: output buffer has exactly count * stride bytes, and Vertex has size = stride.
            // The decode function reads/writes through the slice as raw bytes internally.
            let vertex_slice: &mut [Vertex] =
                unsafe { std::slice::from_raw_parts_mut($output.as_mut_ptr() as *mut Vertex, $count) };
            meshopt_rs::vertex::buffer::decode_vertex_buffer(vertex_slice, $encoded)
                .map_err(|e| Report::new(GeometryError::DecodeError(format!("{e:?}"))))?;
        }};
    }

    match stride {
        4 => decode_with_stride!(4, count, encoded, output),
        8 => decode_with_stride!(8, count, encoded, output),
        12 => decode_with_stride!(12, count, encoded, output),
        16 => decode_with_stride!(16, count, encoded, output),
        20 => decode_with_stride!(20, count, encoded, output),
        24 => decode_with_stride!(24, count, encoded, output),
        28 => decode_with_stride!(28, count, encoded, output),
        32 => decode_with_stride!(32, count, encoded, output),
        36 => decode_with_stride!(36, count, encoded, output),
        40 => decode_with_stride!(40, count, encoded, output),
        44 => decode_with_stride!(44, count, encoded, output),
        48 => decode_with_stride!(48, count, encoded, output),
        52 => decode_with_stride!(52, count, encoded, output),
        56 => decode_with_stride!(56, count, encoded, output),
        60 => decode_with_stride!(60, count, encoded, output),
        64 => decode_with_stride!(64, count, encoded, output),
        _ => return Err(Report::new(GeometryError::InvalidStride(stride))),
    }

    Ok(output)
}

impl VertexData<'_> {
    pub fn decode(&self) -> Result<Vec<u8>, Report<GeometryError>> {
        match self {
            VertexData::Encoded { element_count, payload, stride } => {
                decode_vertex_buffer_dynamic(*element_count as usize, *stride as usize, payload)
            }
            VertexData::Raw(data) => Ok(data.to_vec()),
        }
    }
}

impl IndexData<'_> {
    pub fn decode(&self) -> Result<Vec<u8>, Report<GeometryError>> {
        match self {
            IndexData::Encoded { element_count, payload, index_size } => {
                let count = *element_count as usize;
                let mut output = vec![0u32; count];
                meshopt_rs::index::buffer::decode_index_buffer(&mut output, payload)
                    .map_err(|e| Report::new(GeometryError::DecodeError(format!("{e:?}"))))?;

                match index_size {
                    2 => Ok(output.iter().flat_map(|i| (*i as u16).to_le_bytes()).collect()),
                    4 => Ok(output.iter().flat_map(|i| i.to_le_bytes()).collect()),
                    other => Err(Report::new(GeometryError::UnsupportedIndexSize(*other))),
                }
            }
            IndexData::Raw(data) => Ok(data.to_vec()),
        }
    }
}

fn parse_mapping_entry(input: &mut &[u8]) -> WResult<MappingEntry> {
    let mapping_id = le_u32.parse_next(input)?;
    let merged_buffer_index = le_u16.parse_next(input)?;
    let packed_texel_density = le_u16.parse_next(input)?;
    let items_offset = le_u32.parse_next(input)?;
    let items_count = le_u32.parse_next(input)?;
    Ok(MappingEntry { mapping_id, merged_buffer_index, packed_texel_density, items_offset, items_count })
}

/// Header fields parsed by winnow, before sub-structure resolution.
struct HeaderFields {
    merged_vertices_count: u32,
    merged_indices_count: u32,
    vertices_mapping_count: u32,
    indices_mapping_count: u32,
    collision_model_count: u32,
    armor_model_count: u32,
    vertices_mapping_ptr: i64,
    indices_mapping_ptr: i64,
    merged_vertices_ptr: i64,
    merged_indices_ptr: i64,
    collision_models_ptr: i64,
    armor_models_ptr: i64,
}

fn parse_header(input: &mut &[u8]) -> WResult<HeaderFields> {
    let merged_vertices_count = le_u32.parse_next(input)?;
    let merged_indices_count = le_u32.parse_next(input)?;
    let vertices_mapping_count = le_u32.parse_next(input)?;
    let indices_mapping_count = le_u32.parse_next(input)?;
    let collision_model_count = le_u32.parse_next(input)?;
    let armor_model_count = le_u32.parse_next(input)?;
    let vertices_mapping_ptr = le_i64.parse_next(input)?;
    let indices_mapping_ptr = le_i64.parse_next(input)?;
    let merged_vertices_ptr = le_i64.parse_next(input)?;
    let merged_indices_ptr = le_i64.parse_next(input)?;
    let collision_models_ptr = le_i64.parse_next(input)?;
    let armor_models_ptr = le_i64.parse_next(input)?;
    Ok(HeaderFields {
        merged_vertices_count,
        merged_indices_count,
        vertices_mapping_count,
        indices_mapping_count,
        collision_model_count,
        armor_model_count,
        vertices_mapping_ptr,
        indices_mapping_ptr,
        merged_vertices_ptr,
        merged_indices_ptr,
        collision_models_ptr,
        armor_models_ptr,
    })
}

/// Parse the header and resolve all sub-structures from the full file data.
pub fn parse_geometry(file_data: &[u8]) -> Result<MergedGeometry<'_>, Report<GeometryError>> {
    let input = &mut &file_data[..];

    let hdr = parse_header(input).map_err(|e| Report::new(GeometryError::ParseError(format!("header: {e}"))))?;

    let header_base = 0usize;

    let vm_offset = resolve_relptr(header_base, hdr.vertices_mapping_ptr);
    let vertices_mapping = parse_mapping_array(file_data, vm_offset, hdr.vertices_mapping_count as usize)?;

    let im_offset = resolve_relptr(header_base, hdr.indices_mapping_ptr);
    let indices_mapping = parse_mapping_array(file_data, im_offset, hdr.indices_mapping_count as usize)?;

    let mv_offset = resolve_relptr(header_base, hdr.merged_vertices_ptr);
    let merged_vertices = parse_vertices_array(file_data, mv_offset, hdr.merged_vertices_count as usize)?;

    let mi_offset = resolve_relptr(header_base, hdr.merged_indices_ptr);
    let merged_indices = parse_indices_array(file_data, mi_offset, hdr.merged_indices_count as usize)?;

    let collision_models = if hdr.collision_model_count > 0 {
        let cm_offset = resolve_relptr(header_base, hdr.collision_models_ptr);
        parse_model_array(file_data, cm_offset, hdr.collision_model_count as usize)?
    } else {
        Vec::new()
    };

    let armor_models = if hdr.armor_model_count > 0 {
        let am_offset = resolve_relptr(header_base, hdr.armor_models_ptr);
        parse_armor_model_array(file_data, am_offset, hdr.armor_model_count as usize)?
    } else {
        Vec::new()
    };

    Ok(MergedGeometry {
        vertices_mapping,
        indices_mapping,
        merged_vertices,
        merged_indices,
        collision_models,
        armor_models,
    })
}

fn parse_mapping_array(
    file_data: &[u8],
    offset: usize,
    count: usize,
) -> Result<Vec<MappingEntry>, Report<GeometryError>> {
    let input = &mut &file_data[offset..];
    let entries: Vec<MappingEntry> = repeat(count, parse_mapping_entry)
        .parse_next(input)
        .map_err(|e| Report::new(GeometryError::ParseError(format!("mapping entries at 0x{offset:X}: {e}"))))?;
    Ok(entries)
}

fn parse_vertex_data<'a>(
    file_data: &'a [u8],
    data_offset: usize,
    size_in_bytes: u32,
    stride_in_bytes: u16,
) -> Result<VertexData<'a>, Report<GeometryError>> {
    if data_offset + size_in_bytes as usize > file_data.len() {
        return Err(Report::new(GeometryError::OutOfBounds { offset: data_offset }));
    }
    let blob = &file_data[data_offset..data_offset + size_in_bytes as usize];

    if blob.len() >= 8 {
        let magic = u32::from_le_bytes(blob[0..4].try_into().unwrap());
        if magic == ENCD_MAGIC {
            let element_count = u32::from_le_bytes(blob[4..8].try_into().unwrap());
            return Ok(VertexData::Encoded { element_count, payload: &blob[8..], stride: stride_in_bytes });
        }
    }

    Ok(VertexData::Raw(blob))
}

fn parse_index_data<'a>(
    file_data: &'a [u8],
    data_offset: usize,
    size_in_bytes: u32,
    index_size: u16,
) -> Result<IndexData<'a>, Report<GeometryError>> {
    if data_offset + size_in_bytes as usize > file_data.len() {
        return Err(Report::new(GeometryError::OutOfBounds { offset: data_offset }));
    }
    let blob = &file_data[data_offset..data_offset + size_in_bytes as usize];

    if blob.len() >= 8 {
        let magic = u32::from_le_bytes(blob[0..4].try_into().unwrap());
        if magic == ENCD_MAGIC {
            let element_count = u32::from_le_bytes(blob[4..8].try_into().unwrap());
            return Ok(IndexData::Encoded { element_count, payload: &blob[8..], index_size });
        }
    }

    Ok(IndexData::Raw(blob))
}

/// Parse the struct fields of a VerticesPrototype (0x20 bytes).
/// Returns (data_relptr, size_in_bytes, stride_in_bytes, is_skinned, is_bumped).
fn parse_vertices_fields(input: &mut &[u8]) -> WResult<(i64, u32, u16, bool, bool)> {
    let data_relptr = le_i64.parse_next(input)?;
    let _packed_string: &[u8] = take(16usize).parse_next(input)?;
    let size_in_bytes = le_u32.parse_next(input)?;
    let stride_in_bytes = le_u16.parse_next(input)?;
    let is_skinned = le_u8.parse_next(input)? != 0;
    let is_bumped = le_u8.parse_next(input)? != 0;
    Ok((data_relptr, size_in_bytes, stride_in_bytes, is_skinned, is_bumped))
}

fn parse_vertices_array<'a>(
    file_data: &'a [u8],
    offset: usize,
    count: usize,
) -> Result<Vec<VerticesPrototype<'a>>, Report<GeometryError>> {
    let mut result = Vec::with_capacity(count);

    for i in 0..count {
        let struct_base = offset + i * 0x20;
        let input = &mut &file_data[struct_base..];

        let (data_relptr, size_in_bytes, stride_in_bytes, is_skinned, is_bumped) = parse_vertices_fields(input)
            .map_err(|e| Report::new(GeometryError::ParseError(format!("vertices[{i}]: {e}"))))?;

        let packed_string_base = struct_base + 0x08;
        let format_name = parse_packed_string(file_data, packed_string_base)
            .map_err(|e| Report::new(GeometryError::ParseError(format!("vertices[{i}] packed string: {e}"))))?;
        let data_offset = resolve_relptr(struct_base, data_relptr);
        let data = parse_vertex_data(file_data, data_offset, size_in_bytes, stride_in_bytes)?;

        result.push(VerticesPrototype { data, format_name, size_in_bytes, stride_in_bytes, is_skinned, is_bumped });
    }

    Ok(result)
}

/// Parse a `.splash` file containing named axis-aligned bounding boxes.
///
/// Format: `u32 count`, then per box: `u32 name_len`, `name` bytes, `6× f32 (min_xyz, max_xyz)`.
pub fn parse_splash_file(data: &[u8]) -> Result<Vec<SplashBox>, Report<GeometryError>> {
    let err = || GeometryError::ParseError("splash: unexpected end of data".into());
    let mut pos = 0usize;

    let read_u32 = |pos: &mut usize| -> Result<u32, Report<GeometryError>> {
        let end = *pos + 4;
        let bytes = data.get(*pos..end).ok_or_else(err)?;
        *pos = end;
        Ok(u32::from_le_bytes(bytes.try_into().unwrap()))
    };

    let read_f32 = |pos: &mut usize| -> Result<f32, Report<GeometryError>> {
        let end = *pos + 4;
        let bytes = data.get(*pos..end).ok_or_else(err)?;
        *pos = end;
        Ok(f32::from_le_bytes(bytes.try_into().unwrap()))
    };

    let count = read_u32(&mut pos)? as usize;
    let mut boxes = Vec::with_capacity(count);

    for _ in 0..count {
        let name_len = read_u32(&mut pos)? as usize;
        let name_end = pos + name_len;
        let name_bytes = data.get(pos..name_end).ok_or_else(err)?;
        let name = std::str::from_utf8(name_bytes)
            .map_err(|_| GeometryError::ParseError("splash: invalid UTF-8 name".into()))?
            .to_string();
        pos = name_end;

        let min_x = read_f32(&mut pos)?;
        let min_y = read_f32(&mut pos)?;
        let min_z = read_f32(&mut pos)?;
        let max_x = read_f32(&mut pos)?;
        let max_y = read_f32(&mut pos)?;
        let max_z = read_f32(&mut pos)?;

        boxes.push(SplashBox { name, min: [min_x, min_y, min_z], max: [max_x, max_y, max_z] });
    }

    Ok(boxes)
}

/// Parse the struct fields of an IndicesPrototype (0x10 bytes).
/// Returns (data_relptr, size_in_bytes, index_size).
fn parse_indices_fields(input: &mut &[u8]) -> WResult<(i64, u32, u16)> {
    let data_relptr = le_i64.parse_next(input)?;
    let size_in_bytes = le_u32.parse_next(input)?;
    let _reserved = le_u16.parse_next(input)?;
    let index_size = le_u16.parse_next(input)?;
    Ok((data_relptr, size_in_bytes, index_size))
}

fn parse_indices_array<'a>(
    file_data: &'a [u8],
    offset: usize,
    count: usize,
) -> Result<Vec<IndicesPrototype<'a>>, Report<GeometryError>> {
    let mut result = Vec::with_capacity(count);

    for i in 0..count {
        let struct_base = offset + i * 0x10;
        let input = &mut &file_data[struct_base..];

        let (data_relptr, size_in_bytes, index_size) = parse_indices_fields(input)
            .map_err(|e| Report::new(GeometryError::ParseError(format!("indices[{i}]: {e}"))))?;

        let data_offset = resolve_relptr(struct_base, data_relptr);
        let data = parse_index_data(file_data, data_offset, size_in_bytes, index_size)?;

        result.push(IndicesPrototype { data, size_in_bytes, index_size });
    }

    Ok(result)
}

/// Parse the struct fields of a ModelPrototype (0x20 bytes: armor or collision).
/// Returns (data_relptr, size_in_bytes).
fn parse_model_fields(input: &mut &[u8]) -> WResult<(i64, u32)> {
    let data_relptr = le_i64.parse_next(input)?;
    let _packed_string: &[u8] = take(16usize).parse_next(input)?;
    let size_in_bytes = le_u32.parse_next(input)?;
    let _padding = le_u32.parse_next(input)?;
    Ok((data_relptr, size_in_bytes))
}

/// Parse the BVH + triangle data from an armor model's raw 16-byte entry stream.
///
/// Format (all entries are 16 bytes):
///   - 2 global header entries (bounding box + BVH node count)
///   - N BVH node groups, each:
///       - 2 entries: node header (flags, bbox min) + bbox max with vertex_count
///       - vertex_count triangle vertices (groups of 3 = triangles)
///   - Each vertex: f32 x, f32 y, f32 z, u8[3] packed_normal, u8 zero
fn parse_armor_data(data: &[u8]) -> Result<Vec<ArmorTriangle>, Report<GeometryError>> {
    const ENTRY_SIZE: usize = 16;

    if data.len() < ENTRY_SIZE * 2 {
        return Ok(Vec::new());
    }

    let entry_count = data.len() / ENTRY_SIZE;
    if entry_count < 2 {
        return Ok(Vec::new());
    }

    let read_f32 = |off: usize| -> f32 { f32::from_le_bytes(data[off..off + 4].try_into().unwrap()) };
    let read_u32 = |off: usize| -> u32 { u32::from_le_bytes(data[off..off + 4].try_into().unwrap()) };

    // Skip 2-entry global header, then walk BVH node groups
    let mut pos = 2; // entry index
    let mut triangles = Vec::new();

    while pos < entry_count {
        // Each BVH node group: 2 header entries + vertex_count vertex entries
        if pos + 1 >= entry_count {
            break;
        }

        // First entry of node header: u32 encoding (layer_index << 16) | material_id
        // byte 0 = collision material ID, byte 2 = 1-based layer index
        // This matches the GameParams key encoding: (model_index << 16) | material_id
        let node_entry0_off = pos * ENTRY_SIZE;
        let material_id = data[node_entry0_off];
        let layer_index = data[node_entry0_off + 2];

        // Second entry of the node header has vertex_count at bytes 12..16
        let node_entry1_off = (pos + 1) * ENTRY_SIZE;
        let vertex_count = read_u32(node_entry1_off + 12) as usize;
        pos += 2; // skip 2 node header entries

        if vertex_count == 0 {
            continue;
        }
        if pos + vertex_count > entry_count {
            return Err(Report::new(GeometryError::ParseError(format!(
                "armor BVH node claims {} vertices but only {} entries remain",
                vertex_count,
                entry_count - pos
            ))));
        }

        // vertex_count should be divisible by 3 (triangle soup)
        let tri_count = vertex_count / 3;
        for t in 0..tri_count {
            let mut tri = ArmorTriangle { vertices: [[0.0; 3]; 3], normals: [[0.0; 3]; 3], material_id, layer_index };
            for v in 0..3 {
                let entry_off = (pos + t * 3 + v) * ENTRY_SIZE;
                tri.vertices[v] = [read_f32(entry_off), read_f32(entry_off + 4), read_f32(entry_off + 8)];
                // Packed normal: 3 bytes at offset 12, each maps [-1, 1]
                let nx = data[entry_off + 12] as f32 / 127.5 - 1.0;
                let ny = data[entry_off + 13] as f32 / 127.5 - 1.0;
                let nz = data[entry_off + 14] as f32 / 127.5 - 1.0;
                tri.normals[v] = [nx, ny, nz];
            }
            triangles.push(tri);
        }

        pos += vertex_count;
    }

    Ok(triangles)
}

/// Parse armor model array. Unlike collision models where data_relptr points to
/// the start of the data, for armor models the actual data extends from right
/// after the struct (struct_base + 0x20) to data_offset + size_in_bytes.
fn parse_armor_model_array(
    file_data: &[u8],
    offset: usize,
    count: usize,
) -> Result<Vec<ArmorModel>, Report<GeometryError>> {
    let mut result = Vec::with_capacity(count);

    for i in 0..count {
        let struct_base = offset + i * 0x20;
        let input = &mut &file_data[struct_base..];

        let (data_relptr, size_in_bytes) = parse_model_fields(input)
            .map_err(|e| Report::new(GeometryError::ParseError(format!("armor[{i}]: {e}"))))?;

        let packed_string_base = struct_base + 0x08;
        let name = parse_packed_string(file_data, packed_string_base)
            .map_err(|e| Report::new(GeometryError::ParseError(format!("armor[{i}] packed string: {e}"))))?;

        // Armor data starts right after the struct and extends to where
        // data_relptr + size_in_bytes points (relptr points near the end)
        let data_start = struct_base + 0x20;
        let data_end = resolve_relptr(struct_base, data_relptr) + size_in_bytes as usize;

        if data_end > file_data.len() {
            return Err(Report::new(GeometryError::OutOfBounds { offset: data_end }));
        }

        let armor_data = &file_data[data_start..data_end];
        let triangles = parse_armor_data(armor_data)?;

        result.push(ArmorModel { name, triangles });
    }

    Ok(result)
}

fn parse_model_array<'a>(
    file_data: &'a [u8],
    offset: usize,
    count: usize,
) -> Result<Vec<ModelPrototype<'a>>, Report<GeometryError>> {
    let mut result = Vec::with_capacity(count);

    for i in 0..count {
        let struct_base = offset + i * 0x20;
        let input = &mut &file_data[struct_base..];

        let (data_relptr, size_in_bytes) = parse_model_fields(input)
            .map_err(|e| Report::new(GeometryError::ParseError(format!("model[{i}]: {e}"))))?;

        let packed_string_base = struct_base + 0x08;
        let name = parse_packed_string(file_data, packed_string_base)
            .map_err(|e| Report::new(GeometryError::ParseError(format!("model[{i}] packed string: {e}"))))?;
        let data_offset = resolve_relptr(struct_base, data_relptr);

        if data_offset + size_in_bytes as usize > file_data.len() {
            return Err(Report::new(GeometryError::OutOfBounds { offset: data_offset }));
        }
        let data = &file_data[data_offset..data_offset + size_in_bytes as usize];

        result.push(ModelPrototype { data, name, size_in_bytes });
    }

    Ok(result)
}