use std::path::PathBuf;
use bevy_grimoire_core::import::{self, Severity};
use bevy_grimoire_core::library::Library;
fn main() -> Result<(), String> {
let args: Vec<String> = std::env::args().skip(1).collect();
let (asset_root, dir, library_path) = match args.as_slice() {
[a, d, l] => (a, d, l),
_ => {
return Err("usage: import_dir <asset-root> <dir under asset-root> <library.ron>".to_owned());
}
};
let asset_root = PathBuf::from(asset_root);
let dir = asset_root.join(dir);
let library_path = PathBuf::from(library_path);
let text = std::fs::read_to_string(&library_path)
.map_err(|e| format!("{}: {e}", library_path.display()))?;
let mut library = Library::parse(&text).map_err(|e| format!("{}: {e}", library_path.display()))?;
let candidates = import::scan(&asset_root, &dir, &library)?;
let mut added = 0usize;
let mut warned = 0usize;
for c in candidates {
if c.blocked() {
let why: Vec<&str> = c.findings.iter().map(|f| f.message.as_str()).collect();
eprintln!("skipped {}: {}", c.mesh, why.join("; "));
continue;
}
if c.worst() == Some(Severity::Warn) {
warned += 1;
}
if library.get(&c.proposed.id).is_some() {
eprintln!("skipped {}: id `{}` is already in the library", c.mesh, c.proposed.id);
continue;
}
library.descriptors.push(c.proposed);
added += 1;
}
library.validate().map_err(|e| format!("not written, the result is invalid: {e}"))?;
let out = library.to_ron()?;
bevy_grimoire_core::ron_surgery::save_atomic(&library_path, &out)?;
println!(
"imported {added} mesh(es) from {} into {} ({warned} with warnings), {} descriptor(s) in all",
dir.display(),
library_path.display(),
library.descriptors.len()
);
Ok(())
}