Search in sources :

Example 51 with Structure

use of com.jme3.scene.plugins.blender.file.Structure in project jmonkeyengine by jMonkeyEngine.

the class MapSerializer method readObject.

/*

    Structure:

    struct Map {
        INT length
        BYTE flags = { 0x01 = all keys have the same type,
                       0x02 = all values have the same type }
        if (flags has 0x01 set)
            SHORT keyType
        if (flags has 0x02 set)
            SHORT valType

        struct MapEntry[length] entries {
            if (flags does not have 0x01 set)
                SHORT keyType
            OBJECT key

            if (flags does not have 0x02 set)
                SHORT valType
            OBJECT value
        }
    }

     */
@SuppressWarnings("unchecked")
public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
    int length = data.getInt();
    Map map;
    try {
        map = (Map) c.newInstance();
    } catch (Exception e) {
        log.log(Level.WARNING, "[Serializer][???] Could not determine map type. Using HashMap.");
        map = new HashMap();
    }
    if (length == 0)
        return (T) map;
    int flags = data.get() & 0xff;
    boolean uniqueKeys = (flags & 0x01) == 0;
    boolean uniqueVals = (flags & 0x02) == 0;
    Class keyClazz = null;
    Class valClazz = null;
    Serializer keySerial = null;
    Serializer valSerial = null;
    if (!uniqueKeys) {
        SerializerRegistration reg = Serializer.readClass(data);
        keyClazz = reg.getType();
        keySerial = reg.getSerializer();
    }
    if (!uniqueVals) {
        SerializerRegistration reg = Serializer.readClass(data);
        valClazz = reg.getType();
        valSerial = reg.getSerializer();
    }
    for (int i = 0; i < length; i++) {
        Object key;
        Object value;
        if (uniqueKeys) {
            key = Serializer.readClassAndObject(data);
        } else {
            key = keySerial.readObject(data, keyClazz);
        }
        if (uniqueVals) {
            value = Serializer.readClassAndObject(data);
        } else {
            value = valSerial.readObject(data, valClazz);
        }
        map.put(key, value);
    }
    return (T) map;
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) IOException(java.io.IOException) SerializerRegistration(com.jme3.network.serializing.SerializerRegistration) Serializer(com.jme3.network.serializing.Serializer)

Aggregations

Structure (com.jme3.scene.plugins.blender.file.Structure)34 Pointer (com.jme3.scene.plugins.blender.file.Pointer)30 ArrayList (java.util.ArrayList)16 Vector3f (com.jme3.math.Vector3f)8 BlenderFileException (com.jme3.scene.plugins.blender.file.BlenderFileException)7 DynamicArray (com.jme3.scene.plugins.blender.file.DynamicArray)7 FileBlockHeader (com.jme3.scene.plugins.blender.file.FileBlockHeader)7 List (java.util.List)6 Map (java.util.Map)6 TemporalMesh (com.jme3.scene.plugins.blender.meshes.TemporalMesh)5 ObjectHelper (com.jme3.scene.plugins.blender.objects.ObjectHelper)5 HashMap (java.util.HashMap)5 ColorRGBA (com.jme3.math.ColorRGBA)4 Node (com.jme3.scene.Node)4 Texture (com.jme3.texture.Texture)4 CameraNode (com.jme3.scene.CameraNode)3 LightNode (com.jme3.scene.LightNode)3 Spatial (com.jme3.scene.Spatial)3 AnimationHelper (com.jme3.scene.plugins.blender.animations.AnimationHelper)3 ConstIpo (com.jme3.scene.plugins.blender.animations.Ipo.ConstIpo)3