From 511c417fe7e955b707cbdbdc067c26b43fae0e66 Mon Sep 17 00:00:00 2001 From: "sha..rd" Date: Tue, 8 Nov 2011 04:56:15 +0000 Subject: [PATCH] * AssetEventListener now has an additional callback that can be used to detect when a dependent asset has failed to load. Since dependent assets that were not located have no exception thrown, detection of this event could be needed by some tools e.g. jMonkeyPlatform to indicate to the user that the asset may have not been loaded correctly. git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8597 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../com/jme3/asset/AssetEventListener.java | 13 +++++++++ .../com/jme3/asset/DesktopAssetManager.java | 10 +++++++ .../desktop/com/jme3/asset/ImplHandler.java | 29 ++++++++++++++++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/engine/src/core/com/jme3/asset/AssetEventListener.java b/engine/src/core/com/jme3/asset/AssetEventListener.java index 133c0e763..6907bcf59 100644 --- a/engine/src/core/com/jme3/asset/AssetEventListener.java +++ b/engine/src/core/com/jme3/asset/AssetEventListener.java @@ -60,5 +60,18 @@ public interface AssetEventListener { * @param key */ public void assetRequested(AssetKey key); + + /** + * Called when an asset dependency cannot be found for an asset. + * When an asset is loaded, each of its dependent assets that + * have failed to load due to a {@link AssetNotFoundException}, will cause + * an invocation of this callback. + * + * @param parentKey The key of the parent asset that is being loaded + * from within the user application. + * @param dependentAssetKey The asset key of the dependent asset that has + * failed to load. + */ + public void assetDependencyNotFound(AssetKey parentKey, AssetKey dependentAssetKey); } diff --git a/engine/src/desktop/com/jme3/asset/DesktopAssetManager.java b/engine/src/desktop/com/jme3/asset/DesktopAssetManager.java index 7cbd7a88f..e8fac3610 100644 --- a/engine/src/desktop/com/jme3/asset/DesktopAssetManager.java +++ b/engine/src/desktop/com/jme3/asset/DesktopAssetManager.java @@ -236,13 +236,23 @@ public class DesktopAssetManager implements AssetManager { AssetInfo info = handler.tryLocate(key); if (info == null){ + if (handler.getParentKey() != null && eventListener != null){ + // Inform event listener that an asset has failed to load. + // If the parent AssetLoader chooses not to propagate + // the exception, this is the only means of finding + // that something went wrong. + eventListener.assetDependencyNotFound(handler.getParentKey(), key); + } throw new AssetNotFoundException(key.toString()); } try { + handler.establishParentKey(key); o = loader.load(info); } catch (IOException ex) { throw new AssetLoadException("An exception has occured while loading asset: " + key, ex); + } finally { + handler.releaseParentKey(key); } if (o == null){ throw new AssetLoadException("Error occured while loading asset \"" + key + "\" using" + loader.getClass().getSimpleName()); diff --git a/engine/src/desktop/com/jme3/asset/ImplHandler.java b/engine/src/desktop/com/jme3/asset/ImplHandler.java index 366ff3324..6362ac865 100644 --- a/engine/src/desktop/com/jme3/asset/ImplHandler.java +++ b/engine/src/desktop/com/jme3/asset/ImplHandler.java @@ -51,6 +51,9 @@ public class ImplHandler { private final AssetManager owner; + private final ThreadLocal parentAssetKey + = new ThreadLocal(); + private final ArrayList genericLocators = new ArrayList(); @@ -103,6 +106,30 @@ public class ImplHandler { } } + /** + * Establishes the asset key that is used for tracking dependent assets + * that have failed to load. When set, the {@link DesktopAssetManager} + * gets a hint that it should suppress {@link AssetNotFoundException}s + * and instead call the listener callback (if set). + * + * @param key The parent key + */ + public void establishParentKey(AssetKey parentKey){ + if (parentAssetKey.get() == null){ + parentAssetKey.set(parentKey); + } + } + + public void releaseParentKey(AssetKey parentKey){ + if (parentAssetKey.get() == parentKey){ + parentAssetKey.set(null); + } + } + + public AssetKey getParentKey(){ + return parentAssetKey.get(); + } + /** * Attempts to locate the given resource name. * @param name The full name of the resource. @@ -111,7 +138,7 @@ public class ImplHandler { */ public AssetInfo tryLocate(AssetKey key){ synchronized (genericLocators){ - if (genericLocators.size() == 0) + if (genericLocators.isEmpty()) return null; for (ImplThreadLocal local : genericLocators){