您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-js jsb native 运行嵌套proto文件读取失败问题解决方案

2016-09-09 16:11 621 查看
Util.fetch = function(path, callback) {
if (callback && typeof callback != 'function')
callback = null;
if (Util.IS_NODE) {
var fs = require("fs");
if (callback) {
fs.readFile(path, function(err, data) {
if (err)
callback(null);
else
callback(""+data);
});
} else
try {
return fs.readFileSync(path);
} catch (e) {
return null;
}
} else {
if(cc.sys.isNative) {
//Native版本需要特殊处理下ProtoBuf的读取方式
var msg = jsb.fileUtils.getStringFromFile(path);
if(callback){
callback(msg);
}else{
return msg;
}
}else{
var xhr = Util.XHR();
xhr.open('GET', path, callback ? true : false);
// xhr.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
xhr.setRequestHeader('Accept', 'text/plain');
if (typeof xhr.overrideMimeType === 'function') xhr.overrideMimeType('text/plain');
if (callback) {
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;
if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))
callback(xhr.responseText);
else
callback(null);
};
if (xhr.readyState == 4)
return;
xhr.send(null);
} else {
xhr.send(null);
if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))
return xhr.responseText;
return null;
}
}
}
};

cc.loader 加载文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: