Search code examples
javaandroidlibgdxphp-ziparchivetexture-atlas

Couldn't load TextureAtlas from zip archive via AssetManager - libgdx


I have 2 instances of AssetManager: one for basic textures and one for high quality textures. Basic textures are located in "android/assets" folder and high quality textures are packed in zip file. Content (file names) in this folders are the same - there are only better quality textures in zip archive.

AssetManager throws an exception: "Couldn't load dependencies of asset: teamLogo.png" when I'm trying to load TextureAtlas from zip file. When I'm loading Texture file everything is ok. Loading TextureAtlas works only from 'android/assets' folder.

AssetManager using 'android/assets' - everything is ok:

AssetManager am = new AssetManager();
am.load( "images/image.png", Texture.class );
am.load( "images/teamLogo.pack", TextureAtlas.class );

AssetManager using zip archive - can't load TextureAtlas:

ZipFile archive = new ZipFile(expansionFileHandle.file());
ArchiveFileHandleResolver resolver = new ArchiveFileHandleResolver(archive);
AssetManager amHQ = new AssetManager(resolver);

This works fine:

amHQ.load( "images/image.png", Texture.class );

This doesn't work:

amHQ.load( "images/teamLogo.pack", TextureAtlas.class );

ArchiveFileHandle class:

public class ArchiveFileHandle extends FileHandle 
{ 
final ZipFile archive;
final ZipEntry archiveEntry;

public ArchiveFileHandle (ZipFile archive, File file) 
{
    super(file, FileType.Classpath);
    this.archive = archive;
    archiveEntry = this.archive.getEntry(file.getPath());
}

public ArchiveFileHandle (ZipFile archive, String fileName) 
{
    super(fileName.replace('\\', '/'), FileType.Classpath);
    this.archive = archive;
    this.archiveEntry = archive.getEntry(fileName.replace('\\', '/'));
}

@Override
public FileHandle child (String name) 
{
    name = name.replace('\\', '/');
    if (file.getPath().length() == 0) 
        return new ArchiveFileHandle(archive, new File(name));
    return new ArchiveFileHandle(archive, new File(file, name));
}

@Override
public FileHandle sibling (String name) 
{
    name = name.replace('\\', '/');
    if (file.getPath().length() == 0) 
        throw new GdxRuntimeException("Cannot get the sibling of the root.");
    return new ArchiveFileHandle(archive, new File(file.getParent(), name));
}

@Override
public FileHandle parent () 
{
    File parent = file.getParentFile();
    if (parent == null) 
    {
        if (type == FileType.Absolute)
            parent = new File("/");
        else
            parent = new File("");
    }
    return new ArchiveFileHandle(archive, parent);
}

@Override
public InputStream read () 
{
    try 
    {
        return archive.getInputStream(archiveEntry);
    } 
    catch (IOException e) 
    {
        throw new GdxRuntimeException("File not found: " + file + " (Archive)");
    }
}

@Override
public boolean exists() 
{
    return archiveEntry != null;
}

@Override
public long length () 
{
    return archiveEntry.getSize();
}

@Override
public long lastModified () 
{
    return archiveEntry.getTime();
}

What am I doing wrong?


Solution

  • Yeaaahhh, I found it :) ArchiveFileHandle couldn't load dependencies of TextureAtlas because he can't find those files. When looking in zip archive you have to replace '\' char to '/'. The bug was in one of ArchiveFileHandle constructors. This line:

    archiveEntry = this.archive.getEntry(file.getPath());
    

    should be:

    archiveEntry = this.archive.getEntry(file.getPath().replace('\\', '/'));
    

    Now everything works fine