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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//! Elements related to loading saved world state.
//!
//! # Example
//! ```
//! use bevy::prelude::*;
//! use moonshine_save::prelude::*;
//!
//! #[derive(Component, Default, Reflect)]
//! #[reflect(Component)]
//! struct Data(u32);
//!
//! # fn generate_data() {
//! #   let mut app = App::new();
//! #   app.add_plugins((MinimalPlugins, SavePlugin))
//! #       .register_type::<Data>()
//! #       .add_systems(PreUpdate, save_default().into_file("example.ron"));
//! #   app.world_mut().spawn((Data(12), Save));
//! #   app.update();
//! # }
//! #
//! # generate_data();
//! #
//! let mut app = App::new();
//! app.add_plugins((MinimalPlugins, LoadPlugin))
//!     .register_type::<Data>()
//!     .add_systems(PreUpdate, load_from_file("example.ron"));
//!
//! app.update();
//!
//! let data = std::fs::read_to_string("example.ron").unwrap();
//! # assert!(data.contains("(12)"));
//! # std::fs::remove_file("example.ron");
//! ```

use std::io;
use std::path::{Path, PathBuf};

use bevy_app::{App, Plugin, PreUpdate};
use bevy_ecs::entity::EntityHashMap;
use bevy_ecs::{prelude::*, query::QueryFilter, schedule::SystemConfigs};
use bevy_hierarchy::DespawnRecursiveExt;
use bevy_scene::{serde::SceneDeserializer, SceneSpawnError};
use bevy_utils::tracing::{error, info, warn};
use moonshine_util::system::*;
use serde::de::DeserializeSeed;

use crate::save::{Save, SaveSystem, Saved};
use crate::FilePath;

/// A [`Plugin`] which configures [`LoadSystem`] in [`PreUpdate`] schedule.
pub struct LoadPlugin;

impl Plugin for LoadPlugin {
    fn build(&self, app: &mut App) {
        app.configure_sets(
            PreUpdate,
            (
                LoadSystem::Load,
                LoadSystem::PostLoad.run_if(has_resource::<Loaded>),
            )
                .chain()
                .before(SaveSystem::Save),
        )
        .add_systems(
            PreUpdate,
            remove_resource::<Loaded>.in_set(LoadSystem::PostLoad),
        );
    }
}

/// A [`SystemSet`] for systems that process loading [`Saved`] data.
#[derive(Clone, Debug, Hash, PartialEq, Eq, SystemSet)]
pub enum LoadSystem {
    /// Reserved for systems which deserialize [`Saved`] data and process the output.
    Load,
    /// Runs after [`LoadSystem::Load`].
    PostLoad,
}

/// A [`Component`] which marks its [`Entity`] to be despawned prior to load.
///
/// # Usage
/// When saving game state, it is often undesirable to save visual and aesthetic elements of the game.
/// Elements such as transforms, camera settings, scene hierarchy, or UI elements are typically either
/// spawned at game start, or added during initialization of the game data they represent.
///
/// This component may be used on such entities to despawn them prior to loading.
///
/// # Example
/// ```
/// use bevy::prelude::*;
/// use moonshine_save::prelude::*;
///
/// #[derive(Bundle)]
/// struct PlayerBundle {
///     player: Player,
///     /* Saved Player Data */
///     save: Save,
/// }
///
/// #[derive(Component, Default, Reflect)]
/// #[reflect(Component)]
/// struct Player;
///
/// #[derive(Component)] // <-- Not serialized!
/// struct PlayerSprite(Entity);
///
/// #[derive(Bundle, Default)]
/// struct PlayerSpriteBundle {
///     /* Player Visuals/Aesthetics */
///     unload: Unload,
/// }
///
/// fn spawn_player_sprite(query: Query<Entity, Added<Player>>, mut commands: Commands) {
///     for entity in &query {
///         let sprite = PlayerSprite(commands.spawn(PlayerSpriteBundle::default()).id());
///         commands.entity(entity).insert(sprite);
///     }
/// }
/// ```
#[derive(Component, Default, Clone)]
pub struct Unload;

