From 500f57a64fb3ad75820120f5115bc7f14c3548a6 Mon Sep 17 00:00:00 2001 From: Kirill Vainer Date: Mon, 14 Sep 2015 23:19:43 -0400 Subject: [PATCH] AssetManager: set locator path only once per thread --- .../main/java/com/jme3/asset/ImplHandler.java | 21 ++++++----- .../java/jme3test/asset/TestOnlineJar.java | 37 +++++++++++-------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/asset/ImplHandler.java b/jme3-core/src/main/java/com/jme3/asset/ImplHandler.java index 73e6d8df3..3bbd19c14 100644 --- a/jme3-core/src/main/java/com/jme3/asset/ImplHandler.java +++ b/jme3-core/src/main/java/com/jme3/asset/ImplHandler.java @@ -75,7 +75,7 @@ final class ImplHandler { this.assetManager = assetManager; } - protected static class ImplThreadLocal extends ThreadLocal { + protected static class ImplThreadLocal extends ThreadLocal { private final Class type; private final String path; @@ -112,9 +112,13 @@ final class ImplHandler { } @Override - protected Object initialValue(){ + protected T initialValue(){ try { - return type.newInstance(); + T obj = type.newInstance(); + if (path != null) { + ((AssetLocator)obj).setRootPath(path); + } + return obj; } catch (InstantiationException ex) { logger.log(Level.SEVERE,"Cannot create locator of type {0}, does" + " the class have an empty and publically accessible"+ @@ -169,14 +173,11 @@ final class ImplHandler { return null; } - for (ImplThreadLocal local : locatorsList){ - AssetLocator locator = (AssetLocator) local.get(); - if (local.getPath() != null){ - locator.setRootPath((String) local.getPath()); - } - AssetInfo info = locator.locate(assetManager, key); - if (info != null) + for (ImplThreadLocal local : locatorsList){ + AssetInfo info = local.get().locate(assetManager, key); + if (info != null) { return info; + } } return null; diff --git a/jme3-examples/src/main/java/jme3test/asset/TestOnlineJar.java b/jme3-examples/src/main/java/jme3test/asset/TestOnlineJar.java index e12769cd8..036d9318c 100644 --- a/jme3-examples/src/main/java/jme3test/asset/TestOnlineJar.java +++ b/jme3-examples/src/main/java/jme3test/asset/TestOnlineJar.java @@ -36,10 +36,10 @@ import com.jme3.app.SimpleApplication; import com.jme3.asset.TextureKey; import com.jme3.asset.plugins.HttpZipLocator; import com.jme3.material.Material; -import com.jme3.math.Vector3f; import com.jme3.scene.Geometry; import com.jme3.scene.shape.Quad; import com.jme3.texture.Texture; +import com.jme3.ui.Picture; /** * This tests loading a file from a jar stored online. @@ -59,22 +59,27 @@ public class TestOnlineJar extends SimpleApplication { quadMesh.updateGeometry(1, 1, true); Geometry quad = new Geometry("Textured Quad", quadMesh); - assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/town.zip", - HttpZipLocator.class); + + assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/town.zip", + HttpZipLocator.class); + assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/wildhouse.zip", + HttpZipLocator.class); - TextureKey key = new TextureKey("grass.jpg", false); - key.setGenerateMips(true); - Texture tex = assetManager.loadTexture(key); - - Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); - mat.setTexture("ColorMap", tex); - quad.setMaterial(mat); - - 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 pic1 = new Picture("Picture1"); + pic1.move(0, 0, -1); + pic1.setPosition(0, 0); + pic1.setWidth(128); + pic1.setHeight(128); + pic1.setImage(assetManager, "grass.jpg", false); + guiNode.attachChild(pic1); + + 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); } }