1use std::collections::{BTreeMap, HashSet};
8use std::fs::File;
9use std::io::{BufReader, BufWriter, Error, ErrorKind, Read, Seek, SeekFrom, Write};
10use std::path::{Path, PathBuf};
11
12use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
13
14use crate::error::{ErrorExt, printable};
15use crate::io::{ReadExt, WriteExt};
16use crate::warning::{Warning, WarningSink};
17
18fn invalid<S: Into<String>>(msg: S) -> Error {
19 Error::new(ErrorKind::InvalidData, msg.into())
20}
21
22fn capacity(n: u32) -> usize {
24 (n as usize).min(1 << 16)
25}
26
27#[derive(Debug, Default)]
29#[non_exhaustive]
30pub struct Point {
31 pub coords: (f32, f32, f32),
33 pub flags: u32,
35}
36
37#[derive(Debug, Default)]
39#[non_exhaustive]
40pub struct Vertex {
41 pub point_index: u32,
43 pub normal_index: u32,
45 pub uv: (f32, f32),
47}
48
49#[derive(Debug, Default)]
51#[non_exhaustive]
52pub struct Face {
53 pub vertices: Vec<Vertex>,
55 pub flags: u32,
57 pub texture: String,
59 pub material: String,
61 pub spare_vertex: Option<Vertex>,
63}
64
65#[derive(Debug, Clone, PartialEq)]
68#[non_exhaustive]
69pub struct Tagg {
70 pub active: u8,
72 pub name: String,
74 pub data: Vec<u8>,
76}
77
78#[derive(Debug)]
80#[non_exhaustive]
81pub struct LOD {
82 pub version_major: u32,
84 pub version_minor: u32,
86 pub flags: u32,
88 pub resolution: f32,
90 pub points: Vec<Point>,
92 pub face_normals: Vec<(f32, f32, f32)>,
94 pub faces: Vec<Face>,
96 pub taggs: Vec<Tagg>,
98}
99
100#[derive(Debug)]
106#[non_exhaustive]
107pub struct P3D {
108 pub version: u32,
110 pub lods: Vec<LOD>,
112}
113
114impl Point {
115 #[must_use]
117 pub fn new() -> Point {
118 Point {
119 coords: (0.0, 0.0, 0.0),
120 flags: 0,
121 }
122 }
123
124 fn read<I: Read>(input: &mut I) -> Result<Point, Error> {
125 Ok(Point {
126 coords: (
127 input.read_f32::<LittleEndian>()?,
128 input.read_f32::<LittleEndian>()?,
129 input.read_f32::<LittleEndian>()?,
130 ),
131 flags: input.read_u32::<LittleEndian>()?,
132 })
133 }
134
135 fn write<O: Write>(&self, output: &mut O) -> Result<(), Error> {
136 output.write_f32::<LittleEndian>(self.coords.0)?;
137 output.write_f32::<LittleEndian>(self.coords.1)?;
138 output.write_f32::<LittleEndian>(self.coords.2)?;
139 output.write_u32::<LittleEndian>(self.flags)?;
140 Ok(())
141 }
142}
143
144impl Vertex {
145 #[must_use]
147 pub fn new() -> Vertex {
148 Vertex {
149 point_index: 0,
150 normal_index: 0,
151 uv: (0.0, 0.0),
152 }
153 }
154
155 fn read<I: Read>(input: &mut I) -> Result<Vertex, Error> {
156 Ok(Vertex {
157 point_index: input.read_u32::<LittleEndian>()?,
158 normal_index: input.read_u32::<LittleEndian>()?,
159 uv: (
160 input.read_f32::<LittleEndian>()?,
161 input.read_f32::<LittleEndian>()?,
162 ),
163 })
164 }
165
166 fn write<O: Write>(&self, output: &mut O) -> Result<(), Error> {
167 output.write_u32::<LittleEndian>(self.point_index)?;
168 output.write_u32::<LittleEndian>(self.normal_index)?;
169 output.write_f32::<LittleEndian>(self.uv.0)?;
170 output.write_f32::<LittleEndian>(self.uv.1)?;
171 Ok(())
172 }
173}
174
175impl Face {
176 #[must_use]
178 pub fn new() -> Face {
179 Face {
180 vertices: Vec::with_capacity(4),
181 flags: 0,
182 texture: String::new(),
183 material: String::new(),
184 spare_vertex: None,
185 }
186 }
187
188 fn read<I: Read>(input: &mut I) -> Result<Face, Error> {
189 let num_verts = input.read_u32::<LittleEndian>()?;
190 if num_verts != 3 && num_verts != 4 {
191 return Err(invalid(format!(
192 "face with {num_verts} vertices (expected 3 or 4)"
193 )));
194 }
195
196 let mut vertices: Vec<Vertex> = Vec::with_capacity(num_verts as usize);
197 for _i in 0..num_verts {
198 vertices.push(Vertex::read(input)?);
199 }
200
201 let spare_vertex = if num_verts == 3 {
202 Some(Vertex::read(input)?)
203 } else {
204 None
205 };
206
207 let flags = input.read_u32::<LittleEndian>()?;
208 let texture = input.read_cstring()?;
209 let material = input.read_cstring()?;
210
211 Ok(Face {
212 vertices,
213 flags,
214 texture,
215 material,
216 spare_vertex,
217 })
218 }
219
220 fn write<O: Write>(&self, output: &mut O) -> Result<(), Error> {
221 if self.vertices.len() != 3 && self.vertices.len() != 4 {
222 return Err(invalid(format!(
223 "face with {} vertices (expected 3 or 4)",
224 self.vertices.len()
225 )));
226 }
227 let num_verts =
228 u32::try_from(self.vertices.len()).map_err(|_| invalid("too many vertices in face"))?;
229 output.write_u32::<LittleEndian>(num_verts)?;
230
231 for vert in &self.vertices {
232 vert.write(output)?;
233 }
234 if self.vertices.len() == 3 {
235 match self.spare_vertex {
236 Some(ref vert) => vert.write(output)?,
237 None => Vertex::new().write(output)?,
238 }
239 }
240
241 output.write_u32::<LittleEndian>(self.flags)?;
242 output.write_cstring(&self.texture)?;
243 output.write_cstring(&self.material)?;
244 Ok(())
245 }
246}
247
248impl Tagg {
249 #[must_use]
255 pub fn new(active: u8, name: impl Into<String>, data: Vec<u8>) -> Tagg {
256 Tagg {
257 active,
258 name: name.into(),
259 data,
260 }
261 }
262
263 #[must_use]
265 pub fn property(&self) -> Option<(String, String)> {
266 if self.name != "#Property#" || self.data.len() != 128 {
267 return None;
268 }
269 let field = |b: &[u8]| {
270 let end = b.iter().position(|&c| c == 0).unwrap_or(b.len());
271 String::from_utf8_lossy(b.get(..end).unwrap_or(b)).into_owned()
272 };
273 let (Some(low), Some(high)) = (self.data.get(..64), self.data.get(64..)) else {
274 return None;
275 };
276 Some((field(low), field(high)))
277 }
278
279 #[must_use]
281 pub fn is_selection(&self) -> bool {
282 !self.name.starts_with('#')
283 }
284}
285
286#[must_use]
288pub fn lod_name(resolution: f32) -> Option<&'static str> {
289 const NAMES: &[(f32, &str)] = &[
290 (1.0e3, "View - Gunner"),
291 (1.1e3, "View - Pilot"),
292 (1.2e3, "View - Cargo"),
293 (1.0e4, "Shadow Volume"),
294 (1.0e13, "Geometry"),
295 (1.0e15, "Memory"),
296 (2.0e15, "Land Contact"),
297 (3.0e15, "Roadway"),
298 (4.0e15, "Paths"),
299 (5.0e15, "Hit-points"),
300 (6.0e15, "View Geometry"),
301 (7.0e15, "Fire Geometry"),
302 ];
303 NAMES
304 .iter()
305 .find(|(r, _)| (resolution - r).abs() <= r * 1e-4)
306 .map(|(_, n)| *n)
307}
308
309pub(crate) fn resolution_name(resolution: f32) -> String {
311 match lod_name(resolution) {
312 Some(n) => n.to_string(),
313 None if resolution < 1.0e3 => format!("Resolution {resolution}"),
314 None => format!("{resolution:e}"),
315 }
316}
317
318impl LOD {
319 #[must_use]
321 pub fn new(resolution: f32) -> LOD {
322 LOD {
323 version_major: 0x1c,
324 version_minor: 0x100,
325 flags: 0,
326 resolution,
327 points: Vec::new(),
328 face_normals: Vec::new(),
329 faces: Vec::new(),
330 taggs: Vec::new(),
331 }
332 }
333
334 #[must_use]
336 pub fn name(&self) -> String {
337 resolution_name(self.resolution)
338 }
339
340 #[must_use]
342 pub fn properties(&self) -> Vec<(String, String)> {
343 self.taggs.iter().filter_map(Tagg::property).collect()
344 }
345
346 #[must_use]
348 pub fn selections(&self) -> Vec<&str> {
349 self.taggs
350 .iter()
351 .filter(|t| t.is_selection())
352 .map(|t| t.name.as_str())
353 .collect()
354 }
355
356 #[must_use]
358 pub fn uv_set_count(&self) -> usize {
359 self.taggs.iter().filter(|t| t.name == "#UVSet#").count()
360 }
361
362 fn read<I: Read>(input: &mut I) -> Result<LOD, Error> {
363 let mut buffer = [0; 4];
364 input.read_exact(&mut buffer)?;
365 if &buffer != b"P3DM" {
366 return Err(invalid("LOD doesn't start with P3DM"));
367 }
368
369 let version_major = input.read_u32::<LittleEndian>()?;
370 let version_minor = input.read_u32::<LittleEndian>()?;
371
372 let num_points = input.read_u32::<LittleEndian>()?;
373 let num_face_normals = input.read_u32::<LittleEndian>()?;
374 let num_faces = input.read_u32::<LittleEndian>()?;
375 let flags = input.read_u32::<LittleEndian>()?;
376
377 let mut points: Vec<Point> = Vec::with_capacity(capacity(num_points));
378 let mut face_normals: Vec<(f32, f32, f32)> = Vec::with_capacity(capacity(num_face_normals));
379 let mut faces: Vec<Face> = Vec::with_capacity(capacity(num_faces));
380
381 for _i in 0..num_points {
382 points.push(Point::read(input)?);
383 }
384
385 for _i in 0..num_face_normals {
386 face_normals.push((
387 input.read_f32::<LittleEndian>()?,
388 input.read_f32::<LittleEndian>()?,
389 input.read_f32::<LittleEndian>()?,
390 ));
391 }
392
393 for _i in 0..num_faces {
394 faces.push(Face::read(input)?);
395 }
396
397 input.read_exact(&mut buffer)?;
398 if &buffer != b"TAGG" {
399 return Err(invalid("LOD has no TAGG section"));
400 }
401
402 let mut taggs: Vec<Tagg> = Vec::new();
403
404 loop {
405 let active = input.read_u8()?;
406 let name = input.read_cstring()?;
407 let size = input.read_u32::<LittleEndian>()?;
408 let mut data = Vec::with_capacity(capacity(size));
409 let got = input
410 .by_ref()
411 .take(u64::from(size))
412 .read_to_end(&mut data)?;
413 if got != size as usize {
414 return Err(Error::new(
415 ErrorKind::UnexpectedEof,
416 format!("tag \"{}\" is truncated", printable(&name)),
417 ));
418 }
419
420 if name == "#EndOfFile#" {
421 break;
422 }
423
424 taggs.push(Tagg { active, name, data });
425 }
426
427 let resolution = input.read_f32::<LittleEndian>()?;
428
429 Ok(LOD {
430 version_major,
431 version_minor,
432 flags,
433 resolution,
434 points,
435 face_normals,
436 faces,
437 taggs,
438 })
439 }
440
441 fn validate(&self, index: usize) -> Result<(), Error> {
443 let label = format!("LOD {index} ({})", self.name());
445 let expected = self
446 .points
447 .len()
448 .checked_add(self.faces.len())
449 .ok_or_else(|| invalid("LOD section count out of range"))?;
450 for (i, tagg) in self.taggs.iter().enumerate() {
451 if tagg.name.contains('\0') {
452 return Err(invalid(format!(
453 "{label}: tagg {i} name \"{}\" contains a NUL byte",
454 printable(&tagg.name),
455 )));
456 }
457 if tagg.active != 0 && tagg.is_selection() && tagg.data.len() != expected {
459 return Err(invalid(format!(
460 "{label}: tagg {i} \"{}\" is a selection with {} bytes, expected {expected} ({} points + {} faces)",
461 printable(&tagg.name),
462 tagg.data.len(),
463 self.points.len(),
464 self.faces.len(),
465 )));
466 }
467 }
468 for (i, face) in self.faces.iter().enumerate() {
469 for (field, value) in [("texture", &face.texture), ("material", &face.material)] {
470 if value.contains('\0') {
471 return Err(invalid(format!(
472 "{label}: face {i} {field} \"{}\" contains a NUL byte",
473 printable(value),
474 )));
475 }
476 }
477 if face.vertices.len() != 3 && face.vertices.len() != 4 {
478 return Err(invalid(format!(
479 "{label}: face {i} has {} vertices (expected 3 or 4)",
480 face.vertices.len(),
481 )));
482 }
483 for (j, vertex) in face.vertices.iter().enumerate() {
484 let point = usize::try_from(vertex.point_index).unwrap_or(usize::MAX);
485 if point >= self.points.len() {
486 return Err(invalid(format!(
487 "{label}: face {i} vertex {j} has point index {} but there are {} points",
488 vertex.point_index,
489 self.points.len(),
490 )));
491 }
492 let normal = usize::try_from(vertex.normal_index).unwrap_or(usize::MAX);
493 if normal >= self.face_normals.len() {
494 return Err(invalid(format!(
495 "{label}: face {i} vertex {j} has normal index {} but there are {} normals",
496 vertex.normal_index,
497 self.face_normals.len(),
498 )));
499 }
500 }
501 }
502 Ok(())
503 }
504
505 fn write<O: Write>(&self, output: &mut O) -> Result<(), Error> {
506 let len32 =
507 |len: usize| u32::try_from(len).map_err(|_| invalid("LOD section count out of range"));
508 output.write_all(b"P3DM")?;
509 output.write_u32::<LittleEndian>(self.version_major)?;
510 output.write_u32::<LittleEndian>(self.version_minor)?;
511 output.write_u32::<LittleEndian>(len32(self.points.len())?)?;
512 output.write_u32::<LittleEndian>(len32(self.face_normals.len())?)?;
513 output.write_u32::<LittleEndian>(len32(self.faces.len())?)?;
514 output.write_u32::<LittleEndian>(self.flags)?;
515
516 for point in &self.points {
517 point.write(output)?;
518 }
519
520 for normal in &self.face_normals {
521 output.write_f32::<LittleEndian>(normal.0)?;
522 output.write_f32::<LittleEndian>(normal.1)?;
523 output.write_f32::<LittleEndian>(normal.2)?;
524 }
525
526 for face in &self.faces {
527 face.write(output)?;
528 }
529
530 output.write_all(b"TAGG")?;
531
532 for tagg in &self.taggs {
533 output.write_all(&[tagg.active])?;
534 output.write_cstring(&tagg.name)?;
535 let size = u32::try_from(tagg.data.len()).map_err(|_| invalid("tag data too large"))?;
536 output.write_u32::<LittleEndian>(size)?;
537 output.write_all(&tagg.data)?;
538 }
539
540 output.write_all(&[1])?;
541 output.write_cstring("#EndOfFile#")?;
542 output.write_u32::<LittleEndian>(0)?;
543
544 output.write_f32::<LittleEndian>(self.resolution)?;
545
546 Ok(())
547 }
548}
549
550impl P3D {
551 #[must_use]
553 pub fn new(version: u32) -> P3D {
554 P3D {
555 version,
556 lods: Vec::new(),
557 }
558 }
559
560 pub fn read<I: Read>(input: &mut I) -> Result<P3D, Error> {
566 let mut reader = BufReader::new(input);
567
568 let mut buffer = [0; 4];
569 reader.read_exact(&mut buffer)?;
570 match &buffer {
571 b"MLOD" => {}
572 b"ODOL" => {
573 return Err(invalid(
574 "this is a binarized ODOL model; only MLOD models can be read",
575 ));
576 }
577 _ => return Err(invalid("not an MLOD model")),
578 }
579
580 let version = reader.read_u32::<LittleEndian>()?;
581 let num_lods = reader.read_u32::<LittleEndian>()?;
582 let mut lods: Vec<LOD> = Vec::with_capacity(capacity(num_lods));
583
584 for i in 0..num_lods {
585 lods.push(LOD::read(&mut reader).prepend_error(format!("Failed to read LOD {i}:"))?);
586 }
587
588 Ok(P3D { version, lods })
589 }
590
591 pub fn write<O: Write>(&self, output: &mut O) -> Result<(), Error> {
599 for (index, lod) in self.lods.iter().enumerate() {
600 lod.validate(index)?;
601 }
602 let mut writer = BufWriter::new(output);
603
604 writer.write_all(b"MLOD")?;
605 writer.write_u32::<LittleEndian>(self.version)?;
606 let num_lods = u32::try_from(self.lods.len()).map_err(|_| invalid("too many LODs"))?;
607 writer.write_u32::<LittleEndian>(num_lods)?;
608
609 for lod in &self.lods {
610 lod.write(&mut writer)?;
611 }
612
613 writer.flush()
614 }
615
616 #[must_use]
619 pub fn textures(&self) -> BTreeMap<&str, usize> {
620 self.count_faces(|f| &f.texture)
621 }
622
623 #[must_use]
626 pub fn materials(&self) -> BTreeMap<&str, usize> {
627 self.count_faces(|f| &f.material)
628 }
629
630 fn count_faces<'a>(
631 &'a self,
632 field: impl Fn(&'a Face) -> &'a String,
633 ) -> BTreeMap<&'a str, usize> {
634 let mut map = BTreeMap::new();
635 for face in self.lods.iter().flat_map(|l| l.faces.iter()) {
636 let name = field(face);
637 if !name.is_empty() {
638 *map.entry(name.as_str()).or_insert(0) += 1;
639 }
640 }
641 map
642 }
643}
644
645fn exists_ignore_case(root: &Path, rel: &str) -> bool {
647 let mut dir = root.to_path_buf();
648 for part in rel.split('\\').filter(|p| !p.is_empty()) {
649 let found = std::fs::read_dir(&dir).ok().and_then(|rd| {
650 rd.filter_map(std::result::Result::ok)
651 .find(|e| e.file_name().to_string_lossy().to_lowercase() == part)
652 .map(|e| e.path())
653 });
654 match found {
655 Some(p) => dir = p,
656 None => return false,
657 }
658 }
659 dir != root
660}
661
662pub(crate) fn check_dependencies<S: std::hash::BuildHasher>(
671 path: &Path,
672 display: &str,
673 prefix: &str,
674 addon_files: &HashSet<String, S>,
675 search_roots: &[PathBuf],
676 warnings: &mut dyn WarningSink,
677) {
678 let p3d = match File::open(path).and_then(|f| P3D::read(&mut BufReader::new(f))) {
679 Ok(p) => p,
680 Err(e) => {
681 warnings.warn(
682 Warning::new(
683 Some("model-unreadable"),
684 format!(
685 "Cannot check textures and materials: {}",
686 printable(&e.to_string().replace('\n', " "))
687 ),
688 )
689 .in_file(display),
690 );
691 return;
692 }
693 };
694
695 let prefix = prefix.replace('/', "\\").trim_matches('\\').to_lowercase();
696 let own = format!("{prefix}\\");
697
698 let kinds: [(&str, &'static str, BTreeMap<&str, usize>); 2] = [
699 ("texture", "missing-texture", p3d.textures()),
700 ("material", "missing-material", p3d.materials()),
701 ];
702 for (kind, warning_name, refs) in &kinds {
703 for name in refs.keys().filter(|n| !n.starts_with('#')) {
704 let normalized = name
705 .replace('/', "\\")
706 .trim_start_matches('\\')
707 .to_lowercase();
708 let exists = if !prefix.is_empty() && normalized.starts_with(&own) {
709 Some(addon_files.contains(&normalized[own.len()..]))
710 } else if search_roots
711 .iter()
712 .any(|r| exists_ignore_case(r, &normalized))
713 {
714 Some(true)
715 } else {
716 None
717 };
718 if exists == Some(false) {
719 warnings.warn(
720 Warning::new(
721 Some(*warning_name),
722 format!(
723 "Model references {kind} \"{}\", which is not in the addon.",
724 printable(name)
725 ),
726 )
727 .in_file(display),
728 );
729 }
730 }
731 }
732}
733
734pub fn model_magic<I: Read + Seek>(input: &mut I) -> Result<Option<[u8; 4]>, Error> {
741 let start = input.stream_position()?;
742 let mut magic = [0; 4];
743 let complete = input.read_exact(&mut magic).is_ok();
744 input.seek(SeekFrom::Start(start))?;
745 Ok(
746 if complete && (&magic == b"MLOD" || &magic == b"ODOL" || &magic == b"BMTR") {
747 Some(magic)
748 } else {
749 None
750 },
751 )
752}
753
754#[cfg(test)]
755mod tests {
756 use super::*;
757 use std::io::Cursor;
758
759 fn mlod(lod_body: &[u8]) -> Vec<u8> {
760 let mut b = b"MLOD".to_vec();
761 b.extend_from_slice(&[0x01, 0x01, 0, 0, 1, 0, 0, 0]);
762 b.extend_from_slice(lod_body);
763 b
764 }
765
766 #[test]
767 fn garbage_is_an_error_not_a_panic() {
768 assert!(P3D::read(&mut Cursor::new(b"".to_vec())).is_err());
769 assert!(P3D::read(&mut Cursor::new(b"XXXXXXXX".to_vec())).is_err());
770 assert!(P3D::read(&mut Cursor::new(b"ODOL\x38\0\0\0".to_vec())).is_err());
771 assert!(P3D::read(&mut Cursor::new(mlod(b"NOPE"))).is_err());
772 let mut lod = b"P3DM".to_vec();
774 lod.extend_from_slice(&[
775 0x1c, 0, 0, 0, 0, 1, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
776 ]);
777 assert!(P3D::read(&mut Cursor::new(mlod(&lod))).is_err());
778 }
779
780 #[test]
781 fn bad_face_vertex_count_is_an_error() {
782 let mut lod = b"P3DM".to_vec();
783 lod.extend_from_slice(&[
784 0x1c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
785 ]);
786 lod.extend_from_slice(&[7, 0, 0, 0]);
787 let err = P3D::read(&mut Cursor::new(mlod(&lod))).unwrap_err();
788 assert!(err.to_string().contains("7 vertices"), "{}", err);
789 }
790}