2021-02-01 16:11:00 +00:00
|
|
|
window.beforeBoot = function () {
|
2021-02-04 08:12:32 +00:00
|
|
|
|
2021-02-03 13:34:58 +00:00
|
|
|
// console.log("游戏正在启动中.")
|
2021-02-01 16:11:00 +00:00
|
|
|
if (window.remoteUrl) {
|
|
|
|
const settings = window._CCSettings;
|
|
|
|
settings.server = window.remoteUrl;
|
2021-02-03 13:34:58 +00:00
|
|
|
//cc.log("远程资源地址:", settings.server);
|
2021-02-01 16:11:00 +00:00
|
|
|
}
|
|
|
|
let url = window.updateUrl || false;
|
|
|
|
if (!url) {
|
|
|
|
cc.log("未配置版本更新地址,跳过更新.")
|
|
|
|
window.boot();
|
|
|
|
return;
|
|
|
|
}
|
2021-02-04 08:12:32 +00:00
|
|
|
// 游戏启动后再请求更新,避免影响启动速度.
|
|
|
|
url += `?_t=${Math.random()}`;
|
|
|
|
cc.log("请求更新地址:", url);
|
2021-02-02 14:20:01 +00:00
|
|
|
|
2021-02-04 08:12:32 +00:00
|
|
|
if (window.updateBefore) {
|
|
|
|
_get(url).then(resp => {
|
|
|
|
if (!resp) {
|
|
|
|
window.boot();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
window.localStorage.setItem('cur_ver_info', resp);
|
|
|
|
window.onBooting();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
window.onBooting();
|
|
|
|
_get(url).then(resp => {
|
|
|
|
if (!resp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
window.localStorage.setItem('cur_ver_info', resp);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
window.onBooting = function () {
|
2021-02-02 14:20:01 +00:00
|
|
|
// 请求缓存信息,判断是否需要更新.
|
2021-02-03 13:34:58 +00:00
|
|
|
let assetStr = window.localStorage.getItem('cur_ver_info');
|
2021-02-02 14:20:01 +00:00
|
|
|
if (!assetStr) {
|
|
|
|
window.boot();
|
|
|
|
} else {
|
2021-02-04 08:12:32 +00:00
|
|
|
console.log("当前版本信息:", assetStr);
|
2021-02-02 14:20:01 +00:00
|
|
|
let asset = JSON.parse(assetStr);
|
|
|
|
window.mergeVersion(asset);
|
|
|
|
window.boot();
|
2021-02-04 08:12:32 +00:00
|
|
|
|
|
|
|
// 判断当前是否有版本更新.
|
|
|
|
const lastVer = window.localStorage.getItem('last_ver_code') || asset.versionCode;
|
|
|
|
const curVer = asset.versionCode || 0;
|
|
|
|
if (lastVer != curVer) {
|
|
|
|
window.localStorage.setItem('new_ver_flag', "1");
|
|
|
|
} else {
|
|
|
|
window.localStorage.setItem('new_ver_flag', "0");
|
|
|
|
}
|
|
|
|
window.localStorage.setItem('last_ver_code', curVer);
|
|
|
|
// 当前版本名称.
|
|
|
|
window.localStorage.setItem('cur_ver_name', asset.versionName);
|
2021-02-02 14:20:01 +00:00
|
|
|
}
|
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-03 13:34:58 +00:00
|
|
|
settings.server = updateInfo.server;
|
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);
|
2021-02-03 13:34:58 +00:00
|
|
|
console.log('bundle 有更新:', b, bundleVers[b]);
|
2021-02-01 16:11:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
settings.bundleVers = bundleVers;
|
|
|
|
}
|
2021-02-03 13:34:58 +00:00
|
|
|
};
|
2021-02-01 16:11:00 +00:00
|
|
|
|
2021-02-04 08:12:32 +00:00
|
|
|
function _get(url) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
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) {
|
|
|
|
resolve(ajax.responseText);
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ajax.send(null);
|
|
|
|
});
|
|
|
|
};
|