From 92744527667afd994130407364cd7cd52fb974e4 Mon Sep 17 00:00:00 2001 From: Justin Espedal Date: Wed, 12 Jan 2022 13:36:37 +0900 Subject: [PATCH 1/2] Display preloader progress for packed asset libraries. --- src/lime/utils/PackedAssetLibrary.hx | 107 +++++++++++++++++---------- 1 file changed, 68 insertions(+), 39 deletions(-) diff --git a/src/lime/utils/PackedAssetLibrary.hx b/src/lime/utils/PackedAssetLibrary.hx index 4015d1f291..c2e8a73428 100644 --- a/src/lime/utils/PackedAssetLibrary.hx +++ b/src/lime/utils/PackedAssetLibrary.hx @@ -168,17 +168,36 @@ import flash.media.Sound; if (promise == null) { promise = new Promise(); + bytesLoadedCache = new Map(); // TODO: Handle `preload` for individual assets // TODO: Do not preload bytes on native, if we can read from it instead (all non-Android targets?) + assetsLoaded = 0; + assetsTotal = 2; //for our initial __assetLoaded(null) call and __assetLoaded(this.id) + + for (id in preload.keys()) + { + if (!preload.get(id)) continue; + + switch (types.get(id)) + { + case BINARY, FONT, IMAGE, MUSIC, SOUND, TEXT: + assetsTotal++; + + case MUSIC, SOUND: + assetsTotal++; + + default: + } + } + var packedData_onComplete = function(data:Bytes) { cachedBytes.set(id, data); packedData = data; - assetsLoaded = 0; - assetsTotal = 1; + __assetLoaded(this.id); for (id in preload.keys()) { @@ -189,40 +208,30 @@ import flash.media.Sound; switch (types.get(id)) { case BINARY: - assetsTotal++; - var future = loadBytes(id); // future.onProgress (load_onProgress.bind (id)); future.onError(load_onError.bind(id)); future.onComplete(loadBytes_onComplete.bind(id)); case FONT: - assetsTotal++; - var future = loadFont(id); // future.onProgress (load_onProgress.bind (id)); future.onError(load_onError.bind(id)); future.onComplete(loadFont_onComplete.bind(id)); case IMAGE: - assetsTotal++; - var future = loadImage(id); // future.onProgress (load_onProgress.bind (id)); future.onError(load_onError.bind(id)); future.onComplete(loadImage_onComplete.bind(id)); case MUSIC, SOUND: - assetsTotal++; - var future = loadAudioBuffer(id); // future.onProgress (load_onProgress.bind (id)); future.onError(load_onError.bind(id)); future.onComplete(loadAudioBuffer_onComplete.bind(id)); case TEXT: - assetsTotal++; - var future = loadText(id); // future.onProgress (load_onProgress.bind (id)); future.onError(load_onError.bind(id)); @@ -231,10 +240,10 @@ import flash.media.Sound; default: } } - - __assetLoaded(null); }; + __assetLoaded(null); + if (cachedBytes.exists(id)) { packedData_onComplete(cachedBytes.get(id)); @@ -248,7 +257,9 @@ import flash.media.Sound; var path = basePath + (paths.exists(id) ? paths.get(id) : id); path = __cacheBreak(path); - Bytes.loadFromFile(path).onError(promise.error).onComplete(packedData_onComplete); + var packedData_onProgress = load_onProgress.bind(this.id); + + Bytes.loadFromFile(path).onProgress(packedData_onProgress).onError(promise.error).onComplete(packedData_onComplete); } } @@ -401,18 +412,39 @@ import flash.media.Sound; super.__fromManifest(manifest); + var packedBytesTotal = 0; + bytesTotal = 0; + for (asset in manifest.assets) { + var id = asset.id; + if (Reflect.hasField(asset, "position")) { - positions.set(asset.id, Reflect.field(asset, "position")); + positions.set(id, Reflect.field(asset, "position")); } if (Reflect.hasField(asset, "length")) { - lengths.set(asset.id, Reflect.field(asset, "length")); + var length = Reflect.field(asset, "length"); + lengths.set(id, length); + + //for individual packed assets, the size represents the work done unpacking them + //since this is likely to be much faster than downloading, set it to something + //small like the packed length / 10. + sizes.set(id, Math.floor(length / 10)); + + packedBytesTotal += length; + } + + if (preload.exists(id) && preload.get(id) && sizes.exists(id)) + { + bytesTotal += sizes.get(id); } } + + sizes.set(this.id, packedBytesTotal); + bytesTotal += packedBytesTotal; } @:noCompletion private override function __assetLoaded(id:String):Void @@ -424,38 +456,35 @@ import flash.media.Sound; Log.verbose("Loaded asset: " + id + " [" + types.get(id) + "] (" + (assetsLoaded - 1) + "/" + (assetsTotal - 1) + ")"); } - // if (id != null) { - - // var size = sizes.get (id); - - // if (!bytesLoadedCache.exists (id)) { - - // bytesLoaded += size; - - // } else { - - // var cache = bytesLoadedCache.get (id); - - // if (cache < size) { - - // bytesLoaded += (size - cache); - - // } + if (id != null) + { + var size = sizes.exists(id) ? sizes.get(id) : 0; - // } + if (!bytesLoadedCache.exists(id)) + { + bytesLoaded += size; + } + else + { + var cache = bytesLoadedCache.get(id); - // bytesLoadedCache.set (id, size); + if (cache < size) + { + bytesLoaded += (size - cache); + } + } - // } + bytesLoadedCache.set(id, size); + } if (assetsLoaded < assetsTotal) { - // promise.progress (bytesLoaded, bytesTotal); + promise.progress(bytesLoaded, bytesTotal); } else { loaded = true; - // promise.progress (bytesTotal, bytesTotal); + promise.progress(bytesTotal, bytesTotal); promise.complete(this); } } From ea4c3716c1ae643b3d7476543beaf1bf09314411 Mon Sep 17 00:00:00 2001 From: Justin Espedal Date: Wed, 12 Jan 2022 13:37:51 +0900 Subject: [PATCH 2/2] Don't delay music preloading until the packed asset library is loaded --- src/lime/utils/PackedAssetLibrary.hx | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/lime/utils/PackedAssetLibrary.hx b/src/lime/utils/PackedAssetLibrary.hx index c2e8a73428..a05639d47b 100644 --- a/src/lime/utils/PackedAssetLibrary.hx +++ b/src/lime/utils/PackedAssetLibrary.hx @@ -182,12 +182,18 @@ import flash.media.Sound; switch (types.get(id)) { - case BINARY, FONT, IMAGE, MUSIC, SOUND, TEXT: + case BINARY, FONT, IMAGE, TEXT: assetsTotal++; case MUSIC, SOUND: + Log.verbose("Preloading asset: " + id + " [" + types.get(id) + "]"); assetsTotal++; + var future = loadAudioBuffer(id); + future.onProgress(load_onProgress.bind(id)); + future.onError(loadAudioBuffer_onError.bind(id)); + future.onComplete(loadAudioBuffer_onComplete.bind(id)); + default: } } @@ -203,35 +209,31 @@ import flash.media.Sound; { if (!preload.get(id)) continue; - Log.verbose("Preloading asset: " + id + " [" + types.get(id) + "]"); - switch (types.get(id)) { case BINARY: + Log.verbose("Preloading asset: " + id + " [" + types.get(id) + "]"); var future = loadBytes(id); // future.onProgress (load_onProgress.bind (id)); future.onError(load_onError.bind(id)); future.onComplete(loadBytes_onComplete.bind(id)); case FONT: + Log.verbose("Preloading asset: " + id + " [" + types.get(id) + "]"); var future = loadFont(id); // future.onProgress (load_onProgress.bind (id)); future.onError(load_onError.bind(id)); future.onComplete(loadFont_onComplete.bind(id)); case IMAGE: + Log.verbose("Preloading asset: " + id + " [" + types.get(id) + "]"); var future = loadImage(id); // future.onProgress (load_onProgress.bind (id)); future.onError(load_onError.bind(id)); future.onComplete(loadImage_onComplete.bind(id)); - case MUSIC, SOUND: - var future = loadAudioBuffer(id); - // future.onProgress (load_onProgress.bind (id)); - future.onError(load_onError.bind(id)); - future.onComplete(loadAudioBuffer_onComplete.bind(id)); - case TEXT: + Log.verbose("Preloading asset: " + id + " [" + types.get(id) + "]"); var future = loadText(id); // future.onProgress (load_onProgress.bind (id)); future.onError(load_onError.bind(id));