use of com.jme3.export.Savable in project jmonkeyengine by jMonkeyEngine.
the class BinaryExporter method save.
public void save(Savable object, OutputStream os) throws IOException {
// reset some vars
aliasCount = 1;
idCount = 1;
classes.clear();
contentTable.clear();
locationTable.clear();
contentKeys.clear();
// write signature and version
os.write(ByteUtils.convertToBytes(FormatVersion.SIGNATURE));
os.write(ByteUtils.convertToBytes(FormatVersion.VERSION));
int id = processBinarySavable(object);
// write out tag table
int classTableSize = 0;
int classNum = classes.keySet().size();
// make all
int aliasSize = ((int) FastMath.log(classNum, 256) + 1);
// aliases a
// fixed width
os.write(ByteUtils.convertToBytes(classNum));
for (String key : classes.keySet()) {
BinaryClassObject bco = classes.get(key);
// write alias
byte[] aliasBytes = fixClassAlias(bco.alias, aliasSize);
os.write(aliasBytes);
classTableSize += aliasSize;
// jME3 NEW: Write class hierarchy version numbers
os.write(bco.classHierarchyVersions.length);
for (int version : bco.classHierarchyVersions) {
os.write(ByteUtils.convertToBytes(version));
}
classTableSize += 1 + bco.classHierarchyVersions.length * 4;
// write classname size & classname
byte[] classBytes = key.getBytes();
os.write(ByteUtils.convertToBytes(classBytes.length));
os.write(classBytes);
classTableSize += 4 + classBytes.length;
// for each field, write alias, type, and name
os.write(ByteUtils.convertToBytes(bco.nameFields.size()));
for (String fieldName : bco.nameFields.keySet()) {
BinaryClassField bcf = bco.nameFields.get(fieldName);
os.write(bcf.alias);
os.write(bcf.type);
// write classname size & classname
byte[] fNameBytes = fieldName.getBytes();
os.write(ByteUtils.convertToBytes(fNameBytes.length));
os.write(fNameBytes);
classTableSize += 2 + 4 + fNameBytes.length;
}
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
// write out data to a seperate stream
int location = 0;
// keep track of location for each piece
HashMap<String, ArrayList<BinaryIdContentPair>> alreadySaved = new HashMap<String, ArrayList<BinaryIdContentPair>>(contentTable.size());
for (Savable savable : contentKeys) {
// look back at previous written data for matches
String savableName = savable.getClass().getName();
BinaryIdContentPair pair = contentTable.get(savable);
ArrayList<BinaryIdContentPair> bucket = alreadySaved.get(savableName + getChunk(pair));
int prevLoc = findPrevMatch(pair, bucket);
if (prevLoc != -1) {
locationTable.put(pair.getId(), prevLoc);
continue;
}
locationTable.put(pair.getId(), location);
if (bucket == null) {
bucket = new ArrayList<BinaryIdContentPair>();
alreadySaved.put(savableName + getChunk(pair), bucket);
}
bucket.add(pair);
byte[] aliasBytes = fixClassAlias(classes.get(savableName).alias, aliasSize);
out.write(aliasBytes);
location += aliasSize;
BinaryOutputCapsule cap = contentTable.get(savable).getContent();
out.write(ByteUtils.convertToBytes(cap.bytes.length));
// length of bytes
location += 4;
out.write(cap.bytes);
location += cap.bytes.length;
}
// write out location table
// tag/location
int numLocations = locationTable.keySet().size();
os.write(ByteUtils.convertToBytes(numLocations));
int locationTableSize = 0;
for (Integer key : locationTable.keySet()) {
os.write(ByteUtils.convertToBytes(key));
os.write(ByteUtils.convertToBytes(locationTable.get(key)));
locationTableSize += 8;
}
// write out number of root ids - hardcoded 1 for now
os.write(ByteUtils.convertToBytes(1));
// write out root id
os.write(ByteUtils.convertToBytes(id));
// append stream to the output stream
out.writeTo(os);
out = null;
os = null;
if (debug) {
logger.fine("Stats:");
logger.log(Level.FINE, "classes: {0}", classNum);
logger.log(Level.FINE, "class table: {0} bytes", classTableSize);
logger.log(Level.FINE, "objects: {0}", numLocations);
logger.log(Level.FINE, "location table: {0} bytes", locationTableSize);
logger.log(Level.FINE, "data: {0} bytes", location);
}
}
use of com.jme3.export.Savable in project jmonkeyengine by jMonkeyEngine.
the class XMLImporter method load.
public Savable load(File f) throws IOException {
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
Savable sav = load(fis);
return sav;
} finally {
if (fis != null)
fis.close();
}
}
use of com.jme3.export.Savable in project jmonkeyengine by jMonkeyEngine.
the class Spatial method oldClone.
/**
* The old clone() method that did not use the new Cloner utility.
*/
public Spatial oldClone(boolean cloneMaterial) {
try {
Spatial clone = (Spatial) super.clone();
if (worldBound != null) {
clone.worldBound = worldBound.clone();
}
clone.worldLights = worldLights.clone();
clone.localLights = localLights.clone();
// Set the new owner of the light lists
clone.localLights.setOwner(clone);
clone.worldLights.setOwner(clone);
clone.worldOverrides = new SafeArrayList<>(MatParamOverride.class);
clone.localOverrides = new SafeArrayList<>(MatParamOverride.class);
for (MatParamOverride override : localOverrides) {
clone.localOverrides.add((MatParamOverride) override.clone());
}
// No need to force cloned to update.
// This node already has the refresh flags
// set below so it will have to update anyway.
clone.worldTransform = worldTransform.clone();
clone.localTransform = localTransform.clone();
if (clone instanceof Node) {
Node node = (Node) this;
Node nodeClone = (Node) clone;
nodeClone.children = new SafeArrayList<Spatial>(Spatial.class);
for (Spatial child : node.children) {
Spatial childClone = child.clone(cloneMaterial);
childClone.parent = nodeClone;
nodeClone.children.add(childClone);
}
}
clone.parent = null;
clone.setBoundRefresh();
clone.setTransformRefresh();
clone.setLightListRefresh();
clone.setMatParamOverrideRefresh();
clone.controls = new SafeArrayList<Control>(Control.class);
for (int i = 0; i < controls.size(); i++) {
Control newControl = controls.get(i).cloneForSpatial(clone);
newControl.setSpatial(clone);
clone.controls.add(newControl);
}
if (userData != null) {
clone.userData = (HashMap<String, Savable>) userData.clone();
}
return clone;
} catch (CloneNotSupportedException ex) {
throw new AssertionError();
}
}
use of com.jme3.export.Savable in project jmonkeyengine by jMonkeyEngine.
the class DOMInputCapsule method readSavableArray.
public Savable[] readSavableArray(String name, Savable[] defVal) throws IOException {
Savable[] ret = defVal;
try {
Element tmpEl = findChildElement(currentElem, name);
if (tmpEl == null) {
return defVal;
}
String sizeString = tmpEl.getAttribute("size");
List<Savable> savables = new ArrayList<Savable>();
for (currentElem = findFirstChildElement(tmpEl); currentElem != null; currentElem = findNextSiblingElement(currentElem)) {
savables.add(readSavableFromCurrentElem(null));
}
if (sizeString.length() > 0) {
int requiredSize = Integer.parseInt(sizeString);
if (savables.size() != requiredSize)
throw new IOException("Wrong number of Savables for '" + name + "'. size says " + requiredSize + ", data contains " + savables.size());
}
ret = savables.toArray(new Savable[0]);
currentElem = (Element) tmpEl.getParentNode();
return ret;
} catch (IOException ioe) {
throw ioe;
} catch (Exception e) {
IOException io = new IOException(e.toString());
io.initCause(e);
throw io;
}
}
use of com.jme3.export.Savable in project jmonkeyengine by jMonkeyEngine.
the class DOMInputCapsule method readSavableArrayListArray.
public ArrayList<Savable>[] readSavableArrayListArray(String name, ArrayList[] defVal) throws IOException {
try {
Element tmpEl = findChildElement(currentElem, name);
if (tmpEl == null) {
return defVal;
}
currentElem = tmpEl;
String sizeString = tmpEl.getAttribute("size");
int requiredSize = (sizeString.length() > 0) ? Integer.parseInt(sizeString) : -1;
ArrayList<Savable> sal;
List<ArrayList<Savable>> savableArrayLists = new ArrayList<ArrayList<Savable>>();
int i = -1;
while (true) {
sal = readSavableArrayList("SavableArrayList_" + ++i, null);
if (sal == null && savableArrayLists.size() >= requiredSize)
break;
savableArrayLists.add(sal);
}
if (requiredSize > -1 && savableArrayLists.size() != requiredSize)
throw new IOException("String array contains wrong element count. " + "Specified size " + requiredSize + ", data contains " + savableArrayLists.size());
currentElem = (Element) tmpEl.getParentNode();
return savableArrayLists.toArray(new ArrayList[0]);
} catch (IOException ioe) {
throw ioe;
} catch (NumberFormatException nfe) {
IOException io = new IOException(nfe.toString());
io.initCause(nfe);
throw io;
} catch (DOMException de) {
IOException io = new IOException(de.toString());
io.initCause(de);
throw io;
}
}
Aggregations