From 1c405a64400b16414559dc48250b946722f0108a Mon Sep 17 00:00:00 2001 From: 0neGal Date: Sat, 16 Sep 2023 03:09:15 +0200 Subject: handle gamepaths with missing read/write perms We now show an alert if the gamepath is detected to be missing read/write perms, both when selecting the gamepath initially, when it gets auto detected, but also after it's selected, passively. --- src/index.js | 24 ++++++++++++++++--- src/lang/en.json | 5 +++- src/modules/gamepath.js | 64 +++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 79 insertions(+), 14 deletions(-) diff --git a/src/index.js b/src/index.js index 2a7f4d7..a097525 100644 --- a/src/index.js +++ b/src/index.js @@ -216,12 +216,12 @@ ipcMain.on("update-northstar", async () => { }) ipcMain.on("setpath-cli", () => {gamepath.set()}); -ipcMain.on("setpath", (event, value) => { +ipcMain.on("setpath", (event, value, force_dialog) => { if (! value) { if (! win.isVisible()) { - gamepath.set(win); + gamepath.set(win, force_dialog); } else { - gamepath.set(win, true); + gamepath.set(win, force_dialog || true); } } else if (! win.isVisible()) { win.show(); @@ -288,6 +288,24 @@ ipcMain.on("newpath", (event, newpath) => { win.send("wrong-path"); }); +ipcMain.on("found-missing-perms", async (e, selected_gamepath) => { + await win_show.alert(lang("gui.gamepath.found_missing_perms") + selected_gamepath); + ipcMain.emit("setpath", null, false, true); +}) + +ipcMain.on("missing-perms", async (e, selected_gamepath) => { + await win_show.alert(lang("gui.gamepath.missing_perms") + selected_gamepath); + ipcMain.emit("setpath"); +}) + +ipcMain.on("gamepath-lost-perms", async (e, selected_gamepath) => { + if (! gamepath.setting) { + gamepath.setting = true; + await win_show.alert(lang("gui.gamepath.lost_perms") + selected_gamepath); + ipcMain.emit("setpath"); + } +}) + // starts the GUI or CLI if (cli.hasArgs()) { if (cli.hasParam("update-viper")) { diff --git a/src/lang/en.json b/src/lang/en.json index cf314ad..67e7aa9 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -194,7 +194,10 @@ "gamepath": { "must": "The game path must be set to start Viper.", "wrong": "This folder is not a valid game path.", - "lost": "Gamepath no longer exists/can't be found!\n\nMake sure your drive is mounted properly, or if you moved your game location that you update the game path.\n\nViper may not work properly until next restart!" + "lost": "Gamepath no longer exists/can't be found!\n\nMake sure your drive is mounted properly, or if you moved your game location that you update the game path.\n\nViper may not work properly until next restart!", + "lost_perms": "Your user seems to have lost permissions to create and or read files in the selected gamepath, please select a gamepath or gain access to read and write to the folder:\n\n", + "found_missing_perms": "Automatically found a valid gamepath, however your user doesn't have permissions to create and or read files in it, please manually select a different gamepath or gain access to read and write to the folder:\n\n", + "missing_perms": "Your user doesn't have permissions to create and or read files in the selected gamepath, please select a different gamepath or gain access to read and write to the folder:\n\n" }, "toast": { diff --git a/src/modules/gamepath.js b/src/modules/gamepath.js index 6555cbd..0a9dcc6 100644 --- a/src/modules/gamepath.js +++ b/src/modules/gamepath.js @@ -15,15 +15,39 @@ 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); +gamepath.exists = (folder) => { + return fs.existsSync(folder || settings.gamepath); } +// returns false if the user doesn't have read/write permissions to the +// selected gamepath, if no gamepath is set, then this will always +// return `false`, handle that correctly! +gamepath.has_perms = (folder) => { + if (! gamepath.exists(folder)) { + return false; + } + + try { + fs.accessSync( + folder || settings.gamepath, + fs.constants.R_OK | fs.constants.W_OK + ) + + return true; + } catch (err) { + return false; + } +} + +gamepath.setting = false; + // 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, force_dialog) => { + gamepath.setting = true; + // actually sets and saves the gamepath in the settings function set_gamepath(folder) { // set settings @@ -50,12 +74,20 @@ gamepath.set = async (win, force_dialog) => { settings.save(); win.webContents.send("newpath", settings.gamepath); ipcMain.emit("newpath", null, settings.gamepath); + + gamepath.setting = false; } - let gamepath = await findgame(); - if (gamepath) { - set_gamepath(gamepath); - return; + let found_gamepath = await findgame(); + + if (found_gamepath) { + if (! gamepath.has_perms(found_gamepath)) { + ipcMain.emit("found-missing-perms", null, found_gamepath); + return; + } + + set_gamepath(found_gamepath); + return gamepath.setting = false; } await win_show.alert(lang("general.missing_path")); @@ -65,19 +97,27 @@ gamepath.set = async (win, force_dialog) => { dialog.showOpenDialog({properties: ["openDirectory"]}).then(res => { if (res.canceled) { ipcMain.emit("newpath", null, false); - return; + return gamepath.setting = false; } if (! fs.existsSync(path.join(res.filePaths[0], "Titanfall2.exe"))) { ipcMain.emit("wrong-path"); - return; + return gamepath.setting = false; + } + + if (! gamepath.has_perms(res.filePaths[0])) { + ipcMain.emit("missing-perms", null, res.filePaths[0]); + return gamepath.setting = false; } set_gamepath(res.filePaths[0]); cli.exit(); - return; - }).catch(err => {console.error(err)}) + return gamepath.setting = false; + }).catch(err => { + console.error(err); + gamepath.setting = false; + }) } } @@ -86,6 +126,10 @@ gamepath.set = async (win, force_dialog) => { // want to assume the gamepath is available forever and ever. setInterval(() => { if (gamepath.exists()) { + if (! gamepath.has_perms()) { + return ipcMain.emit("gamepath-lost-perms", null, settings.gamepath); + } + ipcMain.emit("gui-getmods"); } else { if (fs.existsSync("viper.json")) { -- cgit v1.2.3