Initial support for libraries using asset bundles
This commit is contained in:
@@ -3,6 +3,7 @@ package lime.tools;
|
||||
enum AssetType
|
||||
{
|
||||
BINARY;
|
||||
BUNDLE;
|
||||
FONT;
|
||||
IMAGE;
|
||||
MANIFEST;
|
||||
|
||||
@@ -38,6 +38,8 @@ class Assets
|
||||
{
|
||||
public static var cache:AssetCache = new AssetCache();
|
||||
public static var onChange = new Event<Void->Void>();
|
||||
|
||||
private static var bundlePaths = new Map<String, String>();
|
||||
private static var defaultRootPath:String;
|
||||
private static var libraries(default, null) = new Map<String, AssetLibrary>();
|
||||
private static var libraryPaths = new Map<String, String>();
|
||||
@@ -406,47 +408,77 @@ class Assets
|
||||
var path = id;
|
||||
var rootPath = null;
|
||||
|
||||
if (libraryPaths.exists(id))
|
||||
if (bundlePaths.exists(id))
|
||||
{
|
||||
path = libraryPaths[id];
|
||||
rootPath = (defaultRootPath != "" ? defaultRootPath + "/" : "") + Path.directory(path);
|
||||
AssetBundle.loadFromFile(bundlePaths.get(id)).onComplete(function(bundle)
|
||||
{
|
||||
if (bundle == null)
|
||||
{
|
||||
promise.error("Cannot load bundle for library \"" + id + "\"");
|
||||
return;
|
||||
}
|
||||
|
||||
var library = AssetLibrary.fromBundle(bundle);
|
||||
|
||||
if (library == null)
|
||||
{
|
||||
promise.error("Cannot open library \"" + id + "\"");
|
||||
}
|
||||
else
|
||||
{
|
||||
libraries.set(id, library);
|
||||
library.onChange.add(onChange.dispatch);
|
||||
promise.completeWith(library.load());
|
||||
}
|
||||
}).onError(function(_)
|
||||
{
|
||||
promise.error("There is no asset library with an ID of \"" + id + "\"");
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
if (StringTools.endsWith(path, ".bundle"))
|
||||
if (libraryPaths.exists(id))
|
||||
{
|
||||
rootPath = path;
|
||||
path += "/library.json";
|
||||
}
|
||||
|
||||
rootPath = (defaultRootPath != "" ? defaultRootPath + "/" : "") + Path.directory(path);
|
||||
path = __cacheBreak(path);
|
||||
}
|
||||
|
||||
AssetManifest.loadFromFile(path, rootPath).onComplete(function(manifest)
|
||||
{
|
||||
if (manifest == null)
|
||||
{
|
||||
promise.error("Cannot parse asset manifest for library \"" + id + "\"");
|
||||
return;
|
||||
}
|
||||
|
||||
var library = AssetLibrary.fromManifest(manifest);
|
||||
|
||||
if (library == null)
|
||||
{
|
||||
promise.error("Cannot open library \"" + id + "\"");
|
||||
path = libraryPaths[id];
|
||||
rootPath = (defaultRootPath != "" ? defaultRootPath + "/" : "") + Path.directory(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
libraries.set(id, library);
|
||||
library.onChange.add(onChange.dispatch);
|
||||
promise.completeWith(library.load());
|
||||
if (StringTools.endsWith(path, ".bundle"))
|
||||
{
|
||||
rootPath = path;
|
||||
path += "/library.json";
|
||||
}
|
||||
|
||||
rootPath = (defaultRootPath != "" ? defaultRootPath + "/" : "") + Path.directory(path);
|
||||
path = __cacheBreak(path);
|
||||
}
|
||||
}).onError(function(_)
|
||||
{
|
||||
promise.error("There is no asset library with an ID of \"" + id + "\"");
|
||||
});
|
||||
|
||||
AssetManifest.loadFromFile(path, rootPath).onComplete(function(manifest)
|
||||
{
|
||||
if (manifest == null)
|
||||
{
|
||||
promise.error("Cannot parse asset manifest for library \"" + id + "\"");
|
||||
return;
|
||||
}
|
||||
|
||||
var library = AssetLibrary.fromManifest(manifest);
|
||||
|
||||
if (library == null)
|
||||
{
|
||||
promise.error("Cannot open library \"" + id + "\"");
|
||||
}
|
||||
else
|
||||
{
|
||||
libraries.set(id, library);
|
||||
library.onChange.add(onChange.dispatch);
|
||||
promise.completeWith(library.load());
|
||||
}
|
||||
}).onError(function(_)
|
||||
{
|
||||
promise.error("There is no asset library with an ID of \"" + id + "\"");
|
||||
});
|
||||
}
|
||||
#end
|
||||
|
||||
return promise.future;
|
||||
|
||||
Reference in New Issue
Block a user