hot-update/packages/update-manager/templates/pre_main.js

69 lines
2.1 KiB
JavaScript
Raw Normal View History

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;
}
// 请求缓存信息,判断是否需要更新.
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;
}
window.localStorage.setItem('version_data', asset);
2021-02-01 16:11:00 +00:00
});
};
window.mergeVersion = function (updateInfo) {
const settings = window._CCSettings;
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;
}
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) {
// 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);
}