diff --git a/crates/bevy_gltf_components/src/process_gltfs.rs b/crates/bevy_gltf_components/src/process_gltfs.rs index b8da9dd..f2f8e9e 100644 --- a/crates/bevy_gltf_components/src/process_gltfs.rs +++ b/crates/bevy_gltf_components/src/process_gltfs.rs @@ -31,8 +31,7 @@ pub fn add_components_from_gltf_extras(world: &mut World) { ); let type_registry: &AppTypeRegistry = world.resource(); - let type_registry = type_registry.read(); - + let type_registry = type_registry.read(); let reflect_components = ronstring_to_reflect_component(&extra.value, &type_registry); // we assign the components specified /xxx_components objects to their parent node diff --git a/crates/bevy_gltf_components/src/ronstring_to_reflect_component.rs b/crates/bevy_gltf_components/src/ronstring_to_reflect_component.rs index c8bfd78..ec81e22 100644 --- a/crates/bevy_gltf_components/src/ronstring_to_reflect_component.rs +++ b/crates/bevy_gltf_components/src/ronstring_to_reflect_component.rs @@ -13,10 +13,84 @@ pub fn ronstring_to_reflect_component( ) -> Vec<(Box, TypeRegistration)> { let lookup: HashMap = ron::from_str(ron_string).unwrap(); let mut components: Vec<(Box, TypeRegistration)> = Vec::new(); - for (key, value) in lookup.into_iter() { - let type_string = key.replace("component: ", "").trim().to_string(); - let capitalized_type_name = capitalize_first_letter(type_string.as_str()); + // println!("ron_string {:?}", ron_string); + for (name, value) in lookup.into_iter() { + + + let parsed_value: String; + match value.clone() { + Value::String(str) => { + parsed_value = str; + } + _ => parsed_value = ron::to_string(&value).unwrap().to_string(), + } + if name.as_str() == "bevy_components" { + bevy_components_string_to_components(parsed_value, type_registry, &mut components) + } else { + components_string_to_components(name, value, parsed_value, type_registry, &mut components) + } + } + components +} + +fn components_string_to_components( + name: String, + value: Value, + parsed_value: String, + type_registry: &TypeRegistry, + components: &mut Vec<(Box, TypeRegistration)> +){ + let type_string = name.replace("component: ", "").trim().to_string(); + let capitalized_type_name = capitalize_first_letter(type_string.as_str()); + + if let Some(type_registration) = + type_registry.get_with_short_type_path(capitalized_type_name.as_str()) + { + debug!("TYPE INFO {:?}", type_registration.type_info()); + + let ron_string = format!( + "{{ \"{}\":{} }}", + type_registration.type_info().type_path(), + parsed_value + ); + + // usefull to determine what an entity looks like Serialized + /*let test_struct = CameraRenderGraph::new("name"); + let serializer = ReflectSerializer::new(&test_struct, &type_registry); + let serialized = + ron::ser::to_string_pretty(&serializer, ron::ser::PrettyConfig::default()).unwrap(); + println!("serialized Component {}", serialized);*/ + + debug!("component data ron string {}", ron_string); + let mut deserializer = ron::Deserializer::from_str(ron_string.as_str()) + .expect("deserialzer should have been generated from string"); + let reflect_deserializer = UntypedReflectDeserializer::new(type_registry); + let component = reflect_deserializer + .deserialize(&mut deserializer) + .unwrap_or_else(|_| { + panic!( + "failed to deserialize component {} with value: {:?}", + name, value + ) + }); + + debug!("component {:?}", component); + debug!("real type {:?}", component.get_represented_type_info()); + components.push((component, type_registration.clone())); + debug!("found type registration for {}", capitalized_type_name); + } else { + warn!("no type registration for {}", capitalized_type_name); + } +} + +fn bevy_components_string_to_components( + parsed_value: String, + type_registry: &TypeRegistry, + components: &mut Vec<(Box, TypeRegistration)> +){ + let lookup: HashMap = ron::from_str(&parsed_value).unwrap(); + for (key, value) in lookup.into_iter() { let parsed_value: String; match value.clone() { Value::String(str) => { @@ -26,7 +100,7 @@ pub fn ronstring_to_reflect_component( } if let Some(type_registration) = - type_registry.get_with_short_type_path(capitalized_type_name.as_str()) + type_registry.get_with_type_path(key.as_str()) { debug!("TYPE INFO {:?}", type_registration.type_info()); @@ -36,13 +110,6 @@ pub fn ronstring_to_reflect_component( parsed_value ); - // usefull to determine what an entity looks like Serialized - /*let test_struct = CameraRenderGraph::new("name"); - let serializer = ReflectSerializer::new(&test_struct, &type_registry); - let serialized = - ron::ser::to_string_pretty(&serializer, ron::ser::PrettyConfig::default()).unwrap(); - println!("serialized Component {}", serialized);*/ - debug!("component data ron string {}", ron_string); let mut deserializer = ron::Deserializer::from_str(ron_string.as_str()) .expect("deserialzer should have been generated from string"); @@ -59,10 +126,9 @@ pub fn ronstring_to_reflect_component( debug!("component {:?}", component); debug!("real type {:?}", component.get_represented_type_info()); components.push((component, type_registration.clone())); - debug!("found type registration for {}", capitalized_type_name); + debug!("found type registration for {}", key); } else { - warn!("no type registration for {}", capitalized_type_name); + warn!("no type registration for {}", key); } } - components -} +} \ No newline at end of file