2021-02-01 16:11:00 +00:00
|
|
|
window.beforeBoot = function () {
|
|
|
|
cc.log("游戏正在启动中.")
|
|
|
|
if (window.remoteUrl) {
|
|
|
|
const settings = window._CCSettings;
|
|
|
|
settings.server = window.remoteUrl;
|
|
|
|
cc.log("远程资源地址:", settings.server);
|
|
|
|
}
|
|
|
|
let url = window.updateUrl || false;
|
|
|
|
if (!url) {
|
|
|
|
cc.log("未配置版本更新地址,跳过更新.")
|
|
|
|
window.boot();
|
|
|
|
return;
|
|
|
|
}
|
2021-02-02 14:20:01 +00:00
|
|
|
|
|
|
|
// 请求缓存信息,判断是否需要更新.
|
|
|
|
let assetStr = window.localStorage.getItem('version_data');
|
|
|
|
if (!assetStr) {
|
|
|
|
window.boot();
|
|
|
|
} else {
|
|
|
|
let asset = JSON.parse(assetStr);
|
|
|
|
window.mergeVersion(asset);
|
|
|
|
window.boot();
|
|
|
|
}
|
|
|
|
// 游戏启动后再请求更新,避免影响启动速度.
|
2021-02-01 16:11:00 +00:00
|
|
|
url += `?_t=${Math.random()}`;
|
|
|
|
cc.log("请求更新地址:", url);
|
|
|
|
get(url, function (err, asset) {
|
|
|
|
if (err || !asset) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-02 14:20:01 +00:00
|
|
|
window.localStorage.setItem('version_data', asset);
|
2021-02-01 16:11:00 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
window.mergeVersion = function (updateInfo) {
|
|
|
|
const settings = window._CCSettings;
|
2021-02-02 14:20:01 +00:00
|
|
|
const bundleVers = updateInfo.bundles;
|
2021-02-01 16:11:00 +00:00
|
|
|
if (bundleVers) {
|
|
|
|
for (let b in bundleVers) {
|
|
|
|
if (bundleVers[b] != settings.bundleVers[b]) {
|
|
|
|
// 配置中的bundleVer版本不一致,则添加到remote列表中去,以供远程加载.
|
|
|
|
if (settings.remoteBundles.indexOf(b) < 0) {
|
|
|
|
settings.remoteBundles.push(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
settings.bundleVers = bundleVers;
|
|
|
|
}
|
2021-02-02 14:20:01 +00:00
|
|
|
return;
|
2021-02-01 16:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ajax 请求.
|
|
|
|
function get(url, cb) {
|
|
|
|
const ajax = new XMLHttpRequest();
|
|
|
|
ajax.open("get", url, true);
|
|
|
|
ajax.setRequestHeader("Content-Type", "application/json;charset=utf-8");
|
|
|
|
ajax.onreadystatechange = function () {
|
|
|
|
if (ajax.readyState == 4) {
|
|
|
|
if (ajax.status == 200) {
|
2021-02-02 14:20:01 +00:00
|
|
|
// var response = JSON.parse(ajax.responseText)
|
|
|
|
cb && cb(null, ajax.responseText);
|
2021-02-01 16:11:00 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
cb && cb("request error!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ajax.send(null);
|
|
|
|
}
|