com.jme3.export.Savable Java Examples

The following examples show how to use com.jme3.export.Savable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BinaryExporter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public int processBinarySavable(Savable object) throws IOException {
    if (object == null) {
        return -1;
    }
    Class<? extends Savable> clazz = object.getClass();
    BinaryClassObject bco = classes.get(object.getClass().getName());
    // is this class been looked at before? in tagTable?
    if (bco == null) {
        bco = createClassObject(object.getClass());
    }

    // is object in contentTable?
    if (contentTable.get(object) != null) {
        return (contentTable.get(object).getId());
    }
    BinaryIdContentPair newPair = generateIdContentPair(bco);
    BinaryIdContentPair old = contentTable.put(object, newPair);
    if (old == null) {
        contentKeys.add(object);
    }
    object.write(this);
    newPair.getContent().finish();
    return newPair.getId();

}
 
Example #2
Source File: BinaryInputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public ArrayList[] readSavableArrayListArray(String name, ArrayList[] defVal)
        throws IOException {
    BinaryClassField field = cObj.nameFields.get(name);
    if (field == null || !fieldData.containsKey(field.alias))
        return defVal;
    Object value = fieldData.get(field.alias);
    if (value instanceof ID[][]) {
        // read 2D Savable array and convert to ArrayList array
        Savable[][] savables = readSavableArray2D(name, null);
        if (savables != null) {
            ArrayList[] arrayLists = new ArrayList[savables.length];
            for (int i = 0; i < savables.length; i++) {
                arrayLists[i] = savableArrayListFromArray(savables[i]);
            }
            value = arrayLists;
        } else
            value = defVal;
        fieldData.put(field.alias, value);
    }
    return (ArrayList[]) value;
}
 
Example #3
Source File: DOMOutputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void writeSavableArrayList(ArrayList array, String name, ArrayList defVal) throws IOException {
    if (array == null) {
        return;
    }
    if (array.equals(defVal)) {
        return;
    }
    Element old = currentElement;
    Element el = appendElement(name);
    currentElement = el;
    el.setAttribute(XMLExporter.ATTRIBUTE_SIZE, String.valueOf(array.size()));
    for (Object o : array) {
            if(o == null) {
                    continue;
            }
            else if (o instanceof Savable) {
            Savable s = (Savable) o;
            write(s, s.getClass().getName(), null);
        } else {
            throw new ClassCastException("Not a Savable instance: " + o);
        }
    }
    currentElement = old;
}
 
Example #4
Source File: BinaryInputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Map<? extends Savable, ? extends Savable> readSavableMap(String name, Map<? extends Savable, ? extends Savable> defVal)
        throws IOException {
    BinaryClassField field = cObj.nameFields.get(name);
    if (field == null || !fieldData.containsKey(field.alias))
        return defVal;
    Object value = fieldData.get(field.alias);
    if (value instanceof ID[][]) {
        // read Savable array and convert to Map
        Savable[][] savables = readSavableArray2D(name, null);
        value = savableMapFrom2DArray(savables);
        fieldData.put(field.alias, value);
    }
    return (Map<? extends Savable, ? extends Savable>) value;
}
 
Example #5
Source File: BinaryInputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Map<String, ? extends Savable> readStringSavableMap(String name, Map<String, ? extends Savable> defVal)
        throws IOException {
    BinaryClassField field = cObj.nameFields.get(name);
    if (field == null || !fieldData.containsKey(field.alias))
        return defVal;
    Object value = fieldData.get(field.alias);
    if (value instanceof StringIDMap) {
        // read Savable array and convert to Map values
        StringIDMap in = (StringIDMap) value;
        Savable[] values = resolveIDs(in.values);
        value = stringSavableMapFromKV(in.keys, values);
        fieldData.put(field.alias, value);
    }
    return (Map<String, Savable>) value;
}
 
Example #6
Source File: BinaryInputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public IntMap<? extends Savable> readIntSavableMap(String name, IntMap<? extends Savable> defVal)
        throws IOException {
    BinaryClassField field = cObj.nameFields.get(name);
    if (field == null || !fieldData.containsKey(field.alias))
        return defVal;
    Object value = fieldData.get(field.alias);
    if (value instanceof IntIDMap) {
        // read Savable array and convert to Map values
        IntIDMap in = (IntIDMap) value;
        Savable[] values = resolveIDs(in.values);
        value = intSavableMapFromKV(in.keys, values);
        fieldData.put(field.alias, value);
    }
    return (IntMap<Savable>) value;
}
 
