Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use FromReflect when extracting entities in dynamic scenes #15174

Merged
merged 9 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions crates/bevy_scene/src/dynamic_scene_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy_ecs::{
reflect::{AppTypeRegistry, ReflectComponent, ReflectResource},
world::World,
};
use bevy_reflect::PartialReflect;
use bevy_reflect::{PartialReflect, ReflectFromReflect};
use bevy_utils::default;
use std::collections::BTreeMap;

Expand Down Expand Up @@ -274,11 +274,22 @@ impl<'w> DynamicSceneBuilder<'w> {
return None;
}

let component = type_registry
.get(type_id)?
let type_registration = type_registry.get(type_id)?;

let component = type_registration
.data::<ReflectComponent>()?
.reflect(original_entity)?;
entry.components.push(component.clone_value());

// Clone via `FromReflect`. Unlike `PartialReflect::clone_value` this
// retains the original type and `ReflectSerialize` type data which is needed to
// deserialize.
let component = type_registration
.data::<ReflectFromReflect>()
.and_then(|fr| fr.from_reflect(component.as_partial_reflect()))
.map(PartialReflect::into_partial_reflect)
.unwrap_or_else(|| component.clone_value());

entry.components.push(component);
Some(())
};
extract_and_push();
Expand Down
91 changes: 72 additions & 19 deletions crates/bevy_scene/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
use crate::{DynamicEntity, DynamicScene};
use bevy_ecs::entity::Entity;
use bevy_reflect::serde::{TypedReflectDeserializer, TypedReflectSerializer};
use bevy_reflect::PartialReflect;
use bevy_reflect::{
serde::{ReflectDeserializer, TypeRegistrationDeserializer},
TypeRegistry,
};
use bevy_reflect::{PartialReflect, ReflectFromReflect};
use bevy_utils::HashSet;
use serde::ser::SerializeMap;
use serde::{
Expand Down Expand Up @@ -471,9 +471,19 @@ impl<'a, 'de> Visitor<'de> for SceneMapVisitor<'a> {
)));
}

entries.push(
map.next_value_seed(TypedReflectDeserializer::new(registration, self.registry))?,
);
let value =
map.next_value_seed(TypedReflectDeserializer::new(registration, self.registry))?;

// Attempt to convert using FromReflect.
let value = self
.registry
.get(registration.type_id())
.and_then(|tr| tr.data::<ReflectFromReflect>())
.and_then(|fr| fr.from_reflect(value.as_partial_reflect()))
.map(PartialReflect::into_partial_reflect)
.unwrap_or(value);

entries.push(value);
}

Ok(entries)
Expand All @@ -491,10 +501,10 @@ mod tests {
use bevy_ecs::query::{With, Without};
use bevy_ecs::reflect::{AppTypeRegistry, ReflectMapEntities};
use bevy_ecs::world::FromWorld;
use bevy_reflect::{Reflect, ReflectSerialize};
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
use bincode::Options;
use serde::de::DeserializeSeed;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use std::io::BufReader;

#[derive(Component, Reflect, Default)]
Expand All @@ -507,6 +517,30 @@ mod tests {
#[reflect(Component)]
struct Baz(i32);

// De/serialize as hex.
mod qux {
use serde::{de::Error, Deserialize, Deserializer, Serializer};

pub fn serialize<S>(value: &u32, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&format!("{:X}", value))
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<u32, D::Error>
where
D: Deserializer<'de>,
{
u32::from_str_radix(<&str as Deserialize>::deserialize(deserializer)?, 16)
.map_err(Error::custom)
}
}

#[derive(Component, Copy, Clone, Reflect, Debug, PartialEq, Serialize, Deserialize)]
#[reflect(Component, Serialize, Deserialize)]
struct Qux(#[serde(with = "qux")] u32);

#[derive(Component, Reflect, Default)]
#[reflect(Component)]
struct MyComponent {
Expand Down Expand Up @@ -555,6 +589,7 @@ mod tests {
registry.register::<Foo>();
registry.register::<Bar>();
registry.register::<Baz>();
registry.register::<Qux>();
registry.register::<MyComponent>();
registry.register::<MyEnum>();
registry.register::<String>();
Expand Down Expand Up @@ -679,6 +714,18 @@ mod tests {
assert_eq!(1, dst_world.query::<&Baz>().iter(&dst_world).count());
}

fn roundtrip_ron(world: &World) -> (DynamicScene, DynamicScene) {
let scene = DynamicScene::from_world(&world);
let registry = world.resource::<AppTypeRegistry>().read();
let serialized = scene.serialize(&registry).unwrap();
let mut deserializer = ron::de::Deserializer::from_str(&serialized).unwrap();
let scene_deserializer = SceneDeserializer {
type_registry: &registry,
};
let deserialized_scene = scene_deserializer.deserialize(&mut deserializer).unwrap();
(scene, deserialized_scene)
}

#[test]
fn should_roundtrip_with_later_generations_and_obsolete_references() {
let mut world = create_world();
Expand All @@ -690,19 +737,7 @@ mod tests {
world.despawn(a);
world.spawn(MyEntityRef(foo)).insert(Bar(123));

let registry = world.resource::<AppTypeRegistry>();

let scene = DynamicScene::from_world(&world);

let serialized = scene
.serialize(&world.resource::<AppTypeRegistry>().read())
.unwrap();
let mut deserializer = ron::de::Deserializer::from_str(&serialized).unwrap();
let scene_deserializer = SceneDeserializer {
type_registry: &registry.0.read(),
};

let deserialized_scene = scene_deserializer.deserialize(&mut deserializer).unwrap();
let (scene, deserialized_scene) = roundtrip_ron(&world);

let mut map = EntityHashMap::default();
let mut dst_world = create_world();
Expand Down Expand Up @@ -730,6 +765,24 @@ mod tests {
.all(|r| world.get_entity(r.0).is_none()));
}

#[test]
fn should_roundtrip_with_custom_serialization() {
let mut world = create_world();
let qux = Qux(42);
world.spawn(qux);

let (scene, deserialized_scene) = roundtrip_ron(&world);

assert_eq!(1, deserialized_scene.entities.len());
assert_scene_eq(&scene, &deserialized_scene);

let mut world = create_world();
deserialized_scene
.write_to_world(&mut world, &mut EntityHashMap::default())
.unwrap();
assert_eq!(&qux, world.query::<&Qux>().single(&world))
}

#[test]
fn should_roundtrip_postcard() {
let mut world = create_world();
Expand Down
Loading