Add lime.utils.Bytes to fix @:file embed and 'readFile' behavior

This commit is contained in:
Joshua Granick
2015-11-24 23:03:13 -08:00
parent d53c06ec1f
commit 49ed4943ee
4 changed files with 113 additions and 55 deletions

View File

@@ -4,7 +4,6 @@ package lime; #if (!lime_legacy || lime_hybrid)
#if !macro
import haxe.io.Bytes;
import haxe.Json;
import haxe.Unserializer;
import lime.app.Event;
@@ -13,6 +12,7 @@ import lime.app.Future;
import lime.audio.AudioBuffer;
import lime.graphics.Image;
import lime.text.Font;
import lime.utils.Bytes;
/**
* <p>The Assets class provides a cross-platform interface to access
@@ -1283,18 +1283,14 @@ class Assets {
var constructor = macro {
super();
var bytes = haxe.Resource.getBytes (resourceName);
#if lime_console
throw "not implemented";
#else
__fromBytes (haxe.Resource.getBytes (resourceName));
#end
super (bytes.length, bytes.b);
};
var args = [ { name: "size", opt: true, type: macro :Int, value: macro 0 } ];
fields.push ({ name: "new", access: [ APublic ], kind: FFun({ args: args, expr: constructor, params: [], ret: null }), pos: Context.currentPos() });
var args = [ { name: "length", opt: false, type: macro :Int }, { name: "bytesData", opt: false, type: macro :haxe.io.BytesData } ];
fields.push ({ name: "new", access: [ APublic ], kind: FFun({ args: args, expr: constructor, params: [], ret: null }), pos: Context.currentPos () });
}

View File

@@ -1,13 +1,13 @@
package lime.net;
import haxe.io.Bytes;
import lime.app.Future;
import lime.app.Promise;
import lime.net.curl.CURLCode;
import lime.net.curl.CURLEasy;
import lime.system.BackgroundWorker;
import lime.system.CFFI;
import lime.utils.Bytes;
#if (js && html5)
import js.html.XMLHttpRequest;
@@ -80,7 +80,7 @@ class HTTPRequest {
}
var bytes = readFile (path);
var bytes = Bytes.readFile (path);
promise.complete (bytes);
});
@@ -144,17 +144,6 @@ class HTTPRequest {
}
private function readFile (path:String):Bytes {
#if (!flash && !html5 && !macro)
var data:Dynamic = lime_bytes_read_file (path);
if (data != null) return @:privateAccess new Bytes (data.length, data.b);
#end
return null;
}
// Event Handlers
@@ -199,9 +188,4 @@ class HTTPRequest {
}
#if (!flash && !html5 && !macro)
private static var lime_bytes_read_file = CFFI.load ("lime", "lime_bytes_read_file", 1);
#end
}

90
lime/utils/Bytes.hx Normal file
View File

@@ -0,0 +1,90 @@
package lime.utils;
import haxe.io.Bytes in HaxeBytes;
import haxe.io.BytesData;
#if !macro
@:build(lime.system.CFFI.build())
#end
@:autoBuild(lime.Assets.embedFile())
class Bytes extends HaxeBytes {
public function new (length:Int, bytesData:BytesData) {
super (length, bytesData);
}
public static function alloc (length:Int):Bytes {
var bytes = HaxeBytes.alloc (length);
return new Bytes (bytes.length, bytes.b);
}
public static inline function fastGet (b:BytesData, pos:Int):Int {
return HaxeBytes.fastGet (b, pos);
}
public static function ofData (b:BytesData):Bytes {
var bytes = HaxeBytes.ofData (b);
return new Bytes (bytes.length, bytes.b);
}
public static function ofString (s:String):Bytes {
var bytes = HaxeBytes.ofString (s);
return new Bytes (bytes.length, bytes.b);
}
public static function readFile (path:String):Bytes {
#if (!html5 && !macro)
var data:Dynamic = lime_bytes_read_file (path);
if (data != null) return new Bytes (data.length, data.b);
#end
return null;
}
#if ((cpp || neko || nodejs) && !macro)
public static function __fromNativePointer (data:Dynamic, length:Int):Bytes {
var bytes:Dynamic = lime_bytes_from_data_pointer (data, length);
return new Bytes (bytes.length, bytes.b);
}
#end
// Native Methods
#if !macro
@:cffi private static function lime_bytes_from_data_pointer (data:Float, length:Int):Dynamic;
@:cffi private static function lime_bytes_get_data_pointer (data:Dynamic):Float;
@:cffi private static function lime_bytes_read_file (path:String):Dynamic;
#end
}

View File

@@ -1,7 +1,6 @@
package;
import haxe.io.Bytes;
import haxe.Timer;
import haxe.Unserializer;
import lime.app.Future;
@@ -14,6 +13,7 @@ import lime.graphics.Image;
import lime.net.HTTPRequest;
import lime.system.CFFI;
import lime.text.Font;
import lime.utils.Bytes;
import lime.utils.UInt8Array;
import lime.Assets;
@@ -152,6 +152,10 @@ class DefaultAssetLibrary extends AssetLibrary {
return true;
} else if (requestedType == TEXT && assetType == BINARY) {
return true;
} else if (requestedType == null || path.exists (id)) {
return true;
@@ -206,7 +210,7 @@ class DefaultAssetLibrary extends AssetLibrary {
case TEXT, BINARY:
return cast (Type.createInstance (className.get (id), []), Bytes);
return Bytes.ofData (cast (Type.createInstance (className.get (id), []), flash.utils.ByteArray));
case IMAGE:
@@ -245,7 +249,7 @@ class DefaultAssetLibrary extends AssetLibrary {
#else
if (className.exists(id)) return cast (Type.createInstance (className.get (id), []), Bytes);
else return readFile (path.get (id));
else return Bytes.readFile (path.get (id));
#end
@@ -621,17 +625,17 @@ class DefaultAssetLibrary extends AssetLibrary {
try {
#if blackberry
var bytes = readFile ("app/native/manifest");
var bytes = Bytes.readFile ("app/native/manifest");
#elseif tizen
var bytes = readFile ("../res/manifest");
var bytes = Bytes.readFile ("../res/manifest");
#elseif emscripten
var bytes = readFile ("assets/manifest");
var bytes = Bytes.readFile ("assets/manifest");
#elseif (mac && java)
var bytes = readFile ("../Resources/manifest");
var bytes = Bytes.readFile ("../Resources/manifest");
#elseif (ios || tvos)
var bytes = readFile ("assets/manifest");
var bytes = Bytes.readFile ("assets/manifest");
#else
var bytes = readFile ("manifest");
var bytes = Bytes.readFile ("manifest");
#end
if (bytes != null) {
@@ -726,22 +730,6 @@ class DefaultAssetLibrary extends AssetLibrary {
}
private function readFile (path:String):Bytes {
#if (!flash && !html5 && !macro)
var data:Dynamic = lime_bytes_read_file (path);
if (data != null) return @:privateAccess new Bytes (data.length, data.b);
#end
return null;
}
#if (!flash && !html5 && !macro)
private static var lime_bytes_read_file = CFFI.load ("lime", "lime_bytes_read_file", 1);
#end
}
@@ -765,10 +753,10 @@ class DefaultAssetLibrary extends AssetLibrary {
::if (assets != null)::
::foreach assets::::if (embed)::::if (type == "image")::@:image("::sourcePath::") #if display private #end class __ASSET__::flatName:: extends lime.graphics.Image {}
::elseif (type == "sound")::@:file("::sourcePath::") #if display private #end class __ASSET__::flatName:: extends haxe.io.Bytes {}
::elseif (type == "music")::@:file("::sourcePath::") #if display private #end class __ASSET__::flatName:: extends haxe.io.Bytes {}
::elseif (type == "sound")::@:file("::sourcePath::") #if display private #end class __ASSET__::flatName:: extends lime.utils.Bytes {}
::elseif (type == "music")::@:file("::sourcePath::") #if display private #end class __ASSET__::flatName:: extends lime.utils.Bytes {}
::elseif (type == "font")::@:font("::sourcePath::") #if display private #end class __ASSET__::flatName:: extends lime.text.Font {}
::else::@:file("::sourcePath::") #if display private #end class __ASSET__::flatName:: extends haxe.io.Bytes {}
::else::@:file("::sourcePath::") #if display private #end class __ASSET__::flatName:: extends lime.utils.Bytes {}
::end::::end::::end::
::end::