bevy_save 2.0.1+4

A framework for saving and loading application state in Bevy.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use std::any::TypeId;

use bevy::{
    ecs::{
        component::ComponentCloneBehavior,
        entity::{
            EntityHashMap,
            SceneEntityMapper,
        },
        query::QueryFilter,
        reflect::ReflectMapEntities,
        relationship::RelationshipHookMode,
        system::EntityCommands,
        world::{
            CommandQueue,
            EntityRef,
        },
    },
    platform::collections::HashMap,
    prelude::*,
    reflect::TypeRegistry,
    scene::SceneSpawnError,
};

use crate::{
    clone_reflect_value,
    error::Error,
    prelude::*,
    utils::{
        MaybeMut,
        MaybeRef,
    },
};

/// A [`Hook`] runs on each entity when applying a snapshot.
///
/// # Example
/// This could be used to apply entities as children of another entity.
/// ```
/// # use bevy::prelude::*;
/// # use bevy_save::prelude::*;
/// # let mut app = App::new();
/// # app.add_plugins(MinimalPlugins);
/// # app.add_plugins(SavePlugins);
/// # let world = app.world_mut();
/// # let snapshot = Snapshot::from_world(world);
/// # let parent = world.spawn_empty().id();
/// snapshot
///     .applier(world)
///     .hook(move |entity, cmds| {
///         if !entity.contains::<ChildOf>() {
///             cmds.insert(ChildOf(parent));
///         }
///     })
///     .apply();
/// ```
pub trait Hook: for<'a> Fn(&'a EntityRef, &'a mut EntityCommands) + Send + Sync {}

impl<T> Hook for T where T: for<'a> Fn(&'a EntityRef, &'a mut EntityCommands) + Send + Sync {}

/// A boxed [`Hook`].
pub type BoxedHook = Box<dyn Hook>;

type SpawnPrefabFn = fn(Box<dyn PartialReflect>, Entity, &mut World);

/// Input used for applying [`Snapshot`] to the [`World`].
pub struct Applier<'a> {
    pub(crate) snapshot: MaybeRef<'a, Snapshot>,
    entity_map: Option<MaybeMut<'a, EntityHashMap<Entity>>>,
    type_registry: Option<MaybeRef<'a, TypeRegistry>>,
    despawns: Vec<fn(&mut World)>,
    hooks: Vec<BoxedHook>,
    prefabs: HashMap<TypeId, SpawnPrefabFn>,
}

impl<'a> Applier<'a> {
    /// Create a new [`Applier`] from the given borrowed or owned [`Snapshot`]
    #[must_use]
    pub fn new(snapshot: impl Into<MaybeRef<'a, Snapshot>>) -> Self {
        Self {
            snapshot: snapshot.into(),
            entity_map: None,
            type_registry: None,
            despawns: Vec::new(),
            hooks: Vec::new(),
            prefabs: HashMap::new(),
        }
    }
}

impl<'i> Applier<'i> {
    /// Creates a temporary, scoped applier from the input.
    #[must_use]
    pub fn scope<'w>(
        self,
        world: &'w mut World,
        scope: impl Fn(ApplierRef<'w, 'i>) -> ApplierRef<'w, 'i>,
    ) -> Self
    where
        'i: 'w,
    {
        scope(ApplierRef::from_parts(world, self)).input
    }
}

/// [`ApplierRef`] lets you configure how a snapshot will be applied to the [`World`].
pub struct ApplierRef<'w, 'i> {
    world: &'w mut World,
    input: Applier<'i>,
}

impl<'w, 'i> ApplierRef<'w, 'i> {
    /// Create a new [`ApplierRef`] from the world and borrowed or owned [`Snapshot`].
    #[must_use]
    pub fn new(snapshot: impl Into<MaybeRef<'i, Snapshot>>, world: &'w mut World) -> Self {
        Self {
            world,
            input: Applier::new(snapshot),
        }
    }

    /// Create a new [`ApplierRef`] from the world and input.
    #[must_use]
    pub fn from_parts(world: &'w mut World, input: Applier<'i>) -> Self {
        Self { world, input }
    }

    /// Reduce the applier into its input
    #[must_use]
    pub fn into_inner(self) -> Applier<'i> {
        self.input
    }
}

