aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/app/lang.js9
-rw-r--r--src/cli.js19
-rw-r--r--src/index.js49
-rw-r--r--src/lang.js22
4 files changed, 78 insertions, 21 deletions
diff --git a/src/app/lang.js b/src/app/lang.js
index 5cc9708..6fdcd8d 100644
--- a/src/app/lang.js
+++ b/src/app/lang.js
@@ -1,12 +1,19 @@
+// Replaces strings in the HTML will language strings properly. This
+// searches for %%<string>%%, aka, %%gui.exit%% will be replaced with
+// "Exit", this works without issues.
function setlang() {
+ // Finds %%%% strings
html = document.body.innerHTML.split("%%");
for (let i = 0; i < html.length; i++) {
+ // Simply checks to make sure it is actually a lang string.
if (html[i][0] != " " &&
html[i][html[i].length - 1] != " ") {
+ // Replaces it with it's string
html[i] = lang(html[i])
}
}
-
+
+ // Replaces the original HTML with the translated/replaced HTML
document.body.innerHTML = html.join("");
}
diff --git a/src/cli.js b/src/cli.js
index d5ee3ad..cef2295 100644
--- a/src/cli.js
+++ b/src/cli.js
@@ -8,6 +8,9 @@ const cli = app.commandLine;
const lang = require("./lang");
function hasArgs() {
+ // Makes sure the GUI isn't launched.
+ // TODO: Perhaps we should get a better way of doing this, at the
+ // very least we should use a switch case here.
if (cli.hasSwitch("cli") ||
cli.hasSwitch("help") ||
cli.hasSwitch("mods") ||
@@ -24,11 +27,20 @@ function hasArgs() {
} else {return false}
}
+// Exits the CLI, when run without CLI being on it'll do nothing, this
+// is needed as without even if no code is executed it'll continue to
+// run as Electron is still technically running.
function exit(code) {
if (hasArgs()) {process.exit(code)}
}
+// General CLI initialization
+//
+// A lot of the CLI is handled through events sent back to the main
+// process or utils.js to handle, this is because we re-use these events
+// for the renderer as well.
async function init() {
+ // --help menu/argument
if (cli.hasSwitch("help")) {
console.log(`options:
--help ${lang("cli.help.help")}
@@ -48,10 +60,14 @@ async function init() {
exit();
}
+ // --update
if (cli.hasSwitch("update")) {ipcMain.emit("update")}
+ // --version
if (cli.hasSwitch("version")) {ipcMain.emit("versioncli")}
+ // --setpath
if (cli.hasSwitch("setpath")) {
+ // Checks to verify the path is legitimate
if (cli.getSwitchValue("setpath") != "") {
ipcMain.emit("setpathcli", cli.getSwitchValue("setpath"));
} else {
@@ -60,6 +76,7 @@ async function init() {
}
}
+ // --launch
if (cli.hasSwitch("launch")) {
switch(cli.getSwitchValue("launch")) {
case "vanilla":
@@ -71,10 +88,12 @@ async function init() {
}
}
+ // Mod related args, --installmod, --removemod, --togglemod
if (cli.hasSwitch("installmod")) {ipcMain.emit("installmod")}
if (cli.hasSwitch("removemod")) {ipcMain.emit("removemod", "", cli.getSwitchValue("removemod"))}
if (cli.hasSwitch("togglemod")) {ipcMain.emit("togglemod", "", cli.getSwitchValue("togglemod"))}
+ // Prints out the list of mods
if (cli.hasSwitch("mods")) {ipcMain.emit("getmods")}
}
diff --git a/src/index.js b/src/index.js
index 333b1c9..98de132 100644
--- a/src/index.js
+++ b/src/index.js
@@ -10,12 +10,20 @@ const utils = require("./utils");
const cli = require("./cli");
const requests = require("./requests");
+// 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,
+
+ // 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,
@@ -26,8 +34,10 @@ function start() {
},
});
+ // When --debug is added it'll open the dev tools
if (cli.hasParam("debug")) {win.openDevTools()}
+ // General setup
win.removeMenu();
win.loadFile(__dirname + "/app/index.html");
@@ -47,11 +57,15 @@ function start() {
win.webContents.send("updateavailable")
});
+ // 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();
})
}
+// General events used to handle utils.js stuff without requiring the
+// module inside the file that sent the event. {
ipcMain.on("installmod", () => {
if (cli.hasArgs()) {
utils.mods.install(cli.param("installmod"))
@@ -79,19 +93,6 @@ ipcMain.on("setpath", (event, value) => {
}
});
-ipcMain.on("newpath", (event, newpath) => {
- if (newpath === false && !win.isVisible()) {
- win.webContents.send("nopathselected");
- } else {
- _sendVersionsInfo();
- if (!win.isVisible()) {
- win.show();
- }
- }
-}); ipcMain.on("wrongpath", (event) => {
- win.webContents.send("wrongpath");
-});
-
function _sendVersionsInfo() {
win.webContents.send("version", {
ns: utils.getNSVersion(),
@@ -100,8 +101,10 @@ function _sendVersionsInfo() {
});
}
-ipcMain.on("getversion", () => _sendVersionsInfo());
+// Sends the version info back to the renderer
+ipcMain.on("getversion", () => {_sendVersionsInfo()});
+// Prints out version info for the CLI
ipcMain.on("versioncli", () => {
console.log("Viper: v" + require("../package.json").version);
console.log("Northstar: " + utils.getNSVersion());
@@ -131,9 +134,25 @@ ipcMain.on("getmods", (event) => {
cli.exit(0);
}
})
+// }
+ipcMain.on("newpath", (event, newpath) => {
+ if (newpath === false && !win.isVisible()) {
+ win.webContents.send("nopathselected");
+ } else {
+ _sendVersionsInfo();
+ if (!win.isVisible()) {
+ win.show();
+ }
+ }
+}); ipcMain.on("wrongpath", (event) => {
+ win.webContents.send("wrongpath");
+});
+
+// Ensures ./ is the config folder where viper.json is located.
process.chdir(app.getPath("appData"));
+// Starts the GUI or CLI
if (cli.hasArgs()) {
if (cli.hasParam("updatevp")) {
utils.updatevp(true);
@@ -147,9 +166,11 @@ if (cli.hasArgs()) {
})
}
+// Returns cached requests
ipcMain.on("get-ns-notes", async () => {
win.webContents.send("ns-notes", await requests.getNsReleaseNotes());
});
+
ipcMain.on("get-vp-notes", async () => {
win.webContents.send("vp-notes", await requests.getVpReleaseNotes());
});
diff --git a/src/lang.js b/src/lang.js
index d404e8e..b3cbb43 100644
--- a/src/lang.js
+++ b/src/lang.js
@@ -1,15 +1,23 @@
const fs = require("fs");
-var lang = "en";
+var lang = "en"; // Default language
+
+// Loads fallback/default language strings
var langDef = JSON.parse(fs.readFileSync(__dirname + `/lang/en.json`, "utf8"));
+
+// If settins are set it'll try to set the language to that instead of
+// the default, however if it can't find it, it'll still fallback to the
+// default language. This might happen as the default language is
+// retrieved from the renderer's navigator.language, which may have
+// languages we don't support yet.
if (fs.existsSync("viper.json")) {
lang = JSON.parse(fs.readFileSync("viper.json", "utf8")).lang;
- if (! lang) {lang = "en"}
+ if (! lang) {lang = "en"} // Uses fallback, if language isn't set
if (! fs.existsSync(__dirname + `/lang/${lang}.json`)) {
if (fs.existsSync(__dirname + `/lang/${lang.replace(/-.*$/, "")}.json`)) {
lang = lang.replace(/-.*$/, "");
} else {
- lang = "en";
+ lang = "en"; // Uses fallback if language doesn't exist
}
}
}
@@ -17,12 +25,14 @@ if (fs.existsSync("viper.json")) {
var langObj = JSON.parse(fs.readFileSync(__dirname + `/lang/${lang}.json`, "utf8"));
module.exports = (string) => {
- if (langObj[string]) {
+ if (langObj[string]) { // Returns string from language
return langObj[string];
- } else {
- if (langDef[string]) {
+ } else { // If string doesn't exist
+ if (langDef[string]) { // Retrieves from default lang instead
return langDef[string];
} else {
+ // If it's not in the default lang either, it returns the
+ // string, this is absolute fallback.
return string;
}
}