Example #7
Source File: AssetDataObject.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Savable loadAsset() {
    if (isModified() && savable != null) {
        return savable;
    }
    ProjectAssetManager mgr = getLookup().lookup(ProjectAssetManager.class);
    if (mgr == null) {
        DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Message("File is not part of a project!\nCannot load without ProjectAssetManager."));
        return null;
    }
    FileLock lock = null;
    try {
        lock = getPrimaryFile().lock();
        Savable spatial = (Savable) mgr.loadAsset(getAssetKey());
        savable = spatial;
        lock.releaseLock();
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    } finally {
        if (lock != null) {
            lock.releaseLock();
        }
    }
    return savable;
}
 
Example #8
Source File: BinaryInputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Savable[][][] readSavableArray3D(String name, Savable[][][] defVal)
        throws IOException {
    BinaryClassField field = cObj.nameFields.get(name);
    if (field == null || !fieldData.containsKey(field.alias))
        return defVal;
    Object[][][] values = (Object[][][]) fieldData.get(field.alias);
    if (values instanceof ID[][][]) {
        Savable[][][] savables = new Savable[values.length][][];
        for (int i = 0; i < values.length; i++) {
            if (values[i] != null) {
                savables[i] = new Savable[values[i].length][];
                for (int j = 0; j < values[i].length; j++) {
                    savables[i][j] = resolveIDs(values[i][j]);
                }
            } else savables[i] = null;
        }
        fieldData.put(field.alias, savables);
        return savables;
    } else
        return defVal;
}
 
Example #9
Source File: BinaryOutputCapsule.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void writeIntSavableMap(IntMap<? extends Savable> array)
        throws IOException {
    if (array == null) {
        write(NULL_OBJECT);
        return;
    }
    write(array.size());

    int[] keys = new int[array.size()];
    Savable[] values = new Savable[keys.length];
    int i = 0;
    for (Entry<? extends Savable> entry : array){
        keys[i] = entry.getKey();
        values[i] = entry.getValue();
        i++;
    }

    // write String array for keys
    write(keys);

    // write Savable array for values
    write(values);
}
 
Example #10
Source File: BinaryOutputCapsule.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void writeStringSavableMap(Map<String, ? extends Savable> array)
        throws IOException {
    if (array == null) {
        write(NULL_OBJECT);
        return;
    }
    write(array.size());

    // write String array for keys
    String[] keys = array.keySet().toArray(new String[] {});
    write(keys);

    // write Savable array for values
    Savable[] values = array.values().toArray(new Savable[] {});
    write(values);
}
 
Example #11
Source File: DOMOutputCapsule.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void writeSavableArrayList(ArrayList array, String name, ArrayList defVal) throws IOException {
    if (array == null) {
        return;
    }
    if (array.equals(defVal)) {
        return;
    }
    Element old = currentElement;
    Element el = appendElement(name);
    currentElement = el;
    el.setAttribute(XMLExporter.ATTRIBUTE_SIZE, String.valueOf(array.size()));
    for (Object o : array) {
            if(o == null) {
                    continue;
            }
            else if (o instanceof Savable) {
            Savable s = (Savable) o;
            write(s, s.getClass().getName(), null);
        } else {
            throw new ClassCastException("Not a Savable instance: " + o);
        }
    }
    currentElement = old;
}
 
Example #12
Source File: AbstractBlenderHelper.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * The method applies properties to the given spatial. The Properties
 * instance cannot be directly applied because the end-user might not have
 * the blender plugin jar file and thus receive ClassNotFoundException. The
 * values are set by name instead.
 * 
 * @param spatial
 *            the spatial that is to have properties applied
 * @param properties
 *            the properties to be applied
 */
protected void applyProperties(Spatial spatial, Properties properties) {
    List<String> propertyNames = properties.getSubPropertiesNames();
    if (propertyNames != null && propertyNames.size() > 0) {
        for (String propertyName : propertyNames) {
            Object value = properties.findValue(propertyName);
            if (value instanceof Savable || value instanceof Boolean || value instanceof String || value instanceof Float || value instanceof Integer || value instanceof Long) {
                spatial.setUserData(propertyName, value);
            } else if (value instanceof Double) {
                spatial.setUserData(propertyName, ((Double) value).floatValue());
            } else if (value instanceof int[]) {
                spatial.setUserData(propertyName, Arrays.toString((int[]) value));
            } else if (value instanceof float[]) {
                spatial.setUserData(propertyName, Arrays.toString((float[]) value));
            } else if (value instanceof double[]) {
                spatial.setUserData(propertyName, Arrays.toString((double[]) value));
            }
        }
    }
}
 
