File size: 2,504 Bytes
918c06e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//! Safe, strict MB3 DAT serialization, independent of the editor or renderer.
//!
//! See `docs/RUST_DAT.md` for compatibility and resource limits. No geometry is
//! computed or validated. Decoding owns its results and never retains the input.
//!
//! ```
//! use mb3_dat::{Document, Limits, Version, World, decode, encode};
//! let document = Document { version: Version::V201512200, world: World::default() };
//! let bytes = encode(&document, &Limits::default())?;
//! assert_eq!(decode(&bytes, &Limits::default())?, document);
//! # Ok::<(), mb3_dat::Error>(())
//! ```
mod codec;
pub use codec::{Error, ErrorKind, Limits, decode, encode, read, write};

pub type Vec3 = [f32; 3];

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u32)]
pub enum Version {
    V201501280 = 201501280,
    V201512200 = 201512200,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Document {
    pub version: Version,
    pub world: World,
}

/// Table order and sparse indices are preserved; no converter optimization runs.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct World {
    pub computed: bool,
    pub string_properties: Vec<String>,
    pub uv_properties: Vec<String>,
    pub brushes: Vec<Brush>,
    pub patches: Vec<Patch9>,
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct Brush {
    pub planes: Vec<Plane>,
}

/// Sparse entries must be unique and sorted by index when encoding. Decoding
/// sorts wire entries by index. An absent entry differs from an empty value.
#[derive(Clone, Debug, PartialEq)]
pub struct Property<T> {
    pub index: u32,
    pub value: T,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Uv {
    pub u: Vec3,
    pub v: Vec3,
    pub u_scale: f32,
    pub u_shift: f32,
    pub v_scale: f32,
    pub v_shift: f32,
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct Plane {
    pub p0: Vec3,
    pub u: Vec3,
    pub v: Vec3,
    pub strings: Vec<Property<String>>,
    pub uv: Vec<Property<Uv>>,
    /// Some (including an empty polygon) iff the world's computed flag is true.
    pub polygon: Option<Vec<Vec3>>,
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct Patch9 {
    pub anchors_u: u32,
    pub anchors_v: u32,
    pub quads_u: u32,
    pub quads_v: u32,
    pub anchors: Vec<Vec3>,
    /// Some iff the world's computed flag is true.
    pub computed: Option<PatchComputed>,
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct PatchComputed {
    pub points: Vec<Vec3>,
    pub normals: Vec<Vec3>,
}

#[cfg(test)]
mod tests;