refactor(gltf_to_components): removed dependency on serde-json , & overhauled code accordinly

This commit is contained in:
kaosat.dev 2023-07-27 22:22:04 +02:00
parent d37e38ada1
commit a2b4025e95
3 changed files with 15 additions and 59 deletions

1
Cargo.lock generated
View File

@ -1217,7 +1217,6 @@ dependencies = [
"bevy_rapier3d", "bevy_rapier3d",
"ron", "ron",
"serde", "serde",
"serde_json",
] ]
[[package]] [[package]]

View File

@ -7,11 +7,10 @@ edition = "2021"
[dependencies] [dependencies]
bevy="0.11.0" bevy="0.11.0"
bevy_editor_pls = { git="https://github.com/jakobhellermann/bevy_editor_pls.git" }
bevy_rapier3d = { version = "0.22.0", features = [ "serde-serialize", "debug-render-3d", "enhanced-determinism"] }
serde_json = "1.0"
serde = "*" serde = "*"
ron="*" ron="*"
bevy_rapier3d = { version = "0.22.0", features = [ "serde-serialize", "debug-render-3d", "enhanced-determinism"] }
bevy_editor_pls = { git="https://github.com/jakobhellermann/bevy_editor_pls.git" }
#### --------------------Dev/ debug------------------------------- #### --------------------Dev/ debug-------------------------------
# Enable high optimizations for dependencies (incl. Bevy), but not for our code: # Enable high optimizations for dependencies (incl. Bevy), but not for our code:

View File

