use std::collections::HashMap;
use crate::bson::{self, Element, ARRAY, DOCUMENT, INT32, INT64, STRING};
use crate::datatree;
use crate::error::{Error, Result};
use crate::index::Index;
const TYPE_FIELDS: [&str; 4] = ["SpawnNodeType", "InputType", "OutputType", "MatchingType"];
pub fn is_type_field(component_type: &str, key: &str) -> bool {
TYPE_FIELDS.contains(&key) || (key == "_type" && component_type.ends_with(".GizmoLink"))
}
#[derive(Debug, Clone, Copy)]
pub struct TypeRef {
pub value_at: usize,
pub ty: u8,
pub index: i64,
}
pub fn is_int(ty: u8) -> bool {
ty == INT32 || ty == INT64
}
pub fn component_type_refs(
buf: &[u8],
comp_off: usize,
types: &[String],
out: &mut Vec<TypeRef>,
) -> Result<()> {
let els = bson::elements(buf, comp_off)?;
let type_el = els
.iter()
.find(|e| e.key == "Type")
.ok_or_else(|| Error::Invalid("component has no Type".into()))?;
if !is_int(type_el.ty) {
return Err(Error::Invalid("component Type is not an integer".into()));
}
let index = bson::decode_int(type_el.value, type_el.ty)?;
let name = type_name(types, index)?;
out.push(TypeRef {
value_at: type_el.value_at,
ty: type_el.ty,
index,
});
if let Some(data) = els.iter().find(|e| e.key == "Data") {
if data.ty == DOCUMENT || data.ty == ARRAY {
field_type_refs(buf, data.value_at, name, out)?;
}
}
Ok(())
}
fn field_type_refs(
buf: &[u8],
doc_off: usize,
component_type: &str,
out: &mut Vec<TypeRef>,
) -> Result<()> {
for el in bson::elements(buf, doc_off)? {
if el.ty == DOCUMENT && is_type_field(component_type, el.key) {
if let Some(found) = type_field_value(buf, &el)? {
out.push(found);
continue;
}
}
if el.ty == DOCUMENT || el.ty == ARRAY {
field_type_refs(buf, el.value_at, component_type, out)?;
}
}
Ok(())
}
pub fn type_field_value(buf: &[u8], el: &Element<'_>) -> Result<Option<TypeRef>> {
let inner = bson::elements(buf, el.value_at)?;
if !inner.iter().any(|e| e.key == "ID" && e.ty == STRING) {
return Ok(None);
}
let Some(data) = inner.iter().find(|e| e.key == "Data" && is_int(e.ty)) else {
return Ok(None);
};
Ok(Some(TypeRef {
value_at: data.value_at,
ty: data.ty,
index: bson::decode_int(data.value, data.ty)?,
}))
}
pub fn type_name(types: &[String], index: i64) -> Result<&str> {
usize::try_from(index)
.ok()
.and_then(|i| types.get(i))
.map(String::as_str)
.ok_or_else(|| Error::Invalid(format!("type index {index} is outside Types")))
}
pub fn int_bytes(ty: u8, value: i64) -> Result<Vec<u8>> {
match ty {
INT32 => {
let v = i32::try_from(value).map_err(|_| Error::TooLarge)?;
Ok(v.to_le_bytes().to_vec())
}
INT64 => Ok(value.to_le_bytes().to_vec()),
_ => Err(Error::Invalid("type index is not an integer".into())),
}
}
pub fn rebuild(buf: &mut Vec<u8>, index: &Index) -> Result<Vec<String>> {
let types = datatree::read_types(buf)?;
let mut refs = Vec::new();
for comp in &index.components {
component_type_refs(buf, comp.offset, &types, &mut refs)?;
}
if let Some(assets) = datatree::asset_array(buf)? {
if assets.ty != ARRAY {
return Err(Error::Invalid("Assets is not an array".into()));
}
for el in bson::elements(buf, assets.value_at)? {
if el.ty != DOCUMENT {
return Err(Error::Invalid("Assets entry is not a document".into()));
}
component_type_refs(buf, el.value_at, &types, &mut refs)?;
}
}
let mut names: Vec<String> = Vec::new();
let mut map: HashMap<i64, i64> = HashMap::new();
for r in &refs {
if map.contains_key(&r.index) {
continue;
}
let Ok(name) = type_name(&types, r.index) else {
continue;
};
map.insert(r.index, names.len() as i64);
names.push(name.to_string());
}
for r in &refs {
if let Some(&new) = map.get(&r.index) {
if new != r.index {
let bytes = int_bytes(r.ty, new)?;
buf[r.value_at..r.value_at + bytes.len()].copy_from_slice(&bytes);
}
}
}
let removed: Vec<String> = types
.iter()
.filter(|name| !names.contains(name))
.cloned()
.collect();
let name_refs: Vec<&str> = names.iter().map(String::as_str).collect();
let new_types = bson::array_of_strings(&name_refs);
let new_versions = {
let mut body = Vec::new();
if let Some(tv) = datatree::field(buf, 0, "TypeVersions")? {
if tv.ty != DOCUMENT {
return Err(Error::Invalid("TypeVersions is not a document".into()));
}
let old = bson::elements(buf, tv.value_at)?;
for name in &names {
if let Some(el) = old.iter().find(|e| e.key == name) {
body.extend_from_slice(el.raw);
}
}
}
bson::finish_doc(&body)?
};
let mut edits: Vec<(String, u8, Vec<u8>)> = vec![("Types".into(), ARRAY, new_types)];
if datatree::field(buf, 0, "TypeVersions")?.is_some() {
edits.push(("TypeVersions".into(), DOCUMENT, new_versions));
}
replace_root_fields(buf, &edits)?;
Ok(removed)
}
pub fn replace_root_fields(buf: &mut Vec<u8>, edits: &[(String, u8, Vec<u8>)]) -> Result<()> {
let root_end = bson::doc_end(buf, 0)?;
let mut ops: Vec<(usize, usize, Vec<u8>)> = Vec::new();
let mut appended = Vec::new();
for (key, ty, value) in edits {
match datatree::field(buf, 0, key)? {
Some(el) => {
if el.ty != *ty {
return Err(Error::Invalid(format!("{key} has an unexpected type")));
}
ops.push((el.value_at, el.value_at + el.value.len(), value.clone()));
}
None => bson::push_element(&mut appended, *ty, key, value),
}
}
if !appended.is_empty() {
ops.push((root_end - 1, root_end - 1, appended));
}
ops.sort_by_key(|op| std::cmp::Reverse(op.0));
for (start, end, bytes) in ops {
bson::splice(buf, start, end, &bytes, &[0])?;
}
Ok(())
}