impl<'i> ApplierRef<'_, 'i> {
    /// Providing an entity map allows you to map ids of spawned entities and
    /// see what entities have been spawned.
    #[must_use]
    pub fn entity_map(
        mut self,
        entity_map: impl Into<MaybeMut<'i, EntityHashMap<Entity>>>,
    ) -> Self {
        self.input.entity_map = Some(entity_map.into());
        self
    }

    /// Set the [`TypeRegistry`] to be used for reflection.
    ///
    /// If this is not provided, the [`AppTypeRegistry`] resource is used as a default.
    #[must_use]
    pub fn type_registry(mut self, type_registry: &'i TypeRegistry) -> Self {
        self.input.type_registry = Some(type_registry.into());
        self
    }

    /// Despawn existing entities matching the filter while applying.
    #[must_use]
    pub fn despawn<F: QueryFilter + 'static>(mut self) -> Self {
        self.input.despawns.push(|w| {
            for entity in w.query_filtered::<Entity, F>().iter(w).collect::<Vec<_>>() {
                w.despawn(entity);
            }
        });
        self
    }

    /// Add a [`Hook`] that will run for each entity after applying.
    #[must_use]
    pub fn hook(mut self, hook: impl Hook + 'static) -> Self {
        self.input.hooks.push(Box::new(hook));
        self
    }

    /// Handle loading for a [`Prefab`].
    #[expect(clippy::missing_panics_doc)]
    #[must_use]
    pub fn prefab<P: Prefab + FromReflect>(mut self) -> Self {
        self.input.prefabs.insert(
            std::any::TypeId::of::<P>(),
            |this: Box<dyn PartialReflect>, target: Entity, world: &mut World| {
                world.entity_mut(target).insert(P::Marker::default());

                P::spawn(
                    <P as FromReflect>::from_reflect(&*this).unwrap(),
                    target,
                    world,
                );
            },
        );
        self
    }
}

struct MapEntitiesMapper<'m, 'w> {
    map: &'m mut EntityHashMap<Entity>,
    world: &'w mut World,
}

impl<'m, 'w> MapEntitiesMapper<'m, 'w> {
    fn new(map: &'m mut EntityHashMap<Entity>, world: &'w mut World) -> Self {
        Self { map, world }
    }
}

impl EntityMapper for MapEntitiesMapper<'_, '_> {
    fn get_mapped(&mut self, source: Entity) -> Entity {
        *self
            .map
            .entry(source)
            .or_insert_with(|| self.world.spawn_empty().id())
    }

    fn set_mapped(&mut self, source: Entity, target: Entity) {
        self.map.insert(source, target);
    }
}

impl Drop for MapEntitiesMapper<'_, '_> {
    fn drop(&mut self) {
        // WORKAROUND: We've already mapped, don't do it again.
        for mapped in self.map.values().copied().collect::<Vec<_>>() {
            self.map.insert(mapped, mapped);
        }
    }
}

