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,6 +408,35 @@ class Assets
var path = id;
var rootPath = null;
if (bundlePaths.exists(id))
{
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 (libraryPaths.exists(id))
{
path = libraryPaths[id];
@@ -447,6 +478,7 @@ class Assets
{
promise.error("There is no asset library with an ID of \"" + id + "\"");
});
}
#end
return promise.future;

View File

@@ -74,7 +74,9 @@ import sys.FileSystem;
library = AssetLibrary.fromManifest (manifest);
Assets.registerLibrary ("::library::", library);
::else::Assets.libraryPaths["::library::"] = rootPath + "::resourceName::";
::end::::end::::end::::end::
::end::::end::::if (type == "bundle")::
Assets.bundlePaths["::library::"] = rootPath + "::resourceName::";
::end::::end::::end::
::foreach libraries::::if (preload)::library = Assets.getLibrary ("::name::");
if (library != null) preloadLibraries.push (library);