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

@ -32,7 +32,6 @@ 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,9 +13,9 @@ 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; let parsed_value: String;
match value.clone() { match value.clone() {
@ -25,6 +25,25 @@ pub fn ronstring_to_reflect_component(
_ => parsed_value = ron::to_string(&value).unwrap().to_string(), _ => 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) = if let Some(type_registration) =
type_registry.get_with_short_type_path(capitalized_type_name.as_str()) type_registry.get_with_short_type_path(capitalized_type_name.as_str())
{ {
@ -52,7 +71,7 @@ pub fn ronstring_to_reflect_component(
.unwrap_or_else(|_| { .unwrap_or_else(|_| {
panic!( panic!(
"failed to deserialize component {} with value: {:?}", "failed to deserialize component {} with value: {:?}",
key, value name, value
) )
}); });
@ -64,5 +83,52 @@ pub fn ronstring_to_reflect_component(
warn!("no type registration for {}", capitalized_type_name); warn!("no type registration for {}", capitalized_type_name);
} }
} }
components
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;
match value.clone() {
Value::String(str) => {
parsed_value = str;
}
_ => parsed_value = ron::to_string(&value).unwrap().to_string(),
}
if let Some(type_registration) =
type_registry.get_with_type_path(key.as_str())
{
debug!("TYPE INFO {:?}", type_registration.type_info());
let ron_string = format!(
"{{ \"{}\":{} }}",
type_registration.type_info().type_path(),
parsed_value
);
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: {:?}",
key, value
)
});
debug!("component {:?}", component);
debug!("real type {:?}", component.get_represented_type_info());
components.push((component, type_registration.clone()));
debug!("found type registration for {}", key);
} else {
warn!("no type registration for {}", key);
}
}
} }