- use JmeSystem.getStorageFolder() for SaveGame util

- move SaveGame util to core plugins
- fix BinaryImporter accessing assetManager if null

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8872 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
nor..67 13 years ago
parent e441f95d5e
commit 5ce347eec8
  1. 9
      engine/src/core-plugins/com/jme3/export/binary/BinaryImporter.java
  2. 118
      engine/src/tools/jme3tools/savegame/SaveGame.java

@ -325,8 +325,13 @@ public final class BinaryImporter implements JmeImporter {
int dataLength = ByteUtils.convertIntFromBytes(dataArray, loc); int dataLength = ByteUtils.convertIntFromBytes(dataArray, loc);
loc+=4; loc+=4;
Savable out = SavableClassUtil.fromName(bco.className, assetManager.getClassLoaders()); Savable out = null;
if (assetManager != null) {
out = SavableClassUtil.fromName(bco.className, assetManager.getClassLoaders());
} else {
out = SavableClassUtil.fromName(bco.className);
}
BinaryInputCapsule cap = new BinaryInputCapsule(this, out, bco); BinaryInputCapsule cap = new BinaryInputCapsule(this, out, bco);
cap.setContent(dataArray, loc, loc+dataLength); cap.setContent(dataArray, loc, loc+dataLength);

@ -0,0 +1,118 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jme3tools.savegame;
import com.jme3.asset.AssetManager;
import com.jme3.export.Savable;
import com.jme3.export.binary.BinaryExporter;
import com.jme3.export.binary.BinaryImporter;
import com.jme3.system.JmeSystem;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
* Tool for saving Savables as SaveGame entries in a system-dependent way.
* @author normenhansen
*/
public class SaveGame {
/**
* Saves a savable in a system-dependent way.
* @param gamePath A unique path for this game, e.g. com/mycompany/mygame
* @param dataName A unique name for this savegame, e.g. "save_001"
* @param data The Savable to save
*/
public static void saveGame(String gamePath, String dataName, Savable data) {
BinaryExporter ex = BinaryExporter.getInstance();
OutputStream os = null;
try {
File daveFolder = new File(JmeSystem.getStorageFolder().getAbsolutePath() + File.separator + gamePath.replaceAll("/", File.separator));
if (!daveFolder.exists() && !daveFolder.mkdirs()) {
Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error creating save file!");
throw new IllegalStateException("SaveGame dataset cannot be created");
}
File saveFile = new File(daveFolder.getAbsolutePath() + File.separator + dataName);
if (!saveFile.exists()) {
if (!saveFile.createNewFile()) {
Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error creating save file!");
throw new IllegalStateException("SaveGame dataset cannot be created");
}
}
os = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(saveFile)));
ex.save(data, os);
} catch (IOException ex1) {
Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error saving data: {0}", ex1);
ex1.printStackTrace();
throw new IllegalStateException("SaveGame dataset cannot be saved");
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException ex1) {
Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error saving data: {0}", ex1);
ex1.printStackTrace();
throw new IllegalStateException("SaveGame dataset cannot be saved");
}
}
}
/**
* Loads a savable that has been saved on this system with saveGame() before.
* @param gamePath A unique path for this game, e.g. com/mycompany/mygame
* @param dataName A unique name for this savegame, e.g. "save_001"
* @return The savable that was saved
*/
public static Savable loadGame(String gamePath, String dataName) {
return loadGame(gamePath, dataName, null);
}
/**
* Loads a savable that has been saved on this system with saveGame() before.
* @param gamePath A unique path for this game, e.g. com/mycompany/mygame
* @param dataName A unique name for this savegame, e.g. "save_001"
* @param assetManager Link to an AssetManager if required for loading the data (e.g. models with textures)
* @return The savable that was saved or null if none was found
*/
public static Savable loadGame(String gamePath, String dataName, AssetManager manager) {
InputStream is = null;
Savable sav = null;
try {
File file = new File(JmeSystem.getStorageFolder().getAbsolutePath() + File.separator + gamePath.replaceAll("/", File.separator) + File.separator + dataName);
if(!file.exists()){
return null;
}
is = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)));
BinaryImporter imp = BinaryImporter.getInstance();
if (manager != null) {
imp.setAssetManager(manager);
}
sav = imp.load(is);
} catch (IOException ex) {
Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error loading data: {0}", ex);
ex.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ex) {
Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error loading data: {0}", ex);
ex.printStackTrace();
}
}
}
return sav;
}
}
Loading…
Cancel
Save