aboutsummaryrefslogtreecommitdiff
path: root/src/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/index.js')
-rw-r--r--src/index.js156
1 files changed, 86 insertions, 70 deletions
diff --git a/src/index.js b/src/index.js
index e718ef9..96976b7 100644
--- a/src/index.js
+++ b/src/index.js
@@ -3,30 +3,30 @@ const path = require("path");
const { autoUpdater } = require("electron-updater");
const { app, ipcMain, BrowserWindow, dialog } = require("electron");
-const Emitter = require("events");
-const events = new Emitter();
-
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() {
win = new BrowserWindow({
width: 1000,
height: 600,
+ title: "Viper",
// Hides the window initially, it'll be shown when the DOM is
// loaded, as to not cause visual issues.
show: false,
- title: "Viper",
// In the future we may want to allow the user to resize the window,
// as it's fairly responsive, but for now we won't allow that.
resizable: false,
- titleBarStyle: "hidden",
+
frame: false,
+ titleBarStyle: "hidden",
icon: path.join(__dirname, "assets/icons/512x512.png"),
webPreferences: {
webviewTag: true,
@@ -35,47 +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("win-log", (event, ...args) => {send("log", ...args)});
+ ipcMain.on("win-alert", (event, ...args) => {send("alert", ...args)});
- ipcMain.on("exit", () => {process.exit(0)})
- ipcMain.on("minimize", () => {win.minimize()})
- ipcMain.on("relaunch", () => {app.relaunch();app.exit()})
- 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())});
+ // mod states
+ ipcMain.on("failed-mod", (event, modname) => {send("failed-mod", modname)});
+ ipcMain.on("removed-mod", (event, modname) => {send("removed-mod", modname)});
+ ipcMain.on("gui-getmods", (event, ...args) => {send("mods", utils.mods.list())});
+ ipcMain.on("installed-mod", (event, modname) => {send("installed-mod", modname)});
+ // install calls
+ ipcMain.on("install-from-path", (event, path) => {utils.mods.install(path)});
+ ipcMain.on("install-from-url", (event, url) => {utils.mods.installFromURL(url)});
+
+ 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) => {
+ ipcMain.on("gamepath-lost", (event, ...args) => {
if (! gamepathlost) {
gamepathlost = true;
- win.webContents.send("gamepathlost");
+ send("gamepath-lost");
}
});
- ipcMain.on("savesettings", (event, obj) => {utils.saveSettings(obj)})
+ ipcMain.on("save-settings", (event, obj) => {utils.saveSettings(obj)});
- ipcMain.on("can-autoupdate", (event) => {
- if (! require("electron-updater").autoUpdater.isUpdaterActive() || cli.hasParam("no-vp-updates")) {
- win.webContents.send("cant-autoupdate")
+ // 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")) {
+ 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();
@@ -87,41 +99,42 @@ function start() {
}
autoUpdater.on("update-downloaded", () => {
- win.webContents.send("updateavailable")
+ send("update-available");
});
- // Updates and restarts Viper, if user says yes to do so.
- // Otherwise it'll do it on the next start up.
- ipcMain.on("updatenow", () => {
+ // updates and restarts Viper, if user says yes to do so.
+ // otherwise it'll do it on the next start up.
+ ipcMain.on("update-now", () => {
autoUpdater.quitAndInstall();
})
}
// General events used to handle utils.js stuff without requiring the
// module inside the file that sent the event. {
-ipcMain.on("installmod", () => {
+ipcMain.on("install-mod", () => {
if (cli.hasArgs()) {
- utils.mods.install(cli.param("installmod"))
+ utils.mods.install(cli.param("installmod"));
} else {
dialog.showOpenDialog({properties: ["openFile"]}).then(res => {
if (res.filePaths.length != 0) {
utils.mods.install(res.filePaths[0]);
} else {
- win.webContents.send("setbuttons", true);
+ send("set-buttons", true);
}
- }).catch(err => {console.error(err)})
+ }).catch(err => {error(err)});
}
})
-ipcMain.on("removemod", (event, mod) => {utils.mods.remove(mod)})
-ipcMain.on("togglemod", (event, mod) => {utils.mods.toggle(mod)})
+ipcMain.on("remove-mod", (event, mod) => {utils.mods.remove(mod)});
+ipcMain.on("toggle-mod", (event, mod) => {utils.mods.toggle(mod)});
+
+ipcMain.on("launch-ns", () => {utils.launch()});
+ipcMain.on("launch-vanilla", () => {utils.launch("vanilla")});
-ipcMain.on("launch", (event) => {utils.launch()})
-ipcMain.on("setlang", (event, lang) => {utils.setlang(lang)})
-ipcMain.on("launchVanilla", (event) => {utils.launch("vanilla")})
+ipcMain.on("setlang", (event, lang) => {utils.setlang(lang)});
-ipcMain.on("update", (event) => {utils.update()})
-ipcMain.on("setpathcli", (event) => {utils.setpath()});
+ipcMain.on("update", () => {utils.update()})
+ipcMain.on("setpath-cli", () => {utils.setpath()});
ipcMain.on("setpath", (event, value) => {
if (! value) {
if (! win.isVisible()) {
@@ -134,66 +147,69 @@ 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("get-version", () => {sendVersionsInfo()});
-// 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);
+// prints out version info for the CLI
+ipcMain.on("version-cli", () => {
+ 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", (event) => {
+// sends installed mods info to renderer
+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);
}
})
// }
+// allows renderer to set a new renderer
ipcMain.on("newpath", (event, newpath) => {
- if (newpath === false && !win.isVisible()) {
- win.webContents.send("nopathselected");
+ if (newpath === false && ! win.isVisible()) {
+ win.send("no-path-selected");
} else {
_sendVersionsInfo();
if (!win.isVisible()) {
win.show();
}
}
-}); ipcMain.on("wrongpath", (event) => {
- win.webContents.send("wrongpath");
+}); ipcMain.on("wrong-path", () => {
+ win.send("wrong-path");
});
-// 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);
@@ -207,11 +223,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());
});