/// A [`Resource`] which contains the loaded entity map. See [`FromLoaded`] for usage.
#[derive(Resource)]
pub struct Loaded {
    pub entity_map: EntityHashMap<Entity>,
}

#[derive(Debug)]
pub enum LoadError {
    Io(io::Error),
    De(ron::de::SpannedError),
    Ron(ron::Error),
    Scene(SceneSpawnError),
}

impl From<io::Error> for LoadError {
    fn from(e: io::Error) -> Self {
        Self::Io(e)
    }
}

impl From<ron::de::SpannedError> for LoadError {
    fn from(e: ron::de::SpannedError) -> Self {
        Self::De(e)
    }
}

impl From<ron::Error> for LoadError {
    fn from(e: ron::Error) -> Self {
        Self::Ron(e)
    }
}

impl From<SceneSpawnError> for LoadError {
    fn from(e: SceneSpawnError) -> Self {
        Self::Scene(e)
    }
}

/// A collection of systems ([`SystemConfigs`]) which perform the load process.
pub type LoadPipeline = SystemConfigs;

/// Default [`LoadPipeline`].
///
/// # Usage
///
/// This pipeline tries to load all saved entities from a file at given `path`. If successful, it
/// despawns all entities marked with [`Unload`] (recursively) and spawns the loaded entities.
///
/// Typically, it should be used with [`run_if`](bevy_ecs::schedule::SystemSet::run_if).
///
/// # Example
/// ```
/// use bevy::prelude::*;
/// use moonshine_save::prelude::*;
///
/// let mut app = App::new();
/// app.add_plugins(LoadPlugin)
///     .add_systems(PreUpdate, load_from_file("example.ron").run_if(should_load));
///
/// fn should_load() -> bool {
///     todo!()
/// }
/// ```
pub fn load_from_file(path: impl Into<PathBuf>) -> LoadPipeline {
    let path = path.into();
    from_file(path)
        .pipe(unload::<DefaultUnloadFilter>)
        .pipe(load)
        .pipe(insert_into_loaded(Save))
        .pipe(finish)
        .in_set(LoadSystem::Load)
}

/// A [`LoadPipeline`] like [`load_from_file`] which is only triggered if a [`LoadFromFileRequest`] [`Resource`] is present.
///
/// # Example
/// ```
/// # use std::path::{Path, PathBuf};
/// # use bevy::prelude::*;
/// # use moonshine_save::prelude::*;
///
/// #[derive(Resource)]
/// struct LoadRequest {
///     pub path: PathBuf,
/// }
///
/// impl FilePath for LoadRequest {
///     fn path(&self) -> &Path {
///         self.path.as_ref()
///     }
/// }
///
/// let mut app = App::new();
/// app.add_plugins((MinimalPlugins, LoadPlugin))
///     .add_systems(Update, load_from_file_on_request::<LoadRequest>());
/// ```
pub fn load_from_file_on_request<R>() -> LoadPipeline
where
    R: FilePath + Resource,
{
    file_from_request::<R>
        .pipe(from_file_dyn)
        .pipe(unload::<DefaultUnloadFilter>)
        .pipe(load)
        .pipe(insert_into_loaded(Save))
        .pipe(finish)
        .pipe(remove_resource::<R>)
        .run_if(has_resource::<R>)
        .in_set(LoadSystem::Load)
}

/// A [`LoadPipeline`] like [`load_from_file`] which is only triggered if a [`LoadFromFileRequest`] [`Event`] is sent.
///
/// Note: If multiple events are sent in a single update cycle, only the first one is processed.
pub fn load_from_file_on_event<R>() -> LoadPipeline
where
    R: FilePath + Event,
{
    file_from_event::<R>
        .pipe(from_file_dyn)
        .pipe(unload::<DefaultUnloadFilter>)
        .pipe(load)
        .pipe(insert_into_loaded(Save))
        .pipe(finish)
        .run_if(has_event::<R>)
        .in_set(LoadSystem::Load)
}

