Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

Commit

Permalink
Refactor manager and add uninstall button
Browse files Browse the repository at this point in the history
  • Loading branch information
moritzruth committed Feb 15, 2021
1 parent ae25e35 commit 1c766e1
Show file tree
Hide file tree
Showing 16 changed files with 338 additions and 270 deletions.
4 changes: 3 additions & 1 deletion src/main/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module.exports = {
"mod": false,
"props": false
}
}]
}],
"@typescript-eslint/no-unsafe-call": "off", // many false positives
"@typescript-eslint/strict-boolean-expressions": "off" // many false positives
}
}
2 changes: 2 additions & 0 deletions src/main/version.ts → src/main/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const isDevelopment: boolean = process.env.IS_DEV === "true"

// @ts-expect-error
// eslint-disable-next-line no-undef,@typescript-eslint/no-unsafe-assignment
export const MANAGER_VERSION: string = __MANAGER_VERSION
4 changes: 2 additions & 2 deletions src/main/handleSquirrelEvents.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import childProcess from "child_process"
import pathLib from "path"
import { app, dialog } from "electron"
import { isDevelopment } from "./isDevelopment"
import { app } from "electron"
import { isDevelopment } from "./constants"

const spawn = (command: string, arguments_: string[]) => {
let spawnedProcess
Expand Down
1 change: 0 additions & 1 deletion src/main/isDevelopment.ts

This file was deleted.

25 changes: 13 additions & 12 deletions src/main/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { app, dialog, Menu } from "electron"
import { app, Menu, shell } from "electron"
import { registerWindowIPC } from "./registerWindowIPC"
import { MANAGER_VERSION } from "./version"
import { initiateManager, isAmongUsInstalled } from "./manager"
import { createWindow, getWindow } from "./window"
import { handleSquirrelEvents } from "./handleSquirrelEvents"
import { getOriginalGameVersion, initiateManager, loadGameVersionOrShowError, STEAM_APPS_DIRECTORY } from "./manager"
import { MANAGER_VERSION } from "./constants"

if (!handleSquirrelEvents()) {
if (!app.requestSingleInstanceLock()) {
Expand All @@ -12,17 +12,18 @@ if (!handleSquirrelEvents()) {
}

app.on("ready", async () => {
if (!await isAmongUsInstalled()) {
dialog.showErrorBox(
"Among Us could not be found",
"Please make sure Among Us is installed in the default location of Steam games."
)

app.exit(1)
}
await loadGameVersionOrShowError()

app.applicationMenu = Menu.buildFromTemplate([
{ label: `Version: ${MANAGER_VERSION}`, enabled: false },
{ label: `Manager: ${MANAGER_VERSION}`, enabled: false },
{ label: `Among Us: ${getOriginalGameVersion()}`, enabled: false },
{ type: "separator" },
{
label: "Open Steam games directory",
click() {
shell.openPath(STEAM_APPS_DIRECTORY)
}
},
{
label: "Show devtools",
click() {
Expand Down
232 changes: 0 additions & 232 deletions src/main/manager.ts

This file was deleted.

19 changes: 19 additions & 0 deletions src/main/manager/detectGameVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pathLib from "path"
import fs from "fs-extra"

export async function detectGameVersion(directory: string): Promise<string> {
const content = await fs.readFile(
pathLib.resolve(directory, "./Among Us_Data/globalgamemanagers"),
{ encoding: "utf8" }
)

const regex = /\d{4}\.\d{1,4}\.\d{1,4}/gu
regex.exec(content)
const match = regex.exec(content)

if (match !== null) {
return match[0]
}

throw new Error("Among Us version could not be detected")
}
14 changes: 14 additions & 0 deletions src/main/manager/gameInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pathLib from "path"
import fs from "fs-extra"
import { detectGameVersion } from "./detectGameVersion"
import { STEAM_APPS_DIRECTORY } from "./util"

export const ORIGINAL_GAME_DIRECTORY = pathLib.resolve(STEAM_APPS_DIRECTORY, "Among Us")

let originalGameVersion: string
export const getOriginalGameVersion = () => originalGameVersion

export async function detectOriginalGameVersion() {
if (!await fs.pathExists(ORIGINAL_GAME_DIRECTORY)) throw new Error("Among Us could not be found")
originalGameVersion = await detectGameVersion(ORIGINAL_GAME_DIRECTORY)
}
35 changes: 35 additions & 0 deletions src/main/manager/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { app, dialog } from "electron"
import { detectOriginalGameVersion } from "./gameInfo"
import { registerIPC, sendUIModData } from "./ipc"
import { fetchRemoteMods } from "./remoteMods"
import { discoverInstalledMods } from "./installedMods"
import { STEAM_APPS_DIRECTORY } from "./util"

export function initiateManager() {
registerIPC()

Promise.all([
discoverInstalledMods(),
fetchRemoteMods(),
detectOriginalGameVersion()
]).then(() => {
sendUIModData()
})
}

export async function loadGameVersionOrShowError() {
try {
await detectOriginalGameVersion()
} catch {
dialog.showErrorBox(
"Among Us could not be found",
`Please make sure Among Us is installed in the default location of Steam games. (${STEAM_APPS_DIRECTORY})`
)

app.exit(1)
}
}

export { isGameRunning } from "./installedMods"
export { getOriginalGameVersion } from "./gameInfo"
export { STEAM_APPS_DIRECTORY } from "./util"
Loading

0 comments on commit 1c766e1

Please sign in to comment.