From 7c4110fbef448848ebdb668d9565b141d8ebe3f0 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Fri, 21 Jun 2019 12:15:58 -0700 Subject: [PATCH] Initial support for libraries using asset bundles --- src/lime/tools/AssetType.hx | 1 + src/lime/utils/Assets.hx | 96 +++++++++++++++++++---------- templates/haxe/ManifestResources.hx | 4 +- 3 files changed, 68 insertions(+), 33 deletions(-) diff --git a/src/lime/tools/AssetType.hx b/src/lime/tools/AssetType.hx index 489615e1f..99869a3f0 100644 --- a/src/lime/tools/AssetType.hx +++ b/src/lime/tools/AssetType.hx @@ -3,6 +3,7 @@ package lime.tools; enum AssetType { BINARY; + BUNDLE; FONT; IMAGE; MANIFEST; diff --git a/src/lime/utils/Assets.hx b/src/lime/utils/Assets.hx index 7c3499d21..f3ea81c21 100644 --- a/src/lime/utils/Assets.hx +++ b/src/lime/utils/Assets.hx @@ -38,6 +38,8 @@ class Assets { public static var cache:AssetCache = new AssetCache(); public static var onChange = new EventVoid>(); + + private static var bundlePaths = new Map(); private static var defaultRootPath:String; private static var libraries(default, null) = new Map(); private static var libraryPaths = new Map(); @@ -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; diff --git a/templates/haxe/ManifestResources.hx b/templates/haxe/ManifestResources.hx index 88bb90e5d..1cafb1db4 100644 --- a/templates/haxe/ManifestResources.hx +++ b/templates/haxe/ManifestResources.hx @@ -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);