Skip to content

Commit

Permalink
Merge pull request #1542 from Stencyl/packed-asset-library-preloader
Browse files Browse the repository at this point in the history
Display preloader progress for packed asset libraries.
  • Loading branch information
player-03 authored Nov 3, 2024
2 parents f917cb0 + ea4c371 commit 7c96738
Showing 1 changed file with 78 additions and 47 deletions.
125 changes: 78 additions & 47 deletions src/lime/utils/PackedAssetLibrary.hx
Original file line number Diff line number Diff line change
Expand Up @@ -168,61 +168,72 @@ import flash.media.Sound;
if (promise == null)
{
promise = new Promise<AssetLibrary>();
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, 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:
}
}

var packedData_onComplete = function(data:Bytes)
{
cachedBytes.set(id, data);
packedData = data;

assetsLoaded = 0;
assetsTotal = 1;
__assetLoaded(this.id);

for (id in preload.keys())
{
if (!preload.get(id)) continue;

Log.verbose("Preloading asset: " + id + " [" + types.get(id) + "]");

switch (types.get(id))
{
case BINARY:
assetsTotal++;

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:
assetsTotal++;

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:
assetsTotal++;

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:
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++;

Log.verbose("Preloading asset: " + id + " [" + types.get(id) + "]");
var future = loadText(id);
// future.onProgress (load_onProgress.bind (id));
future.onError(load_onError.bind(id));
Expand All @@ -231,10 +242,10 @@ import flash.media.Sound;
default:
}
}

__assetLoaded(null);
};

__assetLoaded(null);

if (cachedBytes.exists(id))
{
packedData_onComplete(cachedBytes.get(id));
Expand All @@ -248,7 +259,9 @@ import flash.media.Sound;
var path = Path.join([basePath, libPath]);
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);
}
}

Expand Down Expand Up @@ -401,18 +414,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
Expand All @@ -424,38 +458,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);
}
}
Expand Down

0 comments on commit 7c96738

Please sign in to comment.