aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author0neGal <mail@0negal.com>2022-05-30 22:25:30 +0200
committer0neGal <mail@0negal.com>2022-05-30 22:25:30 +0200
commit451f1709cbdf0caa20c01ec533b96832ba9aa822 (patch)
tree18937a96c2a81cb076f42a70e98403be37eafa00 /src
parent7ddfd200abad2de370d49f6a58862a71c148111d (diff)
downloadViper-451f1709cbdf0caa20c01ec533b96832ba9aa822.tar.gz
Viper-451f1709cbdf0caa20c01ec533b96832ba9aa822.zip
simplify and comment index.js
Diffstat (limited to 'src')
-rw-r--r--src/index.js100
1 files changed, 58 insertions, 42 deletions
diff --git a/src/index.js b/src/index.js
index 259d8ad..37ea11e 100644
--- a/src/index.js
+++ b/src/index.js
@@ -7,6 +7,8 @@ const utils = require("./utils");
const cli = require("./cli");
const requests = require("./extras/requests");
+var log = console.log;
+
// Starts the actual BrowserWindow, which is only run when using the
// GUI, for the CLI this function is never called.
function start() {
@@ -33,46 +35,59 @@ function start() {
},
});
- // When --debug is added it'll open the dev tools
+ // when --debug is added it'll open the dev tools
if (cli.hasParam("debug")) {win.openDevTools()}
- // General setup
+ // general setup
win.removeMenu();
win.loadFile(__dirname + "/app/index.html");
+ win.send = (channel, data) => {
+ win.webContents.send(channel, data);
+ }; send = win.send;
+
ipcMain.on("exit", () => {process.exit(0)});
ipcMain.on("minimize", () => {win.minimize()});
ipcMain.on("relaunch", () => {app.relaunch();app.exit()});
+
+ // passthrough to renderer from main
+ ipcMain.on("winLog", (event, ...args) => {send("log", ...args)});
+ ipcMain.on("winAlert", (event, ...args) => {send("alert", ...args)});
+
+ // mod states
+ ipcMain.on("failedmod", (event, modname) => {send("failedmod", modname)});
+ ipcMain.on("removedmod", (event, modname) => {send("removedmod", modname)});
+ ipcMain.on("guigetmods", (event, ...args) => {send("mods", utils.mods.list())});
+ ipcMain.on("installedmod", (event, modname) => {send("installedmod", modname)});
+
+ // install calls
ipcMain.on("installfrompath", (event, path) => {utils.mods.install(path)});
ipcMain.on("installfromurl", (event, url) => {utils.mods.installFromURL(url)});
- ipcMain.on("winLog", (event, ...args) => {win.webContents.send("log", ...args)});
- ipcMain.on("winAlert", (event, ...args) => {win.webContents.send("alert", ...args)});
- ipcMain.on("ns-update-event", (event) => win.webContents.send("ns-update-event", event));
- ipcMain.on("failedmod", (event, modname) => {win.webContents.send("failedmod", modname)});
- ipcMain.on("removedmod", (event, modname) => {win.webContents.send("removedmod", modname)});
- ipcMain.on("installedmod", (event, modname) => {win.webContents.send("installedmod", modname)});
- ipcMain.on("guigetmods", (event, ...args) => {win.webContents.send("mods", utils.mods.list())});
+ win.webContents.on("dom-ready", () => {
+ send("mods", utils.mods.list());
+ });
+
+ // ensures gamepath still exists and is valid on startup
let gamepathlost = false;
ipcMain.on("gamepathlost", (event, ...args) => {
if (! gamepathlost) {
gamepathlost = true;
- win.webContents.send("gamepathlost");
+ send("gamepathlost");
}
});
ipcMain.on("savesettings", (event, obj) => {utils.saveSettings(obj)});
+ // allows renderer to check for updates
+ ipcMain.on("ns-update-event", (event) => {send("ns-update-event", event)});
ipcMain.on("can-autoupdate", () => {
if (! autoUpdater.isUpdaterActive() || cli.hasParam("no-vp-updates")) {
- win.webContents.send("cant-autoupdate");
+ send("cant-autoupdate");
}
})
- win.webContents.on("dom-ready", () => {
- win.webContents.send("mods", utils.mods.list());
- });
-
+ // start auto-update process
if (utils.settings.autoupdate) {
if (cli.hasParam("no-vp-updates")) {
utils.handleNorthstarUpdating();
@@ -84,11 +99,11 @@ function start() {
}
autoUpdater.on("update-downloaded", () => {
- win.webContents.send("updateavailable");
+ send("updateavailable");
});
- // Updates and restarts Viper, if user says yes to do so.
- // Otherwise it'll do it on the next start up.
+ // updates and restarts Viper, if user says yes to do so.
+ // otherwise it'll do it on the next start up.
ipcMain.on("updatenow", () => {
autoUpdater.quitAndInstall();
})
@@ -104,9 +119,9 @@ ipcMain.on("installmod", () => {
if (res.filePaths.length != 0) {
utils.mods.install(res.filePaths[0]);
} else {
- win.webContents.send("setbuttons", true);
+ send("setbuttons", true);
}
- }).catch(err => {console.error(err)});
+ }).catch(err => {error(err)});
}
})
@@ -132,44 +147,45 @@ ipcMain.on("setpath", (event, value) => {
}
});
-function _sendVersionsInfo() {
- win.webContents.send("version", {
+// retrieves various local version numbers
+function sendVersionsInfo() {
+ send("version", {
ns: utils.getNSVersion(),
tf2: utils.getTF2Version(),
vp: "v" + require("../package.json").version
});
}
-// Sends the version info back to the renderer
-ipcMain.on("getversion", () => {_sendVersionsInfo()});
+// sends the version info back to the renderer
+ipcMain.on("getversion", () => {sendVersionsInfo()});
-// Prints out version info for the CLI
+// prints out version info for the CLI
ipcMain.on("versioncli", () => {
- console.log("Viper: v" + require("../package.json").version);
- console.log("Northstar: " + utils.getNSVersion());
- console.log("Node: " + process.version);
- console.log("Electron: v" + process.versions.electron);
+ log("Viper: v" + require("../package.json").version);
+ log("Northstar: " + utils.getNSVersion());
+ log("Node: " + process.version);
+ log("Electron: v" + process.versions.electron);
cli.exit();
})
ipcMain.on("getmods", () => {
let mods = utils.mods.list();
if (mods.all.length > 0) {
- console.log(`${utils.lang("general.mods.installed")} ${mods.all.length}`);
- console.log(`${utils.lang("general.mods.enabled")} ${mods.enabled.length}`);
+ log(`${utils.lang("general.mods.installed")} ${mods.all.length}`);
+ log(`${utils.lang("general.mods.enabled")} ${mods.enabled.length}`);
for (let i = 0; i < mods.enabled.length; i++) {
- console.log(` ${mods.enabled[i].Name} ${mods.enabled[i].Version}`);
+ log(` ${mods.enabled[i].Name} ${mods.enabled[i].Version}`);
}
if (mods.disabled.length > 0) {
- console.log(`${utils.lang("general.mods.disabled")} ${mods.disabled.length}`);
+ log(`${utils.lang("general.mods.disabled")} ${mods.disabled.length}`);
for (let i = 0; i < mods.disabled.length; i++) {
- console.log(` ${mods.disabled[i].Name} ${mods.disabled[i].Version}`);
+ log(` ${mods.disabled[i].Name} ${mods.disabled[i].Version}`);
}
}
cli.exit(0);
} else {
- console.log("No mods installed");
+ log("No mods installed");
cli.exit(0);
}
})
@@ -177,7 +193,7 @@ ipcMain.on("getmods", () => {
ipcMain.on("newpath", (event, newpath) => {
if (newpath === false && !win.isVisible()) {
- win.webContents.send("nopathselected");
+ win.send("nopathselected");
} else {
_sendVersionsInfo();
if (!win.isVisible()) {
@@ -185,13 +201,13 @@ ipcMain.on("newpath", (event, newpath) => {
}
}
}); ipcMain.on("wrongpath", () => {
- win.webContents.send("wrongpath");
+ win.send("wrongpath");
});
-// Ensures ./ is the config folder where viper.json is located.
+// ensures PWD/CWD is the config folder where viper.json is located
process.chdir(app.getPath("appData"));
-// Starts the GUI or CLI
+// starts the GUI or CLI
if (cli.hasArgs()) {
if (cli.hasParam("updatevp")) {
utils.updatevp(true);
@@ -205,11 +221,11 @@ if (cli.hasArgs()) {
})
}
-// Returns cached requests
+// returns cached requests
ipcMain.on("get-ns-notes", async () => {
- win.webContents.send("ns-notes", await requests.getNsReleaseNotes());
+ win.send("ns-notes", await requests.getNsReleaseNotes());
});
ipcMain.on("get-vp-notes", async () => {
- win.webContents.send("vp-notes", await requests.getVpReleaseNotes());
+ win.send("vp-notes", await requests.getVpReleaseNotes());
});