From ee816e6a1aa908df381dcc488b1193d1e78e0ca6 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Sun, 4 Feb 2024 17:16:00 +0100 Subject: merge src/modules/window.js into src/win.js I intended to do this when creating src/win.js, but wanted it to be in a different commit, as that commit made pretty large changes as well. So no more `main_win`, `win_show` and confusion between what `win` is. --- src/index.js | 12 +++++------ src/modules/gamepath.js | 9 ++++---- src/modules/launch.js | 4 ++-- src/modules/mods.js | 47 ++++++++++++++++++++-------------------- src/modules/packages.js | 11 +++++----- src/modules/settings.js | 6 +++--- src/modules/update.js | 33 ++++++++++++++-------------- src/modules/window.js | 51 ------------------------------------------- src/win.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++- 9 files changed, 115 insertions(+), 115 deletions(-) delete mode 100644 src/modules/window.js diff --git a/src/index.js b/src/index.js index ef6703a..358723f 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,6 @@ const { app, BrowserWindow } = require("electron"); process.chdir(app.getPath("appData")); const cli = require("./cli"); -const main_win = require("./win"); const mods = require("./modules/mods"); const update = require("./modules/update"); @@ -23,7 +22,7 @@ console = require("./modules/console"); // Starts the actual BrowserWindow, which is only run when using the // GUI, for the CLI this function is never called. function start() { - win = new BrowserWindow({ + let win = new BrowserWindow({ width: 1000, height: 600, title: "Viper", @@ -39,11 +38,12 @@ function start() { frame: false, titleBarStyle: "hidden", icon: path.join(__dirname, "assets/icons/512x512.png"), + webPreferences: { webviewTag: true, nodeIntegration: true, - contextIsolation: false, - }, + contextIsolation: false + } }) // makes sending things to the renderer a little more readable @@ -51,9 +51,9 @@ function start() { win.webContents.send(channel, data); }; send = win.send; - // give `main_win` the main window, `main_win()` will then be equal + // give `./win` the main window, `./win()` will then be equal // to `win`, but its accessible anywhere - main_win.set(win); + require("./win").set(win); // when --devtools is added it'll open the dev tools if (cli.hasParam("devtools")) { diff --git a/src/modules/gamepath.js b/src/modules/gamepath.js index 9d12854..69cc446 100644 --- a/src/modules/gamepath.js +++ b/src/modules/gamepath.js @@ -8,7 +8,6 @@ const cli = require("../cli"); const lang = require("../lang"); const version = require("./version"); -const win_show = require("./window"); const settings = require("./settings"); const findgame = require("./findgame"); @@ -46,19 +45,19 @@ ipcMain.on("wrong-path", () => { }) ipcMain.on("found-missing-perms", async (e, selected_gamepath) => { - await win_show.alert(lang("gui.gamepath.found_missing_perms") + selected_gamepath); + await win().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); + await win().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); + await win().alert(lang("gui.gamepath.lost_perms") + selected_gamepath); ipcMain.emit("setpath"); } }) @@ -154,7 +153,7 @@ gamepath.set = async (win, force_dialog) => { return gamepath.setting = false; } - await win_show.alert(lang("general.missing_path")); + await win().alert(lang("general.missing_path")); } // fallback to GUI/manual selection diff --git a/src/modules/launch.js b/src/modules/launch.js index e9ad1a3..73ce408 100644 --- a/src/modules/launch.js +++ b/src/modules/launch.js @@ -2,9 +2,9 @@ const exec = require("child_process").exec; const ipcMain = require("electron").ipcMain; const cli = require("../cli"); +const win = require("../win"); const lang = require("../lang"); -const win = require("./window"); const settings = require("./settings"); console = require("./console"); @@ -21,7 +21,7 @@ ipcMain.on("launch-vanilla", () => { function launch(game_version) { // return early, and show error message if on Linux if (process.platform == "linux") { - win.alert(lang("cli.launch.linux_error")); + win().alert(lang("cli.launch.linux_error")); console.error(lang("cli.launch.linux_error")); cli.exit(1); return; diff --git a/src/modules/mods.js b/src/modules/mods.js index 2e9288b..77436a2 100644 --- a/src/modules/mods.js +++ b/src/modules/mods.js @@ -6,15 +6,14 @@ const { https } = require("follow-redirects"); const { app, ipcMain, dialog } = require("electron"); const json = require("./json"); -const win = require("./window"); const version = require("./version"); const settings = require("./settings"); console = require("./console"); +const win = require("../win"); const cli = require("../cli"); const lang = require("../lang"); -const main_win = require("../win"); var mods = { installing: [], @@ -35,7 +34,7 @@ ipcMain.on("install-from-path", (event, path) => { }) ipcMain.on("no-internet", () => { - main_win().send("no-internet"); + win().send("no-internet"); }) ipcMain.on("install-mod", () => { @@ -46,7 +45,7 @@ ipcMain.on("install-mod", () => { if (res.filePaths.length != 0) { mods.install(res.filePaths[0]); } else { - main_win().send("set-buttons", true); + win().send("set-buttons", true); } }).catch(err => {error(err)}); } @@ -54,7 +53,7 @@ ipcMain.on("install-mod", () => { // sends installed mods info to renderer ipcMain.on("gui-getmods", (event, ...args) => { - main_win().send("mods", mods.list()); + win().send("mods", mods.list()); }) ipcMain.on("getmods", () => { @@ -94,7 +93,7 @@ mods.list = () => { // make sure Northstar is actually installed if (version.northstar() == "unknown") { - win.log(lang("general.not_installed")); + win().log(lang("general.not_installed")); console.error(lang("general.not_installed")); cli.exit(1); return false; @@ -240,7 +239,7 @@ mods.get = (mod) => { // make sure Northstar is actually installed if (version.northstar() == "unknown") { - win.log(lang("general.not_installed")); + win().log(lang("general.not_installed")); console.error(lang("general.not_installed")); cli.exit(1); return false; @@ -374,14 +373,14 @@ mods.install = (mod, opts) => { } if (version.northstar() == "unknown") { - win.log(lang("general.not_installed")); + win().log(lang("general.not_installed")); console.error(lang("general.not_installed")); cli.exit(1); return false; } let notamod = () => { - win.log(lang("gui.mods.not_a_mod")); + win().log(lang("gui.mods.not_a_mod")); console.error(lang("cli.mods.not_a_mod")); cli.exit(1); return false; @@ -391,7 +390,7 @@ mods.install = (mod, opts) => { console.ok(lang("cli.mods.installed")); cli.exit(); - win.log(lang("gui.mods.installedmod")); + win().log(lang("gui.mods.installedmod")); if (modname == "mods") { let manifest = path.join(app.getPath("userData"), "Archives/manifest.json"); @@ -401,26 +400,26 @@ mods.install = (mod, opts) => { } } - main_win().send("installed-mod", { + win().send("installed-mod", { name: modname, malformed: opts.malformed, }) - main_win().send("mods", mods.list()); + win().send("mods", mods.list()); return true; } if (! fs.existsSync(mod)) {return notamod()} if (fs.statSync(mod).isDirectory()) { - win.log(lang("gui.mods.installing")); + win().log(lang("gui.mods.installing")); files = fs.readdirSync(mod); if (fs.existsSync(path.join(mod, "mod.json")) && fs.statSync(path.join(mod, "mod.json")).isFile()) { if (! json(path.join(mod, "mod.json"))) { - main_win().send("failed-mod"); + win().send("failed-mod"); return notamod(); } @@ -435,13 +434,13 @@ mods.install = (mod, opts) => { copy(mod, copydest, (err) => { if (err) { - main_win().send("failed-mod"); + win().send("failed-mod"); return; } copy(opts.manifest_file, path.join(copydest, "manifest.json"), (err) => { if (err) { - main_win().send("failed-mod"); + win().send("failed-mod"); return; } @@ -471,7 +470,7 @@ mods.install = (mod, opts) => { while (mods.installing.includes(mod_name)) { if (! mods.dupe_msg_sent) { mods.dupe_msg_sent = true; - main_win().send("duped-mod", mod_name); + win().send("duped-mod", mod_name); } use_mod_name = true; @@ -502,7 +501,7 @@ mods.install = (mod, opts) => { return notamod(); } } else { - win.log(lang("gui.mods.extracting")); + win().log(lang("gui.mods.extracting")); let cache = path.join(app.getPath("userData"), "Archives"); if (fs.existsSync(cache)) { fs.rmSync(cache, {recursive: true}); @@ -550,7 +549,7 @@ mods.install = (mod, opts) => { } if (files.length == 0) { - main_win().send("failed-mod"); + win().send("failed-mod"); return notamod(); } } @@ -621,7 +620,7 @@ mods.remove = (mod) => { // make sure Northstar is actually installed if (version.northstar() == "unknown") { - win.log(lang("general.not_installed")); + win().log(lang("general.not_installed")); console.error(lang("general.not_installed")); cli.exit(1); return false; @@ -672,11 +671,11 @@ mods.remove = (mod) => { console.ok(lang("cli.mods.removed")); cli.exit(); - main_win().send("mods", mods.list()); // send updated list to renderer + win().send("mods", mods.list()); // send updated list to renderer // tell the renderer that the mod has been removed, along with // relevant info for it to properly update everything graphically - main_win().send("removed-mod", { + win().send("removed-mod", { name: mod.replace(/^.*(\\|\/|\:)/, ""), manifest_name: manifest_name }) @@ -693,7 +692,7 @@ mods.toggle = (mod, fork) => { // make sure Northstar is actually installed if (version.northstar() == "unknown") { - win.log(lang("general.not_installed")); + win().log(lang("general.not_installed")); console.error(lang("general.not_installed")); cli.exit(1); return false; @@ -720,7 +719,7 @@ mods.toggle = (mod, fork) => { } // send updated modlist to renderer - main_win().send("mods", mods.list()); + win().send("mods", mods.list()); } module.exports = mods; diff --git a/src/modules/packages.js b/src/modules/packages.js index 88e389a..fca0a44 100644 --- a/src/modules/packages.js +++ b/src/modules/packages.js @@ -4,11 +4,10 @@ const unzip = require("unzipper"); const { app, ipcMain } = require("electron"); const https = require("follow-redirects").https; +const win = require("../win"); const lang = require("../lang"); -const main_win = require("../win"); const json = require("./json"); -const win = require("./window"); const settings = require("./settings"); console = require("./console"); @@ -232,7 +231,7 @@ packages.install = async (url, author, package_name, version) => { // if the package has plugins, then we want to prompt the // user, and make absolutely certain that they do want to // install this package, as plugins have security concerns - let confirmation = await win.confirm( + let confirmation = await win().confirm( `${lang("gui.mods.confirm_plugins_title")} ${name} \n\n` + lang("gui.mods.confirm_plugins_description") ) @@ -244,7 +243,7 @@ packages.install = async (url, author, package_name, version) => { } break; default: - main_win().send("failed-mod", name); + win().send("failed-mod", name); // other unhandled error console.error( @@ -309,7 +308,7 @@ packages.install = async (url, author, package_name, version) => { let moved = packages.move(package_path); if (! moved) { - main_win().send("failed-mod", name); + win().send("failed-mod", name); console.error("Moving package failed:", name); cleanup(); @@ -317,7 +316,7 @@ packages.install = async (url, author, package_name, version) => { return false; } - main_win().send("installed-mod", { + win().send("installed-mod", { name: name, fancy_name: package_name }) diff --git a/src/modules/settings.js b/src/modules/settings.js index ae02c79..5449570 100644 --- a/src/modules/settings.js +++ b/src/modules/settings.js @@ -2,11 +2,11 @@ const fs = require("fs"); const path = require("path"); const { app, ipcMain } = require("electron"); -const json = require("./json"); +const win = require("../win"); const lang = require("../lang"); -const win = require("./window"); console = require("./console"); +const json = require("./json"); var invalid_settings = false; @@ -15,7 +15,7 @@ ipcMain.on("save-settings", (event, obj) => { }) ipcMain.on("reset-config", async () => { - let confirmation = await win.confirm( + let confirmation = await win().confirm( lang("gui.settings.miscbuttons.reset_config_alert") ) diff --git a/src/modules/update.js b/src/modules/update.js index 1165622..3def83b 100644 --- a/src/modules/update.js +++ b/src/modules/update.js @@ -3,11 +3,10 @@ const fs = require("fs-extra"); const { autoUpdater } = require("electron-updater"); const { app, ipcMain, Notification } = require("electron"); +const win = require("../win"); const cli = require("../cli"); const lang = require("../lang"); -const main_win = require("../win"); -const win = require("./window"); const version = require("./version"); const settings = require("./settings"); const releases = require("./releases"); @@ -36,7 +35,7 @@ ipcMain.on("delete-install-cache", () => { ipcMain.on("update-northstar", async (e, force_install) => { if (await is_running.game()) { - return win.alert(lang("general.auto_updates.game_running")); + return win().alert(lang("general.auto_updates.game_running")); } update.northstar(force_install); @@ -44,7 +43,7 @@ ipcMain.on("update-northstar", async (e, force_install) => { // inform renderer that an update has been downloaded autoUpdater.on("update-downloaded", () => { - main_win().send("update-available"); + win().send("update-available"); }) // updates and restarts Viper, if user says yes to do so. @@ -66,7 +65,7 @@ ipcMain.on("can-autoupdate", () => { // if `update_active` is falsy or `--no-vp-updates` is set, // inform the renderer that auto updates aren't possible if (! update_active || cli.hasParam("no-vp-updates")) { - main_win().send("cant-autoupdate"); + win().send("cant-autoupdate"); } }) @@ -271,7 +270,7 @@ update.northstar = async (force_install) => { return; } - main_win().send("ns-update-event", "cli.update.checking"); + win().send("ns-update-event", "cli.update.checking"); console.info(lang("cli.update.checking")); let ns_version = version.northstar(); @@ -279,16 +278,16 @@ update.northstar = async (force_install) => { if (latest && latest.version == false) { update.northstar.updating = false; - main_win().send("ns-update-event", "cli.update.noInternet"); + win().send("ns-update-event", "cli.update.noInternet"); return; } // Makes sure it is not already the latest version if (! force_install && ! await northstar_update_available()) { - main_win().send("ns-update-event", "cli.update.uptodate_short"); + win().send("ns-update-event", "cli.update.uptodate_short"); console.ok(lang("cli.update.uptodate").replace("%s", ns_version)); - win.log(lang("gui.update.uptodate")); + win().log(lang("gui.update.uptodate")); update.northstar.updating = false; @@ -306,7 +305,7 @@ update.northstar = async (force_install) => { https.get(latest.download_link, (res) => { // cancel out if zip can't be retrieved and or found if (res.statusCode !== 200) { - main_win().send("ns-update-event", "cli.update.uptodate_short"); + win().send("ns-update-event", "cli.update.uptodate_short"); console.ok(lang("cli.update.uptodate"), ns_version); update.northstar.updating = false; return false; @@ -331,7 +330,7 @@ update.northstar = async (force_install) => { } console.info(lang("cli.update.downloading") + ":", latest.version); - main_win().send("ns-update-event", { + win().send("ns-update-event", { progress: 0, btn_text: "1/2", key: "cli.update.downloading", @@ -369,7 +368,7 @@ update.northstar = async (force_install) => { percentage_str = " - " + current_percentage + "%"; } - main_win().send("ns-update-event", { + win().send("ns-update-event", { key: key, progress: current_percentage, btn_text: "1/2" + percentage_str @@ -382,8 +381,8 @@ update.northstar = async (force_install) => { stream.close(); let extract = fs.createReadStream(settings().zip); - win.log(lang("gui.update.extracting")); - main_win().send("ns-update-event", { + win().log(lang("gui.update.extracting")); + win().send("ns-update-event", { progress: 0, btn_text: "2/2 - 0%", key: lang("gui.update.extracting") @@ -404,7 +403,7 @@ update.northstar = async (force_install) => { let percent = Math.floor(extracted / size * 100); let extracted_mb = (extracted / 1024 / 1024).toFixed(1); - main_win().send("ns-update-event", { + win().send("ns-update-event", { progress: percent, btn_text: "2/2 - " + percent + "%", key: lang("gui.update.extracting") + @@ -420,8 +419,8 @@ update.northstar = async (force_install) => { ipcMain.emit("gui-getmods"); ipcMain.emit("get-version"); - main_win().send("ns-update-event", "cli.update.uptodate_short"); - win.log(lang("gui.update.finished")); + win().send("ns-update-event", "cli.update.uptodate_short"); + win().log(lang("gui.update.finished")); console.ok(lang("cli.update.finished")); update.northstar.updating = false; diff --git a/src/modules/window.js b/src/modules/window.js deleted file mode 100644 index ebb0165..0000000 --- a/src/modules/window.js +++ /dev/null @@ -1,51 +0,0 @@ -const main_win = require("../win"); -const ipcMain = require("electron").ipcMain; - -let win = {}; - -// logs into the dev tools of the renderer -win.log = (...args) => { - main_win().send("log", ...args); -} - -// this increments for every alert that's created, the ID is used to -// keep track of popups being opened or closed. -let alert_id = 0; - -// sends an alert to the renderer -win.alert = (msg) => { - alert_id++; - - return new Promise((resolve) => { - ipcMain.once(`alert-closed-${alert_id}`, () => { - resolve(); - }) - - main_win().send("alert", { - id: alert_id, - message: msg - }) - }) -} - -// this increments for every confirm alert that's created, the ID is -// used to keep track of popups being opened or closed. -let confirm_id = 0; - -// sends an alert to the renderer -win.confirm = (msg) => { - confirm_id++; - - return new Promise((resolve) => { - ipcMain.once(`confirm-closed-${confirm_id}`, (event, confirmed) => { - resolve(confirmed); - }) - - main_win().send("confirm", { - message: msg, - id: confirm_id - }) - }) -} - -module.exports = win; diff --git a/src/win.js b/src/win.js index 056dee8..9bec3c9 100644 --- a/src/win.js +++ b/src/win.js @@ -1,5 +1,56 @@ +const ipcMain = require("electron").ipcMain; + +// logs into the dev tools of the renderer +log = (...args) => { + win.send("log", ...args); +} + +// this increments for every alert that's created, the ID is used to +// keep track of popups being opened or closed. +let alert_id = 0; + +// sends an alert to the renderer +alert = async (msg) => { + alert_id++; + + return new Promise((resolve) => { + ipcMain.once(`alert-closed-${alert_id}`, () => { + resolve(); + }) + + win.send("alert", { + id: alert_id, + message: msg + }) + }) +} + +// this increments for every confirm alert that's created, the ID is +// used to keep track of popups being opened or closed. +let confirm_id = 0; + +// sends an alert to the renderer +confirm = async (msg) => { + confirm_id++; + + return new Promise((resolve) => { + ipcMain.once(`confirm-closed-${confirm_id}`, (event, confirmed) => { + resolve(confirmed); + }) + + win.send("confirm", { + message: msg, + id: confirm_id + }) + }) +} + let win = { - send: () => {} + send: () => {}, + + log: log, + alert: alert, + confirm: confirm } let func = () => { @@ -8,6 +59,10 @@ let func = () => { func.set = (main_window) => { win = main_window; + + win.log = log; + win.alert = alert; + win.confirm = confirm; } module.exports = func; -- cgit v1.2.3