Quellcode durchsuchen

SDK:
- improve getAssetFileObject to include items from jar files

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10234 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

nor..67 vor 12 Jahren
Ursprung
Commit
030d138174
1 geänderte Dateien mit 51 neuen und 12 gelöschten Zeilen
  1. 51 12
      jme3-core/src/com/jme3/gde/core/assets/ProjectAssetManager.java

+ 51 - 12
jme3-core/src/com/jme3/gde/core/assets/ProjectAssetManager.java

@@ -127,18 +127,6 @@ public class ProjectAssetManager extends DesktopAssetManager {
         this(null);
     }
 
-    /**
-     * Returns the
-     * <code>FileObject</code> for a given asset key, or null if no such asset
-     * exists. TODO: Only works for real files in the asset folder atm
-     *
-     * @param assetKey The asset key to get the file object for
-     * @return Either a FileObject for the asset or null if not found.
-     */
-    public FileObject getAssetFileObject(AssetKey assetKey) {
-        return getAssetFolder().getFileObject(assetKey.getName());
-    }
-
     private void clearClassLoader() {
         if (jarItems.isEmpty() && classPathItems.isEmpty()) {
             return;
@@ -282,6 +270,57 @@ public class ProjectAssetManager extends DesktopAssetManager {
         });
     }
 
+    /**
+     * Returns the
+     * <code>FileObject</code> for a given asset path, or null if no such asset
+     * exists. First looks in the asset folder(s) for the file, then proceeds to
+     * scan the classpath folders and jar files for it.The returned FileObject
+     * might be inside a jar file and not writeable!
+     *
+     * @param assetKey The asset key to get the file object for
+     * @return Either a FileObject for the asset or null if not found.
+     */
+    public FileObject getAssetFileObject(AssetKey assetKey) {
+        String name = assetKey.getName();
+        return getAssetFileObject(name);
+    }
+
+    /**
+     * Returns the
+     * <code>FileObject</code> for a given asset path, or null if no such asset
+     * exists. First looks in the asset folder(s) for the file, then proceeds to
+     * scan the classpath folders and jar files for it.The returned FileObject
+     * might be inside a jar file and not writeable!
+     *
+     * @param name The asset name to get the file object for
+     * @return Either a FileObject for the asset or null if not found.
+     */
+    public FileObject getAssetFileObject(String name) {
+        assert (name != null);
+        FileObject file = getAssetFolder().getFileObject(name);
+        if (file != null) {
+            return file;
+        }
+        synchronized (classPathItems) {
+            // TODO I need to find out if classPathItems contains all jars added to a project
+            Iterator<ClassPathItem> classPathItemsIter = classPathItems.iterator();
+            while (classPathItemsIter.hasNext()) {
+                ClassPathItem classPathItem = classPathItemsIter.next();
+                FileObject jarFile = classPathItem.object;
+
+                Enumeration<FileObject> jarEntry = (Enumeration<FileObject>) jarFile.getChildren(true);
+                while (jarEntry.hasMoreElements()) {
+                    FileObject jarEntryAsset = jarEntry.nextElement();
+                    String path = jarEntryAsset.getPath();
+                    if (!path.startsWith("/") && path.equals(name)) {
+                        return jarEntryAsset;
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
     public FileObject createAsset(String path) {
         return createAsset(path, null);
     }