use crate::topology::BrepSolid;
use crate::{decode_solid, encode_solid, SolidNames};
use std::collections::BTreeMap;
pub const SNAPSHOT_FORMAT_VERSION: u32 = 1;
const MAGIC: &[u8; 8] = b"BREPSNAP";
#[derive(Debug, Clone)]
pub struct RestoredSolid {
pub name: String,
pub solid: BrepSolid,
}
#[derive(Debug, Clone)]
pub struct RestoredSnapshot {
pub solids: Vec<RestoredSolid>,
pub metadata: Vec<(String, serde_json::Map<String, serde_json::Value>)>,
}
pub fn snapshot_solids(solids: &[(&str, &BrepSolid)]) -> Result<String, String> {
let mut out = Vec::with_capacity(1024);
out.extend_from_slice(MAGIC);
push_u32(&mut out, SNAPSHOT_FORMAT_VERSION);
push_count(&mut out, solids.len())?;
let mut metadata = BTreeMap::<String, String>::new();
let mut capture = |name: &str| -> Result<(), String> {
if let Some(record) = crate::feature_pipeline::scene_metadata::own_record(name) {
let json = serde_json::to_string(&record)
.map_err(|error| format!("snapshot: metadata record for '{name}': {error}"))?;
metadata.insert(name.to_string(), json);
}
Ok(())
};
for (name, solid) in solids {
let (data, names) = encode_solid(solid)?;
let names_json = serde_json::to_string(&names)
.map_err(|error| format!("snapshot: names for '{name}': {error}"))?;
push_str(&mut out, name)?;
push_str(&mut out, &names_json)?;
push_count(&mut out, data.len())?;
for value in &data {
out.extend_from_slice(&value.to_le_bytes());
}
capture(name)?;
for face_name in names.faces.values() {
capture(face_name)?;
}
for edge_name in names.edges.values() {
capture(edge_name)?;
}
}
push_count(&mut out, metadata.len())?;
for (name, record_json) in &metadata {
push_str(&mut out, name)?;
push_str(&mut out, record_json)?;
}
Ok(seal(out))
}
fn seal(mut container: Vec<u8>) -> String {
let digest = fnv1a(&container);
container.extend_from_slice(&digest.to_le_bytes());
base64_encode(&container)
}
fn fnv1a(bytes: &[u8]) -> u64 {
let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
for &byte in bytes {
hash ^= byte as u64;
hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
}
hash
}
pub fn snapshot_resident_solids(named_handles: &[(String, u32)]) -> Result<String, String> {
let mut owned = Vec::with_capacity(named_handles.len());
for (name, handle) in named_handles {
let solid = crate::with_registered_solid_str(*handle, |solid| Ok(solid.clone()))?;
owned.push((name.as_str(), solid));
}
let borrowed: Vec<(&str, &BrepSolid)> = owned
.iter()
.map(|(name, solid)| (*name, solid))
.collect();
snapshot_solids(&borrowed)
}
pub fn restore_solids(payload: &str) -> Result<RestoredSnapshot, String> {
let bytes = base64_decode(payload)?;
if bytes.len() < MAGIC.len() + 8 {
return Err("snapshot: payload too short".into());
}
let (body, trailer) = bytes.split_at(bytes.len() - 8);
let stored = u64::from_le_bytes(trailer.try_into().expect("8-byte trailer"));
if fnv1a(body) != stored {
return Err("snapshot: payload checksum mismatch (corrupted)".into());
}
let mut reader = ByteReader {
data: body,
cursor: 0,
};
if reader.take(MAGIC.len())? != MAGIC {
return Err("snapshot: not a BREP snapshot payload (bad magic)".into());
}
let version = reader.u32()?;
if version != SNAPSHOT_FORMAT_VERSION {
return Err(format!(
"snapshot: unsupported format version {version} (expected {SNAPSHOT_FORMAT_VERSION})"
));
}
let solid_count = reader.u32()? as usize;
let mut solids = Vec::with_capacity(solid_count.min(1024));
for _ in 0..solid_count {
let name = reader.string()?;
let names_json = reader.string()?;
let names: SolidNames = serde_json::from_str(&names_json)
.map_err(|error| format!("snapshot: names for '{name}': {error}"))?;
let data = reader.f64_vec()?;
let solid = decode_solid(&data, &names)
.map_err(|error| format!("snapshot: solid '{name}': {error}"))?;
solids.push(RestoredSolid { name, solid });
}
let record_count = reader.u32()? as usize;
let mut metadata = Vec::with_capacity(record_count.min(4096));
for _ in 0..record_count {
let name = reader.string()?;
let record_json = reader.string()?;
let record: serde_json::Map<String, serde_json::Value> =
serde_json::from_str(&record_json)
.map_err(|error| format!("snapshot: metadata record for '{name}': {error}"))?;
metadata.push((name, record));
}
if reader.cursor != body.len() {
return Err("snapshot: trailing bytes after payload".into());
}
Ok(RestoredSnapshot { solids, metadata })
}
fn push_u32(out: &mut Vec<u8>, value: u32) {
out.extend_from_slice(&value.to_le_bytes());
}
fn push_count(out: &mut Vec<u8>, count: usize) -> Result<(), String> {
let value: u32 = count
.try_into()
.map_err(|_| format!("snapshot: count {count} exceeds u32"))?;
push_u32(out, value);
Ok(())
}
fn push_str(out: &mut Vec<u8>, text: &str) -> Result<(), String> {
push_count(out, text.len())?;
out.extend_from_slice(text.as_bytes());
Ok(())
}
struct ByteReader<'a> {
data: &'a [u8],
cursor: usize,
}
impl<'a> ByteReader<'a> {
fn take(&mut self, count: usize) -> Result<&'a [u8], String> {
let end = self
.cursor
.checked_add(count)
.ok_or("snapshot: truncated payload")?;
let slice = self
.data
.get(self.cursor..end)
.ok_or("snapshot: truncated payload")?;
self.cursor = end;
Ok(slice)
}
fn u32(&mut self) -> Result<u32, String> {
let raw = self.take(4)?;
Ok(u32::from_le_bytes([raw[0], raw[1], raw[2], raw[3]]))
}
fn string(&mut self) -> Result<String, String> {
let length = self.u32()? as usize;
let raw = self.take(length)?;
String::from_utf8(raw.to_vec()).map_err(|_| "snapshot: invalid UTF-8 string".into())
}
fn f64_vec(&mut self) -> Result<Vec<f64>, String> {
let count = self.u32()? as usize;
let raw = self.take(
count
.checked_mul(8)
.ok_or("snapshot: truncated payload")?,
)?;
Ok(raw
.chunks_exact(8)
.map(|chunk| {
f64::from_le_bytes([
chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7],
])
})
.collect())
}
}
const B64_ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
fn base64_encode(bytes: &[u8]) -> String {
let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
for chunk in bytes.chunks(3) {
let b0 = chunk[0] as u32;
let b1 = chunk.get(1).copied().unwrap_or(0) as u32;
let b2 = chunk.get(2).copied().unwrap_or(0) as u32;
let triple = (b0 << 16) | (b1 << 8) | b2;
out.push(B64_ALPHABET[(triple >> 18) as usize & 63] as char);
out.push(B64_ALPHABET[(triple >> 12) as usize & 63] as char);
out.push(if chunk.len() > 1 {
B64_ALPHABET[(triple >> 6) as usize & 63] as char
} else {
'='
});
out.push(if chunk.len() > 2 {
B64_ALPHABET[triple as usize & 63] as char
} else {
'='
});
}
out
}
fn base64_value(byte: u8) -> Result<u32, String> {
match byte {
b'A'..=b'Z' => Ok((byte - b'A') as u32),
b'a'..=b'z' => Ok((byte - b'a') as u32 + 26),
b'0'..=b'9' => Ok((byte - b'0') as u32 + 52),
b'+' => Ok(62),
b'/' => Ok(63),
_ => Err(format!(
"snapshot: invalid base64 character 0x{byte:02x}"
)),
}
}
fn base64_decode(text: &str) -> Result<Vec<u8>, String> {
let bytes = text.as_bytes();
if bytes.len() % 4 != 0 {
return Err("snapshot: base64 length must be a multiple of 4".into());
}
let mut out = Vec::with_capacity(bytes.len() / 4 * 3);
for (index, quad) in bytes.chunks_exact(4).enumerate() {
let is_last = (index + 1) * 4 == bytes.len();
let padding = match (quad[2], quad[3]) {
(b'=', b'=') if is_last => 2,
(_, b'=') if is_last && quad[2] != b'=' => 1,
(b'=', _) => return Err("snapshot: misplaced base64 padding".into()),
_ => 0,
};
if quad[0] == b'=' || quad[1] == b'=' {
return Err("snapshot: misplaced base64 padding".into());
}
let mut triple = base64_value(quad[0])? << 18 | base64_value(quad[1])? << 12;
if padding < 2 {
triple |= base64_value(quad[2])? << 6;
}
if padding < 1 {
triple |= base64_value(quad[3])?;
}
out.push((triple >> 16) as u8);
if padding < 2 {
out.push((triple >> 8) as u8);
}
if padding < 1 {
out.push(triple as u8);
}
}
Ok(out)
}