fix(Blenvy:Bevy): added safeguard against files withouth scene level metadata

* normally this should NOT happen if the blueprints to spawn are correctly generated
(by the Blender add-on) gltf files, but better safe (no unwrap!) than sorry
This commit is contained in:
kaosat.dev 2024-07-19 01:09:43 +02:00
parent a618e0035e
commit be0f4f1377
1 changed files with 35 additions and 33 deletions

View File

@ -156,41 +156,43 @@ pub(crate) fn blueprints_prepare_spawn(
/* prefetch attempt */ /* prefetch attempt */
let gltf = RawGltf::open(format!("assets/{}", blueprint_info.path)).unwrap(); let gltf = RawGltf::open(format!("assets/{}", blueprint_info.path)).unwrap();
for scene in gltf.scenes() { for scene in gltf.scenes() {
let scene_extras = scene.extras().clone().unwrap(); if let Some(scene_extras) = scene.extras().clone()
let lookup: HashMap<String, Value> = serde_json::from_str(scene_extras.get()).unwrap(); {
if lookup.contains_key("BlueprintAssets") { let lookup: HashMap<String, Value> = serde_json::from_str(scene_extras.get()).unwrap();
let assets_raw = &lookup["BlueprintAssets"]; if lookup.contains_key("BlueprintAssets") {
//println!("ASSETS RAW {}", assets_raw); let assets_raw = &lookup["BlueprintAssets"];
let all_assets: BlueprintAssets = //println!("ASSETS RAW {}", assets_raw);
ron::from_str(assets_raw.as_str().unwrap()).unwrap(); let all_assets: BlueprintAssets =
// println!("all_assets {:?}", all_assets); ron::from_str(assets_raw.as_str().unwrap()).unwrap();
// println!("all_assets {:?}", all_assets);
for asset in all_assets.assets.iter() { for asset in all_assets.assets.iter() {
println!("ASSET {}",asset.path); println!("ASSET {}",asset.path);
let untyped_handle = asset_server.load_untyped(&asset.path); let untyped_handle = asset_server.load_untyped(&asset.path);
let asset_id = untyped_handle.id(); let asset_id = untyped_handle.id();
let loaded = asset_server.is_loaded_with_dependencies(asset_id); let loaded = asset_server.is_loaded_with_dependencies(asset_id);
if !loaded { if !loaded {
asset_infos.push(AssetLoadTracker { asset_infos.push(AssetLoadTracker {
name: asset.name.clone(), name: asset.name.clone(),
path: asset.path.clone(), path: asset.path.clone(),
id: asset_id, id: asset_id,
loaded: false, loaded: false,
handle: untyped_handle.clone(), handle: untyped_handle.clone(),
}); });
} }
// FIXME: dang, too early, asset server has not yet started loading yet // FIXME: dang, too early, asset server has not yet started loading yet
// let path_id = asset_server.get_path_id(&asset.path).expect("we should have alread checked for this asset"); // let path_id = asset_server.get_path_id(&asset.path).expect("we should have alread checked for this asset");
let path_id = asset.path.clone(); let path_id = asset.path.clone();
// TODO: make this dependant on if hot reload is enabled or not // TODO: make this dependant on if hot reload is enabled or not
if !assets_to_blueprint_instances.untyped_id_to_blueprint_entity_ids.contains_key(&path_id) { if !assets_to_blueprint_instances.untyped_id_to_blueprint_entity_ids.contains_key(&path_id) {
assets_to_blueprint_instances.untyped_id_to_blueprint_entity_ids.insert(path_id.clone(), vec![]); assets_to_blueprint_instances.untyped_id_to_blueprint_entity_ids.insert(path_id.clone(), vec![]);
} }
// only insert if not already present in mapping // only insert if not already present in mapping
if !assets_to_blueprint_instances.untyped_id_to_blueprint_entity_ids[&path_id].contains(&entity) { if !assets_to_blueprint_instances.untyped_id_to_blueprint_entity_ids[&path_id].contains(&entity) {
println!("adding mapping between {} and entity {:?}", path_id, all_names.get(entity)); println!("adding mapping between {} and entity {:?}", path_id, all_names.get(entity));
assets_to_blueprint_instances.untyped_id_to_blueprint_entity_ids.get_mut(&path_id).unwrap().push(entity); assets_to_blueprint_instances.untyped_id_to_blueprint_entity_ids.get_mut(&path_id).unwrap().push(entity);
}
} }
} }
} }