cobble_core/instance/
save_games.rs

1use crate::error::CobbleResult;
2use crate::minecraft::{load_save_games, SaveGame};
3use crate::Instance;
4use std::path::PathBuf;
5
6impl Instance {
7    /// Path to the .minecraft/saves folder.
8    #[cfg_attr(doc_cfg, doc(cfg(feature = "save-games")))]
9    pub fn save_games_path(&self) -> PathBuf {
10        let mut saves_path = self.dot_minecraft_path();
11        saves_path.push("saves");
12        saves_path
13    }
14
15    /// Loads all save games from this instance.
16    #[cfg_attr(doc_cfg, doc(cfg(feature = "save-games")))]
17    pub async fn load_save_games(&self) -> CobbleResult<Vec<SaveGame>> {
18        load_save_games(self.save_games_path()).await
19    }
20
21    /// Imports a save game from an exported archive.
22    /// Parses the imported save game and returns `None` if parsing fails.
23    #[cfg_attr(doc_cfg, doc(cfg(feature = "backup")))]
24    #[cfg(feature = "backup")]
25    pub async fn import_save_game(
26        &self,
27        src: impl AsRef<std::path::Path>,
28    ) -> CobbleResult<Option<SaveGame>> {
29        let save_games_path = self.save_games_path();
30
31        if !save_games_path.is_dir() {
32            tokio::fs::create_dir_all(&save_games_path).await?;
33        }
34
35        SaveGame::import(save_games_path, src).await
36    }
37}