159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/* 259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 jMonkeyEngine 359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved. 459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without 659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are 759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met: 859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright 1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * notice, this list of conditions and the following disclaimer. 1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright 1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * notice, this list of conditions and the following disclaimer in the 1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * documentation and/or other materials provided with the distribution. 1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * may be used to endorse or promote products derived from this software 1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * without specific prior written permission. 1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage com.jme3.scene.plugins.blender.file; 3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.plugins.blender.BlenderContext; 3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.plugins.blender.exceptions.BlenderFileException; 3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.HashMap; 3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.LinkedList; 3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.List; 3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.Map; 4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/** 4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * A class representing a single structure in the file. 4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author Marcin Roguski 4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class Structure implements Cloneable { 4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** The blender context. */ 4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private BlenderContext blenderContext; 4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** The address of the block that fills the structure. */ 5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private transient Long oldMemoryAddress; 5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** The type of the structure. */ 5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private String type; 5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * The fields of the structure. Each field consists of a pair: name-type. 5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private Field[] fields; 5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Constructor that copies the data of the structure. 6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param structure 6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the structure to copy. 6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param blenderContext 6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the blender context of the structure 6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @throws CloneNotSupportedException 6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * this exception should never be thrown 6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private Structure(Structure structure, BlenderContext blenderContext) throws CloneNotSupportedException { 6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta type = structure.type; 6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta fields = new Field[structure.fields.length]; 7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (int i = 0; i < fields.length; ++i) { 7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta fields[i] = (Field) structure.fields[i].clone(); 7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta this.blenderContext = blenderContext; 7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta this.oldMemoryAddress = structure.oldMemoryAddress; 7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Constructor. Loads the structure from the given stream during instance creation. 7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param inputStream 8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the stream we read the structure from 8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param names 8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the names from which the name of structure and its fields will be taken 8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param types 8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the names of types for the structure 8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param blenderContext 8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the blender context 8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @throws BlenderFileException 8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * this exception occurs if the amount of fields, defined in the file, is negative 8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public Structure(BlenderInputStream inputStream, String[] names, String[] types, BlenderContext blenderContext) throws BlenderFileException { 9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta int nameIndex = inputStream.readShort(); 9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta type = types[nameIndex]; 9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta this.blenderContext = blenderContext; 9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta int fieldsAmount = inputStream.readShort(); 9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (fieldsAmount < 0) { 9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta throw new BlenderFileException("The amount of fields of " + this.type + " structure cannot be negative!"); 9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (fieldsAmount > 0) { 9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta fields = new Field[fieldsAmount]; 10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (int i = 0; i < fieldsAmount; ++i) { 10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta int typeIndex = inputStream.readShort(); 10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta nameIndex = inputStream.readShort(); 10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta fields[i] = new Field(names[nameIndex], types[typeIndex], blenderContext); 10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta this.oldMemoryAddress = Long.valueOf(-1L); 10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This method fills the structure with data. 11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param inputStream 11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the stream we read data from, its read cursor should be placed at the start position of the data for the 11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * structure 11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @throws BlenderFileException 11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * an exception is thrown when the blend file is somehow invalid or corrupted 11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void fill(BlenderInputStream inputStream) throws BlenderFileException { 11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta int position = inputStream.getPosition(); 11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta inputStream.setPosition(position - 8 - inputStream.getPointerSize()); 12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta this.oldMemoryAddress = Long.valueOf(inputStream.readPointer()); 12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta inputStream.setPosition(position); 12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (Field field : fields) { 12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta field.fill(inputStream); 12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This method returns the value of the filed with a given name. 12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param fieldName 13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the name of the field 13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @return the value of the field or null if no field with a given name is found 13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public Object getFieldValue(String fieldName) { 13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (Field field : fields) { 13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (field.name.equalsIgnoreCase(fieldName)) { 13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return field.value; 13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return null; 14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This method returns the value of the filed with a given name. The structure is considered to have flat fields 14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * only (no substructures). 14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param fieldName 14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the name of the field 14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @return the value of the field or null if no field with a given name is found 14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public Object getFlatFieldValue(String fieldName) { 15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (Field field : fields) { 15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Object value = field.value; 15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (field.name.equalsIgnoreCase(fieldName)) { 15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return value; 15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } else if (value instanceof Structure) { 15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta value = ((Structure) value).getFlatFieldValue(fieldName); 15659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (value != null) {//we can compare references here, since we use one static object as a NULL field value 15759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return value; 15859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 15959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 16059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 16159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return null; 16259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 16359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 16459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 16559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This methos should be used on structures that are of a 'ListBase' type. It creates a List of structures that are 16659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * held by this structure within the blend file. 16759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param blenderContext 16859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the blender context 16959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @return a list of filled structures 17059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @throws BlenderFileException 17159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * this exception is thrown when the blend file structure is somehow invalid or corrupted 17259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @throws IllegalArgumentException 17359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * this exception is thrown if the type of the structure is not 'ListBase' 17459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 17559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public List<Structure> evaluateListBase(BlenderContext blenderContext) throws BlenderFileException { 17659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (!"ListBase".equals(this.type)) { 17759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta throw new IllegalStateException("This structure is not of type: 'ListBase'"); 17859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 17959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Pointer first = (Pointer) this.getFieldValue("first"); 18059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Pointer last = (Pointer) this.getFieldValue("last"); 18159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta long currentAddress = 0; 18259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta long lastAddress = last.getOldMemoryAddress(); 18359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta List<Structure> result = new LinkedList<Structure>(); 18459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta while (currentAddress != lastAddress) { 18559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta currentAddress = first.getOldMemoryAddress(); 18659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Structure structure = first.fetchData(blenderContext.getInputStream()).get(0); 18759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta result.add(structure); 18859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta first = (Pointer) structure.getFlatFieldValue("next"); 18959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 19059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return result; 19159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 19259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 19359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 19459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This method returns the type of the structure. 19559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @return the type of the structure 19659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 19759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public String getType() { 19859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return type; 19959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 20059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 20159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 20259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This method returns the amount of fields for the current structure. 20359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @return the amount of fields for the current structure 20459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 20559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public int getFieldsAmount() { 20659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return fields.length; 20759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 20859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 20959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 21059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This method returns the field name of the given index. 21159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param fieldIndex 21259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the index of the field 21359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @return the field name of the given index 21459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 21559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public String getFieldName(int fieldIndex) { 21659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return fields[fieldIndex].name; 21759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 21859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 21959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 22059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This method returns the field type of the given index. 22159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param fieldIndex 22259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the index of the field 22359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @return the field type of the given index 22459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 22559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public String getFieldType(int fieldIndex) { 22659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return fields[fieldIndex].type; 22759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 22859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 22959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 23059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This method returns the address of the structure. The strucutre should be filled with data otherwise an exception 23159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * is thrown. 23259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @return the address of the feature stored in this structure 23359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 23459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public Long getOldMemoryAddress() { 23559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (oldMemoryAddress.longValue() == -1L) { 23659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta throw new IllegalStateException("Call the 'fill' method and fill the structure with data first!"); 23759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 23859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return oldMemoryAddress; 23959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 24059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 24159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/** 24259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This method returns the name of the structure. If the structure has an ID field then the name is returned. 24359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Otherwise the name does not exists and the method returns null. 24459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @return the name of the structure read from the ID field or null 24559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 24659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public String getName() { 24759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Object fieldValue = this.getFieldValue("ID"); 24859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if(fieldValue instanceof Structure) { 24959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Structure id = (Structure)fieldValue; 25059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return id == null ? null : id.getFieldValue("name").toString().substring(2);//blender adds 2-charactes as a name prefix 25159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 25259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return null; 25359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 25459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 25559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta @Override 25659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public String toString() { 25759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta StringBuilder result = new StringBuilder("struct ").append(type).append(" {\n"); 25859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (int i = 0; i < fields.length; ++i) { 25959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta result.append(fields[i].toString()).append('\n'); 26059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 26159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return result.append('}').toString(); 26259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 26359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 26459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta @Override 26559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public Object clone() throws CloneNotSupportedException { 26659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return new Structure(this, blenderContext); 26759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 26859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 26959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 27059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This enum enumerates all known data types that can be found in the blend file. 27159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author Marcin Roguski 27259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 27359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /*package*/ 27459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta static enum DataType { 27559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 27659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta CHARACTER, SHORT, INTEGER, LONG, FLOAT, DOUBLE, VOID, STRUCTURE, POINTER; 27759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** The map containing the known primary types. */ 27859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private static final Map<String, DataType> PRIMARY_TYPES = new HashMap<String, DataType>(10); 27959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 28059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta static { 28159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta PRIMARY_TYPES.put("char", CHARACTER); 28259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta PRIMARY_TYPES.put("uchar", CHARACTER); 28359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta PRIMARY_TYPES.put("short", SHORT); 28459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta PRIMARY_TYPES.put("ushort", SHORT); 28559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta PRIMARY_TYPES.put("int", INTEGER); 28659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta PRIMARY_TYPES.put("long", LONG); 28759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta PRIMARY_TYPES.put("ulong", LONG); 28859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta PRIMARY_TYPES.put("uint64_t", LONG); 28959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta PRIMARY_TYPES.put("float", FLOAT); 29059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta PRIMARY_TYPES.put("double", DOUBLE); 29159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta PRIMARY_TYPES.put("void", VOID); 29259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 29359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 29459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 29559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This method returns the data type that is appropriate to the given type name. WARNING! The type recognition 29659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * is case sensitive! 29759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param type 29859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the type name of the data 29959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param blenderContext 30059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the blender context 30159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @return appropriate enum value to the given type name 30259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @throws BlenderFileException 30359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * this exception is thrown if the given type name does not exist in the blend file 30459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 30559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public static DataType getDataType(String type, BlenderContext blenderContext) throws BlenderFileException { 30659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta DataType result = PRIMARY_TYPES.get(type); 30759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (result != null) { 30859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return result; 30959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 31059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (blenderContext.getDnaBlockData().hasStructure(type)) { 31159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return STRUCTURE; 31259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 31359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta throw new BlenderFileException("Unknown data type: " + type); 31459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 31559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 31659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta} 317