@ -1,8 +1,7 @@
use std::any::TypeId;
use std::collections::HashMap; use std::collections::HashMap;
use core::ops::Deref; use core::ops::Deref;
use serde_json::Value; use ron::Value;
use serde::de::DeserializeSeed; use serde::de::DeserializeSeed;
use bevy::prelude::*; use bevy::prelude::*;
@ -110,7 +109,6 @@ pub fn gltf_extras_to_components(
info!("done extracting gltf_extras /n"); info!("done extracting gltf_extras /n");
} }
pub fn ronstring_to_reflect_component( pub fn ronstring_to_reflect_component(
ron_string: &String, ron_string: &String,
type_registry: &TypeRegistryInternal type_registry: &TypeRegistryInternal
@ -122,12 +120,18 @@ pub fn gltf_extras_to_components(
let capitalized_type_name = capitalize_first_letter(type_string.as_str()); let capitalized_type_name = capitalize_first_letter(type_string.as_str());
// println!("capitalized_type_name {}", capitalized_type_name); // println!("capitalized_type_name {}", capitalized_type_name);
let mut parsed_value = format!("{}", value); let mut parsed_value:String;
parsed_value = ron::from_str(parsed_value.as_str()).unwrap_or(parsed_value); match value.clone() {
Value::String(str) => {
parsed_value = str;
}
_=> {
parsed_value = format!("{}", ron::to_string(&value).unwrap() )
}
}
if let Some(type_registration) = type_registry.get_with_short_name(capitalized_type_name.as_str()) { if let Some(type_registration) = type_registry.get_with_short_name(capitalized_type_name.as_str()) {
// println!("TYPE INFO {:?}", type_registration.type_info()); // println!("TYPE INFO {:?}", type_registration.type_info());
match type_registration.type_info() { match type_registration.type_info() {
TypeInfo::TupleStruct (info) => { TypeInfo::TupleStruct (info) => {
// we handle tupple strucs with only one field differently, as Blender's custom properties with custom ui (float, int, bool, etc) always give us a tupple struct // we handle tupple strucs with only one field differently, as Blender's custom properties with custom ui (float, int, bool, etc) always give us a tupple struct
@ -172,7 +176,6 @@ pub fn gltf_extras_to_components(
"u128" => { "u128" => {
formated = parsed_value.parse::<u128>().unwrap().to_string(); formated = parsed_value.parse::<u128>().unwrap().to_string();
} }
"glam::f32::vec2::Vec2" => { "glam::f32::vec2::Vec2" => {
//println!("WE HAVE A VEC2 {}", parsed_value); //println!("WE HAVE A VEC2 {}", parsed_value);
let parsed: Vec<f32> = ron::from_str(&parsed_value).unwrap(); let parsed: Vec<f32> = ron::from_str(&parsed_value).unwrap();
@ -199,64 +202,19 @@ pub fn gltf_extras_to_components(
} }
} }
_ => { _ => {}
}
} }
// println!("parsed value {}",parsed_value); // println!("parsed value {}",parsed_value);
if parsed_value == "" { if parsed_value == "" {
parsed_value = "()".to_string(); parsed_value = "()".to_string();
} }
/*else if parsed_value.starts_with("[") && parsed_value.ends_with("]") {
// FIXME/ horrible, and how about actual vec!s and not vec2/vec3 s?
// the worst part is, we have the component information, so we SHOULD be able to infer the input struct type
// perhaps use better logic than the unwrap ? we could do parsed:Vec<u64> as well, and drop it otherwise ?
let parsed: Vec<f32> = ron::from_str(&parsed_value).unwrap();
if parsed.len() == 2 {
parsed_value = format!("((x:{},y:{}))", parsed[0], parsed[1]);
}
// FIXME god awfull hacks
if parsed.len() == 3 && !key.to_lowercase().contains("color"){
parsed_value = format!("((x:{},y:{},z:{}))", parsed[0], parsed[1], parsed[2]);
}
if parsed.len() == 3 && key.to_lowercase().contains("color"){
parsed_value = format!("(Rgba(red:{},green:{},blue:{}, alpha: 1.0))", parsed[0], parsed[1], parsed[2]);
}
if parsed.len() == 4 && !key.to_lowercase().contains("color") {
parsed_value = format!("((x:{},y:{},z:{},w:{}))", parsed[0], parsed[1], parsed[2], parsed[3]);
}
if parsed.len() == 4 && key.to_lowercase().contains("color"){
parsed_value = format!("(Rgba(red:{},green:{},blue:{}, alpha:{}))", parsed[0], parsed[1], parsed[2], parsed[3]);
}
}
// FIXME: inneficient
else if parsed_value.starts_with("\"") && parsed_value.ends_with("\"") {
parsed_value = format!("({})",parsed_value);
}
else {
// FIXME: inneficient
if let Ok(parsed) = parsed_value.parse::<f32>() {
parsed_value = format!("({})",parsed);
}
else if let Ok(parsed) = parsed_value.parse::<f64>() {
parsed_value = format!("({})",parsed);
}
else if let Ok(parsed) = parsed_value.parse::<u64>() {
parsed_value = format!("({})",parsed);
}
else if let Ok(parsed) = parsed_value.parse::<bool>() {
parsed_value = format!("({})",parsed);
}
}*/
let ron_string = format!("{{ \"{}\":{} }}", let ron_string = format!("{{ \"{}\":{} }}",
type_registration.type_name(), type_registration.type_name(),
parsed_value parsed_value
); );
// usefull to determine what an entity looks like Serialized // usefull to determine what an entity looks like Serialized
/*let test_struct = TuppleTestStr::default(); /*let test_struct = TuppleTestStr::default();
let serializer = ReflectSerializer::new(&test_struct, &type_registry); let serializer = ReflectSerializer::new(&test_struct, &type_registry);
@ -267,7 +225,7 @@ pub fn gltf_extras_to_components(
println!("component data ron string {}", ron_string); println!("component data ron string {}", ron_string);
let mut deserializer = ron::Deserializer::from_str(ron_string.as_str()).unwrap(); let mut deserializer = ron::Deserializer::from_str(ron_string.as_str()).unwrap();
let reflect_deserializer = UntypedReflectDeserializer::new(&type_registry); let reflect_deserializer = UntypedReflectDeserializer::new(&type_registry);
let component = reflect_deserializer.deserialize(&mut deserializer).expect(format!("failed to deserialize component {} with value: {}", key, value).as_str()); let component = reflect_deserializer.deserialize(&mut deserializer).expect(format!("failed to deserialize component {} with value: {:?}", key, value).as_str());
components.push(component); components.push(component);
debug!("found type registration for {}", capitalized_type_name); debug!("found type registration for {}", capitalized_type_name);