Fix isLocal function always true with test Sound on Flash. When embed sound is set on false, isLocal can't by pass exist test.
684 lines
13 KiB
Haxe
684 lines
13 KiB
Haxe
package;
|
|
|
|
|
|
import haxe.Timer;
|
|
import haxe.Unserializer;
|
|
import lime.app.Preloader;
|
|
import lime.audio.openal.AL;
|
|
import lime.audio.AudioBuffer;
|
|
import lime.graphics.Font;
|
|
import lime.graphics.Image;
|
|
import lime.utils.ByteArray;
|
|
import lime.utils.UInt8Array;
|
|
import lime.Assets;
|
|
|
|
#if sys
|
|
import sys.FileSystem;
|
|
#end
|
|
|
|
#if flash
|
|
import flash.display.Bitmap;
|
|
import flash.display.BitmapData;
|
|
import flash.display.Loader;
|
|
import flash.events.Event;
|
|
import flash.media.Sound;
|
|
import flash.net.URLLoader;
|
|
import flash.net.URLRequest;
|
|
#end
|
|
|
|
|
|
class DefaultAssetLibrary extends AssetLibrary {
|
|
|
|
|
|
public var className (default, null) = new Map <String, Dynamic> ();
|
|
public var path (default, null) = new Map <String, String> ();
|
|
public var type (default, null) = new Map <String, AssetType> ();
|
|
|
|
private var lastModified:Float;
|
|
private var timer:Timer;
|
|
|
|
|
|
public function new () {
|
|
|
|
super ();
|
|
|
|
#if flash
|
|
|
|
::if (assets != null)::::foreach assets::::if (embed)::className.set ("::id::", __ASSET__::flatName::);::else::path.set ("::id::", "::resourceName::");::end::
|
|
type.set ("::id::", AssetType.$$upper(::type::));
|
|
::end::::end::
|
|
|
|
#elseif html5
|
|
|
|
::if (assets != null)::var id;
|
|
::foreach assets::id = "::id::";
|
|
::if (embed)::::if (type == "font")::className.set (id, __ASSET__::flatName::);::else::path.set (id, ::if (resourceName == id)::id::else::"::resourceName::"::end::);::end::
|
|
::else::path.set (id, ::if (resourceName == id)::id::else::"::resourceName::"::end::);::end::
|
|
type.set (id, AssetType.$$upper(::type::));
|
|
::end::::end::
|
|
|
|
var assetsPrefix = ApplicationMain.config.assetsPrefix;
|
|
if (assetsPrefix != null) {
|
|
for (k in path.keys()) {
|
|
path.set(k, assetsPrefix + path[k]);
|
|
}
|
|
}
|
|
|
|
#else
|
|
|
|
#if openfl
|
|
::if (assets != null)::
|
|
::foreach assets::::if (type == "font")::openfl.text.Font.registerFont (__ASSET__::flatName::);::end::
|
|
::end::::end::
|
|
#end
|
|
|
|
#if (windows || mac || linux)
|
|
|
|
var useManifest = false;
|
|
::if (assets != null)::::foreach assets::::if (embed)::
|
|
className.set ("::id::", __ASSET__::flatName::);
|
|
type.set ("::id::", AssetType.$$upper(::type::));
|
|
::else::useManifest = true;
|
|
::end::::end::::end::
|
|
|
|
if (useManifest) {
|
|
|
|
loadManifest ();
|
|
|
|
if (Sys.args ().indexOf ("-livereload") > -1) {
|
|
|
|
var path = FileSystem.fullPath ("manifest");
|
|
lastModified = FileSystem.stat (path).mtime.getTime ();
|
|
|
|
timer = new Timer (2000);
|
|
timer.run = function () {
|
|
|
|
var modified = FileSystem.stat (path).mtime.getTime ();
|
|
|
|
if (modified > lastModified) {
|
|
|
|
lastModified = modified;
|
|
loadManifest ();
|
|
|
|
if (eventCallback != null) {
|
|
|
|
eventCallback (this, "change");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
loadManifest ();
|
|
|
|
#end
|
|
#end
|
|
|
|
}
|
|
|
|
|
|
public override function exists (id:String, type:String):Bool {
|
|
|
|
var requestedType = type != null ? cast (type, AssetType) : null;
|
|
var assetType = this.type.get (id);
|
|
|
|
if (assetType != null) {
|
|
|
|
if (assetType == requestedType || ((requestedType == SOUND || requestedType == MUSIC) && (assetType == MUSIC || assetType == SOUND))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#if flash
|
|
|
|
if ((assetType == BINARY || assetType == TEXT) && requestedType == BINARY) {
|
|
|
|
return true;
|
|
|
|
} else if (path.exists (id)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (requestedType == BINARY || requestedType == null || (assetType == BINARY && requestedType == TEXT)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#end
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
public override function getAudioBuffer (id:String):AudioBuffer {
|
|
|
|
#if flash
|
|
|
|
var buffer = new AudioBuffer ();
|
|
buffer.src = cast (Type.createInstance (className.get (id), []), Sound);
|
|
return buffer;
|
|
|
|
#elseif html5
|
|
|
|
return null;
|
|
//return new Sound (new URLRequest (path.get (id)));
|
|
|
|
#else
|
|
|
|
return AudioBuffer.fromFile (path.get (id));
|
|
//if (className.exists(id)) return cast (Type.createInstance (className.get (id), []), Sound);
|
|
//else return new Sound (new URLRequest (path.get (id)), null, type.get (id) == MUSIC);
|
|
|
|
#end
|
|
|
|
}
|
|
|
|
|
|
public override function getBytes (id:String):ByteArray {
|
|
|
|
#if flash
|
|
|
|
return cast (Type.createInstance (className.get (id), []), ByteArray);
|
|
|
|
#elseif html5
|
|
|
|
var bytes:ByteArray = null;
|
|
var data = Preloader.loaders.get (path.get (id)).data;
|
|
|
|
if (Std.is (data, String)) {
|
|
|
|
bytes = new ByteArray ();
|
|
bytes.writeUTFBytes (data);
|
|
|
|
} else if (Std.is (data, ByteArray)) {
|
|
|
|
bytes = cast data;
|
|
|
|
} else {
|
|
|
|
bytes = null;
|
|
|
|
}
|
|
|
|
if (bytes != null) {
|
|
|
|
bytes.position = 0;
|
|
return bytes;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
}
|
|
|
|
#else
|
|
|
|
if (className.exists(id)) return cast (Type.createInstance (className.get (id), []), ByteArray);
|
|
else return ByteArray.readFile (path.get (id));
|
|
|
|
#end
|
|
|
|
}
|
|
|
|
|
|
public override function getFont (id:String):Dynamic /*Font*/ {
|
|
|
|
// TODO: Complete Lime Font API
|
|
|
|
#if openfl
|
|
#if (flash || html5)
|
|
|
|
return cast (Type.createInstance (className.get (id), []), openfl.text.Font);
|
|
|
|
#else
|
|
|
|
if (className.exists (id)) {
|
|
|
|
var fontClass = className.get (id);
|
|
openfl.text.Font.registerFont (fontClass);
|
|
return cast (Type.createInstance (fontClass, []), openfl.text.Font);
|
|
|
|
} else {
|
|
|
|
return new openfl.text.Font (path.get (id));
|
|
|
|
}
|
|
|
|
#end
|
|
#end
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
public override function getImage (id:String):Image {
|
|
|
|
#if flash
|
|
|
|
return Image.fromBitmapData (cast (Type.createInstance (className.get (id), []), BitmapData));
|
|
|
|
#elseif html5
|
|
|
|
return Image.fromImageElement (Preloader.images.get (path.get (id)));
|
|
|
|
#else
|
|
|
|
return Image.fromFile (path.get (id));
|
|
|
|
#end
|
|
|
|
}
|
|
|
|
|
|
/*public override function getMusic (id:String):Dynamic {
|
|
|
|
#if flash
|
|
|
|
return cast (Type.createInstance (className.get (id), []), Sound);
|
|
|
|
#elseif openfl_html5
|
|
|
|
//var sound = new Sound ();
|
|
//sound.__buffer = true;
|
|
//sound.load (new URLRequest (path.get (id)));
|
|
//return sound;
|
|
return null;
|
|
|
|
#elseif html5
|
|
|
|
return null;
|
|
//return new Sound (new URLRequest (path.get (id)));
|
|
|
|
#else
|
|
|
|
return null;
|
|
//if (className.exists(id)) return cast (Type.createInstance (className.get (id), []), Sound);
|
|
//else return new Sound (new URLRequest (path.get (id)), null, true);
|
|
|
|
#end
|
|
|
|
}*/
|
|
|
|
|
|
public override function getPath (id:String):String {
|
|
|
|
//#if ios
|
|
|
|
//return SystemPath.applicationDirectory + "/assets/" + path.get (id);
|
|
|
|
//#else
|
|
|
|
return path.get (id);
|
|
|
|
//#end
|
|
|
|
}
|
|
|
|
|
|
public override function getText (id:String):String {
|
|
|
|
#if html5
|
|
|
|
var bytes:ByteArray = null;
|
|
var data = Preloader.loaders.get (path.get (id)).data;
|
|
|
|
if (Std.is (data, String)) {
|
|
|
|
return cast data;
|
|
|
|
} else if (Std.is (data, ByteArray)) {
|
|
|
|
bytes = cast data;
|
|
|
|
} else {
|
|
|
|
bytes = null;
|
|
|
|
}
|
|
|
|
if (bytes != null) {
|
|
|
|
bytes.position = 0;
|
|
return bytes.readUTFBytes (bytes.length);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
}
|
|
|
|
#else
|
|
|
|
var bytes = getBytes (id);
|
|
|
|
if (bytes == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return bytes.readUTFBytes (bytes.length);
|
|
|
|
}
|
|
|
|
#end
|
|
|
|
}
|
|
|
|
|
|
public override function isLocal (id:String, type:String):Bool {
|
|
|
|
var requestedType = type != null ? cast (type, AssetType) : null;
|
|
|
|
#if flash
|
|
|
|
//if (requestedType != AssetType.MUSIC && requestedType != AssetType.SOUND) {
|
|
|
|
return className.exists (id);
|
|
|
|
//}
|
|
|
|
#end
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
public override function list (type:String):Array<String> {
|
|
|
|
var requestedType = type != null ? cast (type, AssetType) : null;
|
|
var items = [];
|
|
|
|
for (id in this.type.keys ()) {
|
|
|
|
if (requestedType == null || exists (id, type)) {
|
|
|
|
items.push (id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
public override function loadAudioBuffer (id:String, handler:AudioBuffer -> Void):Void {
|
|
|
|
#if (flash)
|
|
if (path.exists (id)) {
|
|
|
|
var soundLoader = new Sound ();
|
|
soundLoader.addEventListener (Event.COMPLETE, function (event) {
|
|
|
|
var audioBuffer:AudioBuffer = new AudioBuffer();
|
|
audioBuffer.src = event.currentTarget;
|
|
handler (audioBuffer);
|
|
|
|
});
|
|
soundLoader.load (new URLRequest (path.get (id)));
|
|
|
|
} else {
|
|
handler (getAudioBuffer (id));
|
|
|
|
}
|
|
#else
|
|
handler (getAudioBuffer (id));
|
|
|
|
#end
|
|
|
|
}
|
|
|
|
|
|
public override function loadBytes (id:String, handler:ByteArray -> Void):Void {
|
|
|
|
#if flash
|
|
|
|
if (path.exists (id)) {
|
|
|
|
var loader = new URLLoader ();
|
|
loader.addEventListener (Event.COMPLETE, function (event:Event) {
|
|
|
|
var bytes = new ByteArray ();
|
|
bytes.writeUTFBytes (event.currentTarget.data);
|
|
bytes.position = 0;
|
|
|
|
handler (bytes);
|
|
|
|
});
|
|
loader.load (new URLRequest (path.get (id)));
|
|
|
|
} else {
|
|
|
|
handler (getBytes (id));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
handler (getBytes (id));
|
|
|
|
#end
|
|
|
|
}
|
|
|
|
|
|
public override function loadImage (id:String, handler:Image -> Void):Void {
|
|
|
|
#if flash
|
|
|
|
if (path.exists (id)) {
|
|
|
|
var loader = new Loader ();
|
|
loader.contentLoaderInfo.addEventListener (Event.COMPLETE, function (event:Event) {
|
|
|
|
var bitmapData = cast (event.currentTarget.content, Bitmap).bitmapData;
|
|
handler (Image.fromBitmapData (bitmapData));
|
|
|
|
});
|
|
loader.load (new URLRequest (path.get (id)));
|
|
|
|
} else {
|
|
|
|
handler (getImage (id));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
handler (getImage (id));
|
|
|
|
#end
|
|
|
|
}
|
|
|
|
|
|
#if (!flash && !html5)
|
|
private function loadManifest ():Void {
|
|
|
|
try {
|
|
|
|
#if blackberry
|
|
var bytes = ByteArray.readFile ("app/native/manifest");
|
|
#elseif tizen
|
|
var bytes = ByteArray.readFile ("../res/manifest");
|
|
#elseif emscripten
|
|
var bytes = ByteArray.readFile ("assets/manifest");
|
|
#elseif (mac && java)
|
|
var bytes = ByteArray.readFile ("../Resources/manifest");
|
|
#else
|
|
var bytes = ByteArray.readFile ("manifest");
|
|
#end
|
|
|
|
if (bytes != null) {
|
|
|
|
bytes.position = 0;
|
|
|
|
if (bytes.length > 0) {
|
|
|
|
var data = bytes.readUTFBytes (bytes.length);
|
|
|
|
if (data != null && data.length > 0) {
|
|
|
|
var manifest:Array<Dynamic> = Unserializer.run (data);
|
|
|
|
for (asset in manifest) {
|
|
|
|
if (!className.exists (asset.id)) {
|
|
|
|
path.set (asset.id, asset.path);
|
|
type.set (asset.id, cast (asset.type, AssetType));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
trace ("Warning: Could not load asset manifest (bytes was null)");
|
|
|
|
}
|
|
|
|
} catch (e:Dynamic) {
|
|
|
|
trace ('Warning: Could not load asset manifest (${e})');
|
|
|
|
}
|
|
|
|
}
|
|
#end
|
|
|
|
|
|
/*public override function loadMusic (id:String, handler:Dynamic -> Void):Void {
|
|
|
|
#if (flash || html5)
|
|
|
|
//if (path.exists (id)) {
|
|
|
|
// var loader = new Loader ();
|
|
// loader.contentLoaderInfo.addEventListener (Event.COMPLETE, function (event) {
|
|
|
|
// handler (cast (event.currentTarget.content, Bitmap).bitmapData);
|
|
|
|
// });
|
|
// loader.load (new URLRequest (path.get (id)));
|
|
|
|
//} else {
|
|
|
|
handler (getMusic (id));
|
|
|
|
//}
|
|
|
|
#else
|
|
|
|
handler (getMusic (id));
|
|
|
|
#end
|
|
|
|
}*/
|
|
|
|
|
|
public override function loadText (id:String, handler:String -> Void):Void {
|
|
|
|
//#if html5
|
|
|
|
/*if (path.exists (id)) {
|
|
|
|
var loader = new URLLoader ();
|
|
loader.addEventListener (Event.COMPLETE, function (event:Event) {
|
|
|
|
handler (event.currentTarget.data);
|
|
|
|
});
|
|
loader.load (new URLRequest (path.get (id)));
|
|
|
|
} else {
|
|
|
|
handler (getText (id));
|
|
|
|
}*/
|
|
|
|
//#else
|
|
|
|
var callback = function (bytes:ByteArray):Void {
|
|
|
|
if (bytes == null) {
|
|
|
|
handler (null);
|
|
|
|
} else {
|
|
|
|
handler (bytes.readUTFBytes (bytes.length));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loadBytes (id, callback);
|
|
|
|
//#end
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#if !display
|
|
#if flash
|
|
|
|
::foreach assets::::if (embed)::::if (type == "image")::@:keep @:bind #if display private #end class __ASSET__::flatName:: extends flash.display.BitmapData { public function new () { super (0, 0, true, 0); } }::else::@:keep @:bind #if display private #end class __ASSET__::flatName:: extends ::flashClass:: { }::end::::end::
|
|
::end::
|
|
|
|
#elseif html5
|
|
|
|
#if openfl
|
|
::foreach assets::::if (type == "font")::@:keep #if display private #end class __ASSET__::flatName:: extends openfl.text.Font { public function new () { super (); fontName = "::fontName::"; } } ::end::
|
|
::end::
|
|
#end
|
|
|
|
#else
|
|
|
|
#if openfl
|
|
::if (assets != null)::::foreach assets::::if (type == "font")::@:keep class __ASSET__::flatName:: extends openfl.text.Font { public function new () { super (); __fontPath = "::targetPath::"; fontName = "::fontName::"; }}
|
|
::end::::end::::end::
|
|
#end
|
|
|
|
#if (windows || mac || linux)
|
|
|
|
::if (assets != null)::
|
|
::foreach assets::::if (embed)::::if (type == "image")::@:bitmap("::sourcePath::") class __ASSET__::flatName:: extends lime.graphics.Image {}
|
|
::elseif (type == "sound")::@:sound("::sourcePath::") class __ASSET__::flatName:: extends lime.audio.AudioSource {}
|
|
::elseif (type == "music")::@:sound("::sourcePath::") class __ASSET__::flatName:: extends lime.audio.AudioSource {}
|
|
::elseif (type == "font")::@:font("::sourcePath::") class __ASSET__::flatName:: extends lime.graphics.Font {}
|
|
::else::@:file("::sourcePath::") class __ASSET__::flatName:: extends lime.utils.ByteArray {}
|
|
::end::::end::::end::
|
|
::end::
|
|
|
|
#end
|
|
|
|
#end
|
|
#end
|
|
|