Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: managerクラスの取得方法を変更 #2339

Merged
merged 9 commits into from
Nov 5, 2024
41 changes: 29 additions & 12 deletions src/backend/electron/engineAndVvppController.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -17,13 +17,21 @@ import {
* エンジンとVVPP周りの処理の流れを制御するクラス。
*/
export class EngineAndVvppController {
constructor(
private runtimeInfoManager: RuntimeInfoManager,
private configManager: ElectronConfigManager,
private engineInfoManager: EngineInfoManager,
private engineProcessManager: EngineProcessManager,
private vvppManager: VvppManager,
) {}
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();
}

/**
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
* VVPPエンジンをインストールする。
Expand Down Expand Up @@ -232,3 +240,12 @@ export class EngineAndVvppController {
return { engineCleanupResult, configSavedResult };
}
}

let manager: EngineAndVvppController | undefined;

export function getEngineAndVvppController() {
if (manager == undefined) {
manager = new EngineAndVvppController();
}
return manager;
}
54 changes: 23 additions & 31 deletions src/backend/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -170,35 +176,21 @@ const onEngineProcessError = (engineInfo: EngineInfo, error: Error) => {
dialog.showErrorBox("音声合成エンジンエラー", error.message);
};

const runtimeInfoManager = new RuntimeInfoManager(
path.join(app.getPath("userData"), "runtime-info.json"),
app.getVersion(),
);

const configManager = getConfigManager();

const engineInfoManager = new EngineInfoManager({
configManager,
initializeRuntimeInfoManager({
runtimeInfoPath: path.join(app.getPath("userData"), "runtime-info.json"),
appVersion: app.getVersion(),
});
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 engineProcessManager = getEngineProcessManager();
const engineAndVvppController = getEngineAndVvppController();

// エンジンのフォルダを開く
function openEngineDirectory(engineId: EngineId) {
Expand Down
16 changes: 16 additions & 0 deletions src/backend/electron/manager/RuntimeInfoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,19 @@ export class RuntimeInfoManager {
});
}
}

let manager: RuntimeInfoManager | undefined;

export function initializeRuntimeInfoManager(payload: {
runtimeInfoPath: string;
appVersion: string;
}) {
manager = new RuntimeInfoManager(payload.runtimeInfoPath, payload.appVersion);
}

export function getRuntimeInfoManager() {
if (manager == undefined) {
throw new Error("RuntimeInfoManager is not initialized");
}
return manager;
}
33 changes: 21 additions & 12 deletions src/backend/electron/manager/engineInfoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { dialog } from "electron"; // FIXME: ここでelectronをimportするの

import log from "electron-log/main";

import { getConfigManager } from "../electronConfig";
import {
EngineInfo,
EngineDirValidationResult,
Expand All @@ -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";

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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}`);
Expand All @@ -125,9 +120,9 @@ export class EngineInfoManager {
"エンジンの読み込みに失敗しました。",
`${engineDir}を読み込めませんでした。このエンジンは削除されます。`,
);
this.configManager.set(
configManager.set(
"registeredEngineDirs",
this.configManager
configManager
.get("registeredEngineDirs")
.filter((p) => p !== engineDir),
);
Expand Down Expand Up @@ -221,4 +216,18 @@ export class EngineInfoManager {
}
}

export default EngineInfoManager;
let manager: EngineInfoManager | undefined;

export function initializeEngineInfoManager(payload: {
defaultEngineDir: string;
vvppEngineDir: string;
}) {
manager = new EngineInfoManager(payload);
}

export function getEngineInfoManager() {
if (manager == undefined) {
throw new Error("EngineInfoManager is not initialized");
}
return manager;
}
48 changes: 28 additions & 20 deletions src/backend/electron/manager/engineProcessManager.ts
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,35 +25,29 @@ type EngineProcessContainer = {
/** エンジンプロセスを管理するクラス */
export class EngineProcessManager {
onEngineProcessError: (engineInfo: EngineInfo, error: Error) => void;
engineInfosFetcher: () => Readonly<EngineInfo>[];
engineAltPortUpdater: (engineId: EngineId, port: number) => void;
engineSettingsGetter: () => Partial<EngineSettings>;

defaultEngineInfos: EngineInfo[] = [];
additionalEngineInfos: EngineInfo[] = [];
engineProcessContainers: Record<EngineId, EngineProcessContainer> = {};

constructor(payload: {
configManager: BaseConfigManager;
onEngineProcessError: (engineInfo: EngineInfo, error: Error) => void;
/** エンジン情報を取得する関数 */
engineInfosFetcher: () => Readonly<EngineInfo>[];
/** エンジンの代替ポート情報を更新する関数 */
engineAltPortUpdater: (engineId: EngineId, port: number) => void;
/** エンジン設定を取得する関数 */
engineSettingsGetter: () => Partial<EngineSettings>;
}) {
this.onEngineProcessError = payload.onEngineProcessError;
this.engineInfosFetcher = payload.engineInfosFetcher;
this.engineAltPortUpdater = payload.engineAltPortUpdater;
this.engineSettingsGetter = payload.engineSettingsGetter;
}

private get configManager() {
return getConfigManager();
}
private get engineInfoManager() {
return getEngineInfoManager();
}

/**
* 全てのエンジンを起動する。
*/
async runEngineAll() {
const engineInfos = this.engineInfosFetcher();
const engineInfos = this.engineInfoManager.fetchEngineInfos();
log.info(`Starting ${engineInfos.length} engine/s...`);

for (const engineInfo of engineInfos) {
Expand All @@ -65,7 +60,7 @@ export class EngineProcessManager {
* エンジンを起動する。
*/
async runEngine(engineId: EngineId) {
const engineInfos = this.engineInfosFetcher();
const engineInfos = this.engineInfoManager.fetchEngineInfos();
const engineInfo = engineInfos.find(
(engineInfo) => engineInfo.uuid === engineId,
);
Expand Down Expand Up @@ -126,7 +121,7 @@ export class EngineProcessManager {
}

// 代替ポート情報を更新
this.engineAltPortUpdater(engineId, altPort);
this.engineInfoManager.updateAltPort(engineId, altPort);
log.warn(
`ENGINE ${engineId}: Applied Alternative Port: ${port} -> ${altPort}`,
);
Expand All @@ -145,7 +140,7 @@ export class EngineProcessManager {
const engineProcessContainer = this.engineProcessContainers[engineId];
engineProcessContainer.willQuitEngine = false;

const engineSetting = this.engineSettingsGetter()[engineId];
const engineSetting = this.configManager.get("engineSettings")[engineId];
if (engineSetting == undefined)
throw new Error(`No such engineSetting: engineId == ${engineId}`);

Expand Down Expand Up @@ -366,4 +361,17 @@ export class EngineProcessManager {
}
}

export default EngineProcessManager;
let manager: EngineProcessManager | undefined;

export function initializeEngineProcessManager(payload: {
onEngineProcessError: (engineInfo: EngineInfo, error: Error) => void;
}) {
manager = new EngineProcessManager(payload);
}

export function getEngineProcessManager() {
if (manager == undefined) {
throw new Error("EngineProcessManager is not initialized");
}
return manager;
}
13 changes: 13 additions & 0 deletions src/backend/electron/manager/vvppManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,16 @@ export class VvppManager {
}

export default VvppManager;

let manager: VvppManager | undefined;

export function initializeVvppManager(payload: { vvppEngineDir: string }) {
manager = new VvppManager(payload);
}

export function getVvppManager() {
if (manager == undefined) {
throw new Error("EngineInfoManager is not initialized");
}
return manager;
}
Loading