- add Filter support to AssetManager (thanks to @rectalogic)
git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9684 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
This commit is contained in:
parent
cf103e191d
commit
96b60664a9
@ -38,6 +38,7 @@ import com.jme3.audio.AudioData;
|
|||||||
import com.jme3.audio.AudioKey;
|
import com.jme3.audio.AudioKey;
|
||||||
import com.jme3.font.BitmapFont;
|
import com.jme3.font.BitmapFont;
|
||||||
import com.jme3.material.Material;
|
import com.jme3.material.Material;
|
||||||
|
import com.jme3.post.FilterPostProcessor;
|
||||||
import com.jme3.scene.Spatial;
|
import com.jme3.scene.Spatial;
|
||||||
import com.jme3.scene.plugins.OBJLoader;
|
import com.jme3.scene.plugins.OBJLoader;
|
||||||
import com.jme3.shader.Shader;
|
import com.jme3.shader.Shader;
|
||||||
@ -346,4 +347,22 @@ public interface AssetManager {
|
|||||||
* @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
|
* @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
|
||||||
*/
|
*/
|
||||||
public BitmapFont loadFont(String name);
|
public BitmapFont loadFont(String name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a filter *.j3f file with a FilterKey.
|
||||||
|
* @param key Asset key of the filter file to load
|
||||||
|
* @return The filter that was loaded
|
||||||
|
*
|
||||||
|
* @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
|
||||||
|
*/
|
||||||
|
public FilterPostProcessor loadFilter(FilterKey key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a filter *.j3f file with a FilterKey.
|
||||||
|
* @param name Asset name of the filter file to load
|
||||||
|
* @return The filter that was loaded
|
||||||
|
*
|
||||||
|
* @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
|
||||||
|
*/
|
||||||
|
public FilterPostProcessor loadFilter(String name);
|
||||||
}
|
}
|
||||||
|
@ -1,402 +1,411 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2009-2012 jMonkeyEngine
|
* Copyright (c) 2009-2012 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.asset;
|
package com.jme3.asset;
|
||||||
|
|
||||||
import com.jme3.asset.cache.AssetCache;
|
import com.jme3.asset.cache.AssetCache;
|
||||||
import com.jme3.asset.cache.SimpleAssetCache;
|
import com.jme3.asset.cache.SimpleAssetCache;
|
||||||
import com.jme3.audio.AudioData;
|
import com.jme3.audio.AudioData;
|
||||||
import com.jme3.audio.AudioKey;
|
import com.jme3.audio.AudioKey;
|
||||||
import com.jme3.font.BitmapFont;
|
import com.jme3.font.BitmapFont;
|
||||||
import com.jme3.material.Material;
|
import com.jme3.material.Material;
|
||||||
import com.jme3.scene.Spatial;
|
import com.jme3.post.FilterPostProcessor;
|
||||||
import com.jme3.shader.Shader;
|
import com.jme3.scene.Spatial;
|
||||||
import com.jme3.shader.ShaderKey;
|
import com.jme3.shader.Shader;
|
||||||
import com.jme3.texture.Texture;
|
import com.jme3.shader.ShaderKey;
|
||||||
import java.io.IOException;
|
import com.jme3.texture.Texture;
|
||||||
import java.io.InputStream;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
import java.net.URL;
|
||||||
import java.util.Arrays;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.Collections;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.List;
|
||||||
import java.util.logging.Level;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
/**
|
|
||||||
* <code>AssetManager</code> is the primary method for managing and loading
|
/**
|
||||||
* assets inside jME.
|
* <code>AssetManager</code> is the primary method for managing and loading
|
||||||
*
|
* assets inside jME.
|
||||||
* @author Kirill Vainer
|
*
|
||||||
*/
|
* @author Kirill Vainer
|
||||||
public class DesktopAssetManager implements AssetManager {
|
*/
|
||||||
|
public class DesktopAssetManager implements AssetManager {
|
||||||
private static final Logger logger = Logger.getLogger(AssetManager.class.getName());
|
|
||||||
|
private static final Logger logger = Logger.getLogger(AssetManager.class.getName());
|
||||||
private final ImplHandler handler = new ImplHandler(this);
|
|
||||||
|
private final ImplHandler handler = new ImplHandler(this);
|
||||||
private CopyOnWriteArrayList<AssetEventListener> eventListeners =
|
|
||||||
new CopyOnWriteArrayList<AssetEventListener>();
|
private CopyOnWriteArrayList<AssetEventListener> eventListeners =
|
||||||
|
new CopyOnWriteArrayList<AssetEventListener>();
|
||||||
private List<ClassLoader> classLoaders =
|
|
||||||
Collections.synchronizedList(new ArrayList<ClassLoader>());
|
private List<ClassLoader> classLoaders =
|
||||||
|
Collections.synchronizedList(new ArrayList<ClassLoader>());
|
||||||
public DesktopAssetManager(){
|
|
||||||
this(null);
|
public DesktopAssetManager(){
|
||||||
}
|
this(null);
|
||||||
|
}
|
||||||
@Deprecated
|
|
||||||
public DesktopAssetManager(boolean loadDefaults){
|
@Deprecated
|
||||||
this(Thread.currentThread().getContextClassLoader().getResource("com/jme3/asset/Desktop.cfg"));
|
public DesktopAssetManager(boolean loadDefaults){
|
||||||
}
|
this(Thread.currentThread().getContextClassLoader().getResource("com/jme3/asset/Desktop.cfg"));
|
||||||
|
}
|
||||||
public DesktopAssetManager(URL configFile){
|
|
||||||
if (configFile != null){
|
public DesktopAssetManager(URL configFile){
|
||||||
loadConfigFile(configFile);
|
if (configFile != null){
|
||||||
}
|
loadConfigFile(configFile);
|
||||||
logger.info("DesktopAssetManager created.");
|
}
|
||||||
}
|
logger.info("DesktopAssetManager created.");
|
||||||
|
}
|
||||||
private void loadConfigFile(URL configFile){
|
|
||||||
InputStream stream = null;
|
private void loadConfigFile(URL configFile){
|
||||||
try{
|
InputStream stream = null;
|
||||||
AssetConfig cfg = new AssetConfig(this);
|
try{
|
||||||
stream = configFile.openStream();
|
AssetConfig cfg = new AssetConfig(this);
|
||||||
cfg.loadText(stream);
|
stream = configFile.openStream();
|
||||||
}catch (IOException ex){
|
cfg.loadText(stream);
|
||||||
logger.log(Level.SEVERE, "Failed to load asset config", ex);
|
}catch (IOException ex){
|
||||||
}finally{
|
logger.log(Level.SEVERE, "Failed to load asset config", ex);
|
||||||
if (stream != null)
|
}finally{
|
||||||
try{
|
if (stream != null)
|
||||||
stream.close();
|
try{
|
||||||
}catch (IOException ex){
|
stream.close();
|
||||||
}
|
}catch (IOException ex){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public void addClassLoader(ClassLoader loader) {
|
|
||||||
classLoaders.add(loader);
|
public void addClassLoader(ClassLoader loader) {
|
||||||
}
|
classLoaders.add(loader);
|
||||||
|
}
|
||||||
public void removeClassLoader(ClassLoader loader) {
|
|
||||||
classLoaders.remove(loader);
|
public void removeClassLoader(ClassLoader loader) {
|
||||||
}
|
classLoaders.remove(loader);
|
||||||
|
}
|
||||||
public List<ClassLoader> getClassLoaders(){
|
|
||||||
return Collections.unmodifiableList(classLoaders);
|
public List<ClassLoader> getClassLoaders(){
|
||||||
}
|
return Collections.unmodifiableList(classLoaders);
|
||||||
|
}
|
||||||
public void addAssetEventListener(AssetEventListener listener) {
|
|
||||||
eventListeners.add(listener);
|
public void addAssetEventListener(AssetEventListener listener) {
|
||||||
}
|
eventListeners.add(listener);
|
||||||
|
}
|
||||||
public void removeAssetEventListener(AssetEventListener listener) {
|
|
||||||
eventListeners.remove(listener);
|
public void removeAssetEventListener(AssetEventListener listener) {
|
||||||
}
|
eventListeners.remove(listener);
|
||||||
|
}
|
||||||
public void clearAssetEventListeners() {
|
|
||||||
eventListeners.clear();
|
public void clearAssetEventListeners() {
|
||||||
}
|
eventListeners.clear();
|
||||||
|
}
|
||||||
public void setAssetEventListener(AssetEventListener listener){
|
|
||||||
eventListeners.clear();
|
public void setAssetEventListener(AssetEventListener listener){
|
||||||
eventListeners.add(listener);
|
eventListeners.clear();
|
||||||
}
|
eventListeners.add(listener);
|
||||||
|
}
|
||||||
public void registerLoader(Class<? extends AssetLoader> loader, String ... extensions){
|
|
||||||
handler.addLoader(loader, extensions);
|
public void registerLoader(Class<? extends AssetLoader> loader, String ... extensions){
|
||||||
if (logger.isLoggable(Level.FINER)){
|
handler.addLoader(loader, extensions);
|
||||||
logger.log(Level.FINER, "Registered loader: {0} for extensions {1}",
|
if (logger.isLoggable(Level.FINER)){
|
||||||
new Object[]{loader.getSimpleName(), Arrays.toString(extensions)});
|
logger.log(Level.FINER, "Registered loader: {0} for extensions {1}",
|
||||||
}
|
new Object[]{loader.getSimpleName(), Arrays.toString(extensions)});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public void registerLoader(String clsName, String ... extensions){
|
|
||||||
Class<? extends AssetLoader> clazz = null;
|
public void registerLoader(String clsName, String ... extensions){
|
||||||
try{
|
Class<? extends AssetLoader> clazz = null;
|
||||||
clazz = (Class<? extends AssetLoader>) Class.forName(clsName);
|
try{
|
||||||
}catch (ClassNotFoundException ex){
|
clazz = (Class<? extends AssetLoader>) Class.forName(clsName);
|
||||||
logger.log(Level.WARNING, "Failed to find loader: "+clsName, ex);
|
}catch (ClassNotFoundException ex){
|
||||||
}catch (NoClassDefFoundError ex){
|
logger.log(Level.WARNING, "Failed to find loader: "+clsName, ex);
|
||||||
logger.log(Level.WARNING, "Failed to find loader: "+clsName, ex);
|
}catch (NoClassDefFoundError ex){
|
||||||
}
|
logger.log(Level.WARNING, "Failed to find loader: "+clsName, ex);
|
||||||
if (clazz != null){
|
}
|
||||||
registerLoader(clazz, extensions);
|
if (clazz != null){
|
||||||
}
|
registerLoader(clazz, extensions);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public void unregisterLoader(Class<? extends AssetLoader> loaderClass) {
|
|
||||||
handler.removeLoader(loaderClass);
|
public void unregisterLoader(Class<? extends AssetLoader> loaderClass) {
|
||||||
if (logger.isLoggable(Level.FINER)){
|
handler.removeLoader(loaderClass);
|
||||||
logger.log(Level.FINER, "Unregistered loader: {0}",
|
if (logger.isLoggable(Level.FINER)){
|
||||||
loaderClass.getSimpleName());
|
logger.log(Level.FINER, "Unregistered loader: {0}",
|
||||||
}
|
loaderClass.getSimpleName());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public void registerLocator(String rootPath, Class<? extends AssetLocator> locatorClass){
|
|
||||||
handler.addLocator(locatorClass, rootPath);
|
public void registerLocator(String rootPath, Class<? extends AssetLocator> locatorClass){
|
||||||
if (logger.isLoggable(Level.FINER)){
|
handler.addLocator(locatorClass, rootPath);
|
||||||
logger.log(Level.FINER, "Registered locator: {0}",
|
if (logger.isLoggable(Level.FINER)){
|
||||||
locatorClass.getSimpleName());
|
logger.log(Level.FINER, "Registered locator: {0}",
|
||||||
}
|
locatorClass.getSimpleName());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public void registerLocator(String rootPath, String clsName){
|
|
||||||
Class<? extends AssetLocator> clazz = null;
|
public void registerLocator(String rootPath, String clsName){
|
||||||
try{
|
Class<? extends AssetLocator> clazz = null;
|
||||||
clazz = (Class<? extends AssetLocator>) Class.forName(clsName);
|
try{
|
||||||
}catch (ClassNotFoundException ex){
|
clazz = (Class<? extends AssetLocator>) Class.forName(clsName);
|
||||||
logger.log(Level.WARNING, "Failed to find locator: "+clsName, ex);
|
}catch (ClassNotFoundException ex){
|
||||||
}catch (NoClassDefFoundError ex){
|
logger.log(Level.WARNING, "Failed to find locator: "+clsName, ex);
|
||||||
logger.log(Level.WARNING, "Failed to find loader: "+clsName, ex);
|
}catch (NoClassDefFoundError ex){
|
||||||
}
|
logger.log(Level.WARNING, "Failed to find loader: "+clsName, ex);
|
||||||
if (clazz != null){
|
}
|
||||||
registerLocator(rootPath, clazz);
|
if (clazz != null){
|
||||||
}
|
registerLocator(rootPath, clazz);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public void unregisterLocator(String rootPath, Class<? extends AssetLocator> clazz){
|
|
||||||
handler.removeLocator(clazz, rootPath);
|
public void unregisterLocator(String rootPath, Class<? extends AssetLocator> clazz){
|
||||||
if (logger.isLoggable(Level.FINER)){
|
handler.removeLocator(clazz, rootPath);
|
||||||
logger.log(Level.FINER, "Unregistered locator: {0}",
|
if (logger.isLoggable(Level.FINER)){
|
||||||
clazz.getSimpleName());
|
logger.log(Level.FINER, "Unregistered locator: {0}",
|
||||||
}
|
clazz.getSimpleName());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public AssetInfo locateAsset(AssetKey<?> key){
|
|
||||||
AssetInfo info = handler.tryLocate(key);
|
public AssetInfo locateAsset(AssetKey<?> key){
|
||||||
if (info == null){
|
AssetInfo info = handler.tryLocate(key);
|
||||||
logger.log(Level.WARNING, "Cannot locate resource: {0}", key);
|
if (info == null){
|
||||||
}
|
logger.log(Level.WARNING, "Cannot locate resource: {0}", key);
|
||||||
return info;
|
}
|
||||||
}
|
return info;
|
||||||
|
}
|
||||||
public <T> T getFromCache(AssetKey<T> key) {
|
|
||||||
AssetCache cache = handler.getCache(key.getCacheType());
|
public <T> T getFromCache(AssetKey<T> key) {
|
||||||
if (cache != null) {
|
AssetCache cache = handler.getCache(key.getCacheType());
|
||||||
T asset = cache.getFromCache(key);
|
if (cache != null) {
|
||||||
if (asset != null) {
|
T asset = cache.getFromCache(key);
|
||||||
// Since getFromCache fills the load stack, it has to be popped
|
if (asset != null) {
|
||||||
cache.notifyNoAssetClone();
|
// Since getFromCache fills the load stack, it has to be popped
|
||||||
}
|
cache.notifyNoAssetClone();
|
||||||
return asset;
|
}
|
||||||
} else {
|
return asset;
|
||||||
throw new IllegalArgumentException("Key " + key + " specifies no cache.");
|
} else {
|
||||||
}
|
throw new IllegalArgumentException("Key " + key + " specifies no cache.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public <T> void addToCache(AssetKey<T> key, T asset) {
|
|
||||||
AssetCache cache = handler.getCache(key.getCacheType());
|
public <T> void addToCache(AssetKey<T> key, T asset) {
|
||||||
if (cache != null) {
|
AssetCache cache = handler.getCache(key.getCacheType());
|
||||||
cache.addToCache(key, asset);
|
if (cache != null) {
|
||||||
cache.notifyNoAssetClone();
|
cache.addToCache(key, asset);
|
||||||
} else {
|
cache.notifyNoAssetClone();
|
||||||
throw new IllegalArgumentException("Key " + key + " specifies no cache.");
|
} else {
|
||||||
}
|
throw new IllegalArgumentException("Key " + key + " specifies no cache.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public <T> boolean deleteFromCache(AssetKey<T> key) {
|
|
||||||
AssetCache cache = handler.getCache(key.getCacheType());
|
public <T> boolean deleteFromCache(AssetKey<T> key) {
|
||||||
if (cache != null) {
|
AssetCache cache = handler.getCache(key.getCacheType());
|
||||||
return cache.deleteFromCache(key);
|
if (cache != null) {
|
||||||
} else {
|
return cache.deleteFromCache(key);
|
||||||
throw new IllegalArgumentException("Key " + key + " specifies no cache.");
|
} else {
|
||||||
}
|
throw new IllegalArgumentException("Key " + key + " specifies no cache.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public void clearCache(){
|
|
||||||
handler.clearCache();
|
public void clearCache(){
|
||||||
if (logger.isLoggable(Level.FINER)){
|
handler.clearCache();
|
||||||
logger.log(Level.FINER, "All asset caches cleared.");
|
if (logger.isLoggable(Level.FINER)){
|
||||||
}
|
logger.log(Level.FINER, "All asset caches cleared.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* <font color="red">Thread-safe.</font>
|
/**
|
||||||
*
|
* <font color="red">Thread-safe.</font>
|
||||||
* @param <T>
|
*
|
||||||
* @param key
|
* @param <T>
|
||||||
* @return the loaded asset
|
* @param key
|
||||||
*/
|
* @return the loaded asset
|
||||||
public <T> T loadAsset(AssetKey<T> key){
|
*/
|
||||||
if (key == null)
|
public <T> T loadAsset(AssetKey<T> key){
|
||||||
throw new IllegalArgumentException("key cannot be null");
|
if (key == null)
|
||||||
|
throw new IllegalArgumentException("key cannot be null");
|
||||||
for (AssetEventListener listener : eventListeners){
|
|
||||||
listener.assetRequested(key);
|
for (AssetEventListener listener : eventListeners){
|
||||||
}
|
listener.assetRequested(key);
|
||||||
|
}
|
||||||
AssetCache cache = handler.getCache(key.getCacheType());
|
|
||||||
AssetProcessor proc = handler.getProcessor(key.getProcessorType());
|
AssetCache cache = handler.getCache(key.getCacheType());
|
||||||
|
AssetProcessor proc = handler.getProcessor(key.getProcessorType());
|
||||||
Object obj = cache != null ? cache.getFromCache(key) : null;
|
|
||||||
if (obj == null){
|
Object obj = cache != null ? cache.getFromCache(key) : null;
|
||||||
// Asset not in cache, load it from file system.
|
if (obj == null){
|
||||||
AssetLoader loader = handler.aquireLoader(key);
|
// Asset not in cache, load it from file system.
|
||||||
AssetInfo info = handler.tryLocate(key);
|
AssetLoader loader = handler.aquireLoader(key);
|
||||||
if (info == null){
|
AssetInfo info = handler.tryLocate(key);
|
||||||
if (handler.getParentKey() != null){
|
if (info == null){
|
||||||
// Inform event listener that an asset has failed to load.
|
if (handler.getParentKey() != null){
|
||||||
// If the parent AssetLoader chooses not to propagate
|
// Inform event listener that an asset has failed to load.
|
||||||
// the exception, this is the only means of finding
|
// If the parent AssetLoader chooses not to propagate
|
||||||
// that something went wrong.
|
// the exception, this is the only means of finding
|
||||||
for (AssetEventListener listener : eventListeners){
|
// that something went wrong.
|
||||||
listener.assetDependencyNotFound(handler.getParentKey(), key);
|
for (AssetEventListener listener : eventListeners){
|
||||||
}
|
listener.assetDependencyNotFound(handler.getParentKey(), key);
|
||||||
}
|
}
|
||||||
throw new AssetNotFoundException(key.toString());
|
}
|
||||||
}
|
throw new AssetNotFoundException(key.toString());
|
||||||
|
}
|
||||||
try {
|
|
||||||
handler.establishParentKey(key);
|
try {
|
||||||
obj = loader.load(info);
|
handler.establishParentKey(key);
|
||||||
} catch (IOException ex) {
|
obj = loader.load(info);
|
||||||
throw new AssetLoadException("An exception has occured while loading asset: " + key, ex);
|
} catch (IOException ex) {
|
||||||
} finally {
|
throw new AssetLoadException("An exception has occured while loading asset: " + key, ex);
|
||||||
handler.releaseParentKey(key);
|
} finally {
|
||||||
}
|
handler.releaseParentKey(key);
|
||||||
if (obj == null){
|
}
|
||||||
throw new AssetLoadException("Error occured while loading asset \"" + key + "\" using " + loader.getClass().getSimpleName());
|
if (obj == null){
|
||||||
}else{
|
throw new AssetLoadException("Error occured while loading asset \"" + key + "\" using " + loader.getClass().getSimpleName());
|
||||||
if (logger.isLoggable(Level.FINER)){
|
}else{
|
||||||
logger.log(Level.FINER, "Loaded {0} with {1}",
|
if (logger.isLoggable(Level.FINER)){
|
||||||
new Object[]{key, loader.getClass().getSimpleName()});
|
logger.log(Level.FINER, "Loaded {0} with {1}",
|
||||||
}
|
new Object[]{key, loader.getClass().getSimpleName()});
|
||||||
|
}
|
||||||
if (proc != null){
|
|
||||||
// do processing on asset before caching
|
if (proc != null){
|
||||||
obj = proc.postProcess(key, obj);
|
// do processing on asset before caching
|
||||||
}
|
obj = proc.postProcess(key, obj);
|
||||||
|
}
|
||||||
if (cache != null){
|
|
||||||
// At this point, obj should be of type T
|
if (cache != null){
|
||||||
cache.addToCache(key, (T) obj);
|
// At this point, obj should be of type T
|
||||||
}
|
cache.addToCache(key, (T) obj);
|
||||||
|
}
|
||||||
for (AssetEventListener listener : eventListeners){
|
|
||||||
listener.assetLoaded(key);
|
for (AssetEventListener listener : eventListeners){
|
||||||
}
|
listener.assetLoaded(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// object obj is the original asset
|
|
||||||
// create an instance for user
|
// object obj is the original asset
|
||||||
T clone = (T) obj;
|
// create an instance for user
|
||||||
if (clone instanceof CloneableSmartAsset){
|
T clone = (T) obj;
|
||||||
if (proc == null){
|
if (clone instanceof CloneableSmartAsset){
|
||||||
throw new IllegalStateException("Asset implements "
|
if (proc == null){
|
||||||
+ "CloneableSmartAsset but doesn't "
|
throw new IllegalStateException("Asset implements "
|
||||||
+ "have processor to handle cloning");
|
+ "CloneableSmartAsset but doesn't "
|
||||||
}else{
|
+ "have processor to handle cloning");
|
||||||
clone = (T) proc.createClone(obj);
|
}else{
|
||||||
if (cache != null && clone != obj){
|
clone = (T) proc.createClone(obj);
|
||||||
cache.registerAssetClone(key, clone);
|
if (cache != null && clone != obj){
|
||||||
} else{
|
cache.registerAssetClone(key, clone);
|
||||||
throw new IllegalStateException("Asset implements "
|
} else{
|
||||||
+ "CloneableSmartAsset but doesn't have cache or "
|
throw new IllegalStateException("Asset implements "
|
||||||
+ "was not cloned");
|
+ "CloneableSmartAsset but doesn't have cache or "
|
||||||
}
|
+ "was not cloned");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return clone;
|
|
||||||
}
|
return clone;
|
||||||
|
}
|
||||||
public Object loadAsset(String name){
|
|
||||||
return loadAsset(new AssetKey(name));
|
public Object loadAsset(String name){
|
||||||
}
|
return loadAsset(new AssetKey(name));
|
||||||
|
}
|
||||||
public Texture loadTexture(TextureKey key){
|
|
||||||
return (Texture) loadAsset(key);
|
public Texture loadTexture(TextureKey key){
|
||||||
}
|
return (Texture) loadAsset(key);
|
||||||
|
}
|
||||||
public Material loadMaterial(String name){
|
|
||||||
return (Material) loadAsset(new MaterialKey(name));
|
public Material loadMaterial(String name){
|
||||||
}
|
return (Material) loadAsset(new MaterialKey(name));
|
||||||
|
}
|
||||||
public Texture loadTexture(String name){
|
|
||||||
TextureKey key = new TextureKey(name, true);
|
public Texture loadTexture(String name){
|
||||||
key.setGenerateMips(true);
|
TextureKey key = new TextureKey(name, true);
|
||||||
Texture tex = loadTexture(key);
|
key.setGenerateMips(true);
|
||||||
logger.log(Level.FINE, "{0} - {1}", new Object[]{tex, tex.getMinFilter()});
|
Texture tex = loadTexture(key);
|
||||||
return tex;
|
logger.log(Level.FINE, "{0} - {1}", new Object[]{tex, tex.getMinFilter()});
|
||||||
}
|
return tex;
|
||||||
|
}
|
||||||
public AudioData loadAudio(AudioKey key){
|
|
||||||
return (AudioData) loadAsset(key);
|
public AudioData loadAudio(AudioKey key){
|
||||||
}
|
return (AudioData) loadAsset(key);
|
||||||
|
}
|
||||||
public AudioData loadAudio(String name){
|
|
||||||
return loadAudio(new AudioKey(name, false));
|
public AudioData loadAudio(String name){
|
||||||
}
|
return loadAudio(new AudioKey(name, false));
|
||||||
|
}
|
||||||
public BitmapFont loadFont(String name){
|
|
||||||
return (BitmapFont) loadAsset(new AssetKey(name));
|
public BitmapFont loadFont(String name){
|
||||||
}
|
return (BitmapFont) loadAsset(new AssetKey(name));
|
||||||
|
}
|
||||||
public Spatial loadModel(ModelKey key){
|
|
||||||
return (Spatial) loadAsset(key);
|
public Spatial loadModel(ModelKey key){
|
||||||
}
|
return (Spatial) loadAsset(key);
|
||||||
|
}
|
||||||
public Spatial loadModel(String name){
|
|
||||||
return loadModel(new ModelKey(name));
|
public Spatial loadModel(String name){
|
||||||
}
|
return loadModel(new ModelKey(name));
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Load a vertex/fragment shader combo.
|
public FilterPostProcessor loadFilter(FilterKey key){
|
||||||
*
|
return (FilterPostProcessor) loadAsset(key);
|
||||||
* @param key
|
}
|
||||||
* @return the loaded {@link Shader}
|
|
||||||
*/
|
public FilterPostProcessor loadFilter(String name){
|
||||||
public Shader loadShader(ShaderKey key){
|
return loadFilter(new FilterKey(name));
|
||||||
// cache abuse in method
|
}
|
||||||
// that doesn't use loaders/locators
|
|
||||||
AssetCache cache = handler.getCache(SimpleAssetCache.class);
|
/**
|
||||||
Shader shader = (Shader) cache.getFromCache(key);
|
* Load a vertex/fragment shader combo.
|
||||||
if (shader == null){
|
*
|
||||||
String vertName = key.getVertName();
|
* @param key
|
||||||
String fragName = key.getFragName();
|
* @return the loaded {@link Shader}
|
||||||
|
*/
|
||||||
String vertSource = (String) loadAsset(new AssetKey(vertName));
|
public Shader loadShader(ShaderKey key){
|
||||||
String fragSource = (String) loadAsset(new AssetKey(fragName));
|
// cache abuse in method
|
||||||
|
// that doesn't use loaders/locators
|
||||||
shader = new Shader();
|
AssetCache cache = handler.getCache(SimpleAssetCache.class);
|
||||||
shader.initialize();
|
Shader shader = (Shader) cache.getFromCache(key);
|
||||||
shader.addSource(Shader.ShaderType.Vertex, vertName, vertSource, key.getDefines().getCompiled(), key.getVertexShaderLanguage());
|
if (shader == null){
|
||||||
shader.addSource(Shader.ShaderType.Fragment, fragName, fragSource, key.getDefines().getCompiled(), key.getFragmentShaderLanguage());
|
String vertName = key.getVertName();
|
||||||
|
String fragName = key.getFragName();
|
||||||
cache.addToCache(key, shader);
|
|
||||||
}
|
String vertSource = (String) loadAsset(new AssetKey(vertName));
|
||||||
return shader;
|
String fragSource = (String) loadAsset(new AssetKey(fragName));
|
||||||
}
|
|
||||||
}
|
shader = new Shader();
|
||||||
|
shader.initialize();
|
||||||
|
shader.addSource(Shader.ShaderType.Vertex, vertName, vertSource, key.getDefines().getCompiled(), key.getVertexShaderLanguage());
|
||||||
|
shader.addSource(Shader.ShaderType.Fragment, fragName, fragSource, key.getDefines().getCompiled(), key.getFragmentShaderLanguage());
|
||||||
|
|
||||||
|
cache.addToCache(key, shader);
|
||||||
|
}
|
||||||
|
return shader;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
57
engine/src/core/com/jme3/asset/FilterKey.java
Normal file
57
engine/src/core/com/jme3/asset/FilterKey.java
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2012 jMonkeyEngine
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are
|
||||||
|
* met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||||
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
package com.jme3.asset;
|
||||||
|
|
||||||
|
import com.jme3.asset.cache.AssetCache;
|
||||||
|
import com.jme3.post.FilterPostProcessor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to load FilterPostProcessors which are not cached.
|
||||||
|
*
|
||||||
|
* @author Andrew Wason
|
||||||
|
*/
|
||||||
|
public class FilterKey extends AssetKey<FilterPostProcessor> {
|
||||||
|
|
||||||
|
public FilterKey(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FilterKey() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<? extends AssetCache> getCacheType(){
|
||||||
|
// Do not cache filter processors
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user