Skip to content

Commit

Permalink
fix: prevent launcher from opening MO2 with the wrong profile
Browse files Browse the repository at this point in the history
  • Loading branch information
MattLish committed Nov 23, 2021
1 parent 62486a3 commit 614c91c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 14 deletions.
77 changes: 67 additions & 10 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"electron-store": "^8.0.1",
"find-process": "^1.4.7",
"fs-extra": "^10.0.0",
"ini": "^2.0.0",
"junk": "^3.1.0",
"mitt": "^3.0.0",
"ncp": "^2.0.0",
Expand All @@ -36,6 +37,7 @@
"@commitlint/config-conventional": "^15.0.0",
"@types/electron-devtools-installer": "^2.2.0",
"@types/fs-extra": "^9.0.13",
"@types/ini": "^1.3.31",
"@types/ncp": "^2.0.5",
"@typescript-eslint/eslint-plugin": "^2.33.0",
"@typescript-eslint/parser": "^2.33.0",
Expand Down
6 changes: 3 additions & 3 deletions src/main/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ import Store from "electron-store";
export const isDevelopment = process.env.NODE_ENV !== "production";

export enum USER_PREFERENCE_KEYS {
SKYRIM_DIRECTORY = "SKYRIM_DIRECTORY",
MOD_DIRECTORY = "MOD_DIRECTORY",
PRESET = "PRESET",
ENB_PROFILE = "ENB_PROFILE",
}

export interface UserPreferences {
[USER_PREFERENCE_KEYS.SKYRIM_DIRECTORY]: string;
[USER_PREFERENCE_KEYS.MOD_DIRECTORY]: string;
[USER_PREFERENCE_KEYS.PRESET]: string;
}

export const userPreferences = new Store<UserPreferences>({
name: "userPreferences",
defaults: {
[USER_PREFERENCE_KEYS.SKYRIM_DIRECTORY]: "",
[USER_PREFERENCE_KEYS.MOD_DIRECTORY]: "",
[USER_PREFERENCE_KEYS.PRESET]: "",
},
});

export const skyrimDirectory = () =>
`${userPreferences.get(USER_PREFERENCE_KEYS.MOD_DIRECTORY)}/Stock Game`;

export const modDirectory = () =>
userPreferences.get(USER_PREFERENCE_KEYS.MOD_DIRECTORY);
30 changes: 29 additions & 1 deletion src/main/modOrganizer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import path from "path";
import childProcess from "child_process";
import { USER_PREFERENCE_KEYS, userPreferences } from "@/main/config";
import {
modDirectory,
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";
import fs from "fs";
import { parse, encode } from "ini";

export const MO2EXE = "ModOrganizer.exe";
const MO2Settings = "ModOrganizer.ini";

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

Expand All @@ -28,6 +35,21 @@ const handleRunning = async (): Promise<boolean> => {
}
};

const readSettings = async () =>
parse(
await fs.promises.readFile(`${modDirectory()}/${MO2Settings}`, "utf-8")
);

const updateSelectedProfile = async (profile: string) => {
logger.info(`Updating selected profile to ${profile}`);
const settings = await readSettings();
settings.General["selected_profile"] = `@ByteArray(${profile})`;
await fs.promises.writeFile(
`${modDirectory()}/${MO2Settings}`,
encode(settings)
);
};

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

Expand All @@ -48,6 +70,12 @@ export const launchMO2 = async () => {
}

logger.info("Launching MO2");

// MO2 will not respect the profile set in the launcher until the config is edited
await updateSelectedProfile(
userPreferences.get(USER_PREFERENCE_KEYS.PRESET)
);

const moPath = path.join(
userPreferences.get(USER_PREFERENCE_KEYS.MOD_DIRECTORY),
MO2EXE
Expand Down

0 comments on commit 614c91c

Please sign in to comment.