impl ApplierRef<'_, '_> {
    /// Apply the [`Snapshot`] to the [`World`].
    ///
    /// # Panics
    /// If `type_registry` is not set or the [`AppTypeRegistry`] resource does not exist.
    ///
    /// # Errors
    /// If a type included in the [`Snapshot`] has not been registered with the type registry.
    pub fn apply(&mut self) -> Result<(), Error> {
        let app_type_registry_arc = self.world.get_resource::<AppTypeRegistry>().cloned();

        let app_type_registry = app_type_registry_arc.as_ref().map(|r| r.read());

        let type_registry = self
            .input
            .type_registry
            .as_deref()
            .or(app_type_registry.as_deref())
            .expect("Must set `type_registry` or insert `AppTypeRegistry` resource to apply.");

        let entity_map = self.input.entity_map.get_or_insert_default();

        let mut prefab_entities = HashMap::new();

        // Despawn entities
        for despawn in &self.input.despawns {
            despawn(self.world);
        }

        // First ensure that every entity in the snapshot has a corresponding world
        // entity in the entity map.
        for scene_entity in self.input.snapshot.entities() {
            // Fetch the entity with the given entity id from the `entity_map`
            // or spawn a new entity with a transiently unique id if there is
            // no corresponding entry.
            entity_map
                .entry(scene_entity.entity)
                .or_insert_with(|| self.world.spawn_empty().id());
        }

        for scene_entity in self.input.snapshot.entities() {
            // Fetch the entity with the given entity id from the `entity_map`.
            let entity = *entity_map
                .get(&scene_entity.entity)
                .expect("should have previously spawned an empty entity");

            // Apply/ add each component to the given entity.
            for component in &scene_entity.components {
                let type_info = component.get_represented_type_info().ok_or_else(|| {
                    SceneSpawnError::NoRepresentedType {
                        type_path: component.reflect_type_path().to_string(),
                    }
                })?;
                let type_id = type_info.type_id();
                let registration = type_registry.get(type_id).ok_or_else(|| {
                    SceneSpawnError::UnregisteredButReflectedType {
                        type_path: type_info.type_path().to_string(),
                    }
                })?;

                if self.input.prefabs.contains_key(&type_id) {
                    let mut prefab = clone_reflect_value(&**component, type_registry);

                    if let Some(map_entities) = registration.data::<ReflectMapEntities>() {
                        map_entities.map_entities(
                            &mut *prefab,
                            &mut MapEntitiesMapper::new(entity_map, self.world),
                        );
                    }

                    prefab_entities
                        .entry(type_id)
                        .or_insert_with(Vec::new)
                        .push((entity, prefab));

                    continue;
                }

                let reflect = registration.data::<ReflectComponent>().ok_or_else(|| {
                    SceneSpawnError::UnregisteredComponent {
                        type_path: type_info.type_path().to_string(),
                    }
                })?;

                {
                    let component_id = reflect.register_component(self.world);
                    // SAFETY: we registered the component above. the info exists
                    let component_info =
                        unsafe { self.world.components().get_info_unchecked(component_id) };
                    if *component_info.clone_behavior() == ComponentCloneBehavior::Ignore {
                        continue;
                    }
                }

                let mut cloned = None;

                // If this component references entities in the scene, update
                // them to the entities in the world.
                let component = registration
                    .data::<ReflectMapEntities>()
                    .and_then(|map_entities| {
                        cloned = Some(clone_reflect_value(&**component, type_registry));

                        map_entities.map_entities(
                            cloned.as_deref_mut()?,
                            &mut MapEntitiesMapper::new(entity_map, self.world),
                        );

                        cloned.as_deref()
                    })
                    .unwrap_or(&**component);

                SceneEntityMapper::world_scope(entity_map, self.world, |world, mapper| {
                    let entity_mut = &mut world.entity_mut(entity);

                    // WORKAROUND: apply_or_insert doesn't actually apply
                    reflect.remove(entity_mut);

                    reflect.apply_or_insert_mapped(
                        entity_mut,
                        component,
                        type_registry,
                        mapper,
                        RelationshipHookMode::Run,
                    );
                });
            }
        }

        // Insert resources after all entities have been added to the world.
        // This ensures the entities are available for the resources to reference during
        // mapping.
        for resource in self.input.snapshot.resources() {
            let type_info = resource.get_represented_type_info().ok_or_else(|| {
                SceneSpawnError::NoRepresentedType {
                    type_path: resource.reflect_type_path().to_string(),
                }
            })?;
            let registration = type_registry.get(type_info.type_id()).ok_or_else(|| {
                SceneSpawnError::UnregisteredButReflectedType {
                    type_path: type_info.type_path().to_string(),
                }
            })?;
            let reflect = registration.data::<ReflectResource>().ok_or_else(|| {
                SceneSpawnError::UnregisteredResource {
                    type_path: type_info.type_path().to_string(),
                }
            })?;

            let mut cloned = None;

            // If this resource references entities in the scene, update
            // them to the entities in the world.
            let resource = registration
                .data::<ReflectMapEntities>()
                .and_then(|map_entities| {
                    cloned = Some(clone_reflect_value(&**resource, type_registry));

                    map_entities.map_entities(
                        cloned.as_deref_mut()?,
                        &mut MapEntitiesMapper::new(entity_map, self.world),
                    );

                    cloned.as_deref()
                })
                .unwrap_or(&**resource);

            // If the world already contains an instance of the given resource
            // just apply the (possibly) new value, otherwise insert the resource
            reflect.apply_or_insert(self.world, resource, type_registry);
        }

        // Prefab hooks
        for (type_id, entities) in prefab_entities {
            let Some(hook) = self.input.prefabs.get(&type_id) else {
                continue;
            };

            for (entity, component) in entities {
                hook(component, entity, self.world);
            }
        }

        // Entity hooks
        if !self.input.hooks.is_empty() {
            let mut queue = CommandQueue::default();
            let mut commands = Commands::new(&mut queue, self.world);

            for hook in &self.input.hooks {
                for (_, entity) in entity_map.iter() {
                    let entity_ref = self.world.entity(*entity);
                    let mut entity_mut = commands.entity(*entity);

                    hook(&entity_ref, &mut entity_mut);
                }
            }

            queue.apply(self.world);
        }

        Ok(())
    }
}