From 9dfe1b12f3081d975c6ce0f21c8427fbed65a7b9 Mon Sep 17 00:00:00 2001 From: "Sha..rd" Date: Sun, 15 Jan 2012 23:17:10 +0000 Subject: [PATCH] * New J3O format version: fixed incorrect serialization of savable versions for certain class hierarchies * XML import/export now supports global format version git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9070 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../export/binary/BinaryInputCapsule.java | 2 +- .../core/com/jme3/export/FormatVersion.java | 2 +- .../com/jme3/export/SavableClassUtil.java | 33 +++++++++++-------- .../com/jme3/export/xml/DOMInputCapsule.java | 5 ++- .../com/jme3/export/xml/DOMOutputCapsule.java | 5 +-- .../xml/com/jme3/export/xml/XMLExporter.java | 22 ++++++------- .../xml/com/jme3/export/xml/XMLImporter.java | 3 +- 7 files changed, 41 insertions(+), 31 deletions(-) diff --git a/engine/src/core-plugins/com/jme3/export/binary/BinaryInputCapsule.java b/engine/src/core-plugins/com/jme3/export/binary/BinaryInputCapsule.java index b0b66efaa..cebd76495 100644 --- a/engine/src/core-plugins/com/jme3/export/binary/BinaryInputCapsule.java +++ b/engine/src/core-plugins/com/jme3/export/binary/BinaryInputCapsule.java @@ -260,7 +260,7 @@ final class BinaryInputCapsule implements InputCapsule { public int getSavableVersion(Class desiredClass){ return SavableClassUtil.getSavedSavableVersion(savable, desiredClass, - cObj.classHierarchyVersions); + cObj.classHierarchyVersions, importer.getFormatVersion()); } public BitSet readBitSet(String name, BitSet defVal) throws IOException { diff --git a/engine/src/core/com/jme3/export/FormatVersion.java b/engine/src/core/com/jme3/export/FormatVersion.java index 66814a914..843248d94 100644 --- a/engine/src/core/com/jme3/export/FormatVersion.java +++ b/engine/src/core/com/jme3/export/FormatVersion.java @@ -10,7 +10,7 @@ public final class FormatVersion { /** * Version number of the format */ - public static final int VERSION = 1; + public static final int VERSION = 2; /** * Signature of the format. Currently "JME3" as ASCII diff --git a/engine/src/core/com/jme3/export/SavableClassUtil.java b/engine/src/core/com/jme3/export/SavableClassUtil.java index 5ef47ee3e..db0dc2a72 100644 --- a/engine/src/core/com/jme3/export/SavableClassUtil.java +++ b/engine/src/core/com/jme3/export/SavableClassUtil.java @@ -82,13 +82,8 @@ public class SavableClassUtil { } public static boolean isImplementingSavable(Class clazz){ - Class[] interfaces = clazz.getInterfaces(); - for (Class interfaceClass : interfaces){ - if (interfaceClass == Savable.class){ - return true; - } - } - return false; + boolean result = Savable.class.isAssignableFrom(clazz); + return result; } public static int[] getSavableVersions(Class clazz) throws IOException{ @@ -109,7 +104,12 @@ public class SavableClassUtil { public static int getSavableVersion(Class clazz) throws IOException{ try { Field field = clazz.getField("SAVABLE_VERSION"); - return field.getInt(null); + Class declaringClass = (Class) field.getDeclaringClass(); + if (declaringClass == clazz){ + return field.getInt(null); + }else{ + return 0; // This class doesn't declare this field, e.g. version == 0 + } } catch (IllegalAccessException ex) { IOException ioEx = new IOException(); ioEx.initCause(ex); @@ -121,11 +121,11 @@ public class SavableClassUtil { } } - public static int getSavedSavableVersion(Object savable, Class desiredClass, int[] versions){ + public static int getSavedSavableVersion(Object savable, Class desiredClass, int[] versions, int formatVersion){ Class thisClass = savable.getClass(); int count = 0; - while (true) { + while (thisClass != desiredClass) { thisClass = thisClass.getSuperclass(); if (thisClass != null && SavableClassUtil.isImplementingSavable(thisClass)){ count ++; @@ -139,10 +139,15 @@ public class SavableClassUtil { " does not extend " + desiredClass.getName() + "!"); }else if (count >= versions.length){ - throw new IllegalArgumentException(savable.getClass().getName() + - " cannot access version of " + - desiredClass.getName() + - " because it doesn't implement Savable"); + if (formatVersion <= 1){ + System.out.println("BUGGY J3O ALERT"); + return 0; // for buggy versions of j3o + }else{ + throw new IllegalArgumentException(savable.getClass().getName() + + " cannot access version of " + + desiredClass.getName() + + " because it doesn't implement Savable"); + } } return versions[count]; } diff --git a/engine/src/xml/com/jme3/export/xml/DOMInputCapsule.java b/engine/src/xml/com/jme3/export/xml/DOMInputCapsule.java index 5db35b897..c3be20ad9 100644 --- a/engine/src/xml/com/jme3/export/xml/DOMInputCapsule.java +++ b/engine/src/xml/com/jme3/export/xml/DOMInputCapsule.java @@ -70,12 +70,15 @@ public class DOMInputCapsule implements InputCapsule { this.doc = doc; this.importer = importer; currentElem = doc.getDocumentElement(); + + String version = currentElem.getAttribute("format_version"); + importer.formatVersion = version.equals("") ? 0 : Integer.parseInt(version); } public int getSavableVersion(Class desiredClass) { if (classHierarchyVersions != null){ return SavableClassUtil.getSavedSavableVersion(savable, desiredClass, - classHierarchyVersions); + classHierarchyVersions, importer.getFormatVersion()); }else{ return 0; } diff --git a/engine/src/xml/com/jme3/export/xml/DOMOutputCapsule.java b/engine/src/xml/com/jme3/export/xml/DOMOutputCapsule.java index a35216c12..2baef1f7f 100644 --- a/engine/src/xml/com/jme3/export/xml/DOMOutputCapsule.java +++ b/engine/src/xml/com/jme3/export/xml/DOMOutputCapsule.java @@ -32,6 +32,7 @@ package com.jme3.export.xml; +import com.jme3.export.FormatVersion; import com.jme3.export.JmeExporter; import com.jme3.export.OutputCapsule; import com.jme3.export.Savable; @@ -77,9 +78,9 @@ public class DOMOutputCapsule implements OutputCapsule { * currentElement to be new Element, and returns the new Element as well */ private Element appendElement(String name) { - Element ret = null; - ret = doc.createElement(name); + Element ret = doc.createElement(name); if (currentElement == null) { + ret.setAttribute("format_version", Integer.toString(FormatVersion.VERSION)); doc.appendChild(ret); } else { currentElement.appendChild(ret); diff --git a/engine/src/xml/com/jme3/export/xml/XMLExporter.java b/engine/src/xml/com/jme3/export/xml/XMLExporter.java index 46d5b452d..6e7c395c3 100644 --- a/engine/src/xml/com/jme3/export/xml/XMLExporter.java +++ b/engine/src/xml/com/jme3/export/xml/XMLExporter.java @@ -47,15 +47,15 @@ import javax.xml.parsers.DocumentBuilderFactory; * @author Kai Rabien (hevee) - original author of the code.google.com jmexml project * @author Doug Daniels (dougnukem) - adjustments for jME 2.0 and Java 1.5 */ -public class XMLExporter implements JmeExporter{ - public static final String ELEMENT_MAPENTRY = "MapEntry"; - public static final String ELEMENT_KEY = "Key"; - public static final String ELEMENT_VALUE = "Value"; - public static final String ELEMENT_FLOATBUFFER = "FloatBuffer"; - public static final String ATTRIBUTE_SIZE = "size"; - - private DOMOutputCapsule domOut; +public class XMLExporter implements JmeExporter { + public static final String ELEMENT_MAPENTRY = "MapEntry"; + public static final String ELEMENT_KEY = "Key"; + public static final String ELEMENT_VALUE = "Value"; + public static final String ELEMENT_FLOATBUFFER = "FloatBuffer"; + public static final String ATTRIBUTE_SIZE = "size"; + + private DOMOutputCapsule domOut; public XMLExporter() { @@ -85,8 +85,8 @@ public class XMLExporter implements JmeExporter{ return domOut; } - public static XMLExporter getInstance() { - return new XMLExporter(); - } + public static XMLExporter getInstance() { + return new XMLExporter(); + } } diff --git a/engine/src/xml/com/jme3/export/xml/XMLImporter.java b/engine/src/xml/com/jme3/export/xml/XMLImporter.java index cf30b8c90..7a46b17db 100644 --- a/engine/src/xml/com/jme3/export/xml/XMLImporter.java +++ b/engine/src/xml/com/jme3/export/xml/XMLImporter.java @@ -54,12 +54,13 @@ public class XMLImporter implements JmeImporter { private AssetManager assetManager; private DOMInputCapsule domIn; + int formatVersion = 0; public XMLImporter() { } public int getFormatVersion() { - return 0; + return formatVersion; } public AssetManager getAssetManager(){