aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author0neGal <mail@0negal.com>2023-01-12 19:13:38 +0100
committer0neGal <mail@0negal.com>2023-01-12 19:13:38 +0100
commita67040498d238c3d7b1b947f7be02034f3b71d52 (patch)
treeefcc16b14240fc56748bf558549c192c1594df1a
parentf81558d552e5e3f98438a3fe94290a96041a443a (diff)
downloadViper-a67040498d238c3d7b1b947f7be02034f3b71d52.tar.gz
Viper-a67040498d238c3d7b1b947f7be02034f3b71d52.zip
modularize settings Object
The reasoning behind this is obvious, I overall would like to make utils.js far smaller, and if not get entirely rid of it.
-rw-r--r--src/index.js13
-rw-r--r--src/modules/settings.js72
-rw-r--r--src/utils.js76
3 files changed, 83 insertions, 78 deletions
diff --git a/src/index.js b/src/index.js
index 4ff774a..c3d3556 100644
--- a/src/index.js
+++ b/src/index.js
@@ -3,8 +3,12 @@ const path = require("path");
const { autoUpdater } = require("electron-updater");
const { app, ipcMain, BrowserWindow, dialog } = require("electron");
+// ensures PWD/CWD is the config folder where viper.json is located
+process.chdir(app.getPath("appData"));
+
const utils = require("./utils");
const cli = require("./cli");
+const settings = require("./modules/settings");
const requests = require("./modules/requests");
var log = console.log;
@@ -47,7 +51,7 @@ function start() {
}; send = win.send;
ipcMain.on("exit", () => {
- if (utils.settings.originkill) {
+ if (settings.originkill) {
utils.isOriginRunning().then((running) => {
if (running) {
utils.killOrigin().then(process.exit(0))
@@ -96,7 +100,7 @@ function start() {
}
});
- ipcMain.on("save-settings", (event, obj) => {utils.saveSettings(obj)});
+ ipcMain.on("save-settings", (event, obj) => {settings.save(obj)});
// allows renderer to check for updates
ipcMain.on("ns-update-event", (event) => {send("ns-update-event", event)});
@@ -107,7 +111,7 @@ function start() {
})
// start auto-update process
- if (utils.settings.autoupdate) {
+ if (settings.autoupdate) {
if (cli.hasParam("no-vp-updates")) {
utils.handleNorthstarUpdating();
} else {
@@ -225,9 +229,6 @@ ipcMain.on("newpath", (event, newpath) => {
win.send("wrong-path");
});
-// ensures PWD/CWD 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("update-viper")) {
diff --git a/src/modules/settings.js b/src/modules/settings.js
new file mode 100644
index 0000000..a24f1e0
--- /dev/null
+++ b/src/modules/settings.js
@@ -0,0 +1,72 @@
+const fs = require("fs");
+const path = require("path");
+const app = require("electron").app;
+
+const lang = require("../lang");
+
+var invalid_settings = false;
+
+// Base settings
+var settings = {
+ gamepath: "",
+ lang: "en-US",
+ nsupdate: true,
+ autolang: true,
+ forcedlang: "en",
+ autoupdate: true,
+ originkill: false,
+ nsargs: "-multiple",
+ zip: "/northstar.zip",
+
+ // These files won't be overwritten when installing/updating
+ // Northstar, useful for config files
+ excludes: [
+ "ns_startup_args.txt",
+ "ns_startup_args_dedi.txt"
+ ]
+}
+
+// Creates the settings file with the base settings if it doesn't exist.
+if (fs.existsSync("viper.json")) {
+ let conf = fs.readFileSync("viper.json", "utf8");
+ let json = "{}";
+
+ // Validates viper.json
+ try {
+ json = JSON.parse(conf);
+ }catch (e) {
+ invalid_settings = true;
+ }
+
+ settings = {...settings, ...json};
+ settings.zip = path.join(settings.gamepath + "/northstar.zip");
+
+ let args = path.join(settings.gamepath, "ns_startup_args.txt");
+ if (fs.existsSync(args)) {
+ settings.nsargs = fs.readFileSync(args, "utf8");
+ }
+} else {
+ console.log(lang("general.missingpath"));
+}
+
+// As to not have to do the same one liner a million times, this
+// function exists, as the name suggests, it simply writes the current
+// settings to the disk.
+//
+// You can also pass a settings object to the function and it'll try and
+// merge it together with the already existing settings
+settings.save = (obj = {}) => {
+ if (invalid_settings) {return false}
+
+ let settings_content = {...settings, ...obj};
+
+ delete settings_content.save;
+
+ if (fs.existsSync(settings.gamepath)) {
+ fs.writeFileSync(path.join(settings.gamepath, "ns_startup_args.txt"), settings.nsargs);
+ }
+
+ fs.writeFileSync(app.getPath("appData") + "/viper.json", JSON.stringify({...settings, ...obj}));
+}
+
+module.exports = settings;
diff --git a/src/utils.js b/src/utils.js
index 647e089..e3e47e5 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -8,6 +8,7 @@ const events = new Emitter();
const cli = require("./cli");
const lang = require("./lang");
+const settings = require("./modules/settings");
const requests = require("./modules/requests");
const findgame = require("./modules/findgame");
@@ -16,30 +17,6 @@ const repair = require("jsonrepair");
const exec = require("child_process").exec;
const { https } = require("follow-redirects");
-process.chdir(app.getPath("appData"));
-
-var invalidsettings = false;
-
-// Base settings
-var settings = {
- gamepath: "",
- lang: "en-US",
- nsupdate: true,
- autolang: true,
- forcedlang: "en",
- autoupdate: true,
- originkill: false,
- nsargs: "-multiple",
- zip: "/northstar.zip",
-
- // These files won't be overwritten when installing/updating
- // Northstar, useful for config files
- excludes: [
- "ns_startup_args.txt",
- "ns_startup_args_dedi.txt"
- ]
-}
-
// Logs into the dev tools of the renderer
function winLog(msg) {
ipcMain.emit("win-log", msg, msg);
@@ -50,29 +27,6 @@ function winAlert(msg) {
ipcMain.emit("win-alert", msg, msg);
}
-// Creates the settings file with the base settings if it doesn't exist.
-if (fs.existsSync("viper.json")) {
- let conf = fs.readFileSync("viper.json", "utf8");
- let json = "{}";
-
- // Validates viper.json
- try {
- json = JSON.parse(conf);
- }catch (e) {
- invalidsettings = true;
- }
-
- settings = {...settings, ...json};
- settings.zip = path.join(settings.gamepath + "/northstar.zip");
-
- let args = path.join(settings.gamepath, "ns_startup_args.txt");
- if (fs.existsSync(args)) {
- settings.nsargs = fs.readFileSync(args, "utf8");
- }
-} else {
- console.log(lang("general.missingpath"));
-}
-
// A simple function that checks if the game is running, which we use to
// not update Northstar when it is running.
async function isGameRunning() {
@@ -194,7 +148,7 @@ async function setpath(win, forcedialog) {
function setGamepath(folder) {
settings.gamepath = folder;
settings.zip = path.join(settings.gamepath + "/northstar.zip");
- saveSettings();
+ settings.save();
win.webContents.send("newpath", settings.gamepath);
ipcMain.emit("newpath", null, settings.gamepath);
@@ -208,7 +162,7 @@ async function setpath(win, forcedialog) {
function setGamepath(folder, forcedialog) {
settings.gamepath = folder;
settings.zip = path.join(settings.gamepath + "/northstar.zip");
- saveSettings();
+ settings.save();
win.webContents.send("newpath", settings.gamepath);
ipcMain.emit("newpath", null, settings.gamepath);
}
@@ -241,24 +195,6 @@ async function setpath(win, forcedialog) {
}
}
-// As to not have to do the same one liner a million times, this
-// function exists, as the name suggests, it simply writes the current
-// settings to the disk.
-//
-// You can also pass a settings object to the function and it'll try and
-// merge it together with the already existing settings
-function saveSettings(obj = {}) {
- if (invalidsettings) {return false}
-
- settings = {...settings, ...obj};
-
- if (fs.existsSync(settings.gamepath)) {
- fs.writeFileSync(path.join(settings.gamepath, "ns_startup_args.txt"), settings.nsargs);
- }
-
- fs.writeFileSync(app.getPath("appData") + "/viper.json", JSON.stringify({...settings, ...obj}));
-}
-
// Returns the current Northstar version
// If not installed it'll return "unknown"
function getNSVersion() {
@@ -1052,16 +988,12 @@ module.exports = {
isGameRunning,
isOriginRunning,
-
- settings,
- saveSettings,
-
setpath,
gamepathExists,
lang,
setlang: (lang) => {
settings.lang = lang;
- saveSettings();
+ settings.save();
},
}