From f122f13e84bd1c447d7a426e3ea31f61b2ce2e01 Mon Sep 17 00:00:00 2001 From: sabonerune <102559104+sabonerune@users.noreply.github.com> Date: Mon, 4 Nov 2024 11:30:11 +0900 Subject: [PATCH 1/7] =?UTF-8?q?refactor:=20manager=E3=82=AF=E3=83=A9?= =?UTF-8?q?=E3=82=B9=E3=81=AE=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../electron/engineAndVvppController.ts | 91 +++++++++++-------- src/backend/electron/main.ts | 53 +++++------ .../electron/manager/RuntimeInfoManager.ts | 16 ++++ .../electron/manager/engineInfoManager.ts | 33 ++++--- .../electron/manager/engineProcessManager.ts | 46 +++++----- src/backend/electron/manager/vvppManager.ts | 13 +++ 6 files changed, 152 insertions(+), 100 deletions(-) diff --git a/src/backend/electron/engineAndVvppController.ts b/src/backend/electron/engineAndVvppController.ts index e11e280e8d..76329fa392 100644 --- a/src/backend/electron/engineAndVvppController.ts +++ b/src/backend/electron/engineAndVvppController.ts @@ -1,11 +1,11 @@ import log from "electron-log/main"; import { BrowserWindow, dialog } from "electron"; -import EngineInfoManager from "./manager/engineInfoManager"; -import EngineProcessManager from "./manager/engineProcessManager"; -import VvppManager from "./manager/vvppManager"; -import { RuntimeInfoManager } from "./manager/RuntimeInfoManager"; -import { ElectronConfigManager } from "./electronConfig"; +import { getConfigManager } from "./electronConfig"; +import { getEngineInfoManager } from "./manager/engineInfoManager"; +import { getEngineProcessManager } from "./manager/engineProcessManager"; +import { getRuntimeInfoManager } from "./manager/RuntimeInfoManager"; +import { getVvppManager } from "./manager/vvppManager"; import { EngineId, EngineInfo, @@ -17,20 +17,13 @@ import { * エンジンとVVPP周りの処理の流れを制御するクラス。 */ export class EngineAndVvppController { - constructor( - private runtimeInfoManager: RuntimeInfoManager, - private configManager: ElectronConfigManager, - private engineInfoManager: EngineInfoManager, - private engineProcessManager: EngineProcessManager, - private vvppManager: VvppManager, - ) {} - /** * VVPPエンジンをインストールする。 */ async installVvppEngine(vvppPath: string) { + const vvppManager = getVvppManager(); try { - await this.vvppManager.install(vvppPath); + await vvppManager.install(vvppPath); return true; } catch (e) { dialog.showErrorBox( @@ -96,21 +89,23 @@ export class EngineAndVvppController { */ async uninstallVvppEngine(engineId: EngineId) { let engineInfo: EngineInfo | undefined = undefined; + const engineInfoManager = getEngineInfoManager(); + const vvppManager = getVvppManager(); try { - engineInfo = this.engineInfoManager.fetchEngineInfo(engineId); + engineInfo = engineInfoManager.fetchEngineInfo(engineId); if (!engineInfo) { throw new Error( `No such engineInfo registered: engineId == ${engineId}`, ); } - if (!this.vvppManager.canUninstall(engineInfo)) { + if (!vvppManager.canUninstall(engineInfo)) { throw new Error(`Cannot uninstall: engineId == ${engineId}`); } // Windows環境だとエンジンを終了してから削除する必要がある。 // そのため、アプリの終了時に削除するようにする。 - this.vvppManager.markWillDelete(engineId); + vvppManager.markWillDelete(engineId); return true; } catch (e) { const engineName = engineInfo?.name ?? engineId; @@ -125,33 +120,38 @@ export class EngineAndVvppController { /** エンジンの設定を更新し、保存する */ updateEngineSetting(engineId: EngineId, engineSetting: EngineSettingType) { - const engineSettings = this.configManager.get("engineSettings"); + const configManager = getConfigManager(); + const engineSettings = configManager.get("engineSettings"); engineSettings[engineId] = engineSetting; - this.configManager.set(`engineSettings`, engineSettings); + configManager.set(`engineSettings`, engineSettings); } // エンジンの準備と起動 async launchEngines() { // AltPortInfosを再生成する。 - this.engineInfoManager.initializeAltPortInfo(); + const engineInfoManager = getEngineInfoManager(); + engineInfoManager.initializeAltPortInfo(); // TODO: デフォルトエンジンの処理をConfigManagerに移してブラウザ版と共通化する - const engineInfos = this.engineInfoManager.fetchEngineInfos(); - const engineSettings = this.configManager.get("engineSettings"); + const engineInfos = engineInfoManager.fetchEngineInfos(); + const configManager = getConfigManager(); + const engineSettings = configManager.get("engineSettings"); for (const engineInfo of engineInfos) { if (!engineSettings[engineInfo.uuid]) { // 空オブジェクトをパースさせることで、デフォルト値を取得する engineSettings[engineInfo.uuid] = engineSettingSchema.parse({}); } } - this.configManager.set("engineSettings", engineSettings); + configManager.set("engineSettings", engineSettings); - await this.engineProcessManager.runEngineAll(); - this.runtimeInfoManager.setEngineInfos( + const engineProcessManager = getEngineProcessManager(); + await engineProcessManager.runEngineAll(); + const runtimeInfoManager = getRuntimeInfoManager(); + runtimeInfoManager.setEngineInfos( engineInfos, - this.engineInfoManager.altPortInfos, + engineInfoManager.altPortInfos, ); - await this.runtimeInfoManager.exportFile(); + await runtimeInfoManager.exportFile(); } /** @@ -159,15 +159,18 @@ export class EngineAndVvppController { * エンジンの起動が開始したらresolve、起動が失敗したらreject。 */ async restartEngines(engineId: EngineId) { - await this.engineProcessManager.restartEngine(engineId); + const engineProcessManager = getEngineProcessManager(); + await engineProcessManager.restartEngine(engineId); // ランタイム情報の更新 // TODO: setからexportの処理は排他処理にしたほうがより良い - this.runtimeInfoManager.setEngineInfos( - this.engineInfoManager.fetchEngineInfos(), - this.engineInfoManager.altPortInfos, + const runtimeInfoManager = getRuntimeInfoManager(); + const engineInfoManager = getEngineInfoManager(); + runtimeInfoManager.setEngineInfos( + engineInfoManager.fetchEngineInfos(), + engineInfoManager.altPortInfos, ); - await this.runtimeInfoManager.exportFile(); + await runtimeInfoManager.exportFile(); } /** @@ -176,16 +179,15 @@ export class EngineAndVvppController { * そうでない場合は Promise を返す。 */ cleanupEngines(): Promise | "alreadyCompleted" { - const killingProcessPromises = this.engineProcessManager.killEngineAll(); + const engineProcessManager = getEngineProcessManager(); + const killingProcessPromises = engineProcessManager.killEngineAll(); const numLivingEngineProcess = Object.entries( killingProcessPromises, ).length; // 前処理が完了している場合 - if ( - numLivingEngineProcess === 0 && - !this.vvppManager.hasMarkedEngineDirs() - ) { + const vvppManager = getVvppManager(); + if (numLivingEngineProcess === 0 && !vvppManager.hasMarkedEngineDirs()) { return "alreadyCompleted"; } @@ -217,7 +219,8 @@ export class EngineAndVvppController { log.info( "All ENGINE process kill operations done. Running post engine kill process", ); - return this.vvppManager.handleMarkedEngineDirs(); + const vvppManager = getVvppManager(); + return vvppManager.handleMarkedEngineDirs(); }); } @@ -228,7 +231,17 @@ export class EngineAndVvppController { */ gracefulShutdown() { const engineCleanupResult = this.cleanupEngines(); - const configSavedResult = this.configManager.ensureSaved(); + const configManager = getConfigManager(); + const configSavedResult = configManager.ensureSaved(); return { engineCleanupResult, configSavedResult }; } } + +let manager: EngineAndVvppController | undefined; + +export const getEngineAndVvppController = () => { + if (manager == undefined) { + manager = new EngineAndVvppController(); + } + return manager; +}; diff --git a/src/backend/electron/main.ts b/src/backend/electron/main.ts index 6b636c0883..fa02f3737f 100644 --- a/src/backend/electron/main.ts +++ b/src/backend/electron/main.ts @@ -20,14 +20,20 @@ import log from "electron-log/main"; import dayjs from "dayjs"; import windowStateKeeper from "electron-window-state"; import { hasSupportedGpu } from "./device"; -import EngineInfoManager from "./manager/engineInfoManager"; -import EngineProcessManager from "./manager/engineProcessManager"; -import VvppManager, { isVvppFile } from "./manager/vvppManager"; +import { + getEngineInfoManager, + initializeEngineInfoManager, +} from "./manager/engineInfoManager"; +import { + getEngineProcessManager, + initializeEngineProcessManager, +} from "./manager/engineProcessManager"; +import { initializeVvppManager, isVvppFile } from "./manager/vvppManager"; import configMigration014 from "./configMigration014"; -import { RuntimeInfoManager } from "./manager/RuntimeInfoManager"; +import { initializeRuntimeInfoManager } from "./manager/RuntimeInfoManager"; import { registerIpcMainHandle, ipcMainSendProxy, IpcMainHandle } from "./ipc"; import { getConfigManager } from "./electronConfig"; -import { EngineAndVvppController } from "./engineAndVvppController"; +import { getEngineAndVvppController } from "./engineAndVvppController"; import { writeFileSafely } from "./fileHelper"; import { failure, success } from "@/type/result"; import { AssetTextFileNames } from "@/type/staticResources"; @@ -170,35 +176,25 @@ const onEngineProcessError = (engineInfo: EngineInfo, error: Error) => { dialog.showErrorBox("音声合成エンジンエラー", error.message); }; -const runtimeInfoManager = new RuntimeInfoManager( +initializeRuntimeInfoManager( path.join(app.getPath("userData"), "runtime-info.json"), app.getVersion(), ); -const configManager = getConfigManager(); - -const engineInfoManager = new EngineInfoManager({ - configManager, +initializeEngineInfoManager({ defaultEngineDir: appDirPath, vvppEngineDir, }); -const engineProcessManager = new EngineProcessManager({ - configManager, - onEngineProcessError, - engineInfosFetcher: - engineInfoManager.fetchEngineInfos.bind(engineInfoManager), - engineAltPortUpdater: engineInfoManager.updateAltPort.bind(engineInfoManager), - engineSettingsGetter: () => configManager.get("engineSettings"), -}); -const vvppManager = new VvppManager({ vvppEngineDir }); - -const engineAndVvppController = new EngineAndVvppController( - runtimeInfoManager, - configManager, - engineInfoManager, - engineProcessManager, - vvppManager, -); + +initializeEngineProcessManager({ onEngineProcessError }); + +initializeVvppManager({ vvppEngineDir }); + +const configManager = getConfigManager(); + +const engineInfoManager = getEngineInfoManager(); + +const engineAndVvppController = getEngineAndVvppController(); // エンジンのフォルダを開く function openEngineDirectory(engineId: EngineId) { @@ -649,7 +645,8 @@ registerIpcMainHandle({ }, RESTART_ENGINE: async (_, { engineId }) => { - return engineProcessManager.restartEngine(engineId); + const manager = getEngineProcessManager(); + return manager.restartEngine(engineId); }, OPEN_ENGINE_DIRECTORY: async (_, { engineId }) => { diff --git a/src/backend/electron/manager/RuntimeInfoManager.ts b/src/backend/electron/manager/RuntimeInfoManager.ts index 04683ea375..bc5d10043b 100644 --- a/src/backend/electron/manager/RuntimeInfoManager.ts +++ b/src/backend/electron/manager/RuntimeInfoManager.ts @@ -114,3 +114,19 @@ export class RuntimeInfoManager { }); } } + +let manager: RuntimeInfoManager | undefined; + +export const initializeRuntimeInfoManager = ( + runtimeInfoPath: string, + appVersion: string, +) => { + manager = new RuntimeInfoManager(runtimeInfoPath, appVersion); +}; + +export const getRuntimeInfoManager = () => { + if (manager == undefined) { + throw new Error("RuntimeInfoManager is not initialized"); + } + return manager; +}; diff --git a/src/backend/electron/manager/engineInfoManager.ts b/src/backend/electron/manager/engineInfoManager.ts index 1bc76259e0..9791e9c8fc 100644 --- a/src/backend/electron/manager/engineInfoManager.ts +++ b/src/backend/electron/manager/engineInfoManager.ts @@ -6,6 +6,7 @@ import { dialog } from "electron"; // FIXME: ここでelectronをimportするの import log from "electron-log/main"; +import { getConfigManager } from "../electronConfig"; import { EngineInfo, EngineDirValidationResult, @@ -14,7 +15,6 @@ import { minimumEngineManifestSchema, } from "@/type/preload"; import { AltPortInfos } from "@/store/type"; -import { BaseConfigManager } from "@/backend/common/ConfigManager"; import { loadEnvEngineInfos } from "@/domain/defaultEngine/envEngineInfo"; /** @@ -45,19 +45,13 @@ function fetchDefaultEngineInfos(defaultEngineDir: string): EngineInfo[] { /** エンジンの情報を管理するクラス */ export class EngineInfoManager { - configManager: BaseConfigManager; defaultEngineDir: string; vvppEngineDir: string; /** 代替ポート情報 */ public altPortInfos: AltPortInfos = {}; - constructor(payload: { - configManager: BaseConfigManager; - defaultEngineDir: string; - vvppEngineDir: string; - }) { - this.configManager = payload.configManager; + constructor(payload: { defaultEngineDir: string; vvppEngineDir: string }) { this.defaultEngineDir = payload.defaultEngineDir; this.vvppEngineDir = payload.vvppEngineDir; } @@ -114,8 +108,9 @@ export class EngineInfoManager { log.log(`Failed to load engine: ${result}, ${engineDir}`); } } + const configManager = getConfigManager(); // FIXME: この関数の引数でregisteredEngineDirsを受け取り、動かないエンジンをreturnして、EngineManager外でconfig.setする - for (const engineDir of this.configManager.get("registeredEngineDirs")) { + for (const engineDir of configManager.get("registeredEngineDirs")) { const result = addEngine(engineDir, "path"); if (result !== "ok") { log.log(`Failed to load engine: ${result}, ${engineDir}`); @@ -125,9 +120,9 @@ export class EngineInfoManager { "エンジンの読み込みに失敗しました。", `${engineDir}を読み込めませんでした。このエンジンは削除されます。`, ); - this.configManager.set( + configManager.set( "registeredEngineDirs", - this.configManager + configManager .get("registeredEngineDirs") .filter((p) => p !== engineDir), ); @@ -221,4 +216,18 @@ export class EngineInfoManager { } } -export default EngineInfoManager; +let manager: EngineInfoManager | undefined; + +export const initializeEngineInfoManager = (payload: { + defaultEngineDir: string; + vvppEngineDir: string; +}) => { + manager = new EngineInfoManager(payload); +}; + +export const getEngineInfoManager = () => { + if (manager == undefined) { + throw new Error("EngineInfoManager is not initialized"); + } + return manager; +}; diff --git a/src/backend/electron/manager/engineProcessManager.ts b/src/backend/electron/manager/engineProcessManager.ts index ba17e392c2..9443e3b92b 100644 --- a/src/backend/electron/manager/engineProcessManager.ts +++ b/src/backend/electron/manager/engineProcessManager.ts @@ -1,4 +1,4 @@ -import { spawn, ChildProcess } from "child_process"; +import { ChildProcess, spawn } from "child_process"; import path from "path"; import treeKill from "tree-kill"; @@ -13,8 +13,9 @@ import { isAssignablePort, } from "../portHelper"; -import { EngineInfo, EngineId, EngineSettings } from "@/type/preload"; -import { BaseConfigManager } from "@/backend/common/ConfigManager"; +import { getConfigManager } from "../electronConfig"; +import { getEngineInfoManager } from "./engineInfoManager"; +import { EngineId, EngineInfo } from "@/type/preload"; type EngineProcessContainer = { willQuitEngine: boolean; @@ -24,35 +25,23 @@ type EngineProcessContainer = { /** エンジンプロセスを管理するクラス */ export class EngineProcessManager { onEngineProcessError: (engineInfo: EngineInfo, error: Error) => void; - engineInfosFetcher: () => Readonly[]; - engineAltPortUpdater: (engineId: EngineId, port: number) => void; - engineSettingsGetter: () => Partial; defaultEngineInfos: EngineInfo[] = []; additionalEngineInfos: EngineInfo[] = []; engineProcessContainers: Record = {}; constructor(payload: { - configManager: BaseConfigManager; onEngineProcessError: (engineInfo: EngineInfo, error: Error) => void; - /** エンジン情報を取得する関数 */ - engineInfosFetcher: () => Readonly[]; - /** エンジンの代替ポート情報を更新する関数 */ - engineAltPortUpdater: (engineId: EngineId, port: number) => void; - /** エンジン設定を取得する関数 */ - engineSettingsGetter: () => Partial; }) { this.onEngineProcessError = payload.onEngineProcessError; - this.engineInfosFetcher = payload.engineInfosFetcher; - this.engineAltPortUpdater = payload.engineAltPortUpdater; - this.engineSettingsGetter = payload.engineSettingsGetter; } /** * 全てのエンジンを起動する。 */ async runEngineAll() { - const engineInfos = this.engineInfosFetcher(); + const engineInfoManager = getEngineInfoManager(); + const engineInfos = engineInfoManager.fetchEngineInfos(); log.info(`Starting ${engineInfos.length} engine/s...`); for (const engineInfo of engineInfos) { @@ -65,7 +54,8 @@ export class EngineProcessManager { * エンジンを起動する。 */ async runEngine(engineId: EngineId) { - const engineInfos = this.engineInfosFetcher(); + const engineInfoManager = getEngineInfoManager(); + const engineInfos = engineInfoManager.fetchEngineInfos(); const engineInfo = engineInfos.find( (engineInfo) => engineInfo.uuid === engineId, ); @@ -126,7 +116,7 @@ export class EngineProcessManager { } // 代替ポート情報を更新 - this.engineAltPortUpdater(engineId, altPort); + engineInfoManager.updateAltPort(engineId, altPort); log.warn( `ENGINE ${engineId}: Applied Alternative Port: ${port} -> ${altPort}`, ); @@ -145,7 +135,8 @@ export class EngineProcessManager { const engineProcessContainer = this.engineProcessContainers[engineId]; engineProcessContainer.willQuitEngine = false; - const engineSetting = this.engineSettingsGetter()[engineId]; + const configManager = getConfigManager(); + const engineSetting = configManager.get("engineSettings")[engineId]; if (engineSetting == undefined) throw new Error(`No such engineSetting: engineId == ${engineId}`); @@ -366,4 +357,17 @@ export class EngineProcessManager { } } -export default EngineProcessManager; +let manager: EngineProcessManager | undefined; + +export const initializeEngineProcessManager = (payload: { + onEngineProcessError: (engineInfo: EngineInfo, error: Error) => void; +}) => { + manager = new EngineProcessManager(payload); +}; + +export const getEngineProcessManager = () => { + if (manager == undefined) { + throw new Error("EngineProcessManager is not initialized"); + } + return manager; +}; diff --git a/src/backend/electron/manager/vvppManager.ts b/src/backend/electron/manager/vvppManager.ts index 513da3ecc6..baeed18472 100644 --- a/src/backend/electron/manager/vvppManager.ts +++ b/src/backend/electron/manager/vvppManager.ts @@ -383,3 +383,16 @@ export class VvppManager { } export default VvppManager; + +let manager: VvppManager | undefined; + +export const initializeVvppManager = (payload: { vvppEngineDir: string }) => { + manager = new VvppManager(payload); +}; + +export const getVvppManager = () => { + if (manager == undefined) { + throw new Error("EngineInfoManager is not initialized"); + } + return manager; +}; From 88ddea99c5da9b2fcf8ec04c28fd7ab94ed276c3 Mon Sep 17 00:00:00 2001 From: sabonerune <102559104+sabonerune@users.noreply.github.com> Date: Tue, 5 Nov 2024 18:45:46 +0900 Subject: [PATCH 2/7] fix: Arrow functions to Function --- src/backend/electron/engineAndVvppController.ts | 4 ++-- src/backend/electron/manager/RuntimeInfoManager.ts | 10 +++++----- src/backend/electron/manager/engineInfoManager.ts | 10 +++++----- src/backend/electron/manager/engineProcessManager.ts | 10 +++++----- src/backend/electron/manager/vvppManager.ts | 8 ++++---- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/backend/electron/engineAndVvppController.ts b/src/backend/electron/engineAndVvppController.ts index 76329fa392..09b045b75c 100644 --- a/src/backend/electron/engineAndVvppController.ts +++ b/src/backend/electron/engineAndVvppController.ts @@ -239,9 +239,9 @@ export class EngineAndVvppController { let manager: EngineAndVvppController | undefined; -export const getEngineAndVvppController = () => { +export function getEngineAndVvppController() { if (manager == undefined) { manager = new EngineAndVvppController(); } return manager; -}; +} diff --git a/src/backend/electron/manager/RuntimeInfoManager.ts b/src/backend/electron/manager/RuntimeInfoManager.ts index bc5d10043b..2eb129d3aa 100644 --- a/src/backend/electron/manager/RuntimeInfoManager.ts +++ b/src/backend/electron/manager/RuntimeInfoManager.ts @@ -117,16 +117,16 @@ export class RuntimeInfoManager { let manager: RuntimeInfoManager | undefined; -export const initializeRuntimeInfoManager = ( +export function initializeRuntimeInfoManager( runtimeInfoPath: string, appVersion: string, -) => { +) { manager = new RuntimeInfoManager(runtimeInfoPath, appVersion); -}; +} -export const getRuntimeInfoManager = () => { +export function getRuntimeInfoManager() { if (manager == undefined) { throw new Error("RuntimeInfoManager is not initialized"); } return manager; -}; +} diff --git a/src/backend/electron/manager/engineInfoManager.ts b/src/backend/electron/manager/engineInfoManager.ts index 9791e9c8fc..83308f0844 100644 --- a/src/backend/electron/manager/engineInfoManager.ts +++ b/src/backend/electron/manager/engineInfoManager.ts @@ -218,16 +218,16 @@ export class EngineInfoManager { let manager: EngineInfoManager | undefined; -export const initializeEngineInfoManager = (payload: { +export function initializeEngineInfoManager(payload: { defaultEngineDir: string; vvppEngineDir: string; -}) => { +}) { manager = new EngineInfoManager(payload); -}; +} -export const getEngineInfoManager = () => { +export function getEngineInfoManager() { if (manager == undefined) { throw new Error("EngineInfoManager is not initialized"); } return manager; -}; +} diff --git a/src/backend/electron/manager/engineProcessManager.ts b/src/backend/electron/manager/engineProcessManager.ts index 9443e3b92b..398a0189b0 100644 --- a/src/backend/electron/manager/engineProcessManager.ts +++ b/src/backend/electron/manager/engineProcessManager.ts @@ -359,15 +359,15 @@ export class EngineProcessManager { let manager: EngineProcessManager | undefined; -export const initializeEngineProcessManager = (payload: { +export function initializeEngineProcessManager(payload: { onEngineProcessError: (engineInfo: EngineInfo, error: Error) => void; -}) => { +}) { manager = new EngineProcessManager(payload); -}; +} -export const getEngineProcessManager = () => { +export function getEngineProcessManager() { if (manager == undefined) { throw new Error("EngineProcessManager is not initialized"); } return manager; -}; +} diff --git a/src/backend/electron/manager/vvppManager.ts b/src/backend/electron/manager/vvppManager.ts index baeed18472..fc8a5c7afd 100644 --- a/src/backend/electron/manager/vvppManager.ts +++ b/src/backend/electron/manager/vvppManager.ts @@ -386,13 +386,13 @@ export default VvppManager; let manager: VvppManager | undefined; -export const initializeVvppManager = (payload: { vvppEngineDir: string }) => { +export function initializeVvppManager(payload: { vvppEngineDir: string }) { manager = new VvppManager(payload); -}; +} -export const getVvppManager = () => { +export function getVvppManager() { if (manager == undefined) { throw new Error("EngineInfoManager is not initialized"); } return manager; -}; +} From 4260e7f66bd6b1aa5fdb94987c10ed69685432ed Mon Sep 17 00:00:00 2001 From: sabonerune <102559104+sabonerune@users.noreply.github.com> Date: Tue, 5 Nov 2024 21:53:13 +0900 Subject: [PATCH 3/7] fix: getter manager --- .../electron/engineAndVvppController.ts | 76 ++++++++++--------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/src/backend/electron/engineAndVvppController.ts b/src/backend/electron/engineAndVvppController.ts index 09b045b75c..9a8c16299d 100644 --- a/src/backend/electron/engineAndVvppController.ts +++ b/src/backend/electron/engineAndVvppController.ts @@ -17,13 +17,27 @@ import { * エンジンとVVPP周りの処理の流れを制御するクラス。 */ export class EngineAndVvppController { + private get configManager() { + return getConfigManager(); + } + private get engineInfoManager() { + return getEngineInfoManager(); + } + private get engineProcessManager() { + return getEngineProcessManager(); + } + private get runtimeInfoManager() { + return getRuntimeInfoManager(); + } + private get vvppManager() { + return getVvppManager(); + } /** * VVPPエンジンをインストールする。 */ async installVvppEngine(vvppPath: string) { - const vvppManager = getVvppManager(); try { - await vvppManager.install(vvppPath); + await this.vvppManager.install(vvppPath); return true; } catch (e) { dialog.showErrorBox( @@ -89,23 +103,21 @@ export class EngineAndVvppController { */ async uninstallVvppEngine(engineId: EngineId) { let engineInfo: EngineInfo | undefined = undefined; - const engineInfoManager = getEngineInfoManager(); - const vvppManager = getVvppManager(); try { - engineInfo = engineInfoManager.fetchEngineInfo(engineId); + engineInfo = this.engineInfoManager.fetchEngineInfo(engineId); if (!engineInfo) { throw new Error( `No such engineInfo registered: engineId == ${engineId}`, ); } - if (!vvppManager.canUninstall(engineInfo)) { + if (!this.vvppManager.canUninstall(engineInfo)) { throw new Error(`Cannot uninstall: engineId == ${engineId}`); } // Windows環境だとエンジンを終了してから削除する必要がある。 // そのため、アプリの終了時に削除するようにする。 - vvppManager.markWillDelete(engineId); + this.vvppManager.markWillDelete(engineId); return true; } catch (e) { const engineName = engineInfo?.name ?? engineId; @@ -120,38 +132,33 @@ export class EngineAndVvppController { /** エンジンの設定を更新し、保存する */ updateEngineSetting(engineId: EngineId, engineSetting: EngineSettingType) { - const configManager = getConfigManager(); - const engineSettings = configManager.get("engineSettings"); + const engineSettings = this.configManager.get("engineSettings"); engineSettings[engineId] = engineSetting; - configManager.set(`engineSettings`, engineSettings); + this.configManager.set(`engineSettings`, engineSettings); } // エンジンの準備と起動 async launchEngines() { // AltPortInfosを再生成する。 - const engineInfoManager = getEngineInfoManager(); - engineInfoManager.initializeAltPortInfo(); + this.engineInfoManager.initializeAltPortInfo(); // TODO: デフォルトエンジンの処理をConfigManagerに移してブラウザ版と共通化する - const engineInfos = engineInfoManager.fetchEngineInfos(); - const configManager = getConfigManager(); - const engineSettings = configManager.get("engineSettings"); + const engineInfos = this.engineInfoManager.fetchEngineInfos(); + const engineSettings = this.configManager.get("engineSettings"); for (const engineInfo of engineInfos) { if (!engineSettings[engineInfo.uuid]) { // 空オブジェクトをパースさせることで、デフォルト値を取得する engineSettings[engineInfo.uuid] = engineSettingSchema.parse({}); } } - configManager.set("engineSettings", engineSettings); + this.configManager.set("engineSettings", engineSettings); - const engineProcessManager = getEngineProcessManager(); - await engineProcessManager.runEngineAll(); - const runtimeInfoManager = getRuntimeInfoManager(); - runtimeInfoManager.setEngineInfos( + await this.engineProcessManager.runEngineAll(); + this.runtimeInfoManager.setEngineInfos( engineInfos, - engineInfoManager.altPortInfos, + this.engineInfoManager.altPortInfos, ); - await runtimeInfoManager.exportFile(); + await this.runtimeInfoManager.exportFile(); } /** @@ -159,18 +166,15 @@ export class EngineAndVvppController { * エンジンの起動が開始したらresolve、起動が失敗したらreject。 */ async restartEngines(engineId: EngineId) { - const engineProcessManager = getEngineProcessManager(); - await engineProcessManager.restartEngine(engineId); + await this.engineProcessManager.restartEngine(engineId); // ランタイム情報の更新 // TODO: setからexportの処理は排他処理にしたほうがより良い - const runtimeInfoManager = getRuntimeInfoManager(); - const engineInfoManager = getEngineInfoManager(); - runtimeInfoManager.setEngineInfos( - engineInfoManager.fetchEngineInfos(), - engineInfoManager.altPortInfos, + this.runtimeInfoManager.setEngineInfos( + this.engineInfoManager.fetchEngineInfos(), + this.engineInfoManager.altPortInfos, ); - await runtimeInfoManager.exportFile(); + await this.runtimeInfoManager.exportFile(); } /** @@ -179,15 +183,16 @@ export class EngineAndVvppController { * そうでない場合は Promise を返す。 */ cleanupEngines(): Promise | "alreadyCompleted" { - const engineProcessManager = getEngineProcessManager(); - const killingProcessPromises = engineProcessManager.killEngineAll(); + const killingProcessPromises = this.engineProcessManager.killEngineAll(); const numLivingEngineProcess = Object.entries( killingProcessPromises, ).length; // 前処理が完了している場合 - const vvppManager = getVvppManager(); - if (numLivingEngineProcess === 0 && !vvppManager.hasMarkedEngineDirs()) { + if ( + numLivingEngineProcess === 0 && + !this.vvppManager.hasMarkedEngineDirs() + ) { return "alreadyCompleted"; } @@ -231,8 +236,7 @@ export class EngineAndVvppController { */ gracefulShutdown() { const engineCleanupResult = this.cleanupEngines(); - const configManager = getConfigManager(); - const configSavedResult = configManager.ensureSaved(); + const configSavedResult = this.configManager.ensureSaved(); return { engineCleanupResult, configSavedResult }; } } From 2810028c97f327e5821bed7f1dc477b47623fa5b Mon Sep 17 00:00:00 2001 From: Hiroshiba Date: Wed, 6 Nov 2024 01:25:19 +0900 Subject: [PATCH 4/7] Apply suggestions from code review --- src/backend/electron/engineAndVvppController.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backend/electron/engineAndVvppController.ts b/src/backend/electron/engineAndVvppController.ts index 9a8c16299d..dd8e9a525e 100644 --- a/src/backend/electron/engineAndVvppController.ts +++ b/src/backend/electron/engineAndVvppController.ts @@ -32,6 +32,7 @@ export class EngineAndVvppController { private get vvppManager() { return getVvppManager(); } + /** * VVPPエンジンをインストールする。 */ @@ -224,8 +225,7 @@ export class EngineAndVvppController { log.info( "All ENGINE process kill operations done. Running post engine kill process", ); - const vvppManager = getVvppManager(); - return vvppManager.handleMarkedEngineDirs(); + return this.vvppManager.handleMarkedEngineDirs(); }); } From d4a4bcca6d4bb316ee3e9f1c6410f9f46e2654ad Mon Sep 17 00:00:00 2001 From: Hiroshiba Date: Wed, 6 Nov 2024 01:25:40 +0900 Subject: [PATCH 5/7] =?UTF-8?q?=E5=BE=AE=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../electron/engineAndVvppController.ts | 3 +-- src/backend/electron/main.ts | 19 +++++++------------ .../electron/manager/RuntimeInfoManager.ts | 10 +++++----- .../electron/manager/engineProcessManager.ts | 18 +++++++++++------- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/backend/electron/engineAndVvppController.ts b/src/backend/electron/engineAndVvppController.ts index 9a8c16299d..90c2987366 100644 --- a/src/backend/electron/engineAndVvppController.ts +++ b/src/backend/electron/engineAndVvppController.ts @@ -224,8 +224,7 @@ export class EngineAndVvppController { log.info( "All ENGINE process kill operations done. Running post engine kill process", ); - const vvppManager = getVvppManager(); - return vvppManager.handleMarkedEngineDirs(); + return this.vvppManager.handleMarkedEngineDirs(); }); } diff --git a/src/backend/electron/main.ts b/src/backend/electron/main.ts index fa02f3737f..614ca60207 100644 --- a/src/backend/electron/main.ts +++ b/src/backend/electron/main.ts @@ -121,7 +121,7 @@ process.on("uncaughtException", (error) => { } else { const { message, name } = error; let detailedMessage = ""; - detailedMessage += `メインプロセスで原因不明のエラーが発生しました。\n`; + detailedMessage += `メインプロセスで原因不明��エラーが発生しました。\n`; detailedMessage += `エラー名: ${name}\n`; detailedMessage += `メッセージ: ${message}\n`; if (error.stack) { @@ -176,24 +176,20 @@ const onEngineProcessError = (engineInfo: EngineInfo, error: Error) => { dialog.showErrorBox("音声合成エンジンエラー", error.message); }; -initializeRuntimeInfoManager( - path.join(app.getPath("userData"), "runtime-info.json"), - app.getVersion(), -); - +initializeRuntimeInfoManager({ + runtimeInfoPath: path.join(app.getPath("userData"), "runtime-info.json"), + appVersion: app.getVersion(), +}); initializeEngineInfoManager({ defaultEngineDir: appDirPath, vvppEngineDir, }); - initializeEngineProcessManager({ onEngineProcessError }); - initializeVvppManager({ vvppEngineDir }); const configManager = getConfigManager(); - const engineInfoManager = getEngineInfoManager(); - +const engineProcessManager = getEngineProcessManager(); const engineAndVvppController = getEngineAndVvppController(); // エンジンのフォルダを開く @@ -645,8 +641,7 @@ registerIpcMainHandle({ }, RESTART_ENGINE: async (_, { engineId }) => { - const manager = getEngineProcessManager(); - return manager.restartEngine(engineId); + return engineProcessManager.restartEngine(engineId); }, OPEN_ENGINE_DIRECTORY: async (_, { engineId }) => { diff --git a/src/backend/electron/manager/RuntimeInfoManager.ts b/src/backend/electron/manager/RuntimeInfoManager.ts index 2eb129d3aa..732e0fa341 100644 --- a/src/backend/electron/manager/RuntimeInfoManager.ts +++ b/src/backend/electron/manager/RuntimeInfoManager.ts @@ -117,11 +117,11 @@ export class RuntimeInfoManager { let manager: RuntimeInfoManager | undefined; -export function initializeRuntimeInfoManager( - runtimeInfoPath: string, - appVersion: string, -) { - manager = new RuntimeInfoManager(runtimeInfoPath, appVersion); +export function initializeRuntimeInfoManager(payload: { + runtimeInfoPath: string; + appVersion: string; +}) { + manager = new RuntimeInfoManager(payload.runtimeInfoPath, payload.appVersion); } export function getRuntimeInfoManager() { diff --git a/src/backend/electron/manager/engineProcessManager.ts b/src/backend/electron/manager/engineProcessManager.ts index 398a0189b0..2f877dce43 100644 --- a/src/backend/electron/manager/engineProcessManager.ts +++ b/src/backend/electron/manager/engineProcessManager.ts @@ -1,4 +1,4 @@ -import { ChildProcess, spawn } from "child_process"; +import { spawn, ChildProcess } from "child_process"; import path from "path"; import treeKill from "tree-kill"; @@ -36,12 +36,18 @@ export class EngineProcessManager { this.onEngineProcessError = payload.onEngineProcessError; } + private get configManager() { + return getConfigManager(); + } + private get engineInfoManager() { + return getEngineInfoManager(); + } + /** * 全てのエンジンを起動する。 */ async runEngineAll() { - const engineInfoManager = getEngineInfoManager(); - const engineInfos = engineInfoManager.fetchEngineInfos(); + const engineInfos = this.engineInfoManager.fetchEngineInfos(); log.info(`Starting ${engineInfos.length} engine/s...`); for (const engineInfo of engineInfos) { @@ -54,8 +60,7 @@ export class EngineProcessManager { * エンジンを起動する。 */ async runEngine(engineId: EngineId) { - const engineInfoManager = getEngineInfoManager(); - const engineInfos = engineInfoManager.fetchEngineInfos(); + const engineInfos = this.engineInfoManager.fetchEngineInfos(); const engineInfo = engineInfos.find( (engineInfo) => engineInfo.uuid === engineId, ); @@ -135,8 +140,7 @@ export class EngineProcessManager { const engineProcessContainer = this.engineProcessContainers[engineId]; engineProcessContainer.willQuitEngine = false; - const configManager = getConfigManager(); - const engineSetting = configManager.get("engineSettings")[engineId]; + const engineSetting = this.configManager.get("engineSettings")[engineId]; if (engineSetting == undefined) throw new Error(`No such engineSetting: engineId == ${engineId}`); From ee5c9f1ead67689634800bf8756c471fd30a5dd1 Mon Sep 17 00:00:00 2001 From: Hiroshiba Date: Wed, 6 Nov 2024 01:27:01 +0900 Subject: [PATCH 6/7] Apply suggestions from code review --- src/backend/electron/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/electron/main.ts b/src/backend/electron/main.ts index 614ca60207..bb8877ebaa 100644 --- a/src/backend/electron/main.ts +++ b/src/backend/electron/main.ts @@ -121,7 +121,7 @@ process.on("uncaughtException", (error) => { } else { const { message, name } = error; let detailedMessage = ""; - detailedMessage += `メインプロセスで原因不明��エラーが発生しました。\n`; + detailedMessage += `メインプロセスで原因不明のエラーが発生しました。\n`; detailedMessage += `エラー名: ${name}\n`; detailedMessage += `メッセージ: ${message}\n`; if (error.stack) { From 59ba320a0431cc3e809719034f7c0efa9bb9efdc Mon Sep 17 00:00:00 2001 From: Hiroshiba Date: Wed, 6 Nov 2024 01:28:46 +0900 Subject: [PATCH 7/7] =?UTF-8?q?=E3=83=9F=E3=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/electron/manager/engineProcessManager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/electron/manager/engineProcessManager.ts b/src/backend/electron/manager/engineProcessManager.ts index 2f877dce43..2387b68da5 100644 --- a/src/backend/electron/manager/engineProcessManager.ts +++ b/src/backend/electron/manager/engineProcessManager.ts @@ -121,7 +121,7 @@ export class EngineProcessManager { } // 代替ポート情報を更新 - engineInfoManager.updateAltPort(engineId, altPort); + this.engineInfoManager.updateAltPort(engineId, altPort); log.warn( `ENGINE ${engineId}: Applied Alternative Port: ${port} -> ${altPort}`, );