增加版本信息获取工具类.
可以在项目代码中直接使用 import um = require('UpdateManager') 然后调用相关接口.
This commit is contained in:
parent
ae6566c8dd
commit
ef915a121e
@ -105,7 +105,7 @@
|
||||
"_contentSize": {
|
||||
"__type__": "cc.Size",
|
||||
"width": 700,
|
||||
"height": 255.6
|
||||
"height": 75.6
|
||||
},
|
||||
"_anchorPoint": {
|
||||
"__type__": "cc.Vec2",
|
||||
@ -117,7 +117,7 @@
|
||||
"ctor": "Float64Array",
|
||||
"array": [
|
||||
0,
|
||||
0,
|
||||
282.58,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
@ -156,9 +156,9 @@
|
||||
],
|
||||
"_srcBlendFactor": 770,
|
||||
"_dstBlendFactor": 771,
|
||||
"_string": "这是Bundle2,修改此bundle 并上传更新包,然后运行查看效果.\n当前版本V4",
|
||||
"_N$string": "这是Bundle2,修改此bundle 并上传更新包,然后运行查看效果.\n当前版本V4",
|
||||
"_fontSize": 60,
|
||||
"_string": "",
|
||||
"_N$string": "",
|
||||
"_fontSize": 30,
|
||||
"_lineHeight": 60,
|
||||
"_enableWrapText": true,
|
||||
"_N$file": null,
|
||||
@ -193,8 +193,9 @@
|
||||
"__id__": 1
|
||||
},
|
||||
"_enabled": true,
|
||||
"label": null,
|
||||
"text": "hello",
|
||||
"label": {
|
||||
"__id__": 3
|
||||
},
|
||||
"_id": ""
|
||||
},
|
||||
{
|
||||
|
@ -1,28 +1,21 @@
|
||||
// Learn TypeScript:
|
||||
// - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
|
||||
// Learn Attribute:
|
||||
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
|
||||
// Learn life-cycle callbacks:
|
||||
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
|
||||
|
||||
const {ccclass, property} = cc._decorator;
|
||||
|
||||
//@ts-ignore
|
||||
import UpdateManager = require('UpdateManager');
|
||||
|
||||
@ccclass
|
||||
export default class NewClass extends cc.Component {
|
||||
|
||||
@property(cc.Label)
|
||||
label: cc.Label = null;
|
||||
|
||||
@property
|
||||
text: string = 'hello';
|
||||
|
||||
// LIFE-CYCLE CALLBACKS:
|
||||
|
||||
// onLoad () {}
|
||||
|
||||
start() {
|
||||
|
||||
if (UpdateManager.getUpdateFlag()) {
|
||||
this.label.string = UpdateManager.cur_ver_desc;
|
||||
UpdateManager.resetUpdateFlag();
|
||||
} else {
|
||||
this.label.string = "未发现新版本,\n当前版本:" + UpdateManager.cur_ver_name;
|
||||
}
|
||||
console.log('>>>>>>>>>>>>>>>>>>>>>', UpdateManager.cur_ver_code);
|
||||
}
|
||||
|
||||
// update (dt) {}
|
||||
}
|
||||
|
@ -79,11 +79,11 @@ class FileUploader {
|
||||
if (this.ftps[i].totalTasks && !this.ftps[i].idleState) {
|
||||
p = (this.ftps[i].totalTasks - this.ftps[i].queue.length) / this.ftps[i].totalTasks;
|
||||
}
|
||||
p = Math.floor(p * 10000) / 100;
|
||||
p = parseFloat((Math.floor(p * 10000) / 100).toFixed(2));
|
||||
str.push({
|
||||
name: i,
|
||||
idle: this.ftps[i].idleState,
|
||||
remain: this.ftps[i].queue.length,
|
||||
remain: this.ftps[i].getTaskLength(),
|
||||
hasError: this.ftps[i].hasError,
|
||||
totalTasks: this.ftps[i].totalTasks,
|
||||
curTaskPath: this.ftps[i].curTaskPath,
|
||||
@ -240,7 +240,15 @@ class UploadThread {
|
||||
|
||||
uploadOnce(ftp, once) {
|
||||
return Promise.race([new Promise(function (resolve) {
|
||||
ftp.put(once.src, once.dst, false, resolve)
|
||||
fs.readFile(once.src, (err, data) => {
|
||||
if (err) {
|
||||
logger.error('文件读取异常:', once.src, err);
|
||||
resolve(err)
|
||||
return
|
||||
} else {
|
||||
ftp.put(data, once.dst, false, resolve)
|
||||
}
|
||||
});
|
||||
}), this.sleep(20000)]);
|
||||
}
|
||||
|
||||
|
@ -34,15 +34,13 @@ class FileUtil {
|
||||
* @param src
|
||||
* @param dst
|
||||
*/
|
||||
copy(src, dst) {
|
||||
async copy(src, dst) {
|
||||
const st = fs.statSync(src);
|
||||
if (st.isFile()) {
|
||||
var readable = fs.createReadStream(src);//创建读取流
|
||||
var writable = fs.createWriteStream(dst);//创建写入流
|
||||
const readable = fs.createReadStream(src);//创建读取流
|
||||
const writable = fs.createWriteStream(dst);//创建写入流
|
||||
readable.pipe(writable);
|
||||
return;
|
||||
} else if (!st.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
if (!fs.existsSync(dst)) {
|
||||
fs.mkdirSync(dst, {recursive: true});
|
||||
@ -56,7 +54,6 @@ class FileUtil {
|
||||
|
||||
write(filePath, content) {
|
||||
const pathInfo = path.parse(filePath);
|
||||
// console.log('写文件:', filePath, pathInfo.dir)
|
||||
if (!fs.existsSync(pathInfo.dir)) {
|
||||
fs.mkdirSync(pathInfo.dir, {recursive: true});
|
||||
}
|
||||
|
129
packages/update-manager/lib/UpdateManager.js
Normal file
129
packages/update-manager/lib/UpdateManager.js
Normal file
@ -0,0 +1,129 @@
|
||||
/**
|
||||
* 方便直接获取当前版本号等信息.
|
||||
* 使用方式:
|
||||
* // @ts-ignore
|
||||
* import UpdateManager = require( "UpdateManager");
|
||||
* 然后在代码中直接使用即可.
|
||||
*/
|
||||
class UpdateManager {
|
||||
|
||||
cur_ver_code = 0;
|
||||
cur_ver_name = "1.0.1";
|
||||
cur_ver_desc = "";
|
||||
ver_type = "dev";
|
||||
|
||||
new_ver_code = 0;
|
||||
new_ver_name = "1.0.1";
|
||||
new_ver_desc = "";
|
||||
new_ver_info = {};
|
||||
|
||||
// 记录当前版本的bundles. 如果有新版本, 则自动下载新bundles.
|
||||
cur_bundles = {};
|
||||
|
||||
updateSuccess = "0";
|
||||
|
||||
constructor() {
|
||||
const curVer = window.localStorage.getItem("cur_ver_info");
|
||||
if (curVer) {
|
||||
const verInfo = JSON.parse(curVer);
|
||||
this.new_ver_code = this.cur_ver_code = verInfo.versionCode;
|
||||
this.new_ver_name = this.cur_ver_name = verInfo.versionName;
|
||||
this.ver_type = verInfo.versionType;
|
||||
this.new_ver_desc = this.cur_ver_desc = verInfo.versionLog;
|
||||
this.new_ver_info = verInfo;
|
||||
this.cur_bundles = verInfo.bundleVers;
|
||||
}
|
||||
|
||||
this.updateSuccess = window.localStorage.getItem("ver_updated") || 0;
|
||||
this.fetchNewVerInfo();
|
||||
}
|
||||
|
||||
// 此方法适用于非强制更新包. 需要设计信息界面,显示版本信息.
|
||||
// 手动调用进行更新下载, 下载完成返回true, 此时需要重启游戏才能应用更新. 需要界面提示用户.
|
||||
doUpdate() {
|
||||
if (this.hasNewVersion()) {
|
||||
window.localStorage.setItem('cur_ver_info', JSON.stringify(this.new_ver_info));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
resetUpdateFlag() {
|
||||
window.localStorage.setItem('ver_updated', '0');
|
||||
}
|
||||
|
||||
getUpdateFlag() {
|
||||
return this.updateSuccess == 1;
|
||||
}
|
||||
|
||||
// 检测是否有新版本.
|
||||
hasNewVersion() {
|
||||
return this.new_ver_info && this.new_ver_info.versionCode && this.new_ver_info.versionCode != this.cur_ver_code;
|
||||
}
|
||||
|
||||
fetchNewVerInfo() {
|
||||
let url = window.updateUrl || false;
|
||||
if (!url) {
|
||||
return;
|
||||
}
|
||||
// 游戏启动后再请求更新,避免影响启动速度.
|
||||
url += `?_t=${Math.random()}`;
|
||||
cc.log("请求更新地址:", url);
|
||||
this._get(url).then(version => {
|
||||
if (!version) {
|
||||
console.log("未检测到更新版本内容");
|
||||
return;
|
||||
}
|
||||
if (version) {
|
||||
cc.log('当前版本号:', this.cur_ver_code, this.cur_ver_name);
|
||||
cc.log('请求更新内容:', version);
|
||||
let verInfo = version;
|
||||
if (typeof version === 'string') {
|
||||
verInfo = JSON.parse(version);
|
||||
}
|
||||
this.new_ver_code = verInfo.versionCode;
|
||||
this.new_ver_name = verInfo.versionName;
|
||||
this.new_ver_desc = verInfo.versionLog;
|
||||
|
||||
this.new_ver_info = verInfo;
|
||||
|
||||
// 如果首次运行,本地未缓存版本信息,则进行缓存.
|
||||
if (!this.cur_ver_code) {
|
||||
cc.log("当前首次运行,记录版本信息");
|
||||
window.localStorage.setItem('cur_ver_info', JSON.stringify(this.new_ver_info));
|
||||
|
||||
this.new_ver_code = this.cur_ver_code = verInfo.versionCode;
|
||||
this.new_ver_name = this.cur_ver_name = verInfo.versionName;
|
||||
this.ver_type = verInfo.versionType;
|
||||
this.new_ver_desc = this.cur_ver_desc = verInfo.versionLog;
|
||||
|
||||
} else if (this.hasNewVersion() && verInfo.forceUpdate) {
|
||||
window.localStorage.setItem('cur_ver_info', JSON.stringify(this.new_ver_info));
|
||||
window.localStorage.setItem('ver_updated', '1');
|
||||
cc.log("有新版本,且默认强制更新");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ajax 请求.
|
||||
_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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new UpdateManager();
|
9
packages/update-manager/lib/UpdateManager.js.meta
Normal file
9
packages/update-manager/lib/UpdateManager.js.meta
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.0.8",
|
||||
"uuid": "aa7483f4-ac0c-4809-9c5e-4e5734a26db6",
|
||||
"isPlugin": false,
|
||||
"loadPluginInWeb": true,
|
||||
"loadPluginInNative": true,
|
||||
"loadPluginInEditor": false,
|
||||
"subMetas": {}
|
||||
}
|
@ -45,9 +45,12 @@ const saveNewVersionInfo = function (opt, bundleVers, configJson) {
|
||||
const data = {
|
||||
versionCode: configJson.versionCfg.versionCode,
|
||||
versionName: configJson.versionCfg.versionName,
|
||||
versionType: configJson.versionCfg.versionType,
|
||||
versionLog: configJson.versionCfg.versionLog,
|
||||
forceUpdate: configJson.versionCfg.forceUpdate,
|
||||
server: `${configJson.versionCfg.baseUrl}${configJson.ftpCfg.rootPath}${configJson.versionCfg.versionType}/${configJson.versionCfg.versionCode}`
|
||||
};
|
||||
const updateInfo = Object.assign(data, {bundles: bundleVers})
|
||||
const updateInfo = Object.assign(data, {bundles: bundleVers});
|
||||
// 将版本更新信息写入文件
|
||||
let targetUpdatePath = join(opt.dest, `remote/update-${configJson.versionCfg.versionType}.json`)
|
||||
FileUtil.write(targetUpdatePath, JSON.stringify(updateInfo, null, 2));
|
||||
|
@ -9,6 +9,15 @@
|
||||
"message": "update-manager:open"
|
||||
}
|
||||
},
|
||||
"runtime-resource": {
|
||||
"path": "./lib",
|
||||
"name": "lib"
|
||||
},
|
||||
"reload": {
|
||||
"ignore": [
|
||||
"lib/**/*"
|
||||
]
|
||||
},
|
||||
"panel": {
|
||||
"main": "panel/index.html",
|
||||
"type": "simple",
|
||||
|
@ -28,21 +28,14 @@
|
||||
<el-input v-model="versionCfg.versionType" placeholder="dev"></el-input>
|
||||
</el-form-item>
|
||||
</el-tooltip>
|
||||
<!--<el-form-item label="其他设置:">-->
|
||||
<!--<el-tooltip content="开启时将强制更新用户端版本" placement="top">-->
|
||||
<!--<el-switch v-model="versionCfg.forceUpdate"-->
|
||||
<!--:active-text="versionCfg.forceUpdate?'强制静默更新':'用户决定更新'"-->
|
||||
<!--active-color="#13ce66" inactive-color="#ff4949"-->
|
||||
<!--style="margin-right: 20px;"></el-switch>-->
|
||||
<!--</el-tooltip>-->
|
||||
|
||||
<!--<el-tooltip placement="top">-->
|
||||
<!--<div slot="content">开启时将在每次启动时直接请求更新信息<br/>否则将在进入游戏后才请求更新信息</div>-->
|
||||
<!--<el-switch v-model="versionCfg.updateMoment"-->
|
||||
<!--:active-text="versionCfg.updateMoment?'每次启动时更新':'二次启动时更新'"-->
|
||||
<!--active-color="#13ce66" inactive-color="#ff4949"></el-switch>-->
|
||||
<!--</el-tooltip>-->
|
||||
<!--</el-form-item>-->
|
||||
<el-form-item label="强更开关:">
|
||||
<el-tooltip content="开启时将强制更新用户端版本" placement="top">
|
||||
<el-switch v-model="versionCfg.forceUpdate"
|
||||
:active-text="versionCfg.forceUpdate?'强制静默更新':'手动检测更新'"
|
||||
active-color="#13ce66" inactive-color="#ff4949"
|
||||
style="margin-right: 20px;"></el-switch>
|
||||
</el-tooltip>
|
||||
</el-form-item>
|
||||
<el-tooltip effect="dark" :content="tip.baseUrl" open-delay=500 placement="top">
|
||||
<el-form-item label="热更地址:">
|
||||
<el-input v-model="versionCfg.baseUrl"
|
||||
@ -207,7 +200,7 @@
|
||||
versionType: "dev",
|
||||
versionLog: "",
|
||||
baseUrl: "",
|
||||
// forceUpdate: true, // 强制静默更新.
|
||||
forceUpdate: true, // 强制静默更新.
|
||||
// updateMoment: true // 启动时直接请求更新.
|
||||
},
|
||||
ftpCfg: {
|
||||
|
@ -1,9 +1,9 @@
|
||||
window.beforeBoot = function () {
|
||||
cc.log("游戏正在启动中.")
|
||||
// console.log("游戏正在启动中.")
|
||||
if (window.remoteUrl) {
|
||||
const settings = window._CCSettings;
|
||||
settings.server = window.remoteUrl;
|
||||
cc.log("远程资源地址:", settings.server);
|
||||
//cc.log("远程资源地址:", settings.server);
|
||||
}
|
||||
let url = window.updateUrl || false;
|
||||
if (!url) {
|
||||
@ -13,56 +13,31 @@ window.beforeBoot = function () {
|
||||
}
|
||||
|
||||
// 请求缓存信息,判断是否需要更新.
|
||||
let assetStr = window.localStorage.getItem('version_data');
|
||||
let assetStr = window.localStorage.getItem('cur_ver_info');
|
||||
if (!assetStr) {
|
||||
window.boot();
|
||||
} else {
|
||||
// console.log("当前版本信息:", assetStr);
|
||||
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;
|
||||
settings.server = updateInfo.server;
|
||||
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);
|
||||
console.log('bundle 有更新:', b, bundleVers[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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user