aboutsummaryrefslogtreecommitdiff
path: root/engine/src/android/com/jme3/asset/plugins/AndroidLocator.java
diff options
context:
space:
mode:
authorScott Barta <sbarta@google.com>2012-03-01 12:35:35 -0800
committerScott Barta <sbarta@google.com>2012-03-01 12:40:08 -0800
commit59b2e6871c65f58fdad78cd7229c292f6a177578 (patch)
tree2d4e7bfc05b93f40b34675d77e403dd1c25efafd /engine/src/android/com/jme3/asset/plugins/AndroidLocator.java
parentf9b30489e75ac1eabc365064959804e99534f7ab (diff)
downloadjmonkeyengine-59b2e6871c65f58fdad78cd7229c292f6a177578.tar.gz
Adds the jMonkeyEngine library to the build.
Adds the jMonkeyEngine open source 3D game engine to the build. This is built as a static library and is only used by the Finsky client. Change-Id: I06a3f054df7b8a67757267d884854f70c5a16ca0
Diffstat (limited to 'engine/src/android/com/jme3/asset/plugins/AndroidLocator.java')
-rw-r--r--engine/src/android/com/jme3/asset/plugins/AndroidLocator.java90
1 files changed, 90 insertions, 0 deletions
diff --git a/engine/src/android/com/jme3/asset/plugins/AndroidLocator.java b/engine/src/android/com/jme3/asset/plugins/AndroidLocator.java
new file mode 100644
index 0000000..01b1cab
--- /dev/null
+++ b/engine/src/android/com/jme3/asset/plugins/AndroidLocator.java
@@ -0,0 +1,90 @@
+package com.jme3.asset.plugins;
+
+import com.jme3.asset.*;
+import com.jme3.system.android.JmeAndroidSystem;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class AndroidLocator implements AssetLocator {
+
+ private static final Logger logger = Logger.getLogger(AndroidLocator.class.getName());
+
+ private android.content.res.AssetManager androidManager;
+ private String rootPath = "";
+
+ private class AndroidAssetInfo extends AssetInfo {
+
+ private InputStream in;
+ private final String assetPath;
+
+ public AndroidAssetInfo(com.jme3.asset.AssetManager assetManager, AssetKey<?> key, String assetPath, InputStream in) {
+ super(assetManager, key);
+ this.assetPath = assetPath;
+ this.in = in;
+ }
+
+ @Override
+ public InputStream openStream() {
+ if (in != null){
+ // Reuse the already existing stream (only once)
+ InputStream in2 = in;
+ in = null;
+ return in2;
+ }else{
+ // Create a new stream for subsequent invocations.
+ try {
+ return androidManager.open(assetPath);
+ } catch (IOException ex) {
+ throw new AssetLoadException("Failed to open asset " + assetPath, ex);
+ }
+ }
+ }
+ }
+
+ private AndroidAssetInfo create(AssetManager assetManager, AssetKey key, String assetPath) throws IOException {
+ try {
+ InputStream in = androidManager.open(assetPath);
+ if (in == null){
+ return null;
+ }else{
+ return new AndroidAssetInfo(assetManager, key, assetPath, in);
+ }
+ } catch (IOException ex) {
+ // XXX: Prefer to show warning here?
+ // Should only surpress exceptions for "file missing" type errors.
+ return null;
+ }
+ }
+
+ public AndroidLocator() {
+ androidManager = JmeAndroidSystem.getResources().getAssets();
+ }
+
+ public void setRootPath(String rootPath) {
+ this.rootPath = rootPath;
+ }
+
+ @SuppressWarnings("rawtypes")
+ @Override
+ public AssetInfo locate(com.jme3.asset.AssetManager manager, AssetKey key) {
+ String assetPath = rootPath + key.getName();
+ // Fix path issues
+ if (assetPath.startsWith("/")) {
+ // Remove leading /
+ assetPath = assetPath.substring(1);
+ }
+ assetPath = assetPath.replace("//", "/");
+ try {
+ return create(manager, key, assetPath);
+ }catch (IOException ex){
+ // This is different handling than URL locator
+ // since classpath locating would return null at the getResource()
+ // call, otherwise there's a more critical error...
+ throw new AssetLoadException("Failed to open asset " + assetPath, ex);
+ }
+ }
+}