//! Where could a resource's index buffer be, given the vertex buffer a runtime
//! capture proves the engine drew from? Tests the anchor scan's adjacency
//! assumption against ground truth.
use sylpheed_formats::mesh::{debug_find_index_buffer, debug_resource_params};
fn main() {
    let a: Vec<String> = std::env::args().collect();
    let bytes = std::fs::read(&a[1]).unwrap();
    for pair in a[2..].iter() {
        let (name, off) = pair.split_once('@').unwrap();
        let vb = usize::from_str_radix(off.trim_start_matches("0x"), 16).unwrap();
        let params = debug_resource_params(&bytes, name);
        let markers = params.as_ref().map(|(m, _)| m.clone()).unwrap_or_default();
        println!("{name} @ 0x{vb:x}  markers={markers:?}");
        let hits = debug_find_index_buffer(&bytes, name, vb);
        if hits.is_empty() {
            println!("   no index buffer anywhere in the container validates this block");
        }
        let mut hits = hits;
        hits.sort_by_key(|(_, d)| d.abs());
        for (ib, d) in hits.iter().take(8) {
            println!("   ib 0x{ib:x}  vb-ib = {d} bytes");
        }
        if hits.len() > 8 {
            println!("   … {} total", hits.len());
        }
    }
}
