From 1d601c189e0ce250131cb4f8b9a086b9c777965a Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Mon, 27 Oct 2014 16:51:27 -0700 Subject: [PATCH] Fix for loading type BINARY assets as TEXT on native, improve 'isText' file detection --- templates/haxe/DefaultAssetLibrary.hx | 2 +- tools/helpers/FileHelper.hx | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/templates/haxe/DefaultAssetLibrary.hx b/templates/haxe/DefaultAssetLibrary.hx index 74bd3e58e..289f55cca 100644 --- a/templates/haxe/DefaultAssetLibrary.hx +++ b/templates/haxe/DefaultAssetLibrary.hx @@ -138,7 +138,7 @@ class DefaultAssetLibrary extends AssetLibrary { #else - if (requestedType == BINARY || requestedType == null) { + if (requestedType == BINARY || requestedType == null || (assetType == BINARY && requestedType == TEXT)) { return true; diff --git a/tools/helpers/FileHelper.hx b/tools/helpers/FileHelper.hx index 1a4b66cb7..ad65b9265 100644 --- a/tools/helpers/FileHelper.hx +++ b/tools/helpers/FileHelper.hx @@ -397,6 +397,8 @@ class FileHelper { var numChars = 0; var numBytes = 0; + var byteHeader = []; + var zeroBytes = 0; try { @@ -404,12 +406,24 @@ class FileHelper { var byte = input.readByte (); + if (numBytes < 3) { + + byteHeader.push (byte); + + } else if (byteHeader != null) { + + if (byteHeader[0] == 0xFF && byteHeader[1] == 0xFE) return true; // UCS-2LE or UTF-16LE + if (byteHeader[0] == 0xFE && byteHeader[1] == 0xFF) return true; // UCS-2BE or UTF-16BE + if (byteHeader[0] == 0xEF && byteHeader[1] == 0xBB && byteHeader[2] == 0xBF) return true; // UTF-8 + byteHeader = null; + + } + numBytes++; if (byte == 0) { - input.close (); - return false; + zeroBytes++; } @@ -425,7 +439,7 @@ class FileHelper { input.close (); - if (numBytes == 0 || (numChars / numBytes) > 0.7) { + if (numBytes == 0 || (numChars / numBytes) > 0.9 || ((zeroBytes / numBytes) < 0.015 && (numChars / numBytes) > 0.5)) { return true;