diff options
author | 0neGal <mail@0negal.com> | 2023-03-05 00:25:07 +0100 |
---|---|---|
committer | 0neGal <mail@0negal.com> | 2023-03-05 00:38:53 +0100 |
commit | 5d86a3daa5f762326055469b6bcd8346e0655056 (patch) | |
tree | 110fb1395fac1510dc3bf70f39f073df555445ff /src/modules/gamepath.js | |
parent | 4536aad2c4f19d32569d84a7082e8636a2be6985 (diff) | |
download | Viper-5d86a3daa5f762326055469b6bcd8346e0655056.tar.gz Viper-5d86a3daa5f762326055469b6bcd8346e0655056.zip |
modularized many functions and got rid of utils.js
Notably, winLog() and winAlert() are now win.log() and win.alert()
inside modules/window.js.
updateViper(), updateNorthstar and handleNorthstarUpdating() are now
update.viper(), update.northstar() and update.northstar_autoupdate(),
inside modules/update.js
isGameRunning() and isOriginRunning() are now is_running.origin() and
is_running.game() inside modules/is_running.js, along with a
.titanfall() and .northstar() for more specificity. Not used anywhere
right now, but may in the future be used.
setpath() and gamepathExists() are now gamepath.set() and
gamepath.exists() inside modules/gamepath.js
killOrigin() are now kill.origin() inside modules/kill.js
setlang() is now just inlined into the only event where it's used.
Diffstat (limited to 'src/modules/gamepath.js')
-rw-r--r-- | src/modules/gamepath.js | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/modules/gamepath.js b/src/modules/gamepath.js new file mode 100644 index 0000000..93290ec --- /dev/null +++ b/src/modules/gamepath.js @@ -0,0 +1,86 @@ +const path = require("path"); +const fs = require("fs-extra"); + +const { dialog, ipcMain } = require("electron"); + +const cli = require("../cli"); + +const settings = require("./settings"); + +let gamepath = {}; + +// Returns true/false depending on if the gamepath currently exists/is +// mounted, used to avoid issues... +gamepath.exists = () => { + return fs.existsSync(settings.gamepath); +} + +// Requests to set the game path +// +// If running with CLI it takes in the --setpath argument otherwise it +// open the systems file browser for the user to select a path. +gamepath.set = async (win, forcedialog) => { + function set_gamepath(folder) { + settings.gamepath = folder; + settings.zip = path.join(settings.gamepath + "/northstar.zip"); + settings.save(); + win.webContents.send("newpath", settings.gamepath); + ipcMain.emit("newpath", null, settings.gamepath); + + modpath = path.join(settings.gamepath, "R2Northstar/mods"); + } + + if (! win) { // CLI + set_gamepath(cli.param("setpath")); + } else { // GUI + if (! forcedialog) { + function set_gamepath(folder, forcedialog) { + settings.gamepath = folder; + settings.zip = path.join(settings.gamepath + "/northstar.zip"); + settings.save(); + win.webContents.send("newpath", settings.gamepath); + ipcMain.emit("newpath", null, settings.gamepath); + } + + let gamepath = await findgame(); + if (gamepath) { + set_gamepath(gamepath); + return; + } + + win.alert(lang("general.missingpath")); + } + + // Fallback to manual selection + dialog.showOpenDialog({properties: ["openDirectory"]}).then(res => { + if (res.canceled) { + ipcMain.emit("newpath", null, false); + return; + } + if (! fs.existsSync(path.join(res.filePaths[0], "Titanfall2.exe"))) { + ipcMain.emit("wrong-path"); + return; + } + + set_gamepath(res.filePaths[0]); + + cli.exit(); + return; + }).catch(err => {console.error(err)}) + } +} + +// periodically check for the gamepath still existing +setInterval(() => { + if (gamepath.exists()) { + ipcMain.emit("gui-getmods"); + } else { + if (fs.existsSync("viper.json")) { + if (settings.gamepath != "") { + ipcMain.emit("gamepath-lost"); + } + } + } +}, 1500) + +module.exports = gamepath; |