AssetManager: set locator path only once per thread

experimental
Kirill Vainer 9 years ago
parent 7659a7b986
commit 500f57a64f
  1. 21
      jme3-core/src/main/java/com/jme3/asset/ImplHandler.java
  2. 31
      jme3-examples/src/main/java/jme3test/asset/TestOnlineJar.java

@ -75,7 +75,7 @@ final class ImplHandler {
this.assetManager = assetManager; this.assetManager = assetManager;
} }
protected static class ImplThreadLocal<T> extends ThreadLocal { protected static class ImplThreadLocal<T> extends ThreadLocal<T> {
private final Class<T> type; private final Class<T> type;
private final String path; private final String path;
@ -112,9 +112,13 @@ final class ImplHandler {
} }
@Override @Override
protected Object initialValue(){ protected T initialValue(){
try { try {
return type.newInstance(); T obj = type.newInstance();
if (path != null) {
((AssetLocator)obj).setRootPath(path);
}
return obj;
} catch (InstantiationException ex) { } catch (InstantiationException ex) {
logger.log(Level.SEVERE,"Cannot create locator of type {0}, does" logger.log(Level.SEVERE,"Cannot create locator of type {0}, does"
+ " the class have an empty and publically accessible"+ + " the class have an empty and publically accessible"+
@ -169,15 +173,12 @@ final class ImplHandler {
return null; return null;
} }
for (ImplThreadLocal local : locatorsList){ for (ImplThreadLocal<AssetLocator> local : locatorsList){
AssetLocator locator = (AssetLocator) local.get(); AssetInfo info = local.get().locate(assetManager, key);
if (local.getPath() != null){ if (info != null) {
locator.setRootPath((String) local.getPath());
}
AssetInfo info = locator.locate(assetManager, key);
if (info != null)
return info; return info;
} }
}
return null; return null;
} }

@ -36,10 +36,10 @@ import com.jme3.app.SimpleApplication;
import com.jme3.asset.TextureKey; import com.jme3.asset.TextureKey;
import com.jme3.asset.plugins.HttpZipLocator; import com.jme3.asset.plugins.HttpZipLocator;
import com.jme3.material.Material; import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry; import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Quad; import com.jme3.scene.shape.Quad;
import com.jme3.texture.Texture; import com.jme3.texture.Texture;
import com.jme3.ui.Picture;
/** /**
* This tests loading a file from a jar stored online. * This tests loading a file from a jar stored online.
@ -59,22 +59,27 @@ public class TestOnlineJar extends SimpleApplication {
quadMesh.updateGeometry(1, 1, true); quadMesh.updateGeometry(1, 1, true);
Geometry quad = new Geometry("Textured Quad", quadMesh); Geometry quad = new Geometry("Textured Quad", quadMesh);
assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/town.zip", assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/town.zip",
HttpZipLocator.class); HttpZipLocator.class);
assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/wildhouse.zip",
HttpZipLocator.class);
TextureKey key = new TextureKey("grass.jpg", false); Picture pic1 = new Picture("Picture1");
key.setGenerateMips(true); pic1.move(0, 0, -1);
Texture tex = assetManager.loadTexture(key); pic1.setPosition(0, 0);
pic1.setWidth(128);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); pic1.setHeight(128);
mat.setTexture("ColorMap", tex); pic1.setImage(assetManager, "grass.jpg", false);
quad.setMaterial(mat); guiNode.attachChild(pic1);
float aspect = tex.getImage().getWidth() / (float) tex.getImage().getHeight();
quad.setLocalScale(new Vector3f(aspect * 1.5f, 1.5f, 1));
quad.center();
rootNode.attachChild(quad); Picture pic2 = new Picture("Picture1");
pic2.move(0, 0, -1);
pic2.setPosition(128, 0);
pic2.setWidth(128);
pic2.setHeight(128);
pic2.setImage(assetManager, "glasstile2.png", false);
guiNode.attachChild(pic2);
} }
} }

Loading…
Cancel
Save