Initial support for libraries using asset bundles

This commit is contained in:
Joshua Granick
2019-06-21 12:15:58 -07:00
parent f50904e1d4
commit 7c4110fbef
3 changed files with 68 additions and 33 deletions

View File

@@ -3,6 +3,7 @@ package lime.tools;
enum AssetType
{
BINARY;
BUNDLE;
FONT;
IMAGE;
MANIFEST;

View File

@@ -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;