Fixes for bundle loading

This commit is contained in:
Joshua Granick
2019-06-21 15:07:08 -07:00
parent 7c4110fbef
commit f637bdd8ef
4 changed files with 45 additions and 17 deletions

View File

@@ -1,10 +1,11 @@
package lime.utils; package lime.utils;
import haxe.io.Bytes;
import haxe.io.BytesInput; import haxe.io.BytesInput;
import haxe.io.Input; import haxe.io.Input;
import haxe.zip.Reader; import haxe.zip.Reader;
import lime.app.Future; import lime.app.Future;
import lime.utils.Bytes; import lime.utils.Bytes as LimeBytes;
#if sys #if sys
import sys.io.File; import sys.io.File;
@@ -26,6 +27,12 @@ class AssetBundle
paths = new Array(); paths = new Array();
} }
public static function fromBytes(bytes:Bytes):AssetBundle
{
var input = new BytesInput(bytes);
return __extractBundle(input);
}
public static function fromFile(path:String):AssetBundle public static function fromFile(path:String):AssetBundle
{ {
#if sys #if sys
@@ -36,13 +43,14 @@ class AssetBundle
#end #end
} }
public static function loadFromBytes(bytes:Bytes):Future<AssetBundle>
{
return Future.withValue(fromBytes(bytes));
}
public static function loadFromFile(path:String):Future<AssetBundle> public static function loadFromFile(path:String):Future<AssetBundle>
{ {
return Bytes.loadFromFile(path).then(function(bytes) return LimeBytes.loadFromFile(path).then(loadFromBytes);
{
var input = new BytesInput(bytes);
return Future.withValue(__extractBundle(input));
});
} }
@:noCompletion private static function __extractBundle(input:Input):AssetBundle @:noCompletion private static function __extractBundle(input:Input):AssetBundle
@@ -55,7 +63,7 @@ class AssetBundle
{ {
if (entry.compressed) if (entry.compressed)
{ {
var bytes:Bytes = entry.data; var bytes:LimeBytes = entry.data;
bundle.data.set(entry.fileName, bytes.decompress(DEFLATE)); bundle.data.set(entry.fileName, bytes.decompress(DEFLATE));
} }
else else

View File

@@ -566,6 +566,15 @@ class AssetLibrary
{ {
return Future.withValue(Type.createInstance(classTypes.get(id), [])); return Future.withValue(Type.createInstance(classTypes.get(id), []));
} }
else if (cachedBytes.exists(id))
{
return Image.loadFromBytes(cachedBytes.get(id)).then(function (image)
{
cachedBytes.remove(id);
cachedImages.set(id, image);
return Future.withValue(image);
});
}
else else
{ {
return Image.loadFromFile(paths.get(id)); return Image.loadFromFile(paths.get(id));
@@ -664,12 +673,14 @@ class AssetLibrary
type = asset.type; type = asset.type;
switch(type) switch(type)
{ {
#if !web
case IMAGE: case IMAGE:
cachedImages.set(id, Image.fromBytes(data)); cachedImages.set(id, Image.fromBytes(data));
case MUSIC, SOUND: case MUSIC, SOUND:
cachedAudioBuffers.set(id, AudioBuffer.fromBytes(data)); cachedAudioBuffers.set(id, AudioBuffer.fromBytes(data));
case FONT: case FONT:
cachedFonts.set(id, Font.fromBytes(data)); cachedFonts.set(id, Font.fromBytes(data));
#end
case TEXT: case TEXT:
cachedText.set(id, data != null ? Std.string(data) : null); cachedText.set(id, data != null ? Std.string(data) : null);
default: default:

View File

@@ -92,13 +92,17 @@ class AssetManifest
manifest.libraryArgs = manifestData.libraryArgs; manifest.libraryArgs = manifestData.libraryArgs;
} }
if (Reflect.hasField(manifestData, "version") && manifestData.version <= 2) if (Reflect.hasField(manifestData, "assets"))
{ {
manifest.assets = Unserializer.run(manifestData.assets); var assets:Dynamic = manifestData.assets;
} if (Reflect.hasField(manifestData, "version") && manifestData.version <= 2)
else if (Reflect.hasField(manifestData, "assets") && Std.is(manifestData.assets, Array)) {
{ manifest.assets = Unserializer.run(assets);
manifest.assets = cast manifestData.assets; }
else
{
manifest.assets = assets;
}
} }
if (Reflect.hasField(manifestData, "rootPath")) if (Reflect.hasField(manifestData, "rootPath"))

View File

@@ -1,6 +1,8 @@
package; package;
import haxe.io.Bytes;
import lime.utils.AssetBundle;
import lime.utils.AssetLibrary; import lime.utils.AssetLibrary;
import lime.utils.AssetManifest; import lime.utils.AssetManifest;
import lime.utils.Assets; import lime.utils.Assets;
@@ -56,7 +58,7 @@ import sys.FileSystem;
::end::::end::::end:: ::end::::end::::end::
#end #end
var data, manifest, library; var data, manifest, library, bundle;
#if kha #if kha
@@ -74,9 +76,12 @@ import sys.FileSystem;
library = AssetLibrary.fromManifest (manifest); library = AssetLibrary.fromManifest (manifest);
Assets.registerLibrary ("::library::", library); Assets.registerLibrary ("::library::", library);
::else::Assets.libraryPaths["::library::"] = rootPath + "::resourceName::"; ::else::Assets.libraryPaths["::library::"] = rootPath + "::resourceName::";
::end::::end::::if (type == "bundle"):: ::end::::end::::if (type == "bundle")::::if (embed)::
Assets.bundlePaths["::library::"] = rootPath + "::resourceName::"; bundle = AssetBundle.fromBytes(#if flash Bytes.ofData(new __ASSET__::flatName::() #else new __ASSET__::flatName::() #end));
::end::::end::::end:: library = AssetLibrary.fromBundle(bundle);
Assets.registerLibrary("::library::", library);
::else::Assets.bundlePaths["::library::"] = rootPath + "::resourceName::";
::end::::end::::end::::end::
::foreach libraries::::if (preload)::library = Assets.getLibrary ("::name::"); ::foreach libraries::::if (preload)::library = Assets.getLibrary ("::name::");
if (library != null) preloadLibraries.push (library); if (library != null) preloadLibraries.push (library);