Initial work on AssetBundle

This commit is contained in:
Joshua Granick
2019-06-12 17:09:00 -07:00
parent b684f43172
commit e9bc17ef42
2 changed files with 138 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
package lime.utils;
import haxe.zip.Reader;
import lime.utils.Bytes;
#if sys
import sys.io.File;
#end
#if !lime_debug
@:fileXml('tags="haxe,release"')
@:noDebug
#end
class AssetBundle
{
public var data:Map<String, Bytes>;
public var paths:Array<String>;
public function new()
{
// compressed = new Map();
data = new Map();
paths = new Array();
}
public static function fromFile(path:String):AssetBundle
{
#if sys
var input = File.read(path);
var entries = Reader.readZip(input);
var bundle = new AssetBundle();
for (entry in entries)
{
if (entry.compressed)
{
var bytes:Bytes = entry.data;
bundle.data.set(entry.fileName, bytes.decompress(DEFLATE));
}
else
{
bundle.data.set(entry.fileName, entry.data);
}
bundle.paths.push(entry.fileName);
}
return bundle;
#else
return null;
#end
}
}

View File

@@ -96,6 +96,47 @@ class AssetLibrary
return fromManifest(AssetManifest.fromFile(path, rootPath));
}
public static function fromBundle(bundle:AssetBundle):AssetLibrary
{
if (bundle.data.exists("library.json"))
{
var manifest = AssetManifest.fromBytes(bundle.data.get("library.json"));
if (manifest != null)
{
var library:AssetLibrary = null;
if (manifest.libraryType == null)
{
library = new AssetLibrary();
}
else
{
var libraryClass = Type.resolveClass(manifest.libraryType);
if (libraryClass != null)
{
library = Type.createInstance(libraryClass, manifest.libraryArgs);
}
else
{
Log.warn("Could not find library type: " + manifest.libraryType);
return null;
}
}
library.__fromBundle(bundle, manifest);
return library;
}
}
else
{
var library = new AssetLibrary();
library.__fromBundle(bundle);
return library;
}
return null;
}
public static function fromManifest(manifest:AssetManifest):AssetLibrary
{
if (manifest == null) return null;
@@ -608,6 +649,50 @@ class AssetLibrary
return Assets.__cacheBreak(path);
}
@:noCompletion private function __fromBundle(bundle:AssetBundle, manifest:AssetManifest = null):Void
{
if (manifest != null)
{
var id, data, type:AssetType;
for (asset in manifest.assets)
{
id = Reflect.hasField(asset, "id") ? asset.id : asset.path;
data = bundle.data.get(asset.path);
if (Reflect.hasField(asset, "type"))
{
type = asset.type;
switch(type)
{
case IMAGE:
cachedImages.set(id, Image.fromBytes(data));
case MUSIC, SOUND:
cachedAudioBuffers.set(id, AudioBuffer.fromBytes(data));
case FONT:
cachedFonts.set(id, Font.fromBytes(data));
case TEXT:
cachedText.set(id, data != null ? Std.string(data) : null);
default:
cachedBytes.set(id, data);
}
types.set(id, asset.type);
}
else
{
cachedBytes.set(id, data);
types.set(id, BINARY);
}
}
}
else
{
for (path in bundle.paths)
{
cachedBytes.set(path, bundle.data.get(path));
types.set(path, BINARY);
}
}
}
@:noCompletion private function __fromManifest(manifest:AssetManifest):Void
{
var hasSize = (manifest.version >= 2);