Search in sources :

Example 21 with Savable

use of com.jme3.export.Savable in project jmonkeyengine by jMonkeyEngine.

the class InstancedGeometry method read.

@Override
public void read(JmeImporter importer) throws IOException {
    super.read(importer);
    InputCapsule capsule = importer.getCapsule(this);
    //currentNumInstances = capsule.readInt("cur_num_instances", 1);
    Savable[] geometrySavables = capsule.readSavableArray("geometries", null);
    geometries = new Geometry[geometrySavables.length];
    for (int i = 0; i < geometrySavables.length; i++) {
        geometries[i] = (Geometry) geometrySavables[i];
    }
}
Also used : Savable(com.jme3.export.Savable) InputCapsule(com.jme3.export.InputCapsule)

Example 22 with Savable

use of com.jme3.export.Savable in project jmonkeyengine by jMonkeyEngine.

the class SaveGame method loadGame.

/**
     * 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 manager Link to an AssetManager if required for loading the data (e.g. models with textures)
     * @param storageType The specific type of folder to use to save the data
     * @return The savable that was saved or null if none was found
     */
public static Savable loadGame(String gamePath, String dataName, AssetManager manager, JmeSystem.StorageFolderType storageType) {
    if (storageType == null) {
        Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Base Storage Folder Type is null, using External!");
        storageType = JmeSystem.StorageFolderType.External;
    }
    InputStream is = null;
    Savable sav = null;
    try {
        File baseFolder = JmeSystem.getStorageFolder(storageType);
        if (baseFolder == null) {
            Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error reading base storage folder!");
            return null;
        }
        File file = new File(baseFolder.getAbsolutePath() + File.separator + gamePath.replace('/', File.separatorChar) + 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);
        Logger.getLogger(SaveGame.class.getName()).log(Level.FINE, "Loading data from: {0}", file.getAbsolutePath());
    } 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;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BinaryImporter(com.jme3.export.binary.BinaryImporter) Savable(com.jme3.export.Savable) BufferedInputStream(java.io.BufferedInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 23 with Savable

use of com.jme3.export.Savable in project jmonkeyengine by jMonkeyEngine.

the class SaveGame method 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
     * @param storageType The specific type of folder to use to save the data
     */
public static void saveGame(String gamePath, String dataName, Savable data, JmeSystem.StorageFolderType storageType) {
    if (storageType == null) {
        Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Base Storage Folder Type is null, using External!");
        storageType = JmeSystem.StorageFolderType.External;
    }
    BinaryExporter ex = BinaryExporter.getInstance();
    OutputStream os = null;
    try {
        File baseFolder = JmeSystem.getStorageFolder(storageType);
        if (baseFolder == null) {
            Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error creating save file!");
            throw new IllegalStateException("SaveGame dataset cannot be created");
        }
        File daveFolder = new File(baseFolder.getAbsolutePath() + File.separator + gamePath.replace('/', File.separatorChar));
        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);
        Logger.getLogger(SaveGame.class.getName()).log(Level.FINE, "Saving data to: {0}", saveFile.getAbsolutePath());
    } 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");
        }
    }
}
Also used : BinaryExporter(com.jme3.export.binary.BinaryExporter) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 24 with Savable

use of com.jme3.export.Savable in project jmonkeyengine by jMonkeyEngine.

the class DOMOutputCapsule method writeSavableMap.

@Override
public void writeSavableMap(Map<? extends Savable, ? extends Savable> map, String name, Map<? extends Savable, ? extends Savable> defVal) throws IOException {
    if (map == null) {
        return;
    }
    if (map.equals(defVal)) {
        return;
    }
    Element stringMap = appendElement(name);
    Iterator<? extends Savable> keyIterator = map.keySet().iterator();
    while (keyIterator.hasNext()) {
        Savable key = keyIterator.next();
        Element mapEntry = appendElement(XMLExporter.ELEMENT_MAPENTRY);
        write(key, XMLExporter.ELEMENT_KEY, null);
        Savable value = map.get(key);
        write(value, XMLExporter.ELEMENT_VALUE, null);
        currentElement = stringMap;
    }
    currentElement = (Element) stringMap.getParentNode();
}
Also used : Savable(com.jme3.export.Savable) Element(org.w3c.dom.Element)

Example 25 with Savable

use of com.jme3.export.Savable in project jmonkeyengine by jMonkeyEngine.

the class DOMOutputCapsule method write.

@Override
public void write(Savable[] objects, String name, Savable[] defVal) throws IOException {
    if (objects == null) {
        return;
    }
    if (Arrays.equals(objects, defVal)) {
        return;
    }
    Element old = currentElement;
    Element el = appendElement(name);
    el.setAttribute("size", String.valueOf(objects.length));
    for (int i = 0; i < objects.length; i++) {
        Savable o = objects[i];
        if (o == null) {
            //renderStateList has special loading code, so we can leave out the null values
            if (!name.equals("renderStateList")) {
                Element before = currentElement;
                appendElement("null");
                currentElement = before;
            }
        } else {
            write(o, o.getClass().getName(), null);
        }
    }
    currentElement = old;
}
Also used : Savable(com.jme3.export.Savable) Element(org.w3c.dom.Element)

Aggregations

Savable (com.jme3.export.Savable)22 IOException (java.io.IOException)8 Element (org.w3c.dom.Element)6 MatParamOverride (com.jme3.material.MatParamOverride)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputCapsule (com.jme3.export.InputCapsule)1 BinaryExporter (com.jme3.export.binary.BinaryExporter)1 BinaryImporter (com.jme3.export.binary.BinaryImporter)1 AmbientLight (com.jme3.light.AmbientLight)1 Material (com.jme3.material.Material)1 Bucket (com.jme3.renderer.queue.RenderQueue.Bucket)1 ShadowMode (com.jme3.renderer.queue.RenderQueue.ShadowMode)1 Node (com.jme3.scene.Node)1 Spatial (com.jme3.scene.Spatial)1 Control (com.jme3.scene.control.Control)1 SafeArrayList (com.jme3.util.SafeArrayList)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1