hot-update/packages/update-manager/templates/pre_main.js
2021-02-02 22:20:01 +08:00

69 lines
2.1 KiB
JavaScript

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;
}
// 请求缓存信息,判断是否需要更新.
let assetStr = window.localStorage.getItem('version_data');
if (!assetStr) {
window.boot();
} else {
let asset = JSON.parse(assetStr);
window.mergeVersion(asset);
window.boot();
}
// 游戏启动后再请求更新,避免影响启动速度.
url += `?_t=${Math.random()}`;
cc.log("请求更新地址:", url);
get(url, function (err, asset) {
if (err || !asset) {
return;
}
window.localStorage.setItem('version_data', asset);
});
};
window.mergeVersion = function (updateInfo) {
const settings = window._CCSettings;
const bundleVers = updateInfo.bundles;
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;
}
return;
}
// 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) {
// var response = JSON.parse(ajax.responseText)
cb && cb(null, ajax.responseText);
return;
} else {
cb && cb("request error!");
}
}
}
ajax.send(null);
}