Skip to content

Commit

Permalink
いろいろ使いやすく
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba committed Sep 7, 2024
1 parent c4a3fc8 commit dc2482c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 84 deletions.
13 changes: 5 additions & 8 deletions src/backend/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,11 @@ const engineInfoManager = new EngineInfoManager({
});
const engineProcessManager = new EngineProcessManager({
configManager,
vvppEngineDir,
onEngineProcessError,
engineInfosFetcher:
engineInfoManager.fetchEngineInfos.bind(engineInfoManager),
engineInfoPortChanger:
engineInfoManager.changeEngineInfoPort.bind(engineInfoManager),
engineSettingsFetcher: () => configManager.get("engineSettings"),
engineAltPortUpdater: engineInfoManager.updateAltPort.bind(engineInfoManager),
engineSettingsGetter: () => configManager.get("engineSettings"),
});
const vvppManager = new VvppManager({ vvppEngineDir });

Expand Down Expand Up @@ -532,8 +530,7 @@ async function start() {
// エンジンの準備と起動
async function launchEngines() {
// エンジンの追加と削除を反映させるためEngineInfoとAltPortInfosを再生成する。
engineInfoManager.initializeEngineInfos();
engineProcessManager.initializeAltPortInfos();
engineInfoManager.initializeEngineInfosAndAltPortInfo();

// TODO: デフォルトエンジンの処理をConfigManagerに移してブラウザ版と共通化する
const engineInfos = engineInfoManager.fetchEngineInfos();
Expand Down Expand Up @@ -730,7 +727,7 @@ registerIpcMainHandle<IpcMainHandle>({
},

GET_ALT_PORT_INFOS: () => {
return engineProcessManager.altPortInfos;
return engineInfoManager.altPortInfos;
},

SHOW_AUDIO_SAVE_DIALOG: async (_, { title, defaultPath }) => {
Expand Down Expand Up @@ -1011,7 +1008,7 @@ registerIpcMainHandle<IpcMainHandle>({
},

VALIDATE_ENGINE_DIR: (_, { engineDir }) => {
return engineProcessManager.validateEngineDir(engineDir);
return engineInfoManager.validateEngineDir(engineDir);
},

RELOAD_APP: async (_, { isMultiEngineOffMode }) => {
Expand Down
17 changes: 12 additions & 5 deletions src/backend/electron/manager/engineInfoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export class EngineInfoManager {
defaultEngineInfos: EngineInfo[] = [];
additionalEngineInfos: EngineInfo[] = [];

public altPortInfo: AltPortInfos = {};
/** 代替ポート情報 */
public altPortInfos: AltPortInfos = {};

constructor(payload: {
configManager: BaseConfigManager;
Expand Down Expand Up @@ -169,20 +170,26 @@ export class EngineInfoManager {
}

/**
* EngineInfosを初期化する
* EngineInfosとAltPortInfoを初期化する
*/
initializeEngineInfos() {
initializeEngineInfosAndAltPortInfo() {
this.defaultEngineInfos = createDefaultEngineInfos(this.defaultEngineDir);
this.additionalEngineInfos = this.createAdditionalEngineInfos();
this.altPortInfos = {};
}

/**
* エンジン情報のhost内のportを置き換える
* 代替ポート情報を更新する
* エンジン起動時にポートが競合して代替ポートを使う場合に使用する。
*/
changeEngineInfoPort(engineId: EngineId, port: number) {
updateAltPort(engineId: EngineId, port: number) {
const engineInfo = this.fetchEngineInfo(engineId);
const url = new URL(engineInfo.host);
this.altPortInfos[engineId] = {
from: Number(url.port),
to: port,
};

url.port = port.toString();
engineInfo.host = url.toString();
}
Expand Down
81 changes: 10 additions & 71 deletions src/backend/electron/manager/engineProcessManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { spawn, ChildProcess } from "child_process";
import path from "path";
import fs from "fs";
import treeKill from "tree-kill";

import { app, dialog } from "electron"; // FIXME: ここでelectronをimportするのは良くない
Expand All @@ -14,15 +13,7 @@ import {
url2HostInfo,
} from "../portHelper";

import {
EngineInfo,
EngineDirValidationResult,
MinimumEngineManifestType,
EngineId,
minimumEngineManifestSchema,
EngineSettings,
} from "@/type/preload";
import { AltPortInfos } from "@/store/type";
import { EngineInfo, EngineId, EngineSettings } from "@/type/preload";
import { BaseConfigManager } from "@/backend/common/ConfigManager";

type EngineProcessContainer = {
Expand All @@ -32,44 +23,29 @@ type EngineProcessContainer = {

/** エンジンプロセスを管理するクラス */
export class EngineProcessManager {
configManager: BaseConfigManager;
vvppEngineDir: string;
onEngineProcessError: (engineInfo: EngineInfo, error: Error) => void;
engineInfosFetcher: () => Readonly<EngineInfo>[];
engineInfoPortChanger: (engineId: EngineId, port: number) => void;
engineSettingsFetcher: () => Partial<EngineSettings>;
engineAltPortUpdater: (engineId: EngineId, port: number) => void;
engineSettingsGetter: () => Partial<EngineSettings>;

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

/** 代替ポート情報 */
public altPortInfos: AltPortInfos = {};

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

/**
* 代替ポートの情報を初期化する。
*/
initializeAltPortInfos() {
this.altPortInfos = {};
this.engineAltPortUpdater = payload.engineAltPortUpdater;
this.engineSettingsGetter = payload.engineSettingsGetter;
}

/**
Expand Down Expand Up @@ -151,13 +127,7 @@ export class EngineProcessManager {
}

// 代替ポート情報を更新
this.altPortInfos[engineId] = {
from: engineHostInfo.port,
to: altPort,
};

// エンジン情報に代替ポートを反映
this.engineInfoPortChanger(engineId, altPort);
this.engineAltPortUpdater(engineId, altPort);
log.warn(
`ENGINE ${engineId}: Applied Alternative Port: ${engineHostInfo.port} -> ${altPort}`,
);
Expand All @@ -174,7 +144,7 @@ export class EngineProcessManager {
const engineProcessContainer = this.engineProcessContainers[engineId];
engineProcessContainer.willQuitEngine = false;

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

Expand Down Expand Up @@ -393,37 +363,6 @@ export class EngineProcessManager {
});
});
}

/**
* ディレクトリがエンジンとして正しいかどうかを判定する
*/
validateEngineDir(engineDir: string): EngineDirValidationResult {
if (!fs.existsSync(engineDir)) {
return "directoryNotFound";
} else if (!fs.statSync(engineDir).isDirectory()) {
return "notADirectory";
} else if (!fs.existsSync(path.join(engineDir, "engine_manifest.json"))) {
return "manifestNotFound";
}
const manifest = fs.readFileSync(
path.join(engineDir, "engine_manifest.json"),
"utf-8",
);
let manifestContent: MinimumEngineManifestType;
try {
manifestContent = minimumEngineManifestSchema.parse(JSON.parse(manifest));
} catch (e) {
return "invalidManifest";
}

const engineInfos = this.engineInfosFetcher();
if (
engineInfos.some((engineInfo) => engineInfo.uuid === manifestContent.uuid)
) {
return "alreadyExists";
}
return "ok";
}
}

export default EngineProcessManager;

0 comments on commit dc2482c

Please sign in to comment.