Example #13
Source File: BinaryInputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Savable[][] readSavableArray2D(String name, Savable[][] defVal)
        throws IOException {
    BinaryClassField field = cObj.nameFields.get(name);
    if (field == null ||!fieldData.containsKey(field.alias))
        return defVal;
    Object[][] values = (Object[][]) fieldData.get(field.alias);
    if (values instanceof ID[][]) {
        Savable[][] savables = new Savable[values.length][];
        for (int i = 0; i < values.length; i++) {
            if (values[i] != null) {
                savables[i] = resolveIDs(values[i]);
            } else savables[i] = null;
        }
        values = savables;
        fieldData.put(field.alias, values);
    }
    return (Savable[][]) values;
}
 
Example #14
Source File: BinaryInputCapsule.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public ArrayList[] readSavableArrayListArray(String name, ArrayList[] defVal)
        throws IOException {
    BinaryClassField field = cObj.nameFields.get(name);
    if (field == null || !fieldData.containsKey(field.alias))
        return defVal;
    Object value = fieldData.get(field.alias);
    if (value instanceof ID[][]) {
        // read 2D Savable array and convert to ArrayList array
        Savable[][] savables = readSavableArray2D(name, null);
        if (savables != null) {
            ArrayList[] arrayLists = new ArrayList[savables.length];
            for (int i = 0; i < savables.length; i++) {
                arrayLists[i] = savableArrayListFromArray(savables[i]);
            }
            value = arrayLists;
        } else
            value = defVal;
        fieldData.put(field.alias, value);
    }
    return (ArrayList[]) value;
}
 
Example #15
Source File: BoneLink.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * De-serialize this link, for example when loading from a J3O file.
 *
 * @param im importer (not null)
 * @throws IOException from importer
 */
@Override
public void read(JmeImporter im) throws IOException {
    super.read(im);
    InputCapsule ic = im.getCapsule(this);

    Savable[] tmp = ic.readSavableArray("managedBones", null);
    if (tmp == null) {
        managedBones = null;
    } else {
        managedBones = new Joint[tmp.length];
        for (int i = 0; i < tmp.length; ++i) {
            managedBones[i] = (Joint) tmp[i];
        }
    }

    submode = ic.readEnum("submode", KinematicSubmode.class,
            KinematicSubmode.Animated);
    prevBoneTransforms = RagUtils.readTransformArray(ic,
            "prevBoneTransforms");
    startBoneTransforms = RagUtils.readTransformArray(ic,
            "startBoneTransforms");
}
 
Example #16
Source File: BinaryOutputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void writeStringSavableMap(Map<String, ? extends Savable> array)
        throws IOException {
    if (array == null) {
        write(NULL_OBJECT);
        return;
    }
    write(array.size());

    // write String array for keys
    String[] keys = array.keySet().toArray(new String[] {});
    write(keys);

    // write Savable array for values
    Savable[] values = array.values().toArray(new Savable[] {});
    write(values);
}
 
Example #17
Source File: BinaryOutputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void writeIntSavableMap(IntMap<? extends Savable> array)
        throws IOException {
    if (array == null) {
        write(NULL_OBJECT);
        return;
    }
    write(array.size());

    int[] keys = new int[array.size()];
    Savable[] values = new Savable[keys.length];
    int i = 0;
    for (Entry<? extends Savable> entry : array){
        keys[i] = entry.getKey();
        values[i] = entry.getValue();
        i++;
    }

    // write String array for keys
    write(keys);

    // write Savable array for values
    write(values);
}
 
Example #18
Source File: DacConfiguration.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * De-serialize this control, for example when loading from a J3O file.
 *
 * @param im importer (not null)
 * @throws IOException from importer
 */
@Override
public void read(JmeImporter im) throws IOException {
    super.read(im);
    InputCapsule ic = im.getCapsule(this);

    damping = ic.readFloat("damping", 0.6f);
    eventDispatchImpulseThreshold
            = ic.readFloat("eventDispatchImpulseThreshold", 0f);

    jointMap.clear();
    blConfigMap.clear();
    String[] linkedBoneNames = ic.readStringArray("linkedBoneNames", null);
    Savable[] linkedBoneJoints
            = ic.readSavableArray("linkedBoneJoints", null);
    float[] blConfigs = ic.readFloatArray("blConfigs", null);
    for (int i = 0; i < linkedBoneNames.length; ++i) {
        String boneName = linkedBoneNames[i];
        RangeOfMotion rom = (RangeOfMotion) linkedBoneJoints[i];
        jointMap.put(boneName, rom);
        blConfigMap.put(boneName, blConfigs[i]);
    }

    torsoMass = ic.readFloat("torsoMass", 1f);
    gravityVector = (Vector3f) ic.readSavable("gravity", null);
}
 
