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; } url += `?_t=${Math.random()}`; cc.log("请求更新地址:", url); get(url, function (err, asset) { cc.log("请求更新信息:", url, err && err.toLocaleString(), JSON.stringify(asset)); if (err || !asset) { window.boot(); return; } window.mergeVersion(asset); window.boot(); cc.log("游戏已启动."); }); }; window.mergeVersion = function (updateInfo) { const currentVer = cc.sys.localStorage.getItem("currentVer"); let isFirstRun = false; let newVerFlag = false; if (!currentVer) { isFirstRun = true; cc.log("当前为首次运行"); } else { const oldVerInfo = JSON.parse(currentVer); if (oldVerInfo && oldVerInfo.versionCode != updateInfo.versionCode) { newVerFlag = true; cc.log("发现新版本信息:", updateInfo.versionCode, oldVerInfo.versionCode); } } const settings = window._CCSettings; if (updateInfo.server) { settings.server = updateInfo.server; cc.log("更新远程资源地址:", updateInfo.server); } const bundleVers = updateInfo.bundles if (bundleVers) { let changed = false; for (let b in bundleVers) { if (bundleVers[b] != settings.bundleVers[b]) { // 配置中的bundleVer版本不一致,则添加到remote列表中去,以供远程加载. if (settings.remoteBundles.indexOf(b) < 0) { settings.remoteBundles.push(b); } changed = true; cc.log("发现更新Bundle:", b); } } settings.bundleVers = bundleVers; // 如果首次运行,但检测版本有差异,则标记有更新. if (isFirstRun && changed) { newVerFlag = true; cc.log("标记为新版本."); } } cc.sys.localStorage.setItem('newVerFlag', newVerFlag ? 1 : 0); cc.sys.localStorage.setItem('firstRunFlag', isFirstRun ? 1 : 0); cc.sys.localStorage.setItem('currentVer', JSON.stringify(updateInfo)); } // 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, response); return; } else { cb && cb("request error!"); } } } ajax.send(null); }