优化热更插件.

This commit is contained in:
andrewlu 2021-02-02 11:26:55 +08:00
parent 3106b54566
commit 2282ce8881
6 changed files with 81 additions and 30 deletions

View File

@ -156,8 +156,8 @@
],
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_string": "这是Bundle2,修改此bundle 并上传更新包,然后运行查看效果.\n当前版本V3",
"_N$string": "这是Bundle2,修改此bundle 并上传更新包,然后运行查看效果.\n当前版本V3",
"_string": "这是Bundle2,修改此bundle 并上传更新包,然后运行查看效果.\n当前版本V4",
"_N$string": "这是Bundle2,修改此bundle 并上传更新包,然后运行查看效果.\n当前版本V4",
"_fontSize": 60,
"_lineHeight": 60,
"_enableWrapText": true,

View File

@ -180,7 +180,7 @@
"array": [
0,
0,
533.7828766106993,
535.5690540060046,
0,
0,
0,
@ -795,8 +795,8 @@
],
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_string": "当前为新版本:V3",
"_N$string": "当前为新版本:V3",
"_string": "当前为新版本:V4",
"_N$string": "当前为新版本:V4",
"_fontSize": 40,
"_lineHeight": 40,
"_enableWrapText": true,

View File

@ -1,9 +1,9 @@
{
"versionCfg": {
"versionCode": "3",
"versionName": "1.0.2",
"versionCode": "4",
"versionName": "1.0.3",
"versionType": "dev",
"versionLog": "adfadsfdsfdsfsdf",
"versionLog": "测试V4发布版本",
"baseUrl": "https://cdn-awdxc.8zy.com/"
},
"ftpCfg": {
@ -12,6 +12,7 @@
"user": "cos1",
"password": "dkJrWrVy11",
"rootPath": "temp001/",
"keepalive": 100000
"keepalive": 100000,
"maxThread": "10"
}
}

View File

