//! Every key/value a named unit record actually sets on disc.
//!
//! The live definition object in guest RAM has no field names; to find where a
//! field like `HQRatio` sits inside it, anchor on a unit whose disc record DOES
//! set that field and look for the value. This prints those anchors.
//! Run: unit_fields <disc-root> <substring of ID>...
use sylpheed_formats::idxd::IdxdObject;
use sylpheed_formats::pak::PakArchive;

fn main() {
    let disc = std::env::args().nth(1).expect("disc root");
    let want: Vec<String> = std::env::args().skip(2).collect();
    let pak = PakArchive::open(format!("{disc}/dat/GP_MAIN_GAME_E.pak")).expect("pak");
    for entry in pak.entries() {
        let Ok(bytes) = pak.read(entry) else { continue };
        let Ok(obj) = IdxdObject::parse(&bytes) else { continue };
        let Some(id) = obj.get_raw("ID") else { continue };
        if !want.iter().any(|w| id.contains(w.as_str())) {
            continue;
        }
        println!("=== {id}   (schema {:08x})", obj.schema_hash);
        // The pool interleaves VALUE before KEY (see the module docs), so the
        // pairs read (t[i] = value, t[i+1] = key).
        let t = obj.tokens();
        let mut i = 0;
        while i + 1 < t.len() {
            println!("    {:<30} {}", t[i + 1], t[i]);
            i += 2;
        }
    }
}
