Refactoring: removing BlenderContext cleaning; reference to the instance of this class is lost instead after the loading is done.

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10875 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
experimental
Kae..pl 11 years ago
parent f49992d15b
commit 7987f5edf0
  1. 28
      engine/src/blender/com/jme3/scene/plugins/blender/BlenderContext.java
  2. 12
      engine/src/blender/com/jme3/scene/plugins/blender/BlenderLoader.java
  3. 3
      engine/src/blender/com/jme3/scene/plugins/blender/BlenderModelLoader.java
  4. 13
      engine/src/blender/com/jme3/scene/plugins/blender/file/BlenderInputStream.java

@ -38,7 +38,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Stack; import java.util.Stack;
import java.util.logging.Logger;
import com.jme3.animation.Bone; import com.jme3.animation.Bone;
import com.jme3.animation.Skeleton; import com.jme3.animation.Skeleton;
@ -64,8 +63,6 @@ import com.jme3.scene.plugins.blender.meshes.MeshContext;
* @author Marcin Roguski (Kaelthas) * @author Marcin Roguski (Kaelthas)
*/ */
public class BlenderContext { public class BlenderContext {
private static final Logger LOGGER = Logger.getLogger(BlenderContext.class.getName());
/** The blender file version. */ /** The blender file version. */
private int blenderVersion; private int blenderVersion;
/** The blender key. */ /** The blender key. */
@ -78,9 +75,10 @@ public class BlenderContext {
private BlenderInputStream inputStream; private BlenderInputStream inputStream;
/** The asset manager. */ /** The asset manager. */
private AssetManager assetManager; private AssetManager assetManager;
/** The blocks read from the file. */
protected List<FileBlockHeader> blocks;
/** /**
* A map containing the file block headers. The key is the old pointer * A map containing the file block headers. The key is the old memory address.
* address.
*/ */
private Map<Long, FileBlockHeader> fileBlockHeadersByOma = new HashMap<Long, FileBlockHeader>(); private Map<Long, FileBlockHeader> fileBlockHeadersByOma = new HashMap<Long, FileBlockHeader>();
/** A map containing the file block headers. The key is the block code. */ /** A map containing the file block headers. The key is the block code. */
@ -613,26 +611,6 @@ public class BlenderContext {
return markersMap == null ? null : markersMap.get(feature); return markersMap == null ? null : markersMap.get(feature);
} }
/**
* Clears all sotred resources and closes the blender input stream.
*/
public void dispose() {
LOGGER.fine("Disposing blender context resources.");
inputStream.forceClose();
loadedFeatures.clear();
loadedFeaturesByName.clear();
parentStack.clear();
constraints.clear();
animData.clear();
skeletons.clear();
meshContexts.clear();
boneContexts.clear();
helpers.clear();
fileBlockHeadersByOma.clear();
fileBlockHeadersByCode.clear();
markers.clear();
}
/** /**
* This enum defines what loaded data type user wants to retreive. It can be * This enum defines what loaded data type user wants to retreive. It can be
* either filled structure or already converted data. * either filled structure or already converted data.

@ -139,10 +139,11 @@ public class BlenderLoader implements AssetLoader {
loadingResults.addScene(this.toScene(sceneBlock.getStructure(blenderContext))); loadingResults.addScene(this.toScene(sceneBlock.getStructure(blenderContext)));
} }
blenderContext.dispose();
return loadingResults; return loadingResults;
} catch (BlenderFileException e) { } catch (BlenderFileException e) {
throw new IOException(e.getLocalizedMessage(), e); throw new IOException(e.getLocalizedMessage(), e);
} finally {
this.clear();
} }
} }
@ -267,4 +268,13 @@ public class BlenderLoader implements AssetLoader {
blenderContext.setSceneStructure(sceneFileBlock.getStructure(blenderContext)); blenderContext.setSceneStructure(sceneFileBlock.getStructure(blenderContext));
} }
} }
/**
* The internal data is only needed during loading so make it unreachable so that the GC can release
* that memory (which can be quite large amount).
*/
protected void clear() {
blenderContext = null;
blocks = null;
}
} }

@ -93,10 +93,11 @@ public class BlenderModelLoader extends BlenderLoader {
modelRoot.attachChild(node); modelRoot.attachChild(node);
} }
blenderContext.dispose();
return modelRoot; return modelRoot;
} catch (BlenderFileException e) { } catch (BlenderFileException e) {
throw new IOException(e.getLocalizedMessage(), e); throw new IOException(e.getLocalizedMessage(), e);
} finally {
this.clear();
} }
} }
} }

@ -106,7 +106,7 @@ public class BlenderInputStream extends InputStream {
this.readFileHeader(); this.readFileHeader();
} catch (BlenderFileException e) {// the file might be packed, don't panic, try one more time ;) } catch (BlenderFileException e) {// the file might be packed, don't panic, try one more time ;)
this.decompressFile(); this.decompressFile();
this.position = 0; position = 0;
this.readFileHeader(); this.readFileHeader();
} }
} }
@ -363,16 +363,9 @@ public class BlenderInputStream extends InputStream {
@Override @Override
public void close() throws IOException { public void close() throws IOException {
// this method is unimplemented because some loaders (ie. TGALoader) have flaws that close the stream given from the outside // this method is unimplemented because some loaders (ie. TGALoader) tend close the stream given from the outside
// because the images can be stored directly in the blender file then this stream is properly positioned and given to the loader // because the images can be stored directly in the blender file then this stream is properly positioned and given to the loader
// to read the image file, that is why we do not want it to be closed before the reading is done // to read the image file, that is why we do not want it to be closed before the reading is done
// to properly close the stream use forceClose() method // and anyway this stream is only a cached buffer, so it does not hold any open connection to anything
}
/**
* This method should be used to close the stream because some loaders may close the stream while reading resources from it.
*/
public void forceClose() {
cachedBuffer = null;
} }
} }

Loading…
Cancel
Save