From 2ecb178a7c318e1ef43867fa25832528f1fc9262 Mon Sep 17 00:00:00 2001 From: pyrotechnick <13998+pyrotechnick@users.noreply.github.com> Date: Mon, 26 Jun 2023 08:07:56 +1000 Subject: [PATCH 1/5] Migrate Quat reflection from Value to Struct --- crates/bevy_reflect/src/impls/glam.rs | 44 +++++++++---------- crates/bevy_reflect/src/lib.rs | 61 ++++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 24 deletions(-) diff --git a/crates/bevy_reflect/src/impls/glam.rs b/crates/bevy_reflect/src/impls/glam.rs index 4a3f50cc62b6f..a03cdbae6bb32 100644 --- a/crates/bevy_reflect/src/impls/glam.rs +++ b/crates/bevy_reflect/src/impls/glam.rs @@ -1,7 +1,6 @@ use crate as bevy_reflect; use crate::prelude::ReflectDefault; -use crate::{ReflectDeserialize, ReflectSerialize}; -use bevy_reflect_derive::{impl_from_reflect_value, impl_reflect_struct, impl_reflect_value}; +use bevy_reflect_derive::{impl_reflect_struct, impl_reflect_value}; use glam::*; impl_reflect_struct!( @@ -251,27 +250,26 @@ impl_reflect_struct!( } ); -// Quat fields are read-only (as of now), and reflection is currently missing -// mechanisms for read-only fields. I doubt those mechanisms would be added, -// so for now quaternions will remain as values. They are represented identically -// to Vec4 and DVec4, so you may use those instead and convert between. -impl_reflect_value!(::glam::Quat( - Debug, - PartialEq, - Serialize, - Deserialize, - Default -)); -impl_reflect_value!(::glam::DQuat( - Debug, - PartialEq, - Serialize, - Deserialize, - Default -)); - -impl_from_reflect_value!(Quat); -impl_from_reflect_value!(DQuat); +impl_reflect_struct!( + #[reflect(Debug, PartialEq, Default)] + #[type_path = "glam"] + struct Quat { + x: f32, + y: f32, + z: f32, + w: f32, + } +); +impl_reflect_struct!( + #[reflect(Debug, PartialEq, Default)] + #[type_path = "glam"] + struct DQuat { + x: f64, + y: f64, + z: f64, + w: f64, + } +); impl_reflect_value!(::glam::EulerRot(Debug, Default)); impl_reflect_value!(::glam::BVec3A(Debug, Default)); diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index 9bdd043810b72..3cb1884db84ac 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -545,7 +545,7 @@ pub mod __macro_exports { #[allow(clippy::disallowed_types, clippy::approx_constant)] mod tests { #[cfg(feature = "glam")] - use ::glam::{vec3, Vec3}; + use ::glam::{quat, vec3, Quat, Vec3}; use ::serde::{de::DeserializeSeed, Deserialize, Serialize}; use bevy_utils::HashMap; use ron::{ @@ -1852,6 +1852,65 @@ bevy_reflect::tests::should_reflect_debug::Test { mod glam { use super::*; + #[test] + fn quat_serialization() { + let q = quat(1.0, 2.0, 3.0, 4.0); + + let mut registry = TypeRegistry::default(); + registry.register::(); + registry.register::(); + + let ser = ReflectSerializer::new(&q, ®istry); + + let config = PrettyConfig::default() + .new_line(String::from("\n")) + .indentor(String::from(" ")); + let output = to_string_pretty(&ser, config).unwrap(); + let expected = r#" +{ + "glam::f32::scalar::quat::Quat": ( + x: 1.0, + y: 2.0, + z: 3.0, + w: 4.0, + ), +}"#; + + assert_eq!(expected, format!("\n{output}")); + } + + #[test] + fn quat_deserialization() { + let data = r#" +{ + "glam::f32::scalar::quat::Quat": ( + x: 1.0, + y: 2.0, + z: 3.0, + w: 4.0, + ), +}"#; + + let mut registry = TypeRegistry::default(); + registry.add_registration(Quat::get_type_registration()); + registry.add_registration(f32::get_type_registration()); + + let de = UntypedReflectDeserializer::new(®istry); + + let mut deserializer = + ron::de::Deserializer::from_str(data).expect("Failed to acquire deserializer"); + + let dynamic_struct = de + .deserialize(&mut deserializer) + .expect("Failed to deserialize"); + + let mut result = Quat::default(); + + result.apply(&*dynamic_struct); + + assert_eq!(result, quat(1.0, 2.0, 3.0, 4.0)); + } + #[test] fn vec3_serialization() { let v = vec3(12.0, 3.0, -6.9); From ad692cf881d65b90152d8ebdbbc08679547b5bd3 Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Mon, 9 Oct 2023 23:04:48 +0200 Subject: [PATCH 2/5] cargo fmt --- crates/bevy_reflect/src/impls/glam.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/bevy_reflect/src/impls/glam.rs b/crates/bevy_reflect/src/impls/glam.rs index 3b241a5e7626c..64b3a1ea5220a 100644 --- a/crates/bevy_reflect/src/impls/glam.rs +++ b/crates/bevy_reflect/src/impls/glam.rs @@ -330,7 +330,6 @@ impl_reflect_struct!( } ); - impl_reflect_value!(::glam::EulerRot(Debug, Default)); impl_reflect_value!(::glam::BVec3A(Debug, Default)); impl_reflect_value!(::glam::BVec4A(Debug, Default)); From 882eefd1bbb175341b1feede176ddb334db10060 Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Mon, 9 Oct 2023 23:06:56 +0200 Subject: [PATCH 3/5] shortened add_registration to register --- crates/bevy_reflect/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index 8401c37831649..a64182aaac7e2 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -1988,8 +1988,8 @@ bevy_reflect::tests::should_reflect_debug::Test { }"#; let mut registry = TypeRegistry::default(); - registry.add_registration(Quat::get_type_registration()); - registry.add_registration(f32::get_type_registration()); + registry.register::(); + registry.register::(); let de = UntypedReflectDeserializer::new(®istry); From 65b29f805dfbec748de2be199261002774a409ec Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Mon, 9 Oct 2023 23:28:01 +0200 Subject: [PATCH 4/5] fixed tests --- crates/bevy_reflect/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index a64182aaac7e2..7f5b880c125a3 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -1964,7 +1964,7 @@ bevy_reflect::tests::should_reflect_debug::Test { let output = to_string_pretty(&ser, config).unwrap(); let expected = r#" { - "glam::f32::scalar::quat::Quat": ( + "glam::f32::sse2::quat::Quat": ( x: 1.0, y: 2.0, z: 3.0, @@ -1979,7 +1979,7 @@ bevy_reflect::tests::should_reflect_debug::Test { fn quat_deserialization() { let data = r#" { - "glam::f32::scalar::quat::Quat": ( + "glam::f32::sse2::quat::Quat": ( x: 1.0, y: 2.0, z: 3.0, From 613dfc611a182c99c6554890e7f28285c53deab5 Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Mon, 9 Oct 2023 23:36:56 +0200 Subject: [PATCH 5/5] Fixed tests again? --- crates/bevy_reflect/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index 7f5b880c125a3..25e8eba626498 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -1964,7 +1964,7 @@ bevy_reflect::tests::should_reflect_debug::Test { let output = to_string_pretty(&ser, config).unwrap(); let expected = r#" { - "glam::f32::sse2::quat::Quat": ( + "glam::Quat": ( x: 1.0, y: 2.0, z: 3.0, @@ -1979,7 +1979,7 @@ bevy_reflect::tests::should_reflect_debug::Test { fn quat_deserialization() { let data = r#" { - "glam::f32::sse2::quat::Quat": ( + "glam::Quat": ( x: 1.0, y: 2.0, z: 3.0,