1use std::io::{BufReader, Cursor, Error, ErrorKind, Read, Seek, SeekFrom};
14
15use byteorder::{LittleEndian, ReadBytesExt};
16
17use crate::error::ErrorExt;
18
19const SUPPORTED_VERSION: u32 = 56;
21const MAX_STRING: usize = 64 * 1024;
23const MAX_ITEMS: u32 = 1 << 16;
25const MAX_ARRAY_BYTES: usize = 64 * 1024 * 1024;
27const COMPRESSION_THRESHOLD: usize = 1024;
29const MAX_TOTAL_ARRAY_BYTES: u64 = 256 * 1024 * 1024;
32const MAX_LOD_BYTES: u64 = 256 * 1024 * 1024;
34
35#[derive(Debug, Clone, PartialEq)]
37#[non_exhaustive]
38pub struct Odol {
39 pub version: u32,
41 pub property_class: String,
43 pub property_damage: String,
45 pub skeleton_name: String,
47 pub bone_count: u32,
49 pub mass: f32,
51 pub bbox_min: [f32; 3],
53 pub bbox_max: [f32; 3],
55 pub animations: Vec<OdolAnimation>,
57 pub lods: Vec<OdolLod>,
59}
60
61#[derive(Debug, Clone, PartialEq, Eq)]
63#[non_exhaustive]
64pub struct OdolAnimation {
65 pub kind: u32,
68 pub name: String,
70 pub source: String,
72}
73
74#[derive(Debug, Clone, PartialEq)]
76#[non_exhaustive]
77pub struct OdolLod {
78 pub resolution: f32,
80 pub vertex_count: u32,
82 pub face_count: u32,
84 pub textures: Vec<String>,
86 pub materials: Vec<OdolMaterial>,
88 pub proxies: Vec<String>,
90 pub selections: Vec<String>,
92 pub properties: Vec<(String, String)>,
94}
95
96#[derive(Debug, Clone, PartialEq, Eq)]
98#[non_exhaustive]
99pub struct OdolMaterial {
100 pub name: String,
102 pub textures: Vec<String>,
104}
105
106impl OdolLod {
107 #[must_use]
109 pub fn name(&self) -> String {
110 crate::p3d::resolution_name(self.resolution)
111 }
112}
113
114struct 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
138fn 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
145struct Rd<'a, R: Read + Seek> {
147 r: &'a mut R,
148 len: u64,
149 budget: u64,
151 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 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 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 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 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 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 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 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 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 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
356struct LodTable<'a> {
358 starts: &'a [u32],
359 ends: &'a [u32],
360 resolutions: &'a [f32],
361}
362
363fn 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 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 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 rd.skip(48)?;
439 model.bbox_min = rd.vec3()?;
440 model.bbox_max = rd.vec3()?;
441 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()?; 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()?; }
455 rd.u8()?; rd.skip_array(4)?; model.mass = rd.f32()?;
458 rd.skip(4 + 4 + 4 + 13 + 4 + 1)?;
460 model.property_class = rd.cstr()?;
461 model.property_damage = rd.cstr()?;
462 rd.u8()?; let extra = rd.count("model strings", MAX_ITEMS, 1)?;
464 for _ in 0..extra {
465 rd.cstr()?;
466 }
467 rd.skip(1)?; 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)?; 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 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 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)?; 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 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)?; rd.skip(4 + 1)?; rd.skip_array(4)?; rd.skip_array(2)?; rd.skip_array(1)?; }
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
615const MATERIAL_VERSION: u32 = 20;
617
618fn 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 rd.skip(200 + 4 + 16)?;
636 rd.cstr()?; rd.skip(8)?; 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)?; Ok(OdolMaterial { name, textures })
650}
651
652#[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 #[test]
686 fn the_array_budget_carries_over_across_lods() {
687 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 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}