nmarshall's picture
formats: add safe dat codecs
918c06e
Raw History Blame Contribute Delete
2.5 kB
//! 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;