use std::collections::HashSet;
use std::fmt;
use crate::bytes::{In, Out, fits_u32};
use crate::container::ChunkTag;
use crate::scene::Scene;
pub const SURFACE_TAG: ChunkTag = ChunkTag(*b"SURF");
pub const SURFACE_VERSION: u16 = 1;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SurfaceEntry {
pub leaf: usize,
pub kind: u8,
pub version: u32,
pub document: Vec<u8>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EncodeSurfaceError {
NotALeaf,
SameEntry,
TooLarge,
}
impl fmt::Display for EncodeSurfaceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotALeaf => f.write_str(NOT_A_LEAF),
Self::SameEntry => f.write_str(SAME_ENTRY),
Self::TooLarge => f.write_str("the surface paint is too large for its chunk"),
}
}
}
impl std::error::Error for EncodeSurfaceError {}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DecodeSurfaceError {
Malformed,
NotALeaf,
SameEntry,
}
impl fmt::Display for DecodeSurfaceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Malformed => f.write_str("the surface paint data is malformed"),
Self::NotALeaf => f.write_str(NOT_A_LEAF),
Self::SameEntry => f.write_str(SAME_ENTRY),
}
}
}
impl std::error::Error for DecodeSurfaceError {}
const NOT_A_LEAF: &str = "surface paint names a scene part that is not a leaf";
const SAME_ENTRY: &str = "two surface paint entries have the same leaf and kind";
enum Broken {
NotALeaf,
SameEntry,
}
impl From<Broken> for EncodeSurfaceError {
fn from(broken: Broken) -> Self {
match broken {
Broken::NotALeaf => Self::NotALeaf,
Broken::SameEntry => Self::SameEntry,
}
}
}
impl From<Broken> for DecodeSurfaceError {
fn from(broken: Broken) -> Self {
match broken {
Broken::NotALeaf => Self::NotALeaf,
Broken::SameEntry => Self::SameEntry,
}
}
}
pub fn encode_surface(
entries: &[SurfaceEntry],
scene: &Scene,
) -> Result<Vec<u8>, EncodeSurfaceError> {
check(entries, scene)?;
if !fits_u32(entries.len()) || entries.iter().any(|entry| !fits_u32(entry.document.len())) {
return Err(EncodeSurfaceError::TooLarge);
}
let mut out = Out::default();
out.len(entries.len());
for entry in entries {
out.len(entry.leaf);
out.u8(entry.kind);
out.u32(entry.version);
out.bytes(&entry.document);
}
Ok(out.0)
}
pub fn decode_surface(
payload: &[u8],
scene: &Scene,
) -> Result<Vec<SurfaceEntry>, DecodeSurfaceError> {
let mut input = In(payload);
let malformed = DecodeSurfaceError::Malformed;
let count = input.count(13).ok_or(malformed.clone())?;
let mut entries = Vec::with_capacity(count);
for _ in 0..count {
let leaf = input.len().ok_or(malformed.clone())?;
let kind = input.u8().ok_or(malformed.clone())?;
let version = input.u32().ok_or(malformed.clone())?;
let document = input.bytes().ok_or(malformed.clone())?.to_vec();
entries.push(SurfaceEntry {
leaf,
kind,
version,
document,
});
}
if !input.is_empty() {
return Err(malformed);
}
check(&entries, scene)?;
Ok(entries)
}
fn check(entries: &[SurfaceEntry], scene: &Scene) -> Result<(), Broken> {
let mut seen = HashSet::new();
for entry in entries {
scene.leaf_mesh(entry.leaf).ok_or(Broken::NotALeaf)?;
if !seen.insert((entry.leaf, entry.kind)) {
return Err(Broken::SameEntry);
}
}
Ok(())
}