diff --git a/assets/scenes/00/a.entity b/assets/scenes/00/a.entity index 20cc19d..80a0fec 100644 --- a/assets/scenes/00/a.entity +++ b/assets/scenes/00/a.entity @@ -1 +1,2 @@ name "Hello world" +name "Hello bevy" diff --git a/src/save.rs b/src/save.rs index bba3959..2da2144 100644 --- a/src/save.rs +++ b/src/save.rs @@ -1,3 +1,5 @@ +use std::any::TypeId; + use crate::prelude::*; type ParseFn = @@ -51,7 +53,12 @@ pub(crate) struct SaveScene { // Ironically we basically want DynamicEntity: https://docs.rs/bevy/latest/bevy/scene/struct.DynamicEntity.html #[derive(Asset, TypePath, Default)] pub(crate) struct SaveEntity { - components: Vec>, + components: Vec, +} + +struct SaveEntityComponent { + type_id: TypeId, + data: Box, } impl SaveEntity { @@ -74,8 +81,13 @@ impl SaveEntity { // Run line against all parsers for f in fns { - if let Ok(v) = f(&tokens) { - entity.components.push(v); + if let Ok(c) = f(&tokens) { + // Bundle the Type ID with this entry for auditing purposes + let t = c.get_represented_type_info().unwrap().type_id(); + entity.components.push(SaveEntityComponent { + type_id: t, + data: c, + }); good = true; } } @@ -88,6 +100,29 @@ impl SaveEntity { ); } }); + + // Check for duplicate component types and emit an error + // TODO: It would be nice if this emitted a line refernece instead of a parsed struct! + let l = entity.components.len(); + (0..l).for_each(|i| { + (i..l).for_each(|j| { + if i != j { + let c1 = &entity.components[i]; + let c2 = &entity.components[j]; + let t1 = c1.type_id; + let t2 = c2.type_id; + if t1 == t2 { + error!( + "Duplicate components in {:?}: \n\t{:?}\n\t{:?}", + load_context.asset_path(), + c1.data, + c2.data, + ); + } + } + }); + }); + Ok(entity) } } @@ -239,8 +274,8 @@ fn spawn_entities( // Add each component to the entity debug!("Populating entity with components {:?}", entity); - save_entity.components.iter().for_each(|component| { - e.insert_reflect(component.clone_value()); + save_entity.components.iter().for_each(|item| { + e.insert_reflect(item.data.clone_value()); }); }); }