SimpleJavaEngine/src/main/java/speiger/src/coreengine/assets/AssetManager.java

187 lines
4.2 KiB
Java

package speiger.src.coreengine.assets;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipFile;
import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.collections.objects.maps.impl.hash.Object2ObjectLinkedOpenHashMap;
import speiger.src.coreengine.assets.impl.FolderAssetPackage;
import speiger.src.coreengine.assets.impl.ZipAssetPackage;
import speiger.src.coreengine.assets.reloader.IReloadableResource;
public class AssetManager implements IReloadableResource
{
Map<String, DomainAssets> domains = new Object2ObjectLinkedOpenHashMap<String, DomainAssets>();
File file;
public AssetManager(File file)
{
this.file = file;
}
public File getMainAsset()
{
return file;
}
@Override
public void reload()
{
domains.clear();
loadAssetPackage(file);
}
public void loadAssetPackage(File file)
{
if(file.isDirectory())
{
loadAssets(new FolderAssetPackage(file));
}
else if(isZipFile(file))
{
loadAssets(new ZipAssetPackage(file));
}
}
public void loadAssets(IAssetPackage packages)
{
for(String s : packages.getDomains())
{
DomainAssets entry = domains.get(s);
if(entry == null)
{
entry = new DomainAssets();
domains.put(s, entry);
}
entry.addAssets(packages);
}
}
public Set<String> getDomains()
{
return domains.keySet();
}
public IAsset getAsset(AssetLocation location) throws IOException
{
DomainAssets asset = domains.get(location.getDomain());
if(asset == null)
{
throw new FileNotFoundException("File["+location.toString()+"] not found");
}
return asset.getAsset(location);
}
public MultiAsset getAssets(AssetLocation...locations)
{
IAsset[] asset = new IAsset[locations.length];
for(int i = 0,m=locations.length;i<m;i++)
{
try
{
asset[i] = getAsset(locations[i]);
}
catch(Exception e) {e.printStackTrace();}
}
return new MultiAsset(asset);
}
public MultiAsset getMultiAsset(AssetLocation location, AssetLocation...subLocations) throws IOException
{
DomainAssets asset = domains.get(location.getDomain());
if(asset == null)
{
throw new FileNotFoundException("File["+location.toString()+"] not found");
}
return asset.getMultiAsset(location, subLocations);
}
public MultiAsset getAllAsset(AssetLocation location) throws IOException
{
DomainAssets asset = domains.get(location.getDomain());
if(asset == null)
{
throw new FileNotFoundException("File["+location.toString()+"] not found");
}
return asset.getAllAssets(location);
}
protected boolean isZipFile(File file)
{
try(ZipFile zip = new ZipFile(file))
{
return true;
}
catch(Exception e)
{
}
return false;
}
static class DomainAssets
{
List<IAssetPackage> packages = new ObjectArrayList<IAssetPackage>();
public void addAssets(IAssetPackage assets)
{
packages.add(assets);
}
public IAsset getAsset(AssetLocation location) throws IOException
{
for(int i = packages.size() - 1;i>=0;i--)
{
IAsset asset = packages.get(i).getAsset(location);
if(asset != null)
{
return asset;
}
}
throw new FileNotFoundException("File["+location.toString()+"] not found");
}
public MultiAsset getMultiAsset(AssetLocation location, AssetLocation[] subLocations)
{
for(int i = packages.size() - 1;i>=0;i--)
{
IAssetPackage entry = packages.get(i);
IAsset asset = entry.getAsset(location);
if(asset != null)
{
IAsset[] result = new IAsset[1 + subLocations.length];
result[0] = asset;
for(int x = 0;x<subLocations.length;x++)
{
result[x+1] = entry.getAsset(subLocations[x]);
}
return new MultiAsset(result);
}
}
return null;
}
public MultiAsset getAllAssets(AssetLocation location) throws IOException
{
List<IAsset> assets = new ObjectArrayList<IAsset>();
for(IAssetPackage entry : packages)
{
IAsset asset = entry.getAsset(location);
if(asset != null)
{
assets.add(asset);
}
}
if(assets.isEmpty())
{
throw new FileNotFoundException("File["+location.toString()+"] not found");
}
return new MultiAsset(assets.toArray(new IAsset[assets.size()]));
}
}
}