Getting path to Assets directory?

When loading a model, all that has to be typed is “Models/myModel.j3o.” Is it possible to get the full path to this directory outside of the AssetManager?

In most cases it doesn’t exist as a full path since it’s being loaded from inside a JAR file.

You could maybe do something with the java classpath stuff to see what jar the file loads from but even then depending on the setup it could even be downloaded from a website or anything…

@zarch My plan is to get a directory that contains a specific type of file (i.e. the Audio, or Model folder) and get a list of all of the files in the folder, like this:

[java]
File folder = new File(“your/path”);
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
  if (listOfFiles[i].isFile()) {
    System.out.println("File " + listOfFiles[i].getName());
  } else if (listOfFiles[i].isDirectory()) {
    System.out.println("Directory " + listOfFiles[i].getName());
  }
}

[/java]

Is there any easy way to do this straight from the AssetManager?

What zarch said. You can add any folder to the assetmanger root though, for example to load user assets or to load generated j3o files. Just always remember that any asset path works from the assetmanager root. So if you add a folder c:/data/ and theres a folder “Models” inside the full path to a model would be “Models/UserModel.j3o”. So path==asset name

Just saw your reply now. What you could do is make a list of all files in the assets folder (e.g. In the build script) and add that to your assets. Its not possible to list the contents of the jar file like that, as mentioned its the classpath and not a folder. Furthermore there could be all kinds of sources in the assetmanager root, from web servers to zip files and many of these don’t spport “scanning” at all.

However if you set up a folder inside your deployed application and placed the assets in that then you can scan it as per standard java file io - you will need to set up the asset manager to load from that folder as well as the usual locations but I believe it’s possible…not something I’ve ever tried though.

So what is the recommended way to load user files using Java io ? I have a text file I need to read in my program which I put in my Assets directory. How can I access it ?

Seems this would be necessary to read config files and such.

Either grab the asset locator or just do it the java way:
http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)

What I tend to do is I ship the assets folder as it is, not putting it into a jar. When the game then runs I make sure the assets folder is on the classpath and load assets like normal eg. “Models/model.j3o”

With configs I use Jackson for reading/writing JSON using a File, something like this.

    final File file = new File(location);

    try {
            final ObjectMapper objectMapper = new ObjectMapper();
            final T config = objectMapper.readValue(file, clazz);
            LOGGER.debug("Loaded {} from {}", clazz.getSimpleName(), file.getAbsolutePath());
            return config;
        } catch (FileNotFoundException e) {
            LOGGER.warn("Could not find {} at '{}'", clazz.getSimpleName(), file.getAbsolutePath());
        } catch (IOException e) {
            LOGGER.error("Error parsing config at '{}'", file.getAbsolutePath(), e);
        }

Jackson

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.4</version>
</dependency>

So you want to access all the file in your assets directory ?

I have a dumb solution for this… But still you might be interested in it !

public static String root = System.getProperty("user.dir");
public static File rootFile = new File(System.getProperty("user.dir"));

public static String getLocalAssetPath(File file){
    String[] subDirs = file.getPath().split(Pattern.quote(File.separator));
    String finalPath = "";
    boolean add = false;
    for(String s : subDirs){
        if(add){
            if(!s.contains("j3o") || !s.contains(".jpg")){
                finalPath += "/"+s;
            }else{
                finalPath += "/"+s;                    
            }
        }
        if(s.equals("assets")){
            add = true;
        }         
    }        
    return finalPath;
}

public static String getFullPath(String path){
    String finalPath = "";
    finalPath += root+"/assets/";
    finalPath += path;
    return finalPath;
}    

And then simply use it like this trough JFileChooser

                JFileChooser fc = new JFileChooser();
                fc.setCurrentDirectory(AssetsUtils.rootFile);
                int returnVal = fc.showOpenDialog(null);
                fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    if (file.getName().endsWith(".jpg")) {
                        String path = AssetsUtils.getLocalAssetPath(file);                            
                        terrainEditState.setTexture(path, Integer.parseInt(this.getUID()));
                        this.setButtonIcon(this.getWidth(), this.getHeight(), path);
                    } else {
                        System.err.println("Le fichier doit être un jpg");
                    }
                }

I hope it help you.

This Simple routine seems to work for me

  public AssetInfo LocateAsset(String Assetname)
    {
        AssetInfo aInfo ;
        
        aInfo = assetManager.locateAsset(new AssetKey(Assetname));
        
        System.out.println("Asset Info = " + aInfo.toString());
        
        return aInfo ;
    }

Called by

   AssetInfo vecFile = LocateAsset(filename);
                System.out.println("vecFile = " + vecFile);
                
                BufferedReader textReader = new BufferedReader(new InputStreamReader(vecFile.openStream()));
                
                lineData = textReader.readLine();
.
.
.

where filename is of the form

“Scenes/theFile.txt” in the Assets path

1 Like

In the end, it was you who gave me a nice tips to locate assets. Thanks

Whatever solution you choose, test early on all platforms. Especially on mobile devices.

Reviving old topic, but I have a case where I need to know what folder an asset is in (which asset folder basically, not the absolute path). Default, it will be ‘assets’, but I’d like to be sure, and you mention I can grab the asset locator but I dont know how to do that.

Well, it might not be in a folder at all. It could be a class resource in jar file. It could be loaded from a URL somewhere. In general, you can’t know and the desire to know is an indication of a design problem somewhere else. (Like trying to write an editor using the scene graph instead of using the editor structures that manifest into a scene graph… the scene graph is not designed to be the model for an editor.)

What is it that you are actually trying to do?

Trying to use Tiled’s java implementation - and it requires the absolute path so that it can work with not only the asset (the map), but also the tileset. I’ve created an AssetLoader for the tmx-file, so far so good. But inside the map file is a name of the tileset used, so it looks for that in a folder (which it cant with JME).

But I can see now that this is the wrong road to head down. I can overwrite the map-reader class, and will do so to accomodate for assets in general (as your say, in a jar, a url etc). It just needs a bit more work I guess.

Best case, Tiled would include the tileset directly in the map xml-file. Wonder why they haven’t. But I’ll have to work around it.

But I found I can work with getting the path to the asset-folder - but as you say - it doesn’t really make sense.

The format didn’t look too hard to read, personally. You may end up fighting the Java implementation a lot only to then have to write a bunch of custom code to get it into JME-friendly data structures anyway. But I guess you’ll see one way or the other.