1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 package jme3tools.savegame;
6 
7 import com.jme3.asset.AssetManager;
8 import com.jme3.export.Savable;
9 import com.jme3.export.binary.BinaryExporter;
10 import com.jme3.export.binary.BinaryImporter;
11 import com.jme3.system.JmeSystem;
12 import java.io.BufferedInputStream;
13 import java.io.BufferedOutputStream;
14 import java.io.File;
15 import java.io.FileInputStream;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.OutputStream;
20 import java.util.logging.Level;
21 import java.util.logging.Logger;
22 import java.util.zip.GZIPInputStream;
23 import java.util.zip.GZIPOutputStream;
24 
25 /**
26  * Tool for saving Savables as SaveGame entries in a system-dependent way.
27  * @author normenhansen
28  */
29 public class SaveGame {
30 
31     /**
32      * Saves a savable in a system-dependent way.
33      * @param gamePath A unique path for this game, e.g. com/mycompany/mygame
34      * @param dataName A unique name for this savegame, e.g. "save_001"
35      * @param data The Savable to save
36      */
saveGame(String gamePath, String dataName, Savable data)37     public static void saveGame(String gamePath, String dataName, Savable data) {
38         BinaryExporter ex = BinaryExporter.getInstance();
39         OutputStream os = null;
40         try {
41             File daveFolder = new File(JmeSystem.getStorageFolder().getAbsolutePath() + File.separator + gamePath.replaceAll("/", File.separator));
42             if (!daveFolder.exists() && !daveFolder.mkdirs()) {
43                 Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error creating save file!");
44                 throw new IllegalStateException("SaveGame dataset cannot be created");
45             }
46             File saveFile = new File(daveFolder.getAbsolutePath() + File.separator + dataName);
47             if (!saveFile.exists()) {
48                 if (!saveFile.createNewFile()) {
49                     Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error creating save file!");
50                     throw new IllegalStateException("SaveGame dataset cannot be created");
51                 }
52             }
53             os = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(saveFile)));
54             ex.save(data, os);
55         } catch (IOException ex1) {
56             Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error saving data: {0}", ex1);
57             ex1.printStackTrace();
58             throw new IllegalStateException("SaveGame dataset cannot be saved");
59         } finally {
60             try {
61                 if (os != null) {
62                     os.close();
63                 }
64             } catch (IOException ex1) {
65                 Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error saving data: {0}", ex1);
66                 ex1.printStackTrace();
67                 throw new IllegalStateException("SaveGame dataset cannot be saved");
68             }
69         }
70     }
71 
72     /**
73      * Loads a savable that has been saved on this system with saveGame() before.
74      * @param gamePath A unique path for this game, e.g. com/mycompany/mygame
75      * @param dataName A unique name for this savegame, e.g. "save_001"
76      * @return The savable that was saved
77      */
loadGame(String gamePath, String dataName)78     public static Savable loadGame(String gamePath, String dataName) {
79         return loadGame(gamePath, dataName, null);
80     }
81 
82     /**
83      * Loads a savable that has been saved on this system with saveGame() before.
84      * @param gamePath A unique path for this game, e.g. com/mycompany/mygame
85      * @param dataName A unique name for this savegame, e.g. "save_001"
86      * @param manager Link to an AssetManager if required for loading the data (e.g. models with textures)
87      * @return The savable that was saved or null if none was found
88      */
loadGame(String gamePath, String dataName, AssetManager manager)89     public static Savable loadGame(String gamePath, String dataName, AssetManager manager) {
90         InputStream is = null;
91         Savable sav = null;
92         try {
93             File file = new File(JmeSystem.getStorageFolder().getAbsolutePath() + File.separator + gamePath.replaceAll("/", File.separator) + File.separator + dataName);
94             if(!file.exists()){
95                 return null;
96             }
97             is = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)));
98             BinaryImporter imp = BinaryImporter.getInstance();
99             if (manager != null) {
100                 imp.setAssetManager(manager);
101             }
102             sav = imp.load(is);
103         } catch (IOException ex) {
104             Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error loading data: {0}", ex);
105             ex.printStackTrace();
106         } finally {
107             if (is != null) {
108                 try {
109                     is.close();
110                 } catch (IOException ex) {
111                     Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error loading data: {0}", ex);
112                     ex.printStackTrace();
113                 }
114             }
115         }
116         return sav;
117     }
118 }
119