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