feat(bevy_gltf_components): added support for the new 'nested' bevy_components hashmap from

the bevy_components add-on ! this means support for full component paths is working
 * no more need for short_names in that case: long names work !
 * support for short names for other components (for simplicity & compatibility) is still in but cleaned up
This commit is contained in:
kaosat.dev 2024-05-03 00:24:36 +02:00
parent 98a654095b
commit ff68109f0f
2 changed files with 82 additions and 17 deletions

View File

@ -31,8 +31,7 @@ pub fn add_components_from_gltf_extras(world: &mut World) {
); );
let type_registry: &AppTypeRegistry = world.resource(); 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); let reflect_components = ronstring_to_reflect_component(&extra.value, &type_registry);
// we assign the components specified /xxx_components objects to their parent node // we assign the components specified /xxx_components objects to their parent node

View File

@ -13,10 +13,84 @@ pub fn ronstring_to_reflect_component(
) -> Vec<(Box<dyn Reflect>, TypeRegistration)> { ) -> Vec<(Box<dyn Reflect>, TypeRegistration)> {
let lookup: HashMap<String, Value> = ron::from_str(ron_string).unwrap(); let lookup: HashMap<String, Value> = ron::from_str(ron_string).unwrap();
let mut components: Vec<(Box<dyn Reflect>, TypeRegistration)> = Vec::new(); let mut components: Vec<(Box<dyn Reflect>, TypeRegistration)> = Vec::new();
for (key, value) in lookup.into_iter() { // println!("ron_string {:?}", ron_string);
let type_string = key.replace("component: ", "").trim().to_string(); for (name, value) in lookup.into_iter() {
let capitalized_type_name = capitalize_first_letter(type_string.as_str());
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<dyn Reflect>, 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<dyn Reflect>, TypeRegistration)>
){
let lookup: HashMap<String, Value> = ron::from_str(&parsed_value).unwrap();
for (key, value) in lookup.into_iter() {
let parsed_value: String; let parsed_value: String;
match value.clone() { match value.clone() {
Value::String(str) => { Value::String(str) => {
@ -26,7 +100,7 @@ pub fn ronstring_to_reflect_component(
} }
if let Some(type_registration) = 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()); debug!("TYPE INFO {:?}", type_registration.type_info());
@ -36,13 +110,6 @@ pub fn ronstring_to_reflect_component(
parsed_value 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); debug!("component data ron string {}", ron_string);
let mut deserializer = ron::Deserializer::from_str(ron_string.as_str()) let mut deserializer = ron::Deserializer::from_str(ron_string.as_str())
.expect("deserialzer should have been generated from string"); .expect("deserialzer should have been generated from string");
@ -59,10 +126,9 @@ pub fn ronstring_to_reflect_component(
debug!("component {:?}", component); debug!("component {:?}", component);
debug!("real type {:?}", component.get_represented_type_info()); debug!("real type {:?}", component.get_represented_type_info());
components.push((component, type_registration.clone())); components.push((component, type_registration.clone()));
debug!("found type registration for {}", capitalized_type_name); debug!("found type registration for {}", key);
} else { } else {
warn!("no type registration for {}", capitalized_type_name); warn!("no type registration for {}", key);
} }
} }
components }
}