Skip to main content

dayzmake_core/
odol.rs

1//! Reading binarized ODOL models (version 56), the format of every `.p3d` in the DayZ game files.
2//!
3//! [`Odol::read`](crate::odol::Odol::read) reads what a listing needs: the model-level properties, the skeleton, the animation
4//! classes and, per LOD, the vertex and face counts, textures, materials, proxies, named selections and
5//! properties. It doesn't decode geometry. Every count and size in the file is checked against the bytes
6//! left and against a limit before it is used, and a LOD that fails to parse is reported with its index and
7//! file offset.
8//!
9//! The reader seeks a lot and reads small values, so it reads the header part through its own buffer and the bytes
10//! of each LOD into memory in one piece (at most 256 MiB per LOD), then parses them from there. Pass a
11//! [`std::fs::File`] directly; a [`std::io::BufReader`] around it is not needed.
12
13use std::io::{BufReader, Cursor, Error, ErrorKind, Read, Seek, SeekFrom};
14
15use byteorder::{LittleEndian, ReadBytesExt};
16
17use crate::error::ErrorExt;
18
19/// The only ODOL version this reader understands.
20const SUPPORTED_VERSION: u32 = 56;
21/// Longest string accepted.
22const MAX_STRING: usize = 64 * 1024;
23/// Most LODs, proxies, textures, materials, sections, selections, properties and similar lists.
24const MAX_ITEMS: u32 = 1 << 16;
25/// Largest decompressed array.
26const MAX_ARRAY_BYTES: usize = 64 * 1024 * 1024;
27/// Arrays of at least this many bytes are LZO compressed.
28const COMPRESSION_THRESHOLD: usize = 1024;
29/// Most bytes all arrays of one file may decompress to together. LZO shrinks 255:1, so without a total a file of
30/// a megabyte could ask for terabytes.
31const MAX_TOTAL_ARRAY_BYTES: u64 = 256 * 1024 * 1024;
32/// Most bytes of one LOD that are read into memory.
33const MAX_LOD_BYTES: u64 = 256 * 1024 * 1024;
34
35/// A binarized model: the model-level facts and the LODs.
36#[derive(Debug, Clone, PartialEq)]
37#[non_exhaustive]
38pub struct Odol {
39    /// Format version (always 56).
40    pub version: u32,
41    /// The model's `class` property (for example `house`); empty if none.
42    pub property_class: String,
43    /// The model's `damage` property (for example `tent`); empty if none.
44    pub property_damage: String,
45    /// Skeleton name, empty if the model has none.
46    pub skeleton_name: String,
47    /// Number of bones in the skeleton.
48    pub bone_count: u32,
49    /// Mass of the model.
50    pub mass: f32,
51    /// Bounding box minimum, model space.
52    pub bbox_min: [f32; 3],
53    /// Bounding box maximum, model space.
54    pub bbox_max: [f32; 3],
55    /// Animation classes, in file order.
56    pub animations: Vec<OdolAnimation>,
57    /// The LODs, in file order.
58    pub lods: Vec<OdolLod>,
59}
60
61/// An animation class of the model (a `model.cfg` animation, baked into the model).
62#[derive(Debug, Clone, PartialEq, Eq)]
63#[non_exhaustive]
64pub struct OdolAnimation {
65    /// Animation type: 0 rotation, 1 to 3 rotationX/Y/Z, 4 translation, 5 to 7 translationX/Y/Z, 8 direct,
66    /// 9 hide.
67    pub kind: u32,
68    /// Class name (for example `Door1`).
69    pub name: String,
70    /// Animation source (for example `door1`).
71    pub source: String,
72}
73
74/// A LOD of a binarized model.
75#[derive(Debug, Clone, PartialEq)]
76#[non_exhaustive]
77pub struct OdolLod {
78    /// LOD resolution; special LODs (geometry, memory, ...) have large well known values.
79    pub resolution: f32,
80    /// Number of vertices.
81    pub vertex_count: u32,
82    /// Number of faces.
83    pub face_count: u32,
84    /// Texture paths used by faces.
85    pub textures: Vec<String>,
86    /// Materials with the texture paths of their stages.
87    pub materials: Vec<OdolMaterial>,
88    /// Proxy model paths.
89    pub proxies: Vec<String>,
90    /// Named selection names.
91    pub selections: Vec<String>,
92    /// LOD properties as (name, value) pairs.
93    pub properties: Vec<(String, String)>,
94}
95
96/// A material of a LOD.
97#[derive(Debug, Clone, PartialEq, Eq)]
98#[non_exhaustive]
99pub struct OdolMaterial {
100    /// Material path (`.rvmat`), empty for built-in materials.
101    pub name: String,
102    /// Texture paths of the stages, empty entries left out.
103    pub textures: Vec<String>,
104}
105
106impl OdolLod {
107    /// Human readable name of this LOD (for example "Geometry", or its resolution), as for MLOD LODs.
108    #[must_use]
109    pub fn name(&self) -> String {
110        crate::p3d::resolution_name(self.resolution)
111    }
112}
113
114/// A [`BufReader`] whose relative seeks keep the buffer (the plain one drops it on every `Current` seek, and
115/// each skip would then cost a refill).
116struct Buffered<R: Read + Seek>(BufReader<R>);
117
118impl<R: Read + Seek> Read for Buffered<R> {
119    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
120        self.0.read(buf)
121    }
122}
123
124impl<R: Read + Seek> Seek for Buffered<R> {
125    fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
126        if let SeekFrom::Current(n) = pos {
127            self.0.seek_relative(n)?;
128            return self.0.stream_position();
129        }
130        self.0.seek(pos)
131    }
132
133    fn stream_position(&mut self) -> std::io::Result<u64> {
134        self.0.stream_position()
135    }
136}
137
138/// What is left of `budget` after `total` more decompressed bytes; `limit` is the whole budget, named in the error.
139fn spend(budget: u64, total: u64, limit: u64) -> Result<u64, Error> {
140    budget
141        .checked_sub(total)
142        .ok_or_else(|| error!("the arrays of the file decompress to more than {limit} bytes"))
143}
144
145/// A reader that knows how many bytes are left.
146struct Rd<'a, R: Read + Seek> {
147    r: &'a mut R,
148    len: u64,
149    /// Decompressed bytes still allowed for this file.
150    budget: u64,
151    /// The whole budget of the file, named in the error.
152    limit: u64,
153}
154
155impl<R: Read + Seek> Rd<'_, R> {
156    fn pos(&mut self) -> Result<u64, Error> {
157        self.r.stream_position()
158    }
159
160    fn left(&mut self) -> Result<u64, Error> {
161        let pos = self.pos()?;
162        Ok(self.len.saturating_sub(pos))
163    }
164
165    fn need(&mut self, n: u64) -> Result<(), Error> {
166        if n > self.left()? {
167            return Err(error!("{n} bytes wanted, the file ends sooner"));
168        }
169        Ok(())
170    }
171
172    fn skip(&mut self, n: u64) -> Result<(), Error> {
173        self.need(n)?;
174        let n = i64::try_from(n).map_err(|_| error!("skip too long"))?;
175        self.r.seek(SeekFrom::Current(n))?;
176        Ok(())
177    }
178
179    fn u8(&mut self) -> Result<u8, Error> {
180        self.r.read_u8()
181    }
182
183    fn u32(&mut self) -> Result<u32, Error> {
184        self.r.read_u32::<LittleEndian>()
185    }
186
187    fn f32(&mut self) -> Result<f32, Error> {
188        self.r.read_f32::<LittleEndian>()
189    }
190
191    fn vec3(&mut self) -> Result<[f32; 3], Error> {
192        Ok([self.f32()?, self.f32()?, self.f32()?])
193    }
194
195    /// A NUL terminated string of at most [`MAX_STRING`] bytes.
196    fn cstr(&mut self) -> Result<String, Error> {
197        let mut bytes = Vec::new();
198        loop {
199            let b = self.u8().map_err(|e| {
200                if e.kind() == ErrorKind::UnexpectedEof {
201                    error!("a string without end: the file ends inside it")
202                } else {
203                    e
204                }
205            })?;
206            if b == 0 {
207                break;
208            }
209            if bytes.len() >= MAX_STRING {
210                return Err(error!("a string without end within {MAX_STRING} bytes"));
211            }
212            bytes.push(b);
213        }
214        Ok(String::from_utf8_lossy(&bytes).into_owned())
215    }
216
217    /// A count of elements that take at least `min_size` bytes each, checked against the bytes left and `max`.
218    fn count(&mut self, what: &str, max: u32, min_size: u64) -> Result<usize, Error> {
219        let n = self.u32()?;
220        if n > max {
221            return Err(error!("too many {what}: {n}"));
222        }
223        let bytes = u64::from(n)
224            .checked_mul(min_size)
225            .ok_or_else(|| error!("too many {what}: {n}"))?;
226        if bytes > self.left()? {
227            return Err(error!("{n} {what} don't fit into the rest of the file"));
228        }
229        usize::try_from(n).map_err(|_| error!("too many {what}: {n}"))
230    }
231
232    /// The bytes of an array of `count` elements of `size` bytes: raw below the compression threshold,
233    /// an LZO stream (without a stored length; the reader stops after its end marker) from it.
234    fn array(&mut self, count: usize, size: usize) -> Result<Vec<u8>, Error> {
235        let total = count
236            .checked_mul(size)
237            .filter(|t| *t <= MAX_ARRAY_BYTES)
238            .ok_or_else(|| error!("array of {count} elements is too large"))?;
239        self.budget = spend(self.budget, total as u64, self.limit)?;
240        if total < COMPRESSION_THRESHOLD {
241            self.need(total as u64)?;
242            let mut buf = vec![0; total];
243            self.r.read_exact(&mut buf)?;
244            return Ok(buf);
245        }
246        let out = lzokay_native::decompress_bounded(&mut *self.r, total)
247            .map_err(|e| error!("LZO stream of {total} bytes: {e}"))?;
248        if out.len() != total {
249            return Err(error!(
250                "LZO stream gave {} bytes, {total} declared",
251                out.len()
252            ));
253        }
254        Ok(out)
255    }
256
257    /// Skips `n` items of `size` bytes each.
258    fn skip_items(&mut self, n: usize, size: u64, what: &str) -> Result<(), Error> {
259        let bytes = u64::try_from(n)
260            .ok()
261            .and_then(|n| n.checked_mul(size))
262            .ok_or_else(|| error!("too many {what}: {n}"))?;
263        self.skip(bytes)
264    }
265
266    /// A counted array whose contents are not needed.
267    fn skip_array(&mut self, size: usize) -> Result<(), Error> {
268        let count = usize::try_from(self.u32()?).map_err(|_| error!("count too large"))?;
269        self.array(count, size).map(drop)
270    }
271}
272
273impl Odol {
274    /// Reads a model.
275    ///
276    /// # Errors
277    /// Fails for a file that is not ODOL, for a version other than 56, for truncated or corrupt data
278    /// and for sizes above the reader's limits. An error in a LOD names the LOD and its file offset.
279    pub fn read<R: Read + Seek>(input: &mut R) -> Result<Odol, Error> {
280        Self::read_with_limit(input, MAX_TOTAL_ARRAY_BYTES)
281    }
282
283    /// [`Odol::read`] with the total of decompressed array bytes limited to `limit` (a test makes it small).
284    fn read_with_limit<R: Read + Seek>(input: &mut R, limit: u64) -> Result<Odol, Error> {
285        let len = input.seek(SeekFrom::End(0))?;
286        input.seek(SeekFrom::Start(0))?;
287        // the header part is read through a buffer; each LOD is read into memory in one piece
288        let mut buffered = Buffered(BufReader::new(input));
289        let mut rd = Rd {
290            r: &mut buffered,
291            len,
292            budget: limit,
293            limit,
294        };
295
296        let mut magic = [0; 4];
297        rd.r.read_exact(&mut magic)?;
298        if &magic != b"ODOL" {
299            return Err(error!("not an ODOL model"));
300        }
301        let version = rd.u32()?;
302        if version != SUPPORTED_VERSION {
303            return Err(error!(
304                "ODOL version {version} is not supported, only {SUPPORTED_VERSION}"
305            ));
306        }
307        let lod_count = rd.count("LODs", MAX_ITEMS, 12)?;
308        let mut resolutions = Vec::with_capacity(lod_count);
309        for _ in 0..lod_count {
310            resolutions.push(rd.f32()?);
311        }
312
313        let mut model = Odol {
314            version,
315            property_class: String::new(),
316            property_damage: String::new(),
317            skeleton_name: String::new(),
318            bone_count: 0,
319            mass: 0.0,
320            bbox_min: [0.0; 3],
321            bbox_max: [0.0; 3],
322            animations: Vec::new(),
323            lods: Vec::new(),
324        };
325        model_info(&mut rd, &mut model).prepend_error("ODOL model info:")?;
326        if rd.u8()? != 0 {
327            animations(&mut rd, &mut model, lod_count).prepend_error("ODOL animations:")?;
328        }
329
330        let mut starts = Vec::with_capacity(lod_count);
331        for _ in 0..lod_count {
332            starts.push(rd.u32()?);
333        }
334        let mut ends = Vec::with_capacity(lod_count);
335        for _ in 0..lod_count {
336            ends.push(rd.u32()?);
337        }
338
339        // The LOD data is read in one piece each, without read-ahead: back to the plain input.
340        let budget = rd.budget;
341        let input = buffered.0.into_inner();
342        model.lods = read_lods(
343            input,
344            &LodTable {
345                starts: &starts,
346                ends: &ends,
347                resolutions: &resolutions,
348            },
349            len,
350            (budget, limit),
351        )?;
352        Ok(model)
353    }
354}
355
356/// The offsets, end offsets and resolutions of the LODs, as the header lists them.
357struct LodTable<'a> {
358    starts: &'a [u32],
359    ends: &'a [u32],
360    resolutions: &'a [f32],
361}
362
363/// Reads the LODs of a file of `len` bytes, each from its offset, and returns them in header order.
364fn read_lods<R: Read + Seek>(
365    input: &mut R,
366    table: &LodTable<'_>,
367    len: u64,
368    (mut budget, limit): (u64, u64),
369) -> Result<Vec<OdolLod>, Error> {
370    let (starts, ends, resolutions) = (table.starts, table.ends, table.resolutions);
371    let lod_count = starts.len();
372    // Parse the LODs in file order and let none reach into the data of the one before it: LODs that
373    // share or overlap data would make a small file cost as much work as a big one.
374    let mut order: Vec<usize> = (0..lod_count).collect();
375    order.sort_by_key(|i| starts.get(*i).copied().unwrap_or(0));
376    let mut parsed = Vec::with_capacity(lod_count);
377    let mut previous_end = 0;
378    let mut total_read = 0u64;
379    for (rank, &index) in order.iter().enumerate() {
380        let start = u64::from(starts.get(index).copied().unwrap_or(0));
381        let end = u64::from(ends.get(index).copied().unwrap_or(0));
382        let context = format!("ODOL LOD {index} at offset {start}:");
383        if start >= len {
384            return Err(error!("{context}\nstarts past the end of the file"));
385        }
386        if start < previous_end {
387            return Err(error!(
388                "{context}\nstarts inside the data of the LOD before it"
389            ));
390        }
391        // The bytes of this LOD go into memory: the parser seeks a lot, and counts are checked against
392        // the bytes of this LOD, not those of the whole file.
393        // A LOD never reads past the start of the next one (the file end for the last), and the bytes
394        // read over all LODs never exceed the file: the cost stays linear whatever the declared ends.
395        let next_start = order
396            .iter()
397            .skip(rank + 1)
398            .map(|next| u64::from(starts.get(*next).copied().unwrap_or(0)))
399            .find(|next| *next > start)
400            .unwrap_or(len);
401        let stop = end.min(len).min(next_start);
402        let size = stop.saturating_sub(start);
403        total_read = total_read.saturating_add(size);
404        if total_read > len {
405            return Err(error!(
406                "{context}\nthe LODs together hold more bytes than the file"
407            ));
408        }
409        if size > MAX_LOD_BYTES {
410            return Err(error!(
411                "{context}\nthe LOD is {size} bytes, more than the {MAX_LOD_BYTES} accepted"
412            ));
413        }
414        input.seek(SeekFrom::Start(start))?;
415        let mut bytes = Vec::new();
416        (&mut *input).take(size).read_to_end(&mut bytes)?;
417        let mut cursor = Cursor::new(bytes);
418        let mut lod_rd = Rd {
419            r: &mut cursor,
420            len: size,
421            budget,
422            limit,
423        };
424        let resolution = resolutions.get(index).copied().unwrap_or(0.0);
425        let lod = read_lod(&mut lod_rd, resolution).prepend_error(&context)?;
426        budget = lod_rd.budget;
427        previous_end = start
428            .checked_add(lod_rd.pos()?)
429            .ok_or_else(|| error!("{context}\nthe LOD ends beyond the file"))?;
430        parsed.push((index, lod));
431    }
432    parsed.sort_by_key(|(index, _)| *index);
433    Ok(parsed.into_iter().map(|(_, lod)| lod).collect())
434}
435
436fn model_info<R: Read + Seek>(rd: &mut Rd<'_, R>, model: &mut Odol) -> Result<(), Error> {
437    // index, two sphere radii, remarks, hints, aiming center, colors, view density
438    rd.skip(48)?;
439    model.bbox_min = rd.vec3()?;
440    model.bbox_max = rd.vec3()?;
441    // visual box, three centers, inverse inertia, 5 flags, 8 floats, then 3 flags with a shadow mode and offset
442    rd.skip(24 + 36 + 36 + 5 + 32 + 1 + 4 + 1 + 4 + 1)?;
443
444    model.skeleton_name = rd.cstr()?;
445    if !model.skeleton_name.is_empty() {
446        rd.u8()?; // is_discrete
447        let bones = rd.count("bones", MAX_ITEMS, 2)?;
448        for _ in 0..bones {
449            rd.cstr()?;
450            rd.cstr()?;
451        }
452        model.bone_count = u32::try_from(bones).unwrap_or(u32::MAX);
453        rd.cstr()?; // pivots name
454    }
455    rd.u8()?; // map type
456    rd.skip_array(4)?; // mass array
457    model.mass = rd.f32()?;
458    // inverse mass, alt masses, the special LOD indices, min shadow, can blend
459    rd.skip(4 + 4 + 4 + 13 + 4 + 1)?;
460    model.property_class = rd.cstr()?;
461    model.property_damage = rd.cstr()?;
462    rd.u8()?; // property_frequent
463    let extra = rd.count("model strings", MAX_ITEMS, 1)?;
464    for _ in 0..extra {
465        rd.cstr()?;
466    }
467    rd.skip(1)?; // unknown flag
468    Ok(())
469}
470
471fn animations<R: Read + Seek>(
472    rd: &mut Rd<'_, R>,
473    model: &mut Odol,
474    lod_count: usize,
475) -> Result<(), Error> {
476    let classes = rd.count("animation classes", MAX_ITEMS, 40)?;
477    let mut kinds = Vec::with_capacity(classes);
478    for _ in 0..classes {
479        let kind = rd.u32()?;
480        let name = rd.cstr()?;
481        let source = rd.cstr()?;
482        rd.skip(20)?; // min and max phase, min and max value, source address
483        match kind {
484            0..=7 => rd.skip(8)?,
485            8 => rd.skip(32)?,
486            9 => rd.skip(4)?,
487            _ => return Err(error!("unknown animation type {kind}")),
488        }
489        kinds.push(kind);
490        model.animations.push(OdolAnimation { kind, name, source });
491    }
492    // Without animation classes the block count is 0 and no blocks follow.
493    let resolutions = rd.u32()?;
494    let blocks = usize::try_from(resolutions)
495        .map_err(|_| error!("too many animation blocks: {resolutions}"))?;
496    if blocks != lod_count && !(classes == 0 && blocks == 0) {
497        return Err(error!(
498            "{resolutions} animation LOD blocks for {lod_count} LODs"
499        ));
500    }
501    for _ in 0..blocks {
502        let bones = rd.count("animated bones", MAX_ITEMS, 4)?;
503        for _ in 0..bones {
504            let n = rd.count("bone animations", MAX_ITEMS, 4)?;
505            rd.skip_items(n, 4, "bone animation indices")?;
506        }
507    }
508    for _ in 0..blocks {
509        for kind in &kinds {
510            let bone = rd.u32()?;
511            if bone != u32::MAX && *kind != 8 && *kind != 9 {
512                rd.skip(24)?;
513            }
514        }
515    }
516    Ok(())
517}
518
519fn read_lod<R: Read + Seek>(rd: &mut Rd<'_, R>, resolution: f32) -> Result<OdolLod, Error> {
520    let proxy_count = rd.count("proxies", MAX_ITEMS, 1 + 48 + 16)?;
521    let mut proxies = Vec::with_capacity(proxy_count);
522    for _ in 0..proxy_count {
523        proxies.push(rd.cstr()?);
524        rd.skip(48 + 16)?;
525    }
526    let items = rd.count("items", u32::MAX, 4)?;
527    rd.skip_items(items, 4, "items")?;
528    let links = rd.count("bone links", MAX_ITEMS * 16, 4)?;
529    for _ in 0..links {
530        let n = rd.count("bone link entries", u32::MAX, 4)?;
531        rd.skip_items(n, 4, "bone link entries")?;
532    }
533    let vertex_count = rd.u32()?;
534    // face area, hints, bounding box, center, radius
535    rd.skip(4 + 8 + 36 + 4)?;
536    let texture_count = rd.count("textures", MAX_ITEMS, 1)?;
537    let mut textures = Vec::with_capacity(texture_count);
538    for _ in 0..texture_count {
539        textures.push(rd.cstr()?);
540    }
541    let material_count = rd.count("materials", MAX_ITEMS, 8)?;
542    let mut materials = Vec::with_capacity(material_count);
543    for _ in 0..material_count {
544        materials.push(read_material(rd)?);
545    }
546    rd.skip_array(2)?; // edges
547    rd.skip_array(2)?;
548    let face_count = rd.u32()?;
549    let face_bytes = rd.u32()?;
550    rd.skip(2)?;
551    if u64::from(face_count) > rd.left()? / 7 {
552        return Err(error!(
553            "{face_count} faces don't fit into the rest of the file"
554        ));
555    }
556    // Each face is a u8 vertex count (3 or 4) and that many u16 indices. The declared size counts every
557    // face as the vertex count plus one u16 each.
558    let mut declared = 0u64;
559    for _ in 0..face_count {
560        let n = rd.u8()?;
561        if n != 3 && n != 4 {
562            return Err(error!("a face with {n} vertices"));
563        }
564        rd.skip(u64::from(n) * 2)?;
565        declared += u64::from(n) * 2 + 2;
566    }
567    if declared != u64::from(face_bytes) {
568        return Err(error!(
569            "the faces take {declared} bytes, {face_bytes} declared"
570        ));
571    }
572
573    let sections = rd.count("sections", MAX_ITEMS, 34)?;
574    for _ in 0..sections {
575        rd.skip(26)?;
576        let material_index = rd.u32()?;
577        if material_index == u32::MAX {
578            rd.cstr()?;
579        }
580        let stages = rd.count("section stages", MAX_ITEMS, 4)?;
581        rd.skip_items(stages, 4, "section stages")?;
582    }
583    let selection_count = rd.count("selections", MAX_ITEMS, 22)?;
584    let mut selections = Vec::with_capacity(selection_count);
585    for _ in 0..selection_count {
586        selections.push(rd.cstr()?);
587        rd.skip_array(2)?; // faces
588        rd.skip(4 + 1)?; // zero, is_sectional
589        rd.skip_array(4)?; // sections
590        rd.skip_array(2)?; // vertices
591        rd.skip_array(1)?; // weights
592    }
593    let property_count = rd.count("properties", MAX_ITEMS, 2)?;
594    let mut properties = Vec::with_capacity(property_count);
595    for _ in 0..property_count {
596        let name = rd.cstr()?;
597        let value = rd.cstr()?;
598        properties.push((name, value));
599    }
600    if rd.pos()? > rd.len {
601        return Err(error!("the LOD reads past its end offset"));
602    }
603    Ok(OdolLod {
604        resolution,
605        vertex_count,
606        face_count,
607        textures,
608        materials,
609        proxies,
610        selections,
611        properties,
612    })
613}
614
615/// The only material version the game's models use.
616const MATERIAL_VERSION: u32 = 20;
617
618/// A stage texture: render flags, path, stage id, "use world environment" flag.
619fn read_stage<R: Read + Seek>(rd: &mut Rd<'_, R>) -> Result<String, Error> {
620    rd.skip(4)?;
621    let texture = rd.cstr()?;
622    rd.skip(4 + 1)?;
623    Ok(texture)
624}
625
626fn read_material<R: Read + Seek>(rd: &mut Rd<'_, R>) -> Result<OdolMaterial, Error> {
627    let name = rd.cstr()?;
628    let version = rd.u32()?;
629    if version != MATERIAL_VERSION {
630        return Err(error!(
631            "material {name:?} has version {version}, only {MATERIAL_VERSION} is supported"
632        ));
633    }
634    // colors and lighting values, specular power, pixel and vertex shader, main light, fog mode
635    rd.skip(200 + 4 + 16)?;
636    rd.cstr()?; // surface
637    rd.skip(8)?; // render flag count and flags
638    let stages = rd.count("material stages", MAX_ITEMS, 10)?;
639    let transforms = rd.count("material transforms", MAX_ITEMS, 52)?;
640    let mut textures = Vec::new();
641    for _ in 0..stages {
642        let texture = read_stage(rd)?;
643        if !texture.is_empty() {
644            textures.push(texture);
645        }
646    }
647    rd.skip_items(transforms, 52, "material transforms")?;
648    read_stage(rd)?; // the dummy stage after the transforms
649    Ok(OdolMaterial { name, textures })
650}
651
652/// The synthetic model of the integration tests, for a test of the private limit.
653#[cfg(test)]
654#[path = "../tests/support/odol_builder.rs"]
655mod odol_builder;
656
657#[cfg(test)]
658mod tests {
659    use super::{Odol, odol_builder, spend};
660    use std::io::Cursor;
661
662    fn spend_all(limit: u64, arrays: &[u64]) -> Result<u64, std::io::Error> {
663        let mut budget = limit;
664        for a in arrays {
665            budget = spend(budget, *a, limit)?;
666        }
667        Ok(budget)
668    }
669
670    #[test]
671    fn arrays_up_to_the_limit_pass() {
672        assert_eq!(spend_all(100, &[40, 60]).unwrap(), 0);
673        assert_eq!(spend_all(100, &[30, 30]).unwrap(), 40);
674    }
675
676    #[test]
677    fn arrays_together_past_the_limit_fail() {
678        let err = spend_all(100, &[40, 40, 21]).unwrap_err();
679        assert!(err.to_string().contains("more than 100 bytes"), "{err}");
680        assert!(spend_all(100, &[101]).is_err());
681    }
682
683    /// The array budget is one for the whole file: the arrays of the LODs add up, so two LODs whose arrays
684    /// together, but not each alone, pass the limit are refused.
685    #[test]
686    fn the_array_budget_carries_over_across_lods() {
687        // The arrays of the synthetic model cost 15 bytes in LOD 0 and 2400 bytes in LOD 1.
688        let bytes = odol_builder::build(odol_builder::Knobs::default());
689        let reads = |limit: u64| Odol::read_with_limit(&mut Cursor::new(&bytes), limit);
690        assert!(reads(2415).is_ok());
691        // 2414 holds each LOD alone, but not LOD 1 after LOD 0
692        let err = reads(2414).unwrap_err().to_string();
693        assert!(err.contains("LOD 1"), "{err}");
694        assert!(err.contains("more than 2414 bytes"), "{err}");
695    }
696}