Initial work on AssetBundle
This commit is contained in:
53
src/lime/utils/AssetBundle.hx
Normal file
53
src/lime/utils/AssetBundle.hx
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -96,6 +96,47 @@ class AssetLibrary
|
|||||||
return fromManifest(AssetManifest.fromFile(path, rootPath));
|
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
|
public static function fromManifest(manifest:AssetManifest):AssetLibrary
|
||||||
{
|
{
|
||||||
if (manifest == null) return null;
|
if (manifest == null) return null;
|
||||||
@@ -608,6 +649,50 @@ class AssetLibrary
|
|||||||
return Assets.__cacheBreak(path);
|
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
|
@:noCompletion private function __fromManifest(manifest:AssetManifest):Void
|
||||||
{
|
{
|
||||||
var hasSize = (manifest.version >= 2);
|
var hasSize = (manifest.version >= 2);
|
||||||
|
|||||||
Reference in New Issue
Block a user