Skip to content

Commit

Permalink
fix: prevent user from opening multiple MO2 instances
Browse files Browse the repository at this point in the history
closes #303
  • Loading branch information
MattLish committed Nov 23, 2021
1 parent 4461d8b commit 5271198
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 11 deletions.
95 changes: 86 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dependencies": {
"electron-log": "^4.4.1",
"electron-store": "^8.0.1",
"find-process": "^1.4.7",
"fs-extra": "^10.0.0",
"junk": "^3.1.0",
"mitt": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/main/ipcHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { copyENBFiles, deleteAllENBFiles, getENBPresets } from "@/main/ENB";
import { handleError } from "./errorHandler";

export function registerHandlers() {
ipcMain.handle(IPCEvents.LAUNCH_MO2, () => launchMO2());
ipcMain.handle(IPCEvents.LAUNCH_MO2, async () => await launchMO2());

ipcMain.handle(IPCEvents.LAUNCH_GAME, async () => {
await launchGame();
Expand Down
37 changes: 36 additions & 1 deletion src/main/modOrganizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,30 @@ import { USER_PREFERENCE_KEYS, userPreferences } from "@/main/config";
import { logger } from "@/main/logger";
import { checkENBFilesExist, copyENBFiles } from "@/main/ENB";
import { handleError } from "@/main/errorHandler";
import find from "find-process";
import { dialog } from "electron";

export const MO2EXE = "ModOrganizer.exe";

const isRunning = async () => (await find("name", "ModOrganizer")).length > 0;

const handleRunning = async (): Promise<boolean> => {
const buttonSelectionIndex = await dialog.showMessageBox({
title: "Mod Organizer running",
message:
"Mod Organizer 2 is already running. This could launch the wrong mod list. Would you like to close it first?",
buttons: ["Cancel", "Close MO2 and continue"],
});
if (buttonSelectionIndex.response === 1) {
(await find("name", "ModOrganizer")).forEach((mo2Instance) => {
process.kill(mo2Instance.pid);
});
return true;
} else {
return false;
}
};

async function copyENBFilesOnLaunch() {
logger.info("Copying ENB files on launch");

Expand All @@ -17,8 +38,15 @@ async function copyENBFilesOnLaunch() {
}
}

export const launchMO2 = () => {
export const launchMO2 = async () => {
try {
if (await isRunning()) {
const continueLaunching = await handleRunning();
if (!continueLaunching) {
return;
}
}

logger.info("Launching MO2");
const moPath = path.join(
userPreferences.get(USER_PREFERENCE_KEYS.MOD_DIRECTORY),
Expand All @@ -33,6 +61,13 @@ export const launchMO2 = () => {

export async function launchGame() {
try {
if (await isRunning()) {
const continueLaunching = await handleRunning();
if (!continueLaunching) {
return;
}
}

await copyENBFilesOnLaunch();

logger.info("Launching game");
Expand Down

0 comments on commit 5271198

Please sign in to comment.