mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 16:12:40 +08:00
fix unpacking on mobile
This commit is contained in:
@@ -113,6 +113,6 @@ function downloadBinary (url, callback) {
|
||||
};
|
||||
}
|
||||
xhr.send(null);
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = downloadBinary;
|
||||
@@ -26,6 +26,10 @@ var JS = require('../platform/js');
|
||||
var Path = require('../utils/CCPath');
|
||||
var Pipeline = require('./pipeline');
|
||||
var PackDownloader = require('./pack-downloader');
|
||||
// var downloadBinary = require('./binary-downloader');
|
||||
var downloadText = require('./text-downloader');
|
||||
|
||||
var urlAppendTimestamp = require('./utils').urlAppendTimestamp;
|
||||
|
||||
var downloadAudio;
|
||||
if (!CC_EDITOR || !Editor.isMainProcess) {
|
||||
@@ -34,32 +38,6 @@ if (!CC_EDITOR || !Editor.isMainProcess) {
|
||||
else {
|
||||
downloadAudio = null;
|
||||
}
|
||||
// var downloadBinary = require('binary-downloader');
|
||||
|
||||
function isUrlCrossOrigin (url) {
|
||||
if (!url) {
|
||||
cc.log('invalid URL');
|
||||
return false;
|
||||
}
|
||||
var startIndex = url.indexOf('://');
|
||||
if (startIndex === -1)
|
||||
return false;
|
||||
|
||||
var endIndex = url.indexOf('/', startIndex + 3);
|
||||
var urlOrigin = (endIndex === -1) ? url : url.substring(0, endIndex);
|
||||
return urlOrigin !== location.origin;
|
||||
}
|
||||
|
||||
var _noCacheRex = /\?/;
|
||||
function urlAppendTimestamp (url) {
|
||||
if (cc.game.config['noCache'] && typeof url === 'string') {
|
||||
if(_noCacheRex.test(url))
|
||||
url += '&_t=' + (new Date() - 0);
|
||||
else
|
||||
url += '?_t=' + (new Date() - 0);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
function downloadScript (item, callback, isAsync) {
|
||||
var url = item.url,
|
||||
@@ -84,47 +62,6 @@ function downloadScript (item, callback, isAsync) {
|
||||
d.body.appendChild(s);
|
||||
}
|
||||
|
||||
function downloadText (item, callback) {
|
||||
var url = item.url,
|
||||
xhr = Pipeline.getXMLHttpRequest(),
|
||||
errInfo = 'Load ' + url + ' failed!',
|
||||
navigator = window.navigator;
|
||||
|
||||
url = urlAppendTimestamp(url);
|
||||
|
||||
xhr.open('GET', url, true);
|
||||
if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) {
|
||||
// IE-specific logic here
|
||||
xhr.setRequestHeader('Accept-Charset', 'utf-8');
|
||||
xhr.onreadystatechange = function () {
|
||||
if(xhr.readyState === 4) {
|
||||
if (xhr.status === 200 || xhr.status === 0) {
|
||||
callback(null, xhr.responseText);
|
||||
}
|
||||
else {
|
||||
callback({status:xhr.status, errorMessage:errInfo});
|
||||
}
|
||||
}
|
||||
};
|
||||
} else {
|
||||
if (xhr.overrideMimeType) xhr.overrideMimeType('text\/plain; charset=utf-8');
|
||||
xhr.onload = function () {
|
||||
if(xhr.readyState === 4) {
|
||||
if (xhr.status === 200 || xhr.status === 0) {
|
||||
callback(null, xhr.responseText);
|
||||
}
|
||||
else {
|
||||
callback({status:xhr.status, errorMessage:errInfo});
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.onerror = function(){
|
||||
callback({status:xhr.status, errorMessage:errInfo});
|
||||
};
|
||||
}
|
||||
xhr.send(null);
|
||||
}
|
||||
|
||||
function downloadTextSync (item) {
|
||||
var url = item.url;
|
||||
var xhr = Pipeline.getXMLHttpRequest();
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
****************************************************************************/
|
||||
|
||||
var JsonUnpacker = require('./json-unpacker');
|
||||
var downloadText = require('./text-downloader');
|
||||
|
||||
// {assetUuid: packUuid}
|
||||
var uuidToPack = {};
|
||||
@@ -55,7 +56,7 @@ module.exports = {
|
||||
|
||||
_loadNewPack: function (uuid, packUuid, callback) {
|
||||
var packUrl = cc.AssetLibrary.getImportedDir(packUuid) + '/' + packUuid + '.json';
|
||||
cc.loader.load(packUrl, function (err, packJson) {
|
||||
downloadText({ url: packUrl }, function (err, packJson) {
|
||||
if (err) {
|
||||
cc.error('Failed to download package for ' + uuid);
|
||||
return callback(err);
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
var Pipeline = require('./pipeline');
|
||||
var urlAppendTimestamp = require('./utils').urlAppendTimestamp;
|
||||
|
||||
module.exports = function (item, callback) {
|
||||
var url = item.url,
|
||||
xhr = Pipeline.getXMLHttpRequest(),
|
||||
errInfo = 'Load ' + url + ' failed!',
|
||||
navigator = window.navigator;
|
||||
|
||||
url = urlAppendTimestamp(url);
|
||||
|
||||
xhr.open('GET', url, true);
|
||||
if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) {
|
||||
// IE-specific logic here
|
||||
xhr.setRequestHeader('Accept-Charset', 'utf-8');
|
||||
xhr.onreadystatechange = function () {
|
||||
if(xhr.readyState === 4) {
|
||||
if (xhr.status === 200 || xhr.status === 0) {
|
||||
callback(null, xhr.responseText);
|
||||
}
|
||||
else {
|
||||
callback({status:xhr.status, errorMessage:errInfo});
|
||||
}
|
||||
}
|
||||
};
|
||||
} else {
|
||||
if (xhr.overrideMimeType) xhr.overrideMimeType('text\/plain; charset=utf-8');
|
||||
xhr.onload = function () {
|
||||
if(xhr.readyState === 4) {
|
||||
if (xhr.status === 200 || xhr.status === 0) {
|
||||
callback(null, xhr.responseText);
|
||||
}
|
||||
else {
|
||||
callback({status:xhr.status, errorMessage:errInfo});
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.onerror = function(){
|
||||
callback({status:xhr.status, errorMessage:errInfo});
|
||||
};
|
||||
}
|
||||
xhr.send(null);
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
var _noCacheRex = /\?/;
|
||||
|
||||
module.exports = {
|
||||
//isUrlCrossOrigin: function (url) {
|
||||
// if (!url) {
|
||||
// cc.log('invalid URL');
|
||||
// return false;
|
||||
// }
|
||||
// var startIndex = url.indexOf('://');
|
||||
// if (startIndex === -1)
|
||||
// return false;
|
||||
//
|
||||
// var endIndex = url.indexOf('/', startIndex + 3);
|
||||
// var urlOrigin = (endIndex === -1) ? url : url.substring(0, endIndex);
|
||||
// return urlOrigin !== location.origin;
|
||||
//},
|
||||
urlAppendTimestamp: function (url) {
|
||||
if (cc.game.config['noCache'] && typeof url === 'string') {
|
||||
if(_noCacheRex.test(url))
|
||||
url += '&_t=' + (new Date() - 0);
|
||||
else
|
||||
url += '?_t=' + (new Date() - 0);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user