* Fix Blender 2.62 issue with uint64_t

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9186 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
Sha..om 13 years ago
parent 321c50c514
commit 06f5ad5bce
  1. 631
      engine/src/blender/com/jme3/scene/plugins/blender/file/Structure.java

@ -1,315 +1,316 @@
/* /*
* Copyright (c) 2009-2010 jMonkeyEngine * Copyright (c) 2009-2010 jMonkeyEngine
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are * modification, are permitted provided that the following conditions are
* met: * met:
* *
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* *
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* *
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.jme3.scene.plugins.blender.file; package com.jme3.scene.plugins.blender.file;
import com.jme3.scene.plugins.blender.BlenderContext; import com.jme3.scene.plugins.blender.BlenderContext;
import com.jme3.scene.plugins.blender.exceptions.BlenderFileException; import com.jme3.scene.plugins.blender.exceptions.BlenderFileException;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
* A class representing a single structure in the file. * A class representing a single structure in the file.
* @author Marcin Roguski * @author Marcin Roguski
*/ */
public class Structure implements Cloneable { public class Structure implements Cloneable {
/** The blender context. */ /** The blender context. */
private BlenderContext blenderContext; private BlenderContext blenderContext;
/** The address of the block that fills the structure. */ /** The address of the block that fills the structure. */
private transient Long oldMemoryAddress; private transient Long oldMemoryAddress;
/** The type of the structure. */ /** The type of the structure. */
private String type; private String type;
/** /**
* The fields of the structure. Each field consists of a pair: name-type. * The fields of the structure. Each field consists of a pair: name-type.
*/ */
private Field[] fields; private Field[] fields;
/** /**
* Constructor that copies the data of the structure. * Constructor that copies the data of the structure.
* @param structure * @param structure
* the structure to copy. * the structure to copy.
* @param blenderContext * @param blenderContext
* the blender context of the structure * the blender context of the structure
* @throws CloneNotSupportedException * @throws CloneNotSupportedException
* this exception should never be thrown * this exception should never be thrown
*/ */
private Structure(Structure structure, BlenderContext blenderContext) throws CloneNotSupportedException { private Structure(Structure structure, BlenderContext blenderContext) throws CloneNotSupportedException {
type = structure.type; type = structure.type;
fields = new Field[structure.fields.length]; fields = new Field[structure.fields.length];
for (int i = 0; i < fields.length; ++i) { for (int i = 0; i < fields.length; ++i) {
fields[i] = (Field) structure.fields[i].clone(); fields[i] = (Field) structure.fields[i].clone();
} }
this.blenderContext = blenderContext; this.blenderContext = blenderContext;
this.oldMemoryAddress = structure.oldMemoryAddress; this.oldMemoryAddress = structure.oldMemoryAddress;
} }
/** /**
* Constructor. Loads the structure from the given stream during instance creation. * Constructor. Loads the structure from the given stream during instance creation.
* @param inputStream * @param inputStream
* the stream we read the structure from * the stream we read the structure from
* @param names * @param names
* the names from which the name of structure and its fields will be taken * the names from which the name of structure and its fields will be taken
* @param types * @param types
* the names of types for the structure * the names of types for the structure
* @param blenderContext * @param blenderContext
* the blender context * the blender context
* @throws BlenderFileException * @throws BlenderFileException
* this exception occurs if the amount of fields, defined in the file, is negative * this exception occurs if the amount of fields, defined in the file, is negative
*/ */
public Structure(BlenderInputStream inputStream, String[] names, String[] types, BlenderContext blenderContext) throws BlenderFileException { public Structure(BlenderInputStream inputStream, String[] names, String[] types, BlenderContext blenderContext) throws BlenderFileException {
int nameIndex = inputStream.readShort(); int nameIndex = inputStream.readShort();
type = types[nameIndex]; type = types[nameIndex];
this.blenderContext = blenderContext; this.blenderContext = blenderContext;
int fieldsAmount = inputStream.readShort(); int fieldsAmount = inputStream.readShort();
if (fieldsAmount < 0) { if (fieldsAmount < 0) {
throw new BlenderFileException("The amount of fields of " + this.type + " structure cannot be negative!"); throw new BlenderFileException("The amount of fields of " + this.type + " structure cannot be negative!");
} }
if (fieldsAmount > 0) { if (fieldsAmount > 0) {
fields = new Field[fieldsAmount]; fields = new Field[fieldsAmount];
for (int i = 0; i < fieldsAmount; ++i) { for (int i = 0; i < fieldsAmount; ++i) {
int typeIndex = inputStream.readShort(); int typeIndex = inputStream.readShort();
nameIndex = inputStream.readShort(); nameIndex = inputStream.readShort();
fields[i] = new Field(names[nameIndex], types[typeIndex], blenderContext); fields[i] = new Field(names[nameIndex], types[typeIndex], blenderContext);
} }
} }
this.oldMemoryAddress = Long.valueOf(-1L); this.oldMemoryAddress = Long.valueOf(-1L);
} }
/** /**
* This method fills the structure with data. * This method fills the structure with data.
* @param inputStream * @param inputStream
* the stream we read data from, its read cursor should be placed at the start position of the data for the * the stream we read data from, its read cursor should be placed at the start position of the data for the
* structure * structure
* @throws BlenderFileException * @throws BlenderFileException
* an exception is thrown when the blend file is somehow invalid or corrupted * an exception is thrown when the blend file is somehow invalid or corrupted
*/ */
public void fill(BlenderInputStream inputStream) throws BlenderFileException { public void fill(BlenderInputStream inputStream) throws BlenderFileException {
int position = inputStream.getPosition(); int position = inputStream.getPosition();
inputStream.setPosition(position - 8 - inputStream.getPointerSize()); inputStream.setPosition(position - 8 - inputStream.getPointerSize());
this.oldMemoryAddress = Long.valueOf(inputStream.readPointer()); this.oldMemoryAddress = Long.valueOf(inputStream.readPointer());
inputStream.setPosition(position); inputStream.setPosition(position);
for (Field field : fields) { for (Field field : fields) {
field.fill(inputStream); field.fill(inputStream);
} }
} }
/** /**
* This method returns the value of the filed with a given name. * This method returns the value of the filed with a given name.
* @param fieldName * @param fieldName
* the name of the field * the name of the field
* @return the value of the field or null if no field with a given name is found * @return the value of the field or null if no field with a given name is found
*/ */
public Object getFieldValue(String fieldName) { public Object getFieldValue(String fieldName) {
for (Field field : fields) { for (Field field : fields) {
if (field.name.equalsIgnoreCase(fieldName)) { if (field.name.equalsIgnoreCase(fieldName)) {
return field.value; return field.value;
} }
} }
return null; return null;
} }
/** /**
* This method returns the value of the filed with a given name. The structure is considered to have flat fields * This method returns the value of the filed with a given name. The structure is considered to have flat fields
* only (no substructures). * only (no substructures).
* @param fieldName * @param fieldName
* the name of the field * the name of the field
* @return the value of the field or null if no field with a given name is found * @return the value of the field or null if no field with a given name is found
*/ */
public Object getFlatFieldValue(String fieldName) { public Object getFlatFieldValue(String fieldName) {
for (Field field : fields) { for (Field field : fields) {
Object value = field.value; Object value = field.value;
if (field.name.equalsIgnoreCase(fieldName)) { if (field.name.equalsIgnoreCase(fieldName)) {
return value; return value;
} else if (value instanceof Structure) { } else if (value instanceof Structure) {
value = ((Structure) value).getFlatFieldValue(fieldName); value = ((Structure) value).getFlatFieldValue(fieldName);
if (value != null) {//we can compare references here, since we use one static object as a NULL field value if (value != null) {//we can compare references here, since we use one static object as a NULL field value
return value; return value;
} }
} }
} }
return null; return null;
} }
/** /**
* This methos should be used on structures that are of a 'ListBase' type. It creates a List of structures that are * This methos should be used on structures that are of a 'ListBase' type. It creates a List of structures that are
* held by this structure within the blend file. * held by this structure within the blend file.
* @param blenderContext * @param blenderContext
* the blender context * the blender context
* @return a list of filled structures * @return a list of filled structures
* @throws BlenderFileException * @throws BlenderFileException
* this exception is thrown when the blend file structure is somehow invalid or corrupted * this exception is thrown when the blend file structure is somehow invalid or corrupted
* @throws IllegalArgumentException * @throws IllegalArgumentException
* this exception is thrown if the type of the structure is not 'ListBase' * this exception is thrown if the type of the structure is not 'ListBase'
*/ */
public List<Structure> evaluateListBase(BlenderContext blenderContext) throws BlenderFileException { public List<Structure> evaluateListBase(BlenderContext blenderContext) throws BlenderFileException {
if (!"ListBase".equals(this.type)) { if (!"ListBase".equals(this.type)) {
throw new IllegalStateException("This structure is not of type: 'ListBase'"); throw new IllegalStateException("This structure is not of type: 'ListBase'");
} }
Pointer first = (Pointer) this.getFieldValue("first"); Pointer first = (Pointer) this.getFieldValue("first");
Pointer last = (Pointer) this.getFieldValue("last"); Pointer last = (Pointer) this.getFieldValue("last");
long currentAddress = 0; long currentAddress = 0;
long lastAddress = last.getOldMemoryAddress(); long lastAddress = last.getOldMemoryAddress();
List<Structure> result = new LinkedList<Structure>(); List<Structure> result = new LinkedList<Structure>();
while (currentAddress != lastAddress) { while (currentAddress != lastAddress) {
currentAddress = first.getOldMemoryAddress(); currentAddress = first.getOldMemoryAddress();
Structure structure = first.fetchData(blenderContext.getInputStream()).get(0); Structure structure = first.fetchData(blenderContext.getInputStream()).get(0);
result.add(structure); result.add(structure);
first = (Pointer) structure.getFlatFieldValue("next"); first = (Pointer) structure.getFlatFieldValue("next");
} }
return result; return result;
} }
/** /**
* This method returns the type of the structure. * This method returns the type of the structure.
* @return the type of the structure * @return the type of the structure
*/ */
public String getType() { public String getType() {
return type; return type;
} }
/** /**
* This method returns the amount of fields for the current structure. * This method returns the amount of fields for the current structure.
* @return the amount of fields for the current structure * @return the amount of fields for the current structure
*/ */
public int getFieldsAmount() { public int getFieldsAmount() {
return fields.length; return fields.length;
} }
/** /**
* This method returns the field name of the given index. * This method returns the field name of the given index.
* @param fieldIndex * @param fieldIndex
* the index of the field * the index of the field
* @return the field name of the given index * @return the field name of the given index
*/ */
public String getFieldName(int fieldIndex) { public String getFieldName(int fieldIndex) {
return fields[fieldIndex].name; return fields[fieldIndex].name;
} }
/** /**
* This method returns the field type of the given index. * This method returns the field type of the given index.
* @param fieldIndex * @param fieldIndex
* the index of the field * the index of the field
* @return the field type of the given index * @return the field type of the given index
*/ */
public String getFieldType(int fieldIndex) { public String getFieldType(int fieldIndex) {
return fields[fieldIndex].type; return fields[fieldIndex].type;
} }
/** /**
* This method returns the address of the structure. The strucutre should be filled with data otherwise an exception * This method returns the address of the structure. The strucutre should be filled with data otherwise an exception
* is thrown. * is thrown.
* @return the address of the feature stored in this structure * @return the address of the feature stored in this structure
*/ */
public Long getOldMemoryAddress() { public Long getOldMemoryAddress() {
if (oldMemoryAddress.longValue() == -1L) { if (oldMemoryAddress.longValue() == -1L) {
throw new IllegalStateException("Call the 'fill' method and fill the structure with data first!"); throw new IllegalStateException("Call the 'fill' method and fill the structure with data first!");
} }
return oldMemoryAddress; return oldMemoryAddress;
} }
/** /**
* This method returns the name of the structure. If the structure has an ID field then the name is returned. * This method returns the name of the structure. If the structure has an ID field then the name is returned.
* Otherwise the name does not exists and the method returns null. * Otherwise the name does not exists and the method returns null.
* @return the name of the structure read from the ID field or null * @return the name of the structure read from the ID field or null
*/ */
public String getName() { public String getName() {
Object fieldValue = this.getFieldValue("ID"); Object fieldValue = this.getFieldValue("ID");
if(fieldValue instanceof Structure) { if(fieldValue instanceof Structure) {
Structure id = (Structure)fieldValue; Structure id = (Structure)fieldValue;
return id == null ? null : id.getFieldValue("name").toString().substring(2);//blender adds 2-charactes as a name prefix return id == null ? null : id.getFieldValue("name").toString().substring(2);//blender adds 2-charactes as a name prefix
} }
return null; return null;
} }
@Override @Override
public String toString() { public String toString() {
StringBuilder result = new StringBuilder("struct ").append(type).append(" {\n"); StringBuilder result = new StringBuilder("struct ").append(type).append(" {\n");
for (int i = 0; i < fields.length; ++i) { for (int i = 0; i < fields.length; ++i) {
result.append(fields[i].toString()).append('\n'); result.append(fields[i].toString()).append('\n');
} }
return result.append('}').toString(); return result.append('}').toString();
} }
@Override @Override
public Object clone() throws CloneNotSupportedException { public Object clone() throws CloneNotSupportedException {
return new Structure(this, blenderContext); return new Structure(this, blenderContext);
} }
/** /**
* This enum enumerates all known data types that can be found in the blend file. * This enum enumerates all known data types that can be found in the blend file.
* @author Marcin Roguski * @author Marcin Roguski
*/ */
/*package*/ /*package*/
static enum DataType { static enum DataType {
CHARACTER, SHORT, INTEGER, LONG, FLOAT, DOUBLE, VOID, STRUCTURE, POINTER; CHARACTER, SHORT, INTEGER, LONG, FLOAT, DOUBLE, VOID, STRUCTURE, POINTER;
/** The map containing the known primary types. */ /** The map containing the known primary types. */
private static final Map<String, DataType> PRIMARY_TYPES = new HashMap<String, DataType>(10); private static final Map<String, DataType> PRIMARY_TYPES = new HashMap<String, DataType>(10);
static { static {
PRIMARY_TYPES.put("char", CHARACTER); PRIMARY_TYPES.put("char", CHARACTER);
PRIMARY_TYPES.put("uchar", CHARACTER); PRIMARY_TYPES.put("uchar", CHARACTER);
PRIMARY_TYPES.put("short", SHORT); PRIMARY_TYPES.put("short", SHORT);
PRIMARY_TYPES.put("ushort", SHORT); PRIMARY_TYPES.put("ushort", SHORT);
PRIMARY_TYPES.put("int", INTEGER); PRIMARY_TYPES.put("int", INTEGER);
PRIMARY_TYPES.put("long", LONG); PRIMARY_TYPES.put("long", LONG);
PRIMARY_TYPES.put("ulong", LONG); PRIMARY_TYPES.put("ulong", LONG);
PRIMARY_TYPES.put("float", FLOAT); PRIMARY_TYPES.put("uint64_t", LONG);
PRIMARY_TYPES.put("double", DOUBLE); PRIMARY_TYPES.put("float", FLOAT);
PRIMARY_TYPES.put("void", VOID); PRIMARY_TYPES.put("double", DOUBLE);
} PRIMARY_TYPES.put("void", VOID);
}
/**
* This method returns the data type that is appropriate to the given type name. WARNING! The type recognition /**
* is case sensitive! * This method returns the data type that is appropriate to the given type name. WARNING! The type recognition
* @param type * is case sensitive!
* the type name of the data * @param type
* @param blenderContext * the type name of the data
* the blender context * @param blenderContext
* @return appropriate enum value to the given type name * the blender context
* @throws BlenderFileException * @return appropriate enum value to the given type name
* this exception is thrown if the given type name does not exist in the blend file * @throws BlenderFileException
*/ * this exception is thrown if the given type name does not exist in the blend file
public static DataType getDataType(String type, BlenderContext blenderContext) throws BlenderFileException { */
DataType result = PRIMARY_TYPES.get(type); public static DataType getDataType(String type, BlenderContext blenderContext) throws BlenderFileException {
if (result != null) { DataType result = PRIMARY_TYPES.get(type);
return result; if (result != null) {
} return result;
if (blenderContext.getDnaBlockData().hasStructure(type)) { }
return STRUCTURE; if (blenderContext.getDnaBlockData().hasStructure(type)) {
} return STRUCTURE;
throw new BlenderFileException("Unknown data type: " + type); }
} throw new BlenderFileException("Unknown data type: " + type);
} }
} }
}

Loading…
Cancel
Save