/// A [`System`] which reads [`Saved`] data from a file at given `path`.
pub fn from_file(
    path: impl Into<PathBuf>,
) -> impl Fn(Res<AppTypeRegistry>) -> Result<Saved, LoadError> {
    let path = path.into();
    move |type_registry| {
        let input = std::fs::read(&path)?;
        let mut deserializer = ron::Deserializer::from_bytes(&input)?;
        let scene = {
            let type_registry = &type_registry.read();
            let scene_deserializer = SceneDeserializer { type_registry };
            scene_deserializer.deserialize(&mut deserializer)?
        };
        info!("loaded from file: {path:?}");
        Ok(Saved { scene })
    }
}

/// A [`System`] which reads [`Saved`] data from a file with its path defined at runtime.
pub fn from_file_dyn(
    In(path): In<PathBuf>,
    type_registry: Res<AppTypeRegistry>,
) -> Result<Saved, LoadError> {
    let input = std::fs::read(&path)?;
    let mut deserializer = ron::Deserializer::from_bytes(&input)?;
    let scene = {
        let type_registry = &type_registry.read();
        let scene_deserializer = SceneDeserializer { type_registry };
        scene_deserializer.deserialize(&mut deserializer)?
    };
    info!("loaded from file: {path:?}");
    Ok(Saved { scene })
}

pub type DefaultUnloadFilter = Or<(With<Save>, With<Unload>)>;

/// A [`System`] which recursively despawns all entities that match the given `Filter`.
pub fn unload<Filter: QueryFilter>(
    In(result): In<Result<Saved, LoadError>>,
    world: &mut World,
) -> Result<Saved, LoadError> {
    let saved = result?;
    let entities: Vec<Entity> = world
        .query_filtered::<Entity, Filter>()
        .iter(world)
        .collect();
    for entity in entities {
        if let Some(entity) = world.get_entity_mut(entity) {
            entity.despawn_recursive();
        }
    }
    Ok(saved)
}

/// A [`System`] which writes [`Saved`] data into current [`World`].
pub fn load(
    In(result): In<Result<Saved, LoadError>>,
    world: &mut World,
) -> Result<Loaded, LoadError> {
    let Saved { scene } = result?;
    let mut entity_map = EntityHashMap::default();
    scene.write_to_world(world, &mut entity_map)?;
    Ok(Loaded { entity_map })
}

/// A [`System`] which inserts a clone of the given [`Bundle`] into all loaded entities.
pub fn insert_into_loaded(
    bundle: impl Bundle + Clone,
) -> impl Fn(In<Result<Loaded, LoadError>>, &mut World) -> Result<Loaded, LoadError> {
    move |In(result), world| {
        if let Ok(loaded) = &result {
            for entity in loaded.entity_map.values() {
                world.entity_mut(*entity).insert(bundle.clone());
            }
        }
        result
    }
}

/// A [`System`] which finishes the load process.
///
/// # Usage
///
/// All load pipelines should end with this system.
pub fn finish(In(result): In<Result<Loaded, LoadError>>, world: &mut World) {
    match result {
        Ok(loaded) => world.insert_resource(loaded),
        Err(why) => error!("load failed: {why:?}"),
    }
}

/// A [`System`] which extracts the path from a [`LoadFromFileRequest`] [`Resource`].
pub fn file_from_request<R>(request: Res<R>) -> PathBuf
where
    R: FilePath + Resource,
{
    request.path().to_owned()
}

/// A [`System`] which extracts the path from a [`LoadFromFileRequest`] [`Event`].
///
/// # Warning
///
/// If multiple events are sent in a single update cycle, only the first one is processed.
///
/// This system assumes that at least one event has been sent. It must be used in conjunction with [`has_event`].
pub fn file_from_event<R>(mut events: EventReader<R>) -> PathBuf
where
    R: FilePath + Event,
{
    let mut iter = events.read();
    let event = iter.next().unwrap();
    if iter.next().is_some() {
        warn!("multiple load request events received; only the first one is processed.");
    }
    event.path().to_owned()
}

/// Any type which may be used to trigger [`load_from_file_on_request`] or [`load_from_file_on_event`].
#[deprecated(note = "use `FilePath` instead")]
pub trait LoadFromFileRequest {
    fn path(&self) -> &Path;
}

#[cfg(test)]
mod tests {
    use std::fs::*;

    use bevy::prelude::*;

    use super::*;

