Revert Align Scene::write_to_world_with to match DynamicScene::write_to_world_with (#13800)

# Objective

- https://github.com/bevyengine/bevy/pull/13714 broke scenes pretty
seriously
- Fixes https://github.com/bevyengine/bevy/issues/13796

## Solution

Revert it. We can redo this PR once the behavior is fixed.

Co-authored-by: Dmytro Banin <dima_banin@hotmail.com>
This commit is contained in:
Alice Cecile 2024-06-10 17:14:11 -04:00 committed by GitHub
parent fd30e0c67d
commit 54010cc07e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 30 deletions

View File

@ -1,6 +1,6 @@
use crate::{DynamicScene, SceneSpawnError}; use crate::{DynamicScene, InstanceInfo, SceneSpawnError};
use bevy_asset::Asset; use bevy_asset::Asset;
use bevy_ecs::entity::{Entity, EntityHashMap}; use bevy_ecs::entity::EntityHashMap;
use bevy_ecs::{ use bevy_ecs::{
reflect::{AppTypeRegistry, ReflectComponent, ReflectMapEntities, ReflectResource}, reflect::{AppTypeRegistry, ReflectComponent, ReflectMapEntities, ReflectResource},
world::World, world::World,
@ -43,8 +43,7 @@ impl Scene {
/// provided [`AppTypeRegistry`] or doesn't reflect the [`Component`](bevy_ecs::component::Component) trait. /// provided [`AppTypeRegistry`] or doesn't reflect the [`Component`](bevy_ecs::component::Component) trait.
pub fn clone_with(&self, type_registry: &AppTypeRegistry) -> Result<Scene, SceneSpawnError> { pub fn clone_with(&self, type_registry: &AppTypeRegistry) -> Result<Scene, SceneSpawnError> {
let mut new_world = World::new(); let mut new_world = World::new();
let mut entity_map = EntityHashMap::default(); self.write_to_world_with(&mut new_world, type_registry)?;
self.write_to_world_with(&mut new_world, &mut entity_map, type_registry)?;
Ok(Self { world: new_world }) Ok(Self { world: new_world })
} }
@ -55,9 +54,12 @@ impl Scene {
pub fn write_to_world_with( pub fn write_to_world_with(
&self, &self,
world: &mut World, world: &mut World,
entity_map: &mut EntityHashMap<Entity>,
type_registry: &AppTypeRegistry, type_registry: &AppTypeRegistry,
) -> Result<(), SceneSpawnError> { ) -> Result<InstanceInfo, SceneSpawnError> {
let mut instance_info = InstanceInfo {
entity_map: EntityHashMap::default(),
};
let type_registry = type_registry.read(); let type_registry = type_registry.read();
// Resources archetype // Resources archetype
@ -92,7 +94,8 @@ impl Scene {
for archetype in self.world.archetypes().iter() { for archetype in self.world.archetypes().iter() {
for scene_entity in archetype.entities() { for scene_entity in archetype.entities() {
let entity = entity_map let entity = *instance_info
.entity_map
.entry(scene_entity.id()) .entry(scene_entity.id())
.or_insert_with(|| world.spawn_empty().id()); .or_insert_with(|| world.spawn_empty().id());
for component_id in archetype.components() { for component_id in archetype.components() {
@ -118,7 +121,7 @@ impl Scene {
&self.world, &self.world,
world, world,
scene_entity.id(), scene_entity.id(),
*entity, entity,
&type_registry, &type_registry,
); );
} }
@ -127,10 +130,10 @@ impl Scene {
for registration in type_registry.iter() { for registration in type_registry.iter() {
if let Some(map_entities_reflect) = registration.data::<ReflectMapEntities>() { if let Some(map_entities_reflect) = registration.data::<ReflectMapEntities>() {
map_entities_reflect.map_all_entities(world, entity_map); map_entities_reflect.map_all_entities(world, &mut instance_info.entity_map);
} }
} }
Ok(()) Ok(instance_info)
} }
} }

View File

@ -221,7 +221,6 @@ impl SceneSpawner {
let scene = scenes let scene = scenes
.get(id) .get(id)
.ok_or(SceneSpawnError::NonExistentScene { id })?; .ok_or(SceneSpawnError::NonExistentScene { id })?;
scene.write_to_world(world, entity_map) scene.write_to_world(world, entity_map)
}) })
} }
@ -230,33 +229,27 @@ impl SceneSpawner {
pub fn spawn_sync( pub fn spawn_sync(
&mut self, &mut self,
world: &mut World, world: &mut World,
id: impl Into<AssetId<Scene>>, id: AssetId<Scene>,
) -> Result<InstanceId, SceneSpawnError> { ) -> Result<InstanceId, SceneSpawnError> {
let mut entity_map = EntityHashMap::default(); self.spawn_sync_internal(world, id, InstanceId::new())
let id = id.into();
Self::spawn_sync_internal(world, id, &mut entity_map)?;
let instance_id = InstanceId::new();
self.spawned_instances
.insert(instance_id, InstanceInfo { entity_map });
Ok(instance_id)
} }
fn spawn_sync_internal( fn spawn_sync_internal(
// &mut self, &mut self,
world: &mut World, world: &mut World,
id: AssetId<Scene>, id: AssetId<Scene>,
entity_map: &mut EntityHashMap<Entity>, instance_id: InstanceId,
) -> Result<(), SceneSpawnError> { ) -> Result<InstanceId, SceneSpawnError> {
world.resource_scope(|world, scenes: Mut<Assets<Scene>>| { world.resource_scope(|world, scenes: Mut<Assets<Scene>>| {
let scene = scenes let scene = scenes
.get(id) .get(id)
.ok_or(SceneSpawnError::NonExistentRealScene { id })?; .ok_or(SceneSpawnError::NonExistentRealScene { id })?;
scene.write_to_world_with( let instance_info =
world, scene.write_to_world_with(world, &world.resource::<AppTypeRegistry>().clone())?;
entity_map,
&world.resource::<AppTypeRegistry>().clone(), self.spawned_instances.insert(instance_id, instance_info);
) Ok(instance_id)
}) })
} }
@ -326,9 +319,7 @@ impl SceneSpawner {
let scenes_to_spawn = std::mem::take(&mut self.scenes_to_spawn); let scenes_to_spawn = std::mem::take(&mut self.scenes_to_spawn);
for (scene_handle, instance_id) in scenes_to_spawn { for (scene_handle, instance_id) in scenes_to_spawn {
let mut entity_map = EntityHashMap::default(); match self.spawn_sync_internal(world, scene_handle.id(), instance_id) {
match Self::spawn_sync_internal(world, scene_handle.id(), &mut entity_map) {
Ok(_) => {} Ok(_) => {}
Err(SceneSpawnError::NonExistentRealScene { .. }) => { Err(SceneSpawnError::NonExistentRealScene { .. }) => {
self.scenes_to_spawn.push((scene_handle, instance_id)); self.scenes_to_spawn.push((scene_handle, instance_id));