registerJarTexture

Hi ive got a problem with assigning a URL to the registerJarPath() function of the texture loader.

im getting the URL using the following code



String path = "models/default/textures";
String texPath = getClass().getResource("path").toExternalForm();
TextureLoader.tf.registerJarPath(texPath);



the variable texPath will contain something like

“jar:file:d:\MyProject\plugins\UserInterface.jar!\models\default\texture”

I’ve tried appending a “” to the end and striping off the “jar:file:” from the start and getting the root of the .jar instead of a few levels deep into it, but still i cannot get the textures to load from inside the .jar.

I can load the textures from a normal directory using registerPath() just fine so if anyone can help

Firstly: the jar additions to TextureLoader are mine and I am not 100% sure of their correctness as I have always found strange oddities when loading images from jar files.

From my experiance - be VERY careful of jre/lib/ext - make sure Xith3D and your project are NOT there (jogl is ok).

On to your code: the TextureLoader does the getResource stuff automatically - all you have to do is give it a path (eg. registerJarPath(“models/default/textures/”); If that doesn’t work - try prepending a “/” (NOT “”) to that string. if that doesn’t work - just plonk the images in the root directory of a jar file. NOTE - your directory path needs to end in “/”.

FYI - this is the code in TextureLoader which does the getResource stuff - please feel free to comment on it:


        String filename = (String) jarPath.elementAt(i);  // directory as you registered
            filename += name;  // name of image

           
            URL imageURL = this.getClass().getResource(filename);
            if (imageURL != null)
                return imageURL;

Good luck.

Will.

whay are there quotes around the path variable?

String texPath = getClass().getResource(“path”).toExternalForm();

shouldn’t that be

String texPath = getClass().getResource(path).toExternalForm();

The double quotes are my typo the actal code doesn’t have them, im bad but i’m not that bad :slight_smile:

I’ll try a few of your recomendations will and take a look at the code in the registerJarPath() and get back to you.

The reason i’m using the getClass().getResource() method is because the jar file besides containing .ase models and textures, also contains .class files which i load as dynamically as part of a plugin system. so to make a long story short using a relative path with the registerJarPath() method takes it root not as the root of the .jar file but as the root of the PluginLoader (extension of ClassLoader)

Theres a mouthful.

i made a quick fix to TextureLoader.findImageFileJar()

imho the problem was that it’s not intuitiv that you need "/"s behind and before the path you register with registerJarPath
by checking (and fixing) this you just need to put your data jar in the classpath and register the path in it (anyway you like)

    
public URL findImageFileJar (String name) {

        //accumulate search path to print as part of error msg if file not found
        StringBuffer searchPath = new StringBuffer();

        for (int i = 0; i < jarPath.size(); i++) {
				
            String filename = (String) jarPath.elementAt(i);
			

                        // ---------- CHECK -------------
			// check if the first char is a /
			if ( filename.charAt( 0 ) != '/' ){
				filename= "/" + filename;
			}
			
			// check if the last char is a /
			if ( filename.charAt( filename.length() - 1 ) != '/' ){
				filename += "/";
			}
                        // ------- CHECK END -------
			
            filename += name;

            searchPath.append(filename);
            searchPath.append( File.pathSeparatorChar );

            URL imageURL = this.getClass().getResource(filename);
            if (imageURL != null)
                return imageURL;
        }
        
        Log.error("Cannot find image file in texture loader : "+name+", in path : "+searchPath);

        return null;
    }

(also changed the searchPath.append( File.pathSeparatorChar ); line … semicolons under linux don’t feel ‘native’ :wink:

'nother thing: is this ‘error’ present in the new TextureLoader2 ? (haven’t used it yet)

on the other hand i took the TextureLoader and made it a ASELoader … this is far from perfect … but maybe someone likes it :wink:

current features:

  • register Path/JarPath (which means path on the classpath)
  • load and register Nodes (i believe they are TransformGroups) from a file on the searchpath into memory
  • load those Nodes by name

todo (but i have other priorities):

  • caching
  • optional tg tree loading
  • (and all that other stuff you can do with .ase files :wink:

[edit]maybe i should attach the loader … else nobody gets happy ;)[/edit]

Hi, before you put too much effort into this, the texture loader is actually deprecated, and there is a new one (TextureLoader2) which should fix these things.

Check it out: http://192.18.37.44/forums/index.php?topic=9771.0

If you have improvements to suggest, please make them against TextureLoader2

Will.