Example #19
Source File: BinaryInputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public ArrayList readSavableArrayList(String name, ArrayList defVal)
        throws IOException {
    BinaryClassField field = cObj.nameFields.get(name);
    if (field == null || !fieldData.containsKey(field.alias))
        return defVal;
    Object value = fieldData.get(field.alias);
    if (value instanceof ID[]) {
        // read Savable array and convert to ArrayList
        Savable[] savables = readSavableArray(name, null);
        value = savableArrayListFromArray(savables);
        fieldData.put(field.alias, value);
    }
    return (ArrayList) value;
}
 
Example #20
Source File: BinaryExporter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Saves the object into memory then loads it from memory.
 * 
 * Used by tests to check if the persistence system is working.
 * 
 * @param <T> The type of savable.
 * @param assetManager AssetManager to load assets from.
 * @param object The object to save and then load.
 * @return A new instance that has been saved and loaded from the 
 * original object.
 */
public static <T extends Savable> T saveAndLoad(AssetManager assetManager, T object) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        BinaryExporter exporter = new BinaryExporter();
        exporter.save(object, baos);
        BinaryImporter importer = new BinaryImporter();
        importer.setAssetManager(assetManager);
        return (T) importer.load(baos.toByteArray());
    } catch (IOException ex) {
        // Should never happen.
        throw new AssertionError(ex);
    }
}
 
Example #21
Source File: BinaryOutputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void writeSavableMap(
        Map<? extends Savable, ? extends Savable> array) throws IOException {
    if (array == null) {
        write(NULL_OBJECT);
        return;
    }
    write(array.size());
    for (Savable key : array.keySet()) {
        write(new Savable[] { key, array.get(key) });
    }
}
 
Example #22
Source File: BinaryOutputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void write(Savable[][] objects) throws IOException {
    if (objects == null) {
        write(NULL_OBJECT);
        return;
    }
    write(objects.length);
    for (int x = 0; x < objects.length; x++) {
        write(objects[x]);
    }
}
 
Example #23
Source File: SavableSerializer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
    BufferInputStream in = new BufferInputStream(data);
    Savable s = importer.load(in);
    in.close();
    return (T) s;
}
 
Example #24
Source File: InstancedGeometry.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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];
    }
}
 
Example #25
Source File: BinaryOutputCapsule.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void write(Savable object, String name, Savable defVal)
        throws IOException {
    if (object == defVal)
        return;
    writeAlias(name, BinaryClassField.SAVABLE);
    write(object);
}
 
Example #26
Source File: BinaryOutputCapsule.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected void write(Savable[][] objects) throws IOException {
    if (objects == null) {
        write(NULL_OBJECT);
        return;
    }
    write(objects.length);
    for (int x = 0; x < objects.length; x++) {
        write(objects[x]);
    }
}
 
Example #27
Source File: BinaryInputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Savable[] resolveIDs(Object[] values) {
    if (values != null) {
        Savable[] savables = new Savable[values.length];
        for (int i = 0; i < values.length; i++) {
            final ID id = (ID) values[i];
            savables[i] = id != null ? importer.readObject(id.id) : null;
        }
        return savables;
    } else {
        return null;
    }
}
 
Example #28
Source File: BinaryInputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private IntMap<Savable> intSavableMapFromKV(int[] keys, Savable[] values) {
    if(keys == null || values == null) {
        return null;
    }

    IntMap<Savable> map = new IntMap<Savable>(keys.length);
    for (int x = 0; x < keys.length; x++)
        map.put(keys[x], values[x]);

    return map;
}
 
Example #29
Source File: XMLImporter.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Object load(AssetInfo info) throws IOException{
    assetManager = info.getManager();
    InputStream in = info.openStream();
    Savable obj = load(in);
    in.close();
    return obj;
}
 
Example #30
Source File: BinaryInputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Savable[] readSavableArray(String name, Savable[] defVal)
        throws IOException {
    BinaryClassField field = cObj.nameFields.get(name);
    if (field == null || !fieldData.containsKey(field.alias))
        return defVal;
    Object[] values = (Object[]) fieldData.get(field.alias);
    if (values instanceof ID[]) {
        values = resolveIDs(values);
        fieldData.put(field.alias, values);
        return (Savable[]) values;
    } else
        return defVal;
}