frdt 0.2.2

Edit and inspect Resonite FrDT (.brson) documents without expanding them to JSON
Documentation
//! `Types` and `TypeVersions` rebuilt from the components that remain.
//!
//! A component's `Type` is an index into `Types`. A few component fields also
//! hold an index into `Types`; they are listed in [`is_type_field`]. Rebuilding
//! keeps the names in first-use order (slots depth first, then assets), patches
//! every index in place, and drops names that nothing uses. `TypeVersions` keeps
//! the entries whose name is still in `Types`.

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;

/// Field names that the current MirageX pipeline treats as a `Types` index.
const TYPE_FIELDS: [&str; 4] = ["SpawnNodeType", "InputType", "OutputType", "MatchingType"];

/// Whether a `{ID, Data: int}` field of this component holds a `Types` index.
///
/// `GizmoLink._type` is a `SyncType`. Resonite writes it as an index; a
/// Resonite-saved empty unit has `_type` = 2 with `Types[2]` = `SlotGizmo`.
pub fn is_type_field(component_type: &str, key: &str) -> bool {
    TYPE_FIELDS.contains(&key) || (key == "_type" && component_type.ends_with(".GizmoLink"))
}

/// One integer that indexes `Types`.
#[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
}

/// `Type` of the component document at `comp_off`, then its type fields in
/// document order.
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(())
}

/// `{ID: string, Data: int}` gives the position of `Data`.
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")))
}

/// Encode `value` with the same BSON integer type it replaces.
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())),
    }
}

/// Rebuild `Types` and `TypeVersions` in place. Returns the removed names.
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;
        }
        // A type field that already points outside `Types` is left as it is.
        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)
}

/// Replace (or add at the end) root-level fields. Later offsets go first so
/// earlier offsets stay valid.
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(())
}