BaseLoader: fix load text resource from assets
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package haxework.net;
|
||||
|
||||
import flash.utils.JSON;
|
||||
import haxe.Timer;
|
||||
import flash.net.URLRequestMethod;
|
||||
import flash.events.ProgressEvent;
|
||||
@@ -79,13 +80,25 @@ class BaseLoader<T> extends Deferred<T> implements ILoader<T> {
|
||||
public function GET(url:String, data:Dynamic = null):Deferred<T> {
|
||||
#if (openfl && lime)
|
||||
if (StringTools.startsWith(url, "%assets%")) {
|
||||
var path:String = url.substring(9);
|
||||
var bytes:ByteArray = openfl.Assets.getBytes(path);
|
||||
if (bytes == null) {
|
||||
throwError("Bytes is null for asset: " + path);
|
||||
return null;
|
||||
} else {
|
||||
return fromBytes(bytes);
|
||||
try {
|
||||
var path:String = url.substring(9);
|
||||
// ToDo: get text resources with getText method
|
||||
var extension:String = path.substring(path.lastIndexOf(".")+1, path.length);
|
||||
if (extension == "json" || extension == "txt") {
|
||||
var data:T = cast JSON.parse(openfl.Assets.getText(path));
|
||||
resolve(data);
|
||||
return this;
|
||||
}
|
||||
var bytes:ByteArray = openfl.Assets.getBytes(path);
|
||||
if (bytes == null) {
|
||||
throwError("Bytes is null for asset: " + path);
|
||||
return this;
|
||||
} else {
|
||||
return fromBytes(bytes);
|
||||
}
|
||||
} catch (error:Dynamic) {
|
||||
throwError(error);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
#end
|
||||
|
||||
Reference in New Issue
Block a user