BinaryLoader.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * Load binary data by url.
  24. * @function
  25. * @param {String} url
  26. * @param {Function} [cb]
  27. */
  28. cc.loader.loadBinary = function (url, cb) {
  29. var self = this;
  30. var xhr = this.getXMLHttpRequest(),
  31. errInfo = "load " + url + " failed!";
  32. xhr.open("GET", url, true);
  33. if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) {
  34. // IE-specific logic here
  35. xhr.setRequestHeader("Accept-Charset", "x-user-defined");
  36. xhr.onreadystatechange = function () {
  37. if (xhr.readyState == 4 && xhr.status == 200) {
  38. var fileContents = cc._convertResponseBodyToText(xhr["responseBody"]);
  39. cb(null, self._str2Uint8Array(fileContents));
  40. } else cb(errInfo);
  41. };
  42. } else {
  43. if (xhr.overrideMimeType) xhr.overrideMimeType("text\/plain; charset=x-user-defined");
  44. xhr.onload = function () {
  45. xhr.readyState == 4 && xhr.status == 200 ? cb(null, self._str2Uint8Array(xhr.responseText)) : cb(errInfo);
  46. };
  47. }
  48. xhr.send(null);
  49. };
  50. cc.loader._str2Uint8Array = function (strData) {
  51. if (!strData)
  52. return null;
  53. var arrData = new Uint8Array(strData.length);
  54. for (var i = 0; i < strData.length; i++) {
  55. arrData[i] = strData.charCodeAt(i) & 0xff;
  56. }
  57. return arrData;
  58. };
  59. /**
  60. * Load binary data by url synchronously
  61. * @function
  62. * @param {String} url
  63. * @return {Uint8Array}
  64. */
  65. cc.loader.loadBinarySync = function (url) {
  66. var self = this;
  67. var req = this.getXMLHttpRequest();
  68. var errInfo = "load " + url + " failed!";
  69. req.open('GET', url, false);
  70. var arrayInfo = null;
  71. if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) {
  72. req.setRequestHeader("Accept-Charset", "x-user-defined");
  73. req.send(null);
  74. if (req.status != 200) {
  75. cc.log(errInfo);
  76. return null;
  77. }
  78. var fileContents = cc._convertResponseBodyToText(req["responseBody"]);
  79. if (fileContents) {
  80. arrayInfo = self._str2Uint8Array(fileContents);
  81. }
  82. } else {
  83. if (req.overrideMimeType)
  84. req.overrideMimeType('text\/plain; charset=x-user-defined');
  85. req.send(null);
  86. if (req.status != 200) {
  87. cc.log(errInfo);
  88. return null;
  89. }
  90. arrayInfo = this._str2Uint8Array(req.responseText);
  91. }
  92. return arrayInfo;
  93. };
  94. //Compatibility with IE9
  95. var Uint8Array = Uint8Array || Array;
  96. if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) {
  97. var IEBinaryToArray_ByteStr_Script =
  98. "<!-- IEBinaryToArray_ByteStr -->\r\n" +
  99. //"<script type='text/vbscript'>\r\n" +
  100. "Function IEBinaryToArray_ByteStr(Binary)\r\n" +
  101. " IEBinaryToArray_ByteStr = CStr(Binary)\r\n" +
  102. "End Function\r\n" +
  103. "Function IEBinaryToArray_ByteStr_Last(Binary)\r\n" +
  104. " Dim lastIndex\r\n" +
  105. " lastIndex = LenB(Binary)\r\n" +
  106. " if lastIndex mod 2 Then\r\n" +
  107. " IEBinaryToArray_ByteStr_Last = Chr( AscB( MidB( Binary, lastIndex, 1 ) ) )\r\n" +
  108. " Else\r\n" +
  109. " IEBinaryToArray_ByteStr_Last = " + '""' + "\r\n" +
  110. " End If\r\n" +
  111. "End Function\r\n";// +
  112. //"</script>\r\n";
  113. // inject VBScript
  114. //document.write(IEBinaryToArray_ByteStr_Script);
  115. var myVBScript = cc.newElement('script');
  116. myVBScript.type = "text/vbscript";
  117. myVBScript.textContent = IEBinaryToArray_ByteStr_Script;
  118. document.body.appendChild(myVBScript);
  119. // helper to convert from responseBody to a "responseText" like thing
  120. cc._convertResponseBodyToText = function (binary) {
  121. var byteMapping = {};
  122. for (var i = 0; i < 256; i++) {
  123. for (var j = 0; j < 256; j++) {
  124. byteMapping[ String.fromCharCode(i + j * 256) ] =
  125. String.fromCharCode(i) + String.fromCharCode(j);
  126. }
  127. }
  128. var rawBytes = IEBinaryToArray_ByteStr(binary);
  129. var lastChr = IEBinaryToArray_ByteStr_Last(binary);
  130. return rawBytes.replace(/[\s\S]/g,
  131. function (match) {
  132. return byteMapping[match];
  133. }) + lastChr;
  134. };
  135. }