@ -8,12 +8,23 @@ class FileUploader {
maxClients = 3;
options = {};
queue = [];
idleCallback = null;
loopId = 0;
lastIdleState = true;
constructor() {
this.loopId = setInterval(this.loop.bind(this), 200);
}
setOption(options, maxThread) {
this.options = options;
this.maxClients = maxThread || 1;
}
setStateListener(callback) {
this.idleCallback = callback;
}
prepare() {
const currLen = Object.keys(this.ftps).length;
logger.info('准备创建上传线程:', this.maxClients, currLen);
@ -30,6 +41,28 @@ class FileUploader {
this.ftps[id].destroy();
delete this.ftps[id];
}
if (this.loopId) {
clearInterval(this.loopId)
this.loopId = 0;
}
}
// event loop.
loop() {
const curState = this.checkState();
if (curState != this.lastIdleState) {
this.lastIdleState = curState;
this.idleCallback && this.idleCallback(curState);
}
}
checkState() {
let idleState = true;
for (let i in this.ftps) {
idleState = idleState && this.ftps[i].idleState;
if (!idleState) break;
}
return idleState;
}
/**
@ -71,6 +104,7 @@ class UploadThread {
ready = false;
stopped = false;
options = null;
idleState = true;
constructor(options) {
this.options = options;
@ -89,19 +123,15 @@ class UploadThread {
this.ready = false;
const ftp = new Client();
ftp.on('ready', () => {
logger.log('ftp ready');
this.ready = true;
});
ftp.on('close', () => {
logger.log('ftp client has close');
this.ready = false;
});
ftp.on('end', () => {
logger.log('ftp client has end');
this.ready = false;
});
ftp.on('error', (err) => {
logger.log('ftp client has an error : ', JSON.stringify(err));
this.ready = false;
});
ftp.connect(this.options);
@ -123,8 +153,10 @@ class UploadThread {
while (!this.stopped) {
if (!this.ready || this.queue.length <= 0) {
await this.sleep(500);
this.idleState = true;
continue;
}
this.idleState = false;
const once = this.queue[0];
const err = await this.uploadOnce(this.ftp, once);
if (!err) {

View File

@ -63,10 +63,25 @@ module.exports = {
fs.writeFileSync(
Editor.url('packages://update-manager/.settings.conf'), JSON.stringify({}, null, 2), 'utf-8');
}
if (!this.ftp) {
this.ftp = FileUpload();
}
this.ftp.setStateListener(this.onUploadStateChange.bind(this));
},
onUploadStateChange(state) {
if (!state) {
Editor.info('开始上传更新包');
} else {
Editor.success('所有文件上传完成!');
}
},
unload() {
Editor.Builder.removeListener('build-finished', doMakeUpdatePackage);
if (this.ftp) {
this.ftp.destroy();
}
},
messages: {
'open'() {
@ -74,12 +89,12 @@ module.exports = {
},
'upload'(event, dir, dst, options) {
logger.info('upload....');
if (!this.ftp) {
this.ftp = FileUpload();
}
this.ftp.setOption(options, options.maxThread);
this.ftp.upload(dir, dst);
},
'getProjectPath'(event) {
event.reply(Editor.Project.path);
},
'uploadStop'() {
if (this.ftp) {
this.ftp.destroy();

View File

@ -56,11 +56,11 @@
<el-form ref="ftpCfg" :model="ftpCfg" label-width="100px">
<el-tooltip effect="dark" :content="tip.ip" placement="top" open-delay=500>
<el-form-item label="IP:">
<el-col :span="18">
<el-col :span="15">
<el-input type="text" v-model="ftpCfg.host" placeholder="127.0.0.1"></el-input>
</el-col>
<el-col class="line" :span="2">-</el-col>
<el-col :span="4">
<el-col class="line" :span="1">-</el-col>
<el-col :span="6">
<el-input type="number" v-model="ftpCfg.port" placeholder="端口号"></el-input>
</el-col>
</el-form-item>
@ -68,16 +68,16 @@
<el-tooltip effect="dark" :content="tip.user" open-delay=500 placement="top">
<el-form-item label="登录帐号:">
<el-col :span="11">
<el-col :span="12">
<el-input type="text" v-model="ftpCfg.user" placeholder="登录帐号"></el-input>
</el-col>
<el-col class="line" :span="2">-</el-col>
<el-col class="line" :span="1">-</el-col>
<el-col :span="11">
<el-input type="password" v-model="ftpCfg.password" placeholder="密码"></el-input>
</el-col>
</el-form-item>
</el-tooltip>
<el-tooltip effect="dark" content="FTP长连接持续心跳时间" open-delay=500 placement="top">
<el-tooltip effect="dark" content="FTP长连接持续心跳时间,一般情况下忽略此选项" open-delay=500 placement="top">
<el-form-item label="KeepAlive:">
<el-input type="number" v-model="ftpCfg.keepalive" placeholder="10000"></el-input>
</el-form-item>
@ -85,7 +85,7 @@
<el-tooltip effect="dark" content="FTP同时上传文件数,数量越大,上传效率越高<br/>受限于带宽性能,并非越大越好,推荐5~10" open-delay=500
placement="top">
<el-form-item label="并发数量:">
<el-input type="number" v-model="ftpCfg.maxThread" placeholder="5"></el-input>
<el-input type="number" v-model="ftpCfg.maxThread" placeholder="同时上传文件数"></el-input>
</el-form-item>
</el-tooltip>
<el-tooltip effect="dark" :content="tip.rootPath" open-delay=500 placement="top">
@ -114,7 +114,8 @@
<el-step title="版本配置"
description="使用前需要先配置版本信息,并点击保存配置."></el-step>
<el-step title="构建版本"
description="开始构建版本,构建完成后会在构建目录中生成remote文件夹以及update-dev.json更新描述文件"></el-step>
description="开始构建版本,构建完成后会在构建目录中生成remote文件夹以及update-dev.json更新描述文件
注:不需要设置Bundle为远程包,插件将自动调整远程包设置"></el-step>
<el-step title="上传更新包"
description="构建完成时,如需发布新版本,则需要点击[上传更新包],更新包会向游戏代码中注入热更逻辑."></el-step>
</el-steps>
@ -142,6 +143,7 @@
<script>
const fs = require('fs');
const path = require('path');
new Vue({
el: '#app',
data() {
@ -163,7 +165,7 @@
rootPath: "",
keepalive: 100000,
onlyJson: false,
maxThread: 5
maxThread: 1
},
tip: {
versionCode: "递增版本号,如果与旧版本号相同,则会替换线上版本包.",
@ -184,10 +186,11 @@
if (data) {
Object.assign(this, JSON.parse(data));
}
this.projectPath = Editor.url('packages://update-manager');
this.projectPath = this.projectPath.substring(0, this.projectPath.length - 'packages/update-manager'.length)
Editor.log('当前项目路径:', this.projectPath);
//Bugfix: simple类型的面板无法在渲染进程中拿到项目路径.
const self = this;
Editor.Ipc.sendToMain('update-manager:getProjectPath', function (arg) {
self.projectPath = arg;
});
},
methods: {
@ -261,6 +264,6 @@
return true;
}
}
})
});
</script>
</html>