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
use std::{
    collections::HashSet,
    marker::PhantomData,
    sync::Arc,
};

use bevy::{
    ecs::{
        entity::EntityMap,
        query::ReadOnlyWorldQuery,
        system::EntityCommands,
        world::EntityRef,
    },
    prelude::*,
};

/// A [`ReadOnlyWorldQuery`] filter.
pub trait Filter: Send + Sync {
    /// Collect all entities from the given [`World`] matching the filter.
    fn collect(&self, world: &mut World) -> HashSet<Entity>;
}

struct FilterMarker<F>(PhantomData<F>);

impl<F> Filter for FilterMarker<F>
where
    F: ReadOnlyWorldQuery + Send + Sync,
{
    fn collect(&self, world: &mut World) -> HashSet<Entity> {
        world
            .query_filtered::<Entity, F>()
            .iter(world)
            .collect::<HashSet<_>>()
    }
}

/// A boxed [`Filter`].
pub type BoxedFilter = Box<dyn Filter>;

impl dyn Filter {
    /// Create an opaque type that implements [`Filter`] from a [`ReadOnlyWorldQuery`].
    ///
    /// # Example
    /// ```
    /// # use bevy::prelude::*;
    /// # use bevy_save::prelude::*;
    /// #[derive(Component)]
    /// struct A;
    ///
    /// #[derive(Component)]
    /// struct B;
    ///
    /// let filter = <dyn Filter>::new::<(With<A>, Without<B>)>();
    /// ```
    pub fn new<F>() -> impl Filter
    where
        F: ReadOnlyWorldQuery + Send + Sync,
    {
        FilterMarker::<F>(PhantomData)
    }

    /// Create a [`BoxedFilter`] from a [`ReadOnlyWorldQuery`].
    pub fn boxed<F>() -> BoxedFilter
    where
        F: ReadOnlyWorldQuery + Send + Sync + 'static,
    {
        Box::new(Self::new::<F>())
    }
}

/// Determines what the snapshot will do to existing entities when applied.
#[derive(Default)]
pub enum DespawnMode {
    /// Despawn entities missing from the save
    ///
    /// `bevy_save` default
    #[default]
    Missing,

    /// Despawn entities missing from the save matching filter
    MissingWith(BoxedFilter),

    /// Despawn unmapped entities
    Unmapped,

    /// Despawn unmapped entities matching filter
    UnmappedWith(BoxedFilter),

    /// Despawn all entities
    ///
    /// This is probably not what you want - in most cases this will close your app's [`Window`]
    All,

    /// Despawn all entities matching filter
    AllWith(BoxedFilter),

    /// Keep all entities
    ///
    /// `bevy_scene` default
    None,
}

impl DespawnMode {
    /// Create a new instance of [`DespawnMode::UnmappedWith`] with the given filter.
    pub fn unmapped_with<F>() -> Self
    where
        F: ReadOnlyWorldQuery + Send + Sync + 'static,
    {
        DespawnMode::UnmappedWith(<dyn Filter>::boxed::<F>())
    }

    /// Create a new instance of [`DespawnMode::AllWith`] with the given filter.
    pub fn all_with<F>() -> Self
    where
        F: ReadOnlyWorldQuery + Send + Sync + 'static,
    {
        DespawnMode::UnmappedWith(<dyn Filter>::boxed::<F>())
    }
}

/// 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 = &mut app.world;
/// # let snapshot = Snapshot::from_world(world);
/// # let parent = world.spawn_empty().id();
/// snapshot
///     .applier(world)
///     .hook(move |entity, cmds| {
///         if !entity.contains::<Parent>() {
///             cmds.set_parent(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>;

/// Determines how the snapshot will map entities when applied.
#[derive(Default)]
pub enum MappingMode {
    /// If unmapped, attempt a one-to-one mapping. If that fails, spawn a new entity.
    ///
    /// `bevy_save` default
    #[default]
    Simple,

    /// If unmapped, spawn a new entity.
    ///
    /// `bevy_scene` default
    Strict,
}

/// The App's default [`DespawnMode`].
///
/// `bevy_save` will use this when applying snapshots without a specified [`DespawnMode`].
#[derive(Resource, Default, Deref, DerefMut, Clone)]
pub struct AppDespawnMode(Arc<DespawnMode>);

impl AppDespawnMode {
    /// Create a new [`AppDespawnMode`] from the given [`DespawnMode`].
    pub fn new(mode: DespawnMode) -> Self {
        Self(Arc::new(mode))
    }

    /// Override the current [`DespawnMode`].
    pub fn set(&mut self, mode: DespawnMode) {
        self.0 = Arc::new(mode);
    }
}

/// The App's default [`MappingMode`].
///
/// `bevy_save` will use this when applying snapshots without a specified [`MappingMode`].
#[derive(Resource, Default, Deref, DerefMut, Clone)]
pub struct AppMappingMode(Arc<MappingMode>);

impl AppMappingMode {
    /// Create a new [`AppMappingMode`] from the given [`MappingMode`].
    pub fn new(mode: MappingMode) -> Self {
        Self(Arc::new(mode))
    }

    /// Override the current [`MappingMode`].
    pub fn set(&mut self, mode: MappingMode) {
        self.0 = Arc::new(mode);
    }
}

/// [`Applier`] lets you configure how a snapshot will be applied to the [`World`].
pub struct Applier<'a, S> {
    pub(crate) world: &'a mut World,
    pub(crate) snapshot: S,
    pub(crate) map: EntityMap,
    pub(crate) despawn: Option<DespawnMode>,
    pub(crate) mapping: Option<MappingMode>,
    pub(crate) hook: Option<BoxedHook>,
}

impl<'a, S> Applier<'a, S> {
    /// Create a new [`Applier`] with default settings from the world and snapshot.
    pub fn new(world: &'a mut World, snapshot: S) -> Self {
        Self {
            world,
            snapshot,
            map: EntityMap::default(),
            despawn: None,
            mapping: None,
            hook: None,
        }
    }

    /// Map entities to new ids with the [`EntityMap`].
    pub fn map(mut self, map: EntityMap) -> Self {
        self.map = map;
        self
    }

    /// Change how the snapshot affects entities when applying.
    pub fn despawn(mut self, mode: DespawnMode) -> Self {
        self.despawn = Some(mode);
        self
    }

    /// Change how the snapshot maps entities when applying.
    pub fn mapping(mut self, mode: MappingMode) -> Self {
        self.mapping = Some(mode);
        self
    }

    /// Add a [`Hook`] that will run for each entity when applying.
    pub fn hook<F>(mut self, hook: F) -> Self
    where
        F: Hook + 'static,
    {
        self.hook = Some(Box::new(hook));
        self
    }
}