增加 启动后延迟更新开关.

使用此开关后,游戏包将在启动进入游戏后才请求更新信息, 直到重启后再打开游戏才应用更新.

优化热更流程,可以在构建完成后,再领奖注入热更,上传更新.
This commit is contained in:
andrewlu
2021-02-04 16:12:32 +08:00
parent ef915a121e
commit b81d004641
8 changed files with 141 additions and 204 deletions

View File

@@ -35,6 +35,13 @@
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.updateBefore"
:active-text="versionCfg.updateBefore?'更新完进游戏':'进游戏后请求更新'"
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="热更地址:">
@@ -168,7 +175,7 @@
<el-row style="margin-top: 20px;">
<el-col :span="8">
<el-button type="success" @click="onSave">保存配置信息</el-button>
<el-button type="success" @click="onSave">注入热更配置</el-button>
</el-col>
<el-col :span="8">
<el-button type="danger" @click="onUpload">上传更新包</el-button>
@@ -201,7 +208,7 @@
versionLog: "",
baseUrl: "",
forceUpdate: true, // 强制静默更新.
// updateMoment: true // 启动时直接请求更新.
updateBefore: true // 启动时直接请求更新.
},
ftpCfg: {
host: "127.0.0.1",
@@ -268,9 +275,21 @@
this.ftpCfg.rootPath += "/";
}
const data = Object.assign({}, {versionCfg: this.versionCfg, ftpCfg: this.ftpCfg});
fs && fs.writeFileSync && fs.writeFileSync(
fs.writeFileSync(
Editor.url('packages://update-manager/.settings.conf'), JSON.stringify(data, null, 2), 'utf-8');
this.$message({message: "配置保存成功!", type: "success"});
Editor.Ipc.sendToMain('update-manager:saveConfig');
let filePath = `update-${this.versionCfg.versionType}.json`;
const uploadDstPath = `${this.ftpCfg.rootPath}${this.versionCfg.versionType}/${filePath}`;
let updateUrl = `${this.versionCfg.baseUrl}${uploadDstPath}`;
const serverUrl = `${this.versionCfg.baseUrl}${this.ftpCfg.rootPath}${this.versionCfg.versionType}/${this.versionCfg.versionCode}`;
if (this.notifyMainJs(updateUrl, serverUrl, this.versionCfg.updateBefore)) {
this.$message.info('热更注入成功');
} else {
this.$message.error('热更注入失败,请检查构建目录.');
}
},
onUpload() {
if (this.uploadState) {
@@ -280,28 +299,17 @@
}
this.uploadState = true;
logger.info("准备上传更新包");
this.$message("准备上传更新包");
if (!this.versionCfg.baseUrl.endsWith('/') && this.versionCfg.baseUrl.length > 0) {
this.versionCfg.baseUrl += "/";
}
if (!this.ftpCfg.rootPath.endsWith('/') && this.ftpCfg.rootPath.length > 0) {
this.ftpCfg.rootPath += "/";
}
let filePath = `update-${this.versionCfg.versionType}.json`;
const uploadDstPath = `${this.ftpCfg.rootPath}${this.versionCfg.versionType}/${filePath}`;
let updateUrl = `${this.versionCfg.baseUrl}${uploadDstPath}`;
const serverUrl = `${this.versionCfg.baseUrl}${this.ftpCfg.rootPath}${this.versionCfg.versionType}/${this.versionCfg.versionCode}`;
if (!this.notifyMainJs(updateUrl, serverUrl)) {
return;
}
if (this.ftpCfg.onlyJson) {
logger.warn('当前仅上传JSON文件,请手动上传remote目录');
return;
}
const filePath = `update-${this.versionCfg.versionType}.json`;
const uploadDstPath = `${this.ftpCfg.rootPath}${this.versionCfg.versionType}/${filePath}`;
logger.info('上传文件:', uploadDstPath);
// 上传更新描述文件.
const updateJsonPath = path.join(this.projectPath, `build/jsb-link/remote/${filePath}`);
Editor.Ipc.sendToMain('update-manager:upload', updateJsonPath, uploadDstPath, this.ftpCfg);
@@ -324,7 +332,7 @@
self.uploadState = !args;
});
},
notifyMainJs(updateUrl, serverUrl) {
notifyMainJs(updateUrl, serverUrl, updateBefore) {
this.$message.info("notifyMainJs");
const filePath = path.join(this.projectPath, 'build/jsb-link/main.js');
const exists = fs.existsSync(filePath);
@@ -335,12 +343,13 @@
let mainjs = fs.readFileSync(filePath, 'utf-8');
let varJs = `window.updateUrl="${updateUrl}";\r\n`;
let remoteJs = `window.remoteUrl="${serverUrl}";\r\n`;
let updateBeforeFlag = `window.updateBefore=${updateBefore};\r\n`;
// preMainJS read.
const preMain = fs && fs.readFileSync && fs.readFileSync(
Editor.url('packages://update-manager/templates/pre_main.js'), 'utf-8');
if (!mainjs.startsWith(varJs)) {
mainjs = mainjs.replace('window.boot();', 'window.beforeBoot();');
mainjs = varJs + remoteJs + preMain + mainjs;
mainjs = varJs + remoteJs + updateBeforeFlag + preMain + mainjs;
fs && fs.writeFileSync && fs.writeFileSync(filePath, mainjs, 'utf-8');
}
this.$message.success("Main.js 热更代码注入成功");