From 39fda247996f9171b3bfe94edaf711f9c7bf3516 Mon Sep 17 00:00:00 2001 From: idranme <96647698+idranme@users.noreply.github.com> Date: Tue, 15 Oct 2024 11:51:00 +0800 Subject: [PATCH] fix: config hot update --- src/common/channels.ts | 1 - src/common/config.ts | 2 +- src/main/main.ts | 126 +++++++++++++++++++++-------------------- src/preload.ts | 4 +- 4 files changed, 67 insertions(+), 66 deletions(-) diff --git a/src/common/channels.ts b/src/common/channels.ts index e183524..2119cd1 100644 --- a/src/common/channels.ts +++ b/src/common/channels.ts @@ -1,6 +1,5 @@ export const CHANNEL_GET_CONFIG = 'llonebot_get_config' export const CHANNEL_SET_CONFIG = 'llonebot_set_config' -export const CHANNEL_SET_CONFIG_CONFIRMED = 'llonebot_set_config_confirmed' export const CHANNEL_LOG = 'llonebot_log' export const CHANNEL_ERROR = 'llonebot_error' export const CHANNEL_UPDATE = 'llonebot_update' diff --git a/src/common/config.ts b/src/common/config.ts index 78749de..f7ec98e 100644 --- a/src/common/config.ts +++ b/src/common/config.ts @@ -50,7 +50,7 @@ export class ConfigUtil { token: '', enableLocalFile2Url: false, debug: false, - log: false, + log: true, autoDeleteFile: false, autoDeleteFileSecond: 60, musicSignUrl: '', diff --git a/src/main/main.ts b/src/main/main.ts index 3166402..356b81d 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -15,8 +15,7 @@ import { CHANNEL_LOG, CHANNEL_SELECT_FILE, CHANNEL_SET_CONFIG, - CHANNEL_UPDATE, - CHANNEL_SET_CONFIG_CONFIRMED + CHANNEL_UPDATE } from '../common/channels' import { startHook } from '../ntqqapi/hook' import { checkNewVersion, upgradeLLOneBot } from '../common/utils/upgrade' @@ -35,7 +34,6 @@ import { NTQQWebApi, NTQQWindowApi } from '../ntqqapi/api' -import { mkdir } from 'node:fs/promises' import { existsSync, mkdirSync } from 'node:fs' declare module 'cordis' { @@ -56,6 +54,29 @@ function onLoad() { mkdirSync(LOG_DIR) } + if (!existsSync(TEMP_DIR)) { + mkdirSync(TEMP_DIR) + } + + const dbDir = path.join(DATA_DIR, 'database') + if (!existsSync(dbDir)) { + mkdirSync(dbDir) + } + + const ctx = new Context() + + ctx.plugin(NTQQFileApi) + ctx.plugin(NTQQFileCacheApi) + ctx.plugin(NTQQFriendApi) + ctx.plugin(NTQQGroupApi) + ctx.plugin(NTQQMsgApi) + ctx.plugin(NTQQUserApi) + ctx.plugin(NTQQWebApi) + ctx.plugin(NTQQWindowApi) + ctx.plugin(Database) + + let started = false + ipcMain.handle(CHANNEL_CHECK_VERSION, async () => { return checkNewVersion() }) @@ -114,7 +135,9 @@ function onLoad() { if (!ask) { getConfigUtil().setConfig(config) log('配置已更新', config) - checkFfmpeg(config.ffmpeg).then() + if (started) { + ctx.parallel('llob/config-updated', config) + } resolve(true) return } @@ -131,7 +154,9 @@ function onLoad() { if (result.response === 0) { getConfigUtil().setConfig(config) log('配置已更新', config) - checkFfmpeg(config.ffmpeg).then() + if (started) { + ctx.parallel('llob/config-updated', config) + } resolve(true) } }) @@ -146,61 +171,6 @@ function onLoad() { log(arg) }) - async function start() { - log('process pid', process.pid) - const config = getConfigUtil().getConfig() - if (!existsSync(TEMP_DIR)) { - await mkdir(TEMP_DIR) - } - const dbDir = path.join(DATA_DIR, 'database') - if (!existsSync(dbDir)) { - await mkdir(dbDir) - } - const ctx = new Context() - ctx.plugin(Log, { - enable: config.log!, - filename: logFileName - }) - ctx.plugin(NTQQFileApi) - ctx.plugin(NTQQFileCacheApi) - ctx.plugin(NTQQFriendApi) - ctx.plugin(NTQQGroupApi) - ctx.plugin(NTQQMsgApi) - ctx.plugin(NTQQUserApi) - ctx.plugin(NTQQWebApi) - ctx.plugin(NTQQWindowApi) - ctx.plugin(Core, config) - ctx.plugin(Database) - ctx.plugin(SQLiteDriver, { - path: path.join(dbDir, `${selfInfo.uin}.db`) - }) - ctx.plugin(Store, { - msgCacheExpire: config.msgCacheExpire! * 1000 - }) - if (config.ob11.enable) { - ctx.plugin(OneBot11Adapter, { - ...config.ob11, - heartInterval: config.heartInterval, - token: config.token!, - debug: config.debug!, - musicSignUrl: config.musicSignUrl, - enableLocalFile2Url: config.enableLocalFile2Url!, - ffmpeg: config.ffmpeg, - }) - } - if (config.satori.enable) { - ctx.plugin(SatoriAdapter, { - ...config.satori, - ffmpeg: config.ffmpeg, - }) - } - ctx.start() - llonebotError.otherError = '' - ipcMain.on(CHANNEL_SET_CONFIG_CONFIRMED, (event, config: LLOBConfig) => { - ctx.parallel('llob/config-updated', config) - }) - } - const intervalId = setInterval(() => { const self = Object.assign(selfInfo, { uin: globalThis.authData?.uin, @@ -209,7 +179,41 @@ function onLoad() { }) if (self.uin) { clearInterval(intervalId) - start() + log('process pid', process.pid) + + const config = getConfigUtil().getConfig() + ctx.plugin(Log, { + enable: config.log!, + filename: logFileName + }) + ctx.plugin(SQLiteDriver, { + path: path.join(dbDir, `${selfInfo.uin}.db`) + }) + ctx.plugin(Store, { + msgCacheExpire: config.msgCacheExpire! * 1000 + }) + ctx.plugin(Core, config) + if (config.ob11.enable) { + ctx.plugin(OneBot11Adapter, { + ...config.ob11, + heartInterval: config.heartInterval, + token: config.token!, + debug: config.debug!, + musicSignUrl: config.musicSignUrl, + enableLocalFile2Url: config.enableLocalFile2Url!, + ffmpeg: config.ffmpeg, + }) + } + if (config.satori.enable) { + ctx.plugin(SatoriAdapter, { + ...config.satori, + ffmpeg: config.ffmpeg, + }) + } + + ctx.start() + started = true + llonebotError.otherError = '' } }, 600) } diff --git a/src/preload.ts b/src/preload.ts index 1f34204..8382b9e 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -7,7 +7,6 @@ import { CHANNEL_SELECT_FILE, CHANNEL_SET_CONFIG, CHANNEL_UPDATE, - CHANNEL_SET_CONFIG_CONFIRMED, } from './common/channels' const { contextBridge } = require('electron') @@ -24,8 +23,7 @@ const llonebot = { return ipcRenderer.invoke(CHANNEL_UPDATE) }, setConfig: async (ask: boolean, config: Config) => { - const isSuccess = await ipcRenderer.invoke(CHANNEL_SET_CONFIG, ask, config) - if (isSuccess) ipcRenderer.send(CHANNEL_SET_CONFIG_CONFIRMED, config) + return ipcRenderer.invoke(CHANNEL_SET_CONFIG, ask, config) }, getConfig: async (): Promise => { return ipcRenderer.invoke(CHANNEL_GET_CONFIG)