aboutsummaryrefslogtreecommitdiff
path: root/src/utils.js
diff options
context:
space:
mode:
author0neGal <mail@0negal.com>2022-01-12 14:33:08 +0100
committerGitHub <noreply@github.com>2022-01-12 14:33:08 +0100
commit9a135bfc554ccfddf64e71725b98bc4317d14732 (patch)
tree9d3ec166bf90e9b8ef91d835067b8bada347e997 /src/utils.js
parent9dbf85ca6655d8b14c8f0ad090a9b30e1ec951cf (diff)
parentdd2786488c445fdd8bcc62a0ad735a7715f93d57 (diff)
downloadViper-9a135bfc554ccfddf64e71725b98bc4317d14732.tar.gz
Viper-9a135bfc554ccfddf64e71725b98bc4317d14732.zip
Merge pull request #33 from Alystrasz/feat/northstar-auto-update
feat: Northstar auto-updates
Diffstat (limited to 'src/utils.js')
-rw-r--r--src/utils.js70
1 files changed, 66 insertions, 4 deletions
diff --git a/src/utils.js b/src/utils.js
index 68cd6c1..87499ae 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -1,7 +1,7 @@
const path = require("path");
const fs = require("fs-extra");
const copy = require("copy-dir");
-const { app, dialog, ipcMain } = require("electron");
+const { app, dialog, ipcMain, Notification } = require("electron");
const Emitter = require("events");
const events = new Emitter();
@@ -11,7 +11,8 @@ const lang = require("./lang");
const requests = require("./requests");
const unzip = require("unzipper");
-const exec = require("child_process").spawn;
+const run = require("child_process").spawn;
+const exec = require("child_process").exec;
const { https } = require("follow-redirects");
process.chdir(app.getPath("appData"));
@@ -34,6 +35,66 @@ if (fs.existsSync("viper.json")) {
console.log(lang("general.missingpath"));
}
+
+async function isGameRunning() {
+ return new Promise(resolve => {
+ let procs = ["Titanfall2.exe", "Titanfall2-unpacked.exe", "NorthstarLauncher.exe"];
+ let cmd = (() => {
+ switch (process.platform) {
+ case "linux": return "ps -A";
+ case "win32": return "tasklist";
+ }
+ })();
+
+ exec(cmd, (err, stdout) => {
+ for (let i = 0; i < procs.length; i++) {
+ if (stdout.includes(procs[i])) {
+ resolve(true);
+ break
+ }
+
+ if (i == procs.length - 1) {resolve(false)}
+ }
+ });
+ });
+}
+
+northstar_auto_updates: {
+ if (!settings.autoupdate || !fs.existsSync("viper.json") || settings.gamepath.length === 0) {
+ break northstar_auto_updates;
+ }
+
+ async function _checkForUpdates() {
+ let localVersion = getNSVersion();
+ let distantVersion = await requests.getLatestNsVersion();
+ console.log(lang("cli.autoupdates.checking"));
+
+ if (localVersion !== distantVersion) {
+ console.log(lang("cli.autoupdates.available"));
+ if (await isGameRunning()) {
+ console.log(lang("cli.autoupdates.gamerunning"));
+ new Notification({
+ title: lang("gui.nsupdate.gaming.title"),
+ body: lang("gui.nsupdate.gaming.body")
+ }).show();
+ } else {
+ console.log(lang("cli.autoupdates.updatingns"));
+ update();
+ }
+ } else {
+ console.log(lang("cli.autoupdates.noupdate"))
+ }
+
+ setTimeout(
+ _checkForUpdates,
+ 15 * 60 * 1000 // update checking interval must be bigger than cache validity duration
+ );
+ }
+
+ _checkForUpdates();
+}
+
+
function setpath(win) {
if (! win) {
settings.gamepath = cli.param("setpath");
@@ -164,11 +225,11 @@ function launch(version) {
switch(version) {
case "vanilla":
console.log(lang("general.launching"), "Vanilla...")
- exec(path.join(settings.gamepath + "/Titanfall2.exe"))
+ run(path.join(settings.gamepath + "/Titanfall2.exe"))
break;
default:
console.log(lang("general.launching"), "Northstar...")
- exec(path.join(settings.gamepath + "/NorthstarLauncher.exe"))
+ run(path.join(settings.gamepath + "/NorthstarLauncher.exe"))
break;
}
}
@@ -426,6 +487,7 @@ module.exports = {
updatevp,
settings,
getNSVersion,
+ isGameRunning,
setlang: (lang) => {
settings.lang = lang;
saveSettings();