    pub const DATA: &str = "(
        resources: {},
        entities: {
            4294967296: (
                components: {
                    \"moonshine_save::load::tests::Dummy\": (),
                },
            ),
        },
    )";

    #[derive(Component, Default, Reflect)]
    #[reflect(Component)]
    struct Dummy;

    fn app() -> App {
        let mut app = App::new();
        app.add_plugins((MinimalPlugins, LoadPlugin))
            .register_type::<Dummy>();
        app
    }

    #[test]
    fn test_load_from_file() {
        pub const PATH: &str = "test_load.ron";

        write(PATH, DATA).unwrap();

        let mut app = app();
        app.add_systems(PreUpdate, load_from_file(PATH));

        app.update();

        let world = app.world_mut();
        assert!(!world.contains_resource::<Loaded>());
        assert!(world
            .query_filtered::<(), With<Dummy>>()
            .get_single(world)
            .is_ok());

        remove_file(PATH).unwrap();
    }

    #[test]
    fn test_load_from_file_on_request() {
        pub const PATH: &str = "test_load_on_request_dyn.ron";

        write(PATH, DATA).unwrap();

        #[derive(Resource)]
        struct LoadRequest;

        impl FilePath for LoadRequest {
            fn path(&self) -> &Path {
                Path::new(PATH)
            }
        }

        let mut app = app();
        app.add_systems(PreUpdate, load_from_file_on_request::<LoadRequest>());

        app.world_mut().insert_resource(LoadRequest);
        app.update();

        let world = app.world_mut();
        assert!(world
            .query_filtered::<(), With<Dummy>>()
            .get_single(world)
            .is_ok());

        remove_file(PATH).unwrap();
    }

    #[test]
    fn test_load_from_file_on_event() {
        pub const PATH: &str = "test_load_on_request_event.ron";

        write(PATH, DATA).unwrap();

        #[derive(Event)]
        struct LoadRequest;

        impl FilePath for LoadRequest {
            fn path(&self) -> &Path {
                Path::new(PATH)
            }
        }

        let mut app = app();
        app.add_event::<LoadRequest>()
            .add_systems(PreUpdate, load_from_file_on_event::<LoadRequest>());

        app.world_mut().send_event(LoadRequest);
        app.update();

        let world = app.world_mut();
        assert!(world
            .query_filtered::<(), With<Dummy>>()
            .get_single(world)
            .is_ok());

        remove_file(PATH).unwrap();
    }

    #[test]
    fn test_hierarchy() {
        use std::fs::*;

        use bevy::prelude::*;

        use crate::save::{save_default, SavePlugin};

        pub const PATH: &str = "test_load_hierarchy.ron";

        {
            let mut app = App::new();
            app.add_plugins((MinimalPlugins, HierarchyPlugin, SavePlugin))
                .add_systems(PreUpdate, save_default().into_file(PATH));

            let entity = app
                .world_mut()
                .spawn(Save)
                .with_children(|parent| {
                    parent.spawn(Save);
                    parent.spawn(Save);
                })
                .id();

            app.update();

            let world = app.world();
            let children = world.get::<Children>(entity).unwrap();
            assert_eq!(children.iter().count(), 2);
            for child in children.iter() {
                let parent = world.get::<Parent>(*child).unwrap().get();
                assert_eq!(parent, entity);
            }
        }

        {
            // Hierarchy should not contain children
            let data = std::fs::read_to_string(PATH).unwrap();
            assert!(data.contains("Parent"));
            assert!(data.contains("Children"));
        }

        {
            let mut app = App::new();
            app.add_plugins((MinimalPlugins, HierarchyPlugin, LoadPlugin))
                .add_systems(PreUpdate, load_from_file(PATH));

            // Spawn an entity to offset indices
            app.world_mut().spawn_empty();

            app.update();

            let world = app.world_mut();
            let (entity, children) = world.query::<(Entity, &Children)>().single(world);
            assert_eq!(children.iter().count(), 2);
            for child in children.iter() {
                let parent = world.get::<Parent>(*child).unwrap().get();
                assert_eq!(parent, entity);
            }
        }

        remove_file(PATH).unwrap();
    }
}