Skip to main content

rantz_cereal/
events.rs

1use crate::prelude::*;
2use bevy::prelude::*;
3use std::marker::PhantomData;
4
5/// The context of the save (i.e. global, user, or slot)
6#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
7pub enum Context {
8    /// The global save context, saved in the root folder
9    Global,
10    /// The user save context, saved in the user folder (set by `set_user_id`)
11    User,
12    /// The slot save context, saved in the slot folder (set by `set_save_slot`), default
13    #[default]
14    Slot,
15}
16
17/// An event used to request saving of data local to the `SaveSlot`
18/// non-ascii characters are replaced with underscores
19#[derive(Event, Debug, Clone)]
20pub struct SaveRequest<T>
21where
22    T: SaveData,
23{
24    /// The subdirectory to save the file to relative to the `SaveSlot`
25    pub(crate) sub_directory: Option<String>,
26    /// The name of the file
27    pub(crate) file_name: String,
28    /// The data to save
29    pub(crate) save_data: T,
30    /// The context of the save
31    pub(crate) context: Context,
32}
33
34impl<T> SaveRequest<T>
35where
36    T: SaveData,
37{
38    /// Creates a new `SaveRequest`
39    pub fn new(file_name: &str, save_data: T) -> Self {
40        Self {
41            save_data,
42            file_name: file_name.to_string(),
43            sub_directory: None,
44            context: Context::Slot,
45        }
46    }
47
48    /// Sets the subdirectory relative to the `SaveSlot`
49    pub fn with_sub_directory(mut self, sub_directory: &str) -> Self {
50        self.sub_directory = Some(sub_directory.replace(|c: char| !c.is_ascii(), "_"));
51        self
52    }
53
54    /// Sets the context of the save, `Context::Slot` by default
55    pub fn with_context(mut self, context: Context) -> Self {
56        self.context = context;
57        self
58    }
59}
60
61/// An event used to request loading of data local to the `SaveSlot`
62/// non-ascii characters are replaced with underscores
63#[derive(Event, Debug, Clone)]
64pub struct LoadRequest<T>
65where
66    T: SaveData,
67{
68    /// The subdirectory to save the file to relative to the `SaveSlot`
69    pub(crate) sub_directory: Option<String>,
70    /// The name of the file
71    pub(crate) file_name: String,
72    /// The context of the save
73    pub(crate) context: Context,
74    /// The data to save
75    marker: PhantomData<T>,
76}
77
78impl<T> LoadRequest<T>
79where
80    T: SaveData,
81{
82    /// Creates a new `LoadRequest`
83    pub fn new(file_name: &str) -> Self {
84        Self {
85            sub_directory: None,
86            context: Context::Slot,
87            file_name: file_name.to_string(),
88            marker: PhantomData,
89        }
90    }
91
92    /// Sets the subdirectory relative to the `SaveSlot`
93    pub fn with_sub_directory(mut self, sub_directory: &str) -> Self {
94        self.sub_directory = Some(sub_directory.replace(|c: char| !c.is_ascii(), "_"));
95        self
96    }
97
98    /// Sets the context of the save, `Context::Slot` by default
99    pub fn with_context(mut self, context: Context) -> Self {
100        self.context = context;
101        self
102    }
103}
104
105/// An event sent when a `LoadRequest` has completed
106#[derive(Event, Debug, Clone)]
107pub struct LoadComplete<T>
108where
109    T: SaveData,
110{
111    /// The outcome of the load
112    pub outcome: LoadOutcome,
113    /// The name of the file
114    pub file_name: String,
115    pub(crate) _marker: PhantomData<T>,
116}
117
118/// An event sent when a `SaveRequest` has completed
119#[derive(Event, Debug, Clone)]
120pub struct SaveComplete<T>
121where
122    T: SaveData,
123{
124    /// The outcome of the save
125    pub outcome: SaveOutcome,
126    /// The name of the file
127    pub file_name: String,
128    pub(crate) _marker: PhantomData<T>,
129}