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

Commit

Permalink
Allow selecting different game directories
Browse files Browse the repository at this point in the history
  • Loading branch information
moritzruth committed Feb 16, 2021
1 parent 8dde439 commit b7483d5
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Among Us Mod Manager
> Install and update Among Us mods with a single click*
*\*on PC, with the Steam version of the game.*
*\*on PC. Only the Steam version is tested, but other ones should also work.*

[**🖼️ Show me some screenshots!**](https://ko-fi.com/album/Among-Us-Mod-Manager-L3L13NKR7)

Expand Down
2 changes: 1 addition & 1 deletion README_de.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Among Us Mod Manager
> Installiere und aktualisiere Among Us-Mods mit einem einzigen Klick*
*\*auf dem PC, mit der Steam-Version des Spiels.*
*\*auf dem PC. Nur die Steam-Version ist getestet, aber andere sollten auch funktionieren.*

[**🖼️ Zeig mir Screenshots!**](https://ko-fi.com/album/Among-Us-Mod-Manager-L3L13NKR7)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "among-us-mod-manager",
"version": "1.1.0",
"version": "1.2.0",
"description": "Manager for Among Us Mods",
"main": "dist/main.js",
"repository": "https://github.com/moritzruth/among-us-mod-manager.git",
Expand Down
22 changes: 18 additions & 4 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { app, Menu, shell } from "electron"
import { registerWindowIPC } from "./registerWindowIPC"
import { createWindow, getWindow } from "./window"
import { handleSquirrelEvents } from "./handleSquirrelEvents"
import { getOriginalGameVersion, initiateManager, loadGameVersionOrShowError, STEAM_APPS_DIRECTORY } from "./manager"
import {
doStartupCheck,
getOriginalGameVersion,
initiateManager,
getDirectoryForInstallations,
showOriginalGameDirectorySelectDialog,
detectOriginalGameVersion
} from "./manager"
import { MANAGER_VERSION } from "./constants"

if (!handleSquirrelEvents()) {
Expand All @@ -12,16 +19,23 @@ if (!handleSquirrelEvents()) {
}

app.on("ready", async () => {
await loadGameVersionOrShowError()
await doStartupCheck()
await detectOriginalGameVersion()

app.applicationMenu = Menu.buildFromTemplate([
{ label: `Manager: ${MANAGER_VERSION}`, enabled: false },
{ label: `Among Us: ${getOriginalGameVersion()}`, enabled: false },
{ type: "separator" },
{
label: "Open Steam games directory",
label: "Select the game directory",
click() {
shell.openPath(STEAM_APPS_DIRECTORY)
showOriginalGameDirectorySelectDialog("user")
}
},
{
label: "Open the installations directory",
click() {
shell.openPath(getDirectoryForInstallations())
}
},
{
Expand Down
85 changes: 80 additions & 5 deletions src/main/manager/gameInfo.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,89 @@
/* eslint-disable no-await-in-loop */
import pathLib from "path"
import fs from "fs-extra"
import Store from "electron-store"
import { app, dialog, MessageBoxOptions } from "electron"
import { detectGameVersion } from "./detectGameVersion"
import { STEAM_APPS_DIRECTORY } from "./util"

export const ORIGINAL_GAME_DIRECTORY = pathLib.resolve(STEAM_APPS_DIRECTORY, "Among Us")
const store = new Store<{
originalGameDirectory: string
}>()

export const getOriginalGameDirectory = () => store.get("originalGameDirectory")
const setOriginalGameDirectory = (path: string) => store.set("originalGameDirectory", path)

export const getDirectoryForInstallations = () => pathLib.resolve(store.get("originalGameDirectory"), "..")

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)
export const isValidGameDirectory = async (path: string) => fs.pathExists(pathLib.resolve(path, "Among Us.exe"))

export async function detectOriginalGameVersion(): Promise<boolean> {
if (!await fs.pathExists(getOriginalGameDirectory())) return false
originalGameVersion = await detectGameVersion(getOriginalGameDirectory())
return true
}

export async function showOriginalGameDirectorySelectDialog(reason: "user" | "not-automatically" | "invalid") {
if (reason !== "user") {
const options: MessageBoxOptions = {
title: reason === "not-automatically"
? "Among Us could not be automatically detected."
: "Among Us could not be found.",
message: (reason === "not-automatically" ? "Among Us could not be automatically detected. " : "") +
"Please select the directory which contains the game (Among Us.exe).\n\n",
buttons: ["Cancel", "Select"],
type: "error"
}

const { response } = await dialog.showMessageBox(options)
if (response === 0) app.exit(0)
}

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition,no-constant-condition
while (true) {
const { filePaths } = await dialog.showOpenDialog({
properties: ["openDirectory", "dontAddToRecent"],
buttonLabel: "Select",
title: "Game directory selection"
})

if (filePaths.length === 0) {
if (reason === "user") return
app.exit(0)
} else {
const [path] = filePaths

if (await isValidGameDirectory(path)) {
store.set("originalGameDirectory", path)
app.relaunch()
app.exit()
return
}

const options: MessageBoxOptions = {
type: "error",
title: "Among Us could not be found.",
message: "Please try again.",
buttons: ["Cancel", "Retry"]
}

const { response } = await dialog.showMessageBox(options)
if (response === 0) {
if (reason === "user") return
app.exit()
}
}
}
}

export async function doStartupCheck() {
if (store.has("originalGameDirectory")) {
if (!await isValidGameDirectory(getOriginalGameDirectory())) await showOriginalGameDirectorySelectDialog("invalid")
} else {
const defaultPath = pathLib.resolve("C:\\Program Files (x86)\\Steam\\steamapps\\common", "./Among Us")
if (await isValidGameDirectory(defaultPath)) setOriginalGameDirectory(defaultPath)
else await showOriginalGameDirectorySelectDialog("not-automatically")
}
}
29 changes: 9 additions & 20 deletions src/main/manager/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
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()
fetchRemoteMods()
]).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"
export {
getOriginalGameVersion,
isValidGameDirectory,
doStartupCheck,
getDirectoryForInstallations,
showOriginalGameDirectorySelectDialog,
detectOriginalGameVersion
} from "./gameInfo"
16 changes: 8 additions & 8 deletions src/main/manager/installedMods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import execa from "execa"
import { app } from "electron"
import semver from "semver"
import { getWindow } from "../window"
import { isDevelopment, MANAGER_VERSION } from "../constants"
import { MANAGER_VERSION } from "../constants"
import { detectGameVersion } from "./detectGameVersion"
import { send, STEAM_APPS_DIRECTORY } from "./util"
import { send } from "./util"
import { getRemoteMod, RemoteMod } from "./remoteMods"
import { updateProgress } from "./progress"
import { getOriginalGameVersion, ORIGINAL_GAME_DIRECTORY } from "./gameInfo"
import { getDirectoryForInstallations, getOriginalGameDirectory, getOriginalGameVersion } from "./gameInfo"
import { sendUIModData } from "./ipc"

interface InstalledMod {
Expand All @@ -26,9 +26,9 @@ export const getInstalledMods = () => installedMods
export const getInstalledMod: (id: string) => InstalledMod = id => installedMods.find(mod => mod.id === id)!

export async function discoverInstalledMods() {
const directoryNames = await fs.readdir(STEAM_APPS_DIRECTORY)
const directoryNames = await fs.readdir(getDirectoryForInstallations())
installedMods = (await Promise.all(directoryNames.map<Promise<InstalledMod | null>>(async name => {
const directory = pathLib.resolve(STEAM_APPS_DIRECTORY, name)
const directory = pathLib.resolve(getDirectoryForInstallations(), name)
const dataPath = pathLib.resolve(directory, "aumm.json")

if (await fs.pathExists(dataPath)) {
Expand All @@ -54,13 +54,13 @@ async function installMod(remoteMod: RemoteMod) {
const alreadyInstalledIndex = installedMods.findIndex(mod => mod.id === remoteMod.id)
if (alreadyInstalledIndex !== -1) installedMods.splice(alreadyInstalledIndex, 1)

const directory = pathLib.resolve(STEAM_APPS_DIRECTORY, `Among Us (${remoteMod.title})`)
const directory = pathLib.resolve(getDirectoryForInstallations(), `Among Us (${remoteMod.title})`)

updateProgress({ title: "Install: " + remoteMod.title, text: "Preparing", finished: false })
if (await fs.pathExists(directory)) await fs.remove(directory)

updateProgress({ text: "Copying game files" })
await fs.copy(ORIGINAL_GAME_DIRECTORY, directory)
await fs.copy(getOriginalGameDirectory(), directory)

const request = download(remoteMod.downloadURL, directory, { filename: "__archive" })

Expand Down Expand Up @@ -98,7 +98,7 @@ export async function startMod(id: string) {

const process = execa(
pathLib.resolve(installedMod.path, "Among Us.exe"),
{ detached: false, stdout: "ignore", stderr: isDevelopment ? "inherit" : "ignore", windowsHide: false }
{ detached: false, stdout: "ignore", stderr: "ignore", windowsHide: false }
)

activeModId = installedMod.id
Expand Down
2 changes: 0 additions & 2 deletions src/main/manager/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { getWindow } from "../window"

export const STEAM_APPS_DIRECTORY = "C:\\Program Files (x86)\\Steam\\steamapps\\common"

export function send(channel: string, ...arguments_: unknown[]) {
getWindow().webContents.send(channel, ...arguments_)
}

0 comments on commit b7483d5

Please sign in to comment.