//! Does a FILTERED decode agree with the full one?
//!
//! Distinct anchor assignment resolves collisions against the set of resources
//! being decoded — so `models_named` (a filtered subset, used by the ship
//! assembler and the viewer) can reach a different answer from a whole-container
//! decode. This measures that directly.
//! Usage: filter_consistency <container.xpr> [resource...]
use sylpheed_formats::mesh::Xbg7Model;
use std::collections::HashSet;
fn main() {
    let a: Vec<String> = std::env::args().collect();
    let bytes = std::fs::read(&a[1]).expect("container");
    let full = Xbg7Model::anchor_models_cancellable(&bytes, 0.0, &|| false);
    let names: Vec<String> = if a.len() > 2 {
        a[2..].to_vec()
    } else {
        full.iter().map(|m| m.name.clone()).collect()
    };
    let mut differ = 0usize;
    for n in &names {
        let want: HashSet<String> = std::iter::once(n.clone()).collect();
        let one = Xbg7Model::models_named(&bytes, &want, &|| false);
        let (Some(f), Some(s)) = (full.iter().find(|m| &m.name == n), one.first()) else {
            continue;
        };
        let off = |m: &Xbg7Model| m.meshes.first().and_then(|s| s.vbuf_offset).unwrap_or(0);
        if off(f) != off(s) {
            differ += 1;
            if differ <= 10 {
                println!("{n}: full decode at 0x{:x}, filtered at 0x{:x}", off(f), off(s));
            }
        }
    }
    println!("{differ} of {} resources decode differently when filtered", names.len());
}
