From 31545f96ac56e0bbc7d0ed59f86015d693d464d9 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Mon, 16 May 2022 23:51:26 +0200 Subject: added mod preview browser Instead of opening your normal web browser you can now just open the page inside Viper, many changes aren't finished yet, notably the webview.css file. At some point I'll split the main.css into more files so the folder makes sense even though there's currently only one file in there. --- src/index.js | 1 + 1 file changed, 1 insertion(+) (limited to 'src/index.js') diff --git a/src/index.js b/src/index.js index 6c3c79c..e718ef9 100644 --- a/src/index.js +++ b/src/index.js @@ -29,6 +29,7 @@ function start() { frame: false, icon: path.join(__dirname, "assets/icons/512x512.png"), webPreferences: { + webviewTag: true, nodeIntegration: true, contextIsolation: false, }, -- cgit v1.2.3 From 7ddfd200abad2de370d49f6a58862a71c148111d Mon Sep 17 00:00:00 2001 From: 0neGal Date: Mon, 30 May 2022 21:53:41 +0200 Subject: fixed missing commas and various syntax stuff I know, commas aren't needed, however, going in and out of using commas and not using them also looks bad, so generally I try to always use them, with exceptions. --- src/app/browser.js | 35 ++++++++++----------- src/app/lang.js | 2 +- src/app/launcher.js | 9 ++++-- src/app/main.js | 29 +++++++++--------- src/app/settings.js | 12 ++++---- src/extras/findgame.js | 12 ++++---- src/index.js | 62 ++++++++++++++++++-------------------- src/utils.js | 82 +++++++++++++++++++++++++------------------------- 8 files changed, 123 insertions(+), 120 deletions(-) (limited to 'src/index.js') diff --git a/src/app/browser.js b/src/app/browser.js index a292cf6..72ab24e 100644 --- a/src/app/browser.js +++ b/src/app/browser.js @@ -18,7 +18,6 @@ var Browser = { } } - console.log(other) return pkgs; }, get: () => { @@ -88,8 +87,8 @@ var Browser = { toggle: (state) => { if (state) { browser.scrollTo(0, 0); - overlay.classList.add("shown") - browser.classList.add("shown") + overlay.classList.add("shown"); + browser.classList.add("shown"); if (browserEntries.querySelectorAll(".el").length == 0) { Browser.loadfront(); @@ -98,16 +97,16 @@ var Browser = { } else if (! state) { if (state != undefined) { Browser.filters.toggle(false); - overlay.classList.remove("shown") - browser.classList.remove("shown") - preview.classList.remove("shown") + overlay.classList.remove("shown"); + browser.classList.remove("shown"); + preview.classList.remove("shown"); return } } browser.scrollTo(0, 0); - overlay.classList.toggle("shown") - browser.classList.toggle("shown") + overlay.classList.toggle("shown"); + browser.classList.toggle("shown"); }, loadfront: async () => { Browser.loading(); @@ -162,11 +161,11 @@ var Browser = { } if (pkgs.length == 0 || isEnd) { - Browser.msg(`${lang('gui.browser.endoflist')}`) + Browser.msg(`${lang('gui.browser.endoflist')}`); return } - Browser.msg(``) + Browser.msg(``); loadmore.addEventListener("click", () => { Browser.loadpkgs(pkgs); Browser.endoflist(pkgs); @@ -177,7 +176,7 @@ var Browser = { let res = fuse.search(string); if (res.length < 1) { - Browser.loading(lang("gui.browser.noresults")) + Browser.loading(lang("gui.browser.noresults")); return } @@ -244,12 +243,11 @@ var Browser = { } Browser.endoflist(); - console.log(pkgs) break } try { - new BrowserElFromObj(pkgs[i]) + new BrowserElFromObj(pkgs[i]); }catch(e) {} count++; @@ -420,10 +418,12 @@ ipcRenderer.on("installedmod", (event, mod) => { function normalize(items) { let main = (string) => { - return string.replaceAll(" ", "").replaceAll(".", "").replaceAll("-", "").replaceAll("_", "").toLowerCase() + return string.replaceAll(" ", "") + .replaceAll(".", "").replaceAll("-", "") + .replaceAll("_", "").toLowerCase(); } if (typeof items == "string") { - return main(items) + return main(items); } else { let newArray = []; for (let i = 0; i < items.length; i++) { @@ -462,7 +462,6 @@ events.forEach((event) => { let mouseAt = document.elementsFromPoint(mouseX, mouseY); if (! mouseAt.includes(document.querySelector("#filter")) && ! mouseAt.includes(document.querySelector(".overlay"))) { - console.log(mouseAt) Browser.filters.toggle(false); } }) @@ -498,5 +497,7 @@ browser.addEventListener("mousemove", (event) => { let checks = document.querySelectorAll(".check"); for (let i = 0; i < checks.length; i++) { - checks[i].setAttribute("onclick", "this.classList.toggle('checked');Browser.loadfront();search.value = ''") + checks[i].setAttribute("onclick", + "this.classList.toggle('checked');Browser.loadfront();search.value = ''" + ) } diff --git a/src/app/lang.js b/src/app/lang.js index 6fdcd8d..f1c31d3 100644 --- a/src/app/lang.js +++ b/src/app/lang.js @@ -10,7 +10,7 @@ function setlang() { if (html[i][0] != " " && html[i][html[i].length - 1] != " ") { // Replaces it with it's string - html[i] = lang(html[i]) + html[i] = lang(html[i]); } } diff --git a/src/app/launcher.js b/src/app/launcher.js index 51d3a63..e0b56db 100644 --- a/src/app/launcher.js +++ b/src/app/launcher.js @@ -3,8 +3,8 @@ const markdown = require("marked").parse; // Changes the main page // This is the tabs in the sidebar function page(page) { - let pages = document.querySelectorAll(".mainContainer .contentContainer") - let btns = document.querySelectorAll(".gamesContainer button") + let btns = document.querySelectorAll(".gamesContainer button"); + let pages = document.querySelectorAll(".mainContainer .contentContainer"); for (let i = 0; i < pages.length; i++) { pages[i].classList.add("hidden"); @@ -82,7 +82,10 @@ function showVpSection(section) { } function showNsSection(section) { - if (!["main", "release", "mods"].includes(section)) throw new Error("unknown ns section"); + if (!["main", "release", "mods"].includes(section)) { + throw new Error("unknown ns section"); + } + nsMainBtn.removeAttribute("active"); nsModsBtn.removeAttribute("active"); nsReleaseBtn.removeAttribute("active"); diff --git a/src/app/main.js b/src/app/main.js index 362450a..d132c35 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -36,9 +36,9 @@ if (fs.existsSync("viper.json")) { }catch (e) { let reset = confirm(lang("general.invalidconfig", navigator.language) + e); if (! reset) { - ipcRenderer.send("exit") + ipcRenderer.send("exit"); } else { - fs.writeFileSync("viper.json", "{}") + fs.writeFileSync("viper.json", "{}"); ipcRenderer.send("relaunch"); } @@ -100,12 +100,15 @@ function setButtons(state) { } } - disablearray(document.querySelectorAll(".playBtnContainer .playBtn")) - disablearray(document.querySelectorAll("#nsMods .buttons.modbtns button")) - disablearray(document.querySelectorAll("#browser #browserEntries .text button")) + disablearray(document.querySelectorAll(".playBtnContainer .playBtn")); + disablearray(document.querySelectorAll("#nsMods .buttons.modbtns button")); + disablearray(document.querySelectorAll("#browser #browserEntries .text button")); } -ipcRenderer.on("setbuttons", (event, state) => {setButtons(state)}) +ipcRenderer.on("setbuttons", (event, state) => { + setButtons(state); +}) + ipcRenderer.on("gamepathlost", (event, state) => { page(0); setButtons(false); @@ -172,7 +175,7 @@ function selected(all) { } } - ipcRenderer.send("removemod", selected) + ipcRenderer.send("removemod", selected); }, toggle: () => { if (selected.match(/^Northstar\./)) { @@ -185,7 +188,7 @@ function selected(all) { } } - ipcRenderer.send("togglemod", selected) + ipcRenderer.send("togglemod", selected); } } } @@ -195,19 +198,18 @@ let installqueue = []; // Tells the main process to install a mod through the file selector function installmod() { setButtons(false); - ipcRenderer.send("installmod") + ipcRenderer.send("installmod"); } // Tells the main process to directly install a mod from this path function installFromPath(path) { setButtons(false); - ipcRenderer.send("installfrompath", path) + ipcRenderer.send("installfrompath", path); } // Tells the main process to install a mod from a URL function installFromURL(url, dependencies, clearqueue) { if (clearqueue) {installqueue = []}; - console.log(installqueue) let prettydepends = []; @@ -215,7 +217,6 @@ function installFromURL(url, dependencies, clearqueue) { let newdepends = []; for (let i = 0; i < dependencies.length; i++) { let depend = dependencies[i].toLowerCase(); - console.log(depend) if (! depend.match(/northstar-northstar-.*/)) { depend = dependencies[i].replaceAll("-", "/"); let pkg = depend.split("/"); @@ -237,7 +238,7 @@ function installFromURL(url, dependencies, clearqueue) { } setButtons(false); - ipcRenderer.send("installfromurl", url, dependencies) + ipcRenderer.send("installfromurl", url, dependencies); if (dependencies) { installqueue = dependencies; @@ -355,7 +356,7 @@ document.addEventListener("drop", (e) => { event.stopPropagation(); dragUI.classList.remove("shown"); - installFromPath(event.dataTransfer.files[0].path) + installFromPath(event.dataTransfer.files[0].path); }); document.body.addEventListener("keyup", (e) => { diff --git a/src/app/settings.js b/src/app/settings.js index 23b38c9..051d6a1 100644 --- a/src/app/settings.js +++ b/src/app/settings.js @@ -3,22 +3,22 @@ var Settings = { if (state) { Settings.load(); options.scrollTo(0, 0); - overlay.classList.add("shown") - options.classList.add("shown") + overlay.classList.add("shown"); + options.classList.add("shown"); return } else if (! state) { if (state != undefined) { - overlay.classList.remove("shown") - options.classList.remove("shown") + overlay.classList.remove("shown"); + options.classList.remove("shown"); return } } Settings.load(); options.scrollTo(0, 0); - overlay.classList.toggle("shown") - options.classList.toggle("shown") + overlay.classList.toggle("shown"); + options.classList.toggle("shown"); }, apply: () => { settings = {...settings, ...Settings.get()}; diff --git a/src/extras/findgame.js b/src/extras/findgame.js index 3beca23..615c5b4 100644 --- a/src/extras/findgame.js +++ b/src/extras/findgame.js @@ -38,13 +38,13 @@ module.exports = async () => { // `.length - 1` This is because the last value is `contentstatsid` for (let i = 0; i < values.length; i++) { - let data_array = Object.values(values[i]) + let data_array = Object.values(values[i]); if (fs.existsSync(data_array[0] + "/steamapps/common/Titanfall2/Titanfall2.exe")) { - console.log("Found game in:", data_array[0]) + console.log("Found game in:", data_array[0]); return data_array[0] + "/steamapps/common/Titanfall2"; } else { - console.log("Game not in:", data_array[0]) + console.log("Game not in:", data_array[0]); } } } @@ -69,10 +69,10 @@ module.exports = async () => { if (folders.length > 0) { for (let i = 0; i < folders.length; i++) { if (! fs.existsSync(folders[i])) {continue} - console.log("Searching VDF file at:", folders[i]) + console.log("Searching VDF file at:", folders[i]); - let data = fs.readFileSync(folders[i]) - let read_vdf = readvdf(data.toString()) + let data = fs.readFileSync(folders[i]); + let read_vdf = readvdf(data.toString()); if (read_vdf) {return read_vdf} } } diff --git a/src/index.js b/src/index.js index e718ef9..259d8ad 100644 --- a/src/index.js +++ b/src/index.js @@ -3,9 +3,6 @@ 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"); @@ -16,17 +13,18 @@ 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, @@ -42,12 +40,11 @@ function start() { win.removeMenu(); win.loadFile(__dirname + "/app/index.html"); - - 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("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)); @@ -64,11 +61,11 @@ function start() { } }); - ipcMain.on("savesettings", (event, obj) => {utils.saveSettings(obj)}) + ipcMain.on("savesettings", (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") + ipcMain.on("can-autoupdate", () => { + if (! autoUpdater.isUpdaterActive() || cli.hasParam("no-vp-updates")) { + win.webContents.send("cant-autoupdate"); } }) @@ -87,7 +84,7 @@ function start() { } autoUpdater.on("update-downloaded", () => { - win.webContents.send("updateavailable") + win.webContents.send("updateavailable"); }); // Updates and restarts Viper, if user says yes to do so. @@ -101,7 +98,7 @@ function start() { // module inside the file that sent the event. { ipcMain.on("installmod", () => { 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) { @@ -109,19 +106,20 @@ ipcMain.on("installmod", () => { } else { win.webContents.send("setbuttons", true); } - }).catch(err => {console.error(err)}) + }).catch(err => {console.error(err)}); } }) -ipcMain.on("removemod", (event, mod) => {utils.mods.remove(mod)}) -ipcMain.on("togglemod", (event, mod) => {utils.mods.toggle(mod)}) +ipcMain.on("removemod", (event, mod) => {utils.mods.remove(mod)}); +ipcMain.on("togglemod", (event, mod) => {utils.mods.toggle(mod)}); + +ipcMain.on("launch", () => {utils.launch()}); +ipcMain.on("launchVanilla", () => {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("setpathcli", () => {utils.setpath()}); ipcMain.on("setpath", (event, value) => { if (! value) { if (! win.isVisible()) { @@ -154,19 +152,19 @@ ipcMain.on("versioncli", () => { cli.exit(); }) -ipcMain.on("getmods", (event) => { +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}`) + console.log(`${utils.lang("general.mods.installed")} ${mods.all.length}`); + console.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}`) + console.log(` ${mods.enabled[i].Name} ${mods.enabled[i].Version}`); } if (mods.disabled.length > 0) { - console.log(`${utils.lang("general.mods.disabled")} ${mods.disabled.length}`) + console.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}`) + console.log(` ${mods.disabled[i].Name} ${mods.disabled[i].Version}`); } } cli.exit(0); @@ -186,7 +184,7 @@ ipcMain.on("newpath", (event, newpath) => { win.show(); } } -}); ipcMain.on("wrongpath", (event) => { +}); ipcMain.on("wrongpath", () => { win.webContents.send("wrongpath"); }); diff --git a/src/utils.js b/src/utils.js index de052f5..d8e968c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -129,7 +129,7 @@ function handleNorthstarUpdating() { update(); } } else { - console.log(lang("cli.autoupdates.noupdate")) + console.log(lang("cli.autoupdates.noupdate")); } setTimeout( @@ -191,7 +191,7 @@ async function setpath(win, forcedialog) { return; } - setGamepath(res.filePaths[0]) + setGamepath(res.filePaths[0]); cli.exit(); return; @@ -254,7 +254,7 @@ function restoreExcludedFiles() { for (let i = 0; i < settings.excludes.length; i++) { let exclude = path.join(settings.gamepath + "/" + settings.excludes[i]); if (fs.existsSync(exclude + ".excluded")) { - fs.renameSync(exclude + ".excluded", exclude) + fs.renameSync(exclude + ".excluded", exclude); } } } @@ -299,7 +299,7 @@ async function update() { for (let i = 0; i < settings.excludes.length; i++) { let exclude = path.join(settings.gamepath + "/" + settings.excludes[i]); if (fs.existsSync(exclude)) { - fs.renameSync(exclude, exclude + ".excluded") + fs.renameSync(exclude, exclude + ".excluded"); } } @@ -380,8 +380,8 @@ function updatevp(autoinstall) { // however it'll be added at some point. function launch(version) { if (process.platform == "linux") { - winAlert(lang("cli.launch.linuxerror")) - console.error("error:", lang("cli.launch.linuxerror")) + winAlert(lang("cli.launch.linuxerror")); + console.error("error:", lang("cli.launch.linuxerror")); cli.exit(1); return; } @@ -389,12 +389,12 @@ function launch(version) { process.chdir(settings.gamepath); switch(version) { case "vanilla": - console.log(lang("general.launching"), "Vanilla...") - run(path.join(settings.gamepath + "/Titanfall2.exe")) + console.log(lang("general.launching"), "Vanilla..."); + run(path.join(settings.gamepath + "/Titanfall2.exe")); break; default: - console.log(lang("general.launching"), "Northstar...") - run(path.join(settings.gamepath + "/NorthstarLauncher.exe")) + console.log(lang("general.launching"), "Northstar..."); + run(path.join(settings.gamepath + "/NorthstarLauncher.exe")); break; } } @@ -420,8 +420,8 @@ const mods = { let modpath = path.join(settings.gamepath, "R2Northstar/mods"); if (getNSVersion() == "unknown") { - winLog(lang("general.notinstalled")) - console.log("error: " + lang("general.notinstalled")) + winLog(lang("general.notinstalled")); + console.log("error: " + lang("general.notinstalled")); cli.exit(1); return false; } @@ -430,7 +430,7 @@ const mods = { let disabled = []; if (! fs.existsSync(modpath)) { - fs.mkdirSync(path.join(modpath), {recursive: true}) + fs.mkdirSync(path.join(modpath), {recursive: true}); return { enabled: [], disabled: [], @@ -438,7 +438,7 @@ const mods = { }; } - files = fs.readdirSync(modpath) + files = fs.readdirSync(modpath); files.forEach((file) => { if (fs.statSync(path.join(modpath, file)).isDirectory()) { let modjson = path.join(modpath, file, "mod.json"); @@ -489,8 +489,8 @@ const mods = { let modpath = path.join(settings.gamepath, "R2Northstar/mods"); if (getNSVersion() == "unknown") { - winLog(lang("general.notinstalled")) - console.log("error: " + lang("general.notinstalled")) + winLog(lang("general.notinstalled")); + console.log("error: " + lang("general.notinstalled")); cli.exit(1); return false; } @@ -515,11 +515,11 @@ const mods = { let file = path.join(modpath, "..", "enabledmods.json"); if (! fs.existsSync(modpath)) { - fs.mkdirSync(path.join(modpath), {recursive: true}) + fs.mkdirSync(path.join(modpath), {recursive: true}); } if (! fs.existsSync(file)) { - fs.writeFileSync(file, "{}") + fs.writeFileSync(file, "{}"); } return { @@ -530,7 +530,7 @@ const mods = { names[list[i].Name] = true } - fs.writeFileSync(file, JSON.stringify(names)) + fs.writeFileSync(file, JSON.stringify(names)); }, disable: (mod) => { let data = JSON.parse(repair(fs.readFileSync(file, "utf8"))); @@ -576,15 +576,15 @@ const mods = { let modname = mod.replace(/^.*(\\|\/|\:)/, ""); if (getNSVersion() == "unknown") { - winLog(lang("general.notinstalled")) - console.log("error: " + lang("general.notinstalled")) + winLog(lang("general.notinstalled")); + console.log("error: " + lang("general.notinstalled")); cli.exit(1); return false; } let notamod = () => { - winLog(lang("gui.mods.notamod")) - console.log("error: " + lang("cli.mods.notamod")) + winLog(lang("gui.mods.notamod")); + console.log("error: " + lang("cli.mods.notamod")); cli.exit(1); return false; } @@ -593,10 +593,10 @@ const mods = { console.log(lang("cli.mods.installed")); cli.exit(); - winLog(lang("gui.mods.installedmod")) + winLog(lang("gui.mods.installedmod")); if (modname == "mods") { - let manifest = path.join(app.getPath("userData"), "Archives/manifest.json") + let manifest = path.join(app.getPath("userData"), "Archives/manifest.json"); if (fs.existsSync(manifest)) { modname = require(manifest).name; @@ -614,7 +614,7 @@ const mods = { if (! fs.existsSync(mod)) {return notamod()} if (fs.statSync(mod).isDirectory()) { - winLog(lang("gui.mods.installing")) + winLog(lang("gui.mods.installing")); files = fs.readdirSync(mod); if (fs.existsSync(path.join(mod, "mod.json")) && fs.statSync(path.join(mod, "mod.json")).isFile()) { @@ -624,8 +624,8 @@ const mods = { } let copydest = path.join(modpath, modname); if (typeof destname == "string") {copydest = path.join(modpath, destname)} - copy(mod, copydest) - copy(manifestfile, path.join(copydest, "manifest.json")) + copy(mod, copydest); + copy(manifestfile, path.join(copydest, "manifest.json")); return installed(); } else { @@ -636,7 +636,7 @@ const mods = { if (fs.existsSync(path.join(mod, files[i], "mod.json")) && fs.statSync(path.join(mod, files[i], "mod.json")).isFile()) { - mods.install(path.join(mod, files[i])) + mods.install(path.join(mod, files[i])); if (mods.install(path.join(mod, files[i]))) {return true}; } } @@ -647,7 +647,7 @@ const mods = { return notamod(); } else { - winLog(lang("gui.mods.extracting")) + winLog(lang("gui.mods.extracting")); let cache = path.join(app.getPath("userData"), "Archives"); if (fs.existsSync(cache)) { fs.rmSync(cache, {recursive: true}); @@ -710,12 +710,12 @@ const mods = { if (fs.existsSync(tmp)) { if (! fs.statSync(tmp).isDirectory()) { - fs.rmSync(tmp) + fs.rmSync(tmp); } } else { - fs.mkdirSync(tmp) + fs.mkdirSync(tmp); if (fs.existsSync(modlocation)) { - fs.rmSync(modlocation) + fs.rmSync(modlocation); } } @@ -737,8 +737,8 @@ const mods = { let modpath = path.join(settings.gamepath, "R2Northstar/mods"); if (getNSVersion() == "unknown") { - winLog(lang("general.notinstalled")) - console.log("error: " + lang("general.notinstalled")) + winLog(lang("general.notinstalled")); + console.log("error: " + lang("general.notinstalled")); cli.exit(1); return false; } @@ -746,19 +746,19 @@ const mods = { if (mod == "allmods") { let modlist = mods.list().all; for (let i = 0; i < modlist.length; i++) { - mods.remove(modlist[i].Name) + mods.remove(modlist[i].Name); } return } let disabled = path.join(modpath, "disabled"); if (! fs.existsSync(disabled)) { - fs.mkdirSync(disabled) + fs.mkdirSync(disabled); } let modName = mods.get(mod).FolderName; if (! modName) { - console.log("error: " + lang("cli.mods.cantfind")) + console.log("error: " + lang("cli.mods.cantfind")); cli.exit(1); return; } @@ -796,8 +796,8 @@ const mods = { // function. However we currently have no need for that. toggle: (mod, fork) => { if (getNSVersion() == "unknown") { - winLog(lang("general.notinstalled")) - console.log("error: " + lang("general.notinstalled")) + winLog(lang("general.notinstalled")); + console.log("error: " + lang("general.notinstalled")); cli.exit(1); return false; } @@ -805,7 +805,7 @@ const mods = { if (mod == "allmods") { let modlist = mods.list().all; for (let i = 0; i < modlist.length; i++) { - mods.toggle(modlist[i].Name, true) + mods.toggle(modlist[i].Name, true); } console.log(lang("cli.mods.toggledall")); -- cgit v1.2.3 From 451f1709cbdf0caa20c01ec533b96832ba9aa822 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Mon, 30 May 2022 22:25:30 +0200 Subject: simplify and comment index.js --- src/index.js | 100 ++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 58 insertions(+), 42 deletions(-) (limited to 'src/index.js') 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()); }); -- cgit v1.2.3 From 1c06ab53f1cf2228e1034a66538f3a31a14e3743 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Mon, 30 May 2022 22:48:39 +0200 Subject: refactored event names Quite a lot of them aren't in the same syntax/style, and it's quite bad to look at, this should fix them all without causing issues. --- src/app/browser.js | 6 +++--- src/app/main.js | 30 ++++++++++++++--------------- src/app/settings.js | 2 +- src/index.js | 54 +++++++++++++++++++++++++++-------------------------- src/utils.js | 26 +++++++++++++------------- 5 files changed, 60 insertions(+), 58 deletions(-) (limited to 'src/index.js') diff --git a/src/app/browser.js b/src/app/browser.js index 72ab24e..4b9f2e3 100644 --- a/src/app/browser.js +++ b/src/app/browser.js @@ -373,7 +373,7 @@ function BrowserEl(properties) { browserEntries.appendChild(entry); } -ipcRenderer.on("removedmod", (event, mod) => { +ipcRenderer.on("removed-mod", (event, mod) => { setButtons(true); Browser.setbutton(mod.name, lang("gui.browser.install")); if (mod.manifestname) { @@ -381,7 +381,7 @@ ipcRenderer.on("removedmod", (event, mod) => { } }) -ipcRenderer.on("failedmod", (event, modname) => { +ipcRenderer.on("failed-mod", (event, modname) => { setButtons(true); new Toast({ timeout: 10000, @@ -391,7 +391,7 @@ ipcRenderer.on("failedmod", (event, modname) => { }) }) -ipcRenderer.on("installedmod", (event, mod) => { +ipcRenderer.on("installed-mod", (event, mod) => { setButtons(true); Browser.setbutton(mod.name, lang("gui.browser.reinstall")); diff --git a/src/app/main.js b/src/app/main.js index d132c35..647e5cd 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -76,12 +76,12 @@ function launch() { update(); shouldInstallNorthstar = false; } else { - ipcRenderer.send("launch"); + ipcRenderer.send("launch-ns"); } } // Tells the main process to launch the vanilla game -function launchVanilla() {ipcRenderer.send("launchVanilla")} +function launchVanilla() {ipcRenderer.send("launch-vanilla")} // In conjunction with utils.js' winLog(), it'll send log messages in // the devTools from utils.js @@ -105,11 +105,11 @@ function setButtons(state) { disablearray(document.querySelectorAll("#browser #browserEntries .text button")); } -ipcRenderer.on("setbuttons", (event, state) => { +ipcRenderer.on("set-buttons", (event, state) => { setButtons(state); }) -ipcRenderer.on("gamepathlost", (event, state) => { +ipcRenderer.on("gamepath-lost", (event, state) => { page(0); setButtons(false); alert(lang("gui.gamepath.lost")); @@ -175,7 +175,7 @@ function selected(all) { } } - ipcRenderer.send("removemod", selected); + ipcRenderer.send("remove-mod", selected); }, toggle: () => { if (selected.match(/^Northstar\./)) { @@ -188,7 +188,7 @@ function selected(all) { } } - ipcRenderer.send("togglemod", selected); + ipcRenderer.send("toggle-mod", selected); } } } @@ -198,13 +198,13 @@ let installqueue = []; // Tells the main process to install a mod through the file selector function installmod() { setButtons(false); - ipcRenderer.send("installmod"); + ipcRenderer.send("install-mod"); } // Tells the main process to directly install a mod from this path function installFromPath(path) { setButtons(false); - ipcRenderer.send("installfrompath", path); + ipcRenderer.send("install-from-path", path); } // Tells the main process to install a mod from a URL @@ -238,7 +238,7 @@ function installFromURL(url, dependencies, clearqueue) { } setButtons(false); - ipcRenderer.send("installfromurl", url, dependencies); + ipcRenderer.send("install-from-url", url, dependencies); if (dependencies) { installqueue = dependencies; @@ -263,7 +263,7 @@ function isModInstalled(modname) { // Frontend part of settings a new game path ipcRenderer.on("newpath", (event, newpath) => { settings.gamepath = newpath; - ipcRenderer.send("guigetmods"); + ipcRenderer.send("gui-getmods"); }) // Continuation of log() @@ -311,23 +311,23 @@ ipcRenderer.on("version", (event, versions) => { shouldInstallNorthstar = true; playNsBtn.innerText = lang("gui.installnorthstar"); } -}); ipcRenderer.send("getversion"); +}); ipcRenderer.send("get-version"); // When an update is available it'll ask the user about it -ipcRenderer.on("updateavailable", () => { +ipcRenderer.on("update-available", () => { if (confirm(lang("gui.update.available"))) { - ipcRenderer.send("updatenow"); + ipcRenderer.send("update-now"); } }) // Error out when no game path is set -ipcRenderer.on("nopathselected", () => { +ipcRenderer.on("no-path-selected", () => { alert(lang("gui.gamepath.must")); exit(); }); // Error out when game path is wrong -ipcRenderer.on("wrongpath", () => { +ipcRenderer.on("wrong-path", () => { alert(lang("gui.gamepath.wrong")); setpath(false); }); diff --git a/src/app/settings.js b/src/app/settings.js index 051d6a1..63b4b99 100644 --- a/src/app/settings.js +++ b/src/app/settings.js @@ -22,7 +22,7 @@ var Settings = { }, apply: () => { settings = {...settings, ...Settings.get()}; - ipcRenderer.send("savesettings", Settings.get()); + ipcRenderer.send("save-settings", Settings.get()); }, reloadSwitches: () => { let switches = document.querySelectorAll(".switch"); diff --git a/src/index.js b/src/index.js index 37ea11e..96976b7 100644 --- a/src/index.js +++ b/src/index.js @@ -51,18 +51,18 @@ function start() { 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)}); + ipcMain.on("win-log", (event, ...args) => {send("log", ...args)}); + ipcMain.on("win-alert", (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)}); + 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("installfrompath", (event, path) => {utils.mods.install(path)}); - ipcMain.on("installfromurl", (event, url) => {utils.mods.installFromURL(url)}); + 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()); @@ -70,14 +70,14 @@ function start() { // 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; - send("gamepathlost"); + send("gamepath-lost"); } }); - ipcMain.on("savesettings", (event, obj) => {utils.saveSettings(obj)}); + ipcMain.on("save-settings", (event, obj) => {utils.saveSettings(obj)}); // allows renderer to check for updates ipcMain.on("ns-update-event", (event) => {send("ns-update-event", event)}); @@ -99,19 +99,19 @@ function start() { } autoUpdater.on("update-downloaded", () => { - 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", () => { + 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")); } else { @@ -119,22 +119,22 @@ ipcMain.on("installmod", () => { if (res.filePaths.length != 0) { utils.mods.install(res.filePaths[0]); } else { - send("setbuttons", true); + send("set-buttons", true); } }).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", () => {utils.launch()}); -ipcMain.on("launchVanilla", () => {utils.launch("vanilla")}); +ipcMain.on("launch-ns", () => {utils.launch()}); +ipcMain.on("launch-vanilla", () => {utils.launch("vanilla")}); ipcMain.on("setlang", (event, lang) => {utils.setlang(lang)}); ipcMain.on("update", () => {utils.update()}) -ipcMain.on("setpathcli", () => {utils.setpath()}); +ipcMain.on("setpath-cli", () => {utils.setpath()}); ipcMain.on("setpath", (event, value) => { if (! value) { if (! win.isVisible()) { @@ -157,10 +157,10 @@ function sendVersionsInfo() { } // sends the version info back to the renderer -ipcMain.on("getversion", () => {sendVersionsInfo()}); +ipcMain.on("get-version", () => {sendVersionsInfo()}); // prints out version info for the CLI -ipcMain.on("versioncli", () => { +ipcMain.on("version-cli", () => { log("Viper: v" + require("../package.json").version); log("Northstar: " + utils.getNSVersion()); log("Node: " + process.version); @@ -168,6 +168,7 @@ ipcMain.on("versioncli", () => { cli.exit(); }) +// sends installed mods info to renderer ipcMain.on("getmods", () => { let mods = utils.mods.list(); if (mods.all.length > 0) { @@ -191,17 +192,18 @@ ipcMain.on("getmods", () => { }) // } +// allows renderer to set a new renderer ipcMain.on("newpath", (event, newpath) => { - if (newpath === false && !win.isVisible()) { - win.send("nopathselected"); + if (newpath === false && ! win.isVisible()) { + win.send("no-path-selected"); } else { _sendVersionsInfo(); if (!win.isVisible()) { win.show(); } } -}); ipcMain.on("wrongpath", () => { - win.send("wrongpath"); +}); ipcMain.on("wrong-path", () => { + win.send("wrong-path"); }); // ensures PWD/CWD is the config folder where viper.json is located diff --git a/src/utils.js b/src/utils.js index d8e968c..b69a9a2 100644 --- a/src/utils.js +++ b/src/utils.js @@ -42,12 +42,12 @@ var settings = { // Logs into the dev tools of the renderer function winLog(msg) { - ipcMain.emit("winLog", msg, msg); + ipcMain.emit("win-log", msg, msg); } // Sends an alert to the renderer function winAlert(msg) { - ipcMain.emit("winAlert", msg, msg); + ipcMain.emit("win-alert", msg, msg); } // Creates the settings file with the base settings if it doesn't exist. @@ -187,7 +187,7 @@ async function setpath(win, forcedialog) { return; } if (! fs.existsSync(path.join(res.filePaths[0], "Titanfall2.exe"))) { - ipcMain.emit("wrongpath"); + ipcMain.emit("wrong-path"); return; } @@ -326,11 +326,11 @@ async function update() { fs.createReadStream(settings.zip).pipe(unzip.Extract({path: settings.gamepath})) .on("finish", () => { fs.writeFileSync(path.join(settings.gamepath, "ns_version.txt"), latestAvailableVersion); - ipcMain.emit("getversion"); + ipcMain.emit("get-version"); restoreExcludedFiles(); - ipcMain.emit("guigetmods"); + ipcMain.emit("gui-getmods"); ipcMain.emit("ns-update-event", "cli.update.uptodate.short"); winLog(lang("gui.update.finished")); console.log(lang("cli.update.finished")); @@ -603,11 +603,11 @@ const mods = { } } - ipcMain.emit("installedmod", "", { + ipcMain.emit("installed-mod", "", { name: modname, malformed: malformed, }); - ipcMain.emit("guigetmods"); + ipcMain.emit("gui-getmods"); return true; } @@ -679,7 +679,7 @@ const mods = { } if (files.length == 0) { - ipcMain.emit("failedmod"); + ipcMain.emit("failed-mod"); return notamod(); } } @@ -778,8 +778,8 @@ const mods = { fs.rmSync(modPath, {recursive: true}); console.log(lang("cli.mods.removed")); cli.exit(); - ipcMain.emit("guigetmods"); - ipcMain.emit("removedmod", "", { + ipcMain.emit("gui-getmods"); + ipcMain.emit("removed-mod", "", { name: mod.replace(/^.*(\\|\/|\:)/, ""), manifestname: manifestname }); @@ -818,17 +818,17 @@ const mods = { console.log(lang("cli.mods.toggled")); cli.exit(); } - ipcMain.emit("guigetmods"); + ipcMain.emit("gui-getmods"); } }; setInterval(() => { if (gamepathExists()) { - ipcMain.emit("guigetmods"); + ipcMain.emit("gui-getmods"); } else { if (fs.existsSync("viper.json")) { if (settings.gamepath != "") { - ipcMain.emit("gamepathlost"); + ipcMain.emit("gamepath-lost"); } } } -- cgit v1.2.3 From f9e51cfb04de5316a555d42aa6e23ec445c66f73 Mon Sep 17 00:00:00 2001 From: B Date: Thu, 2 Jun 2022 03:00:06 -0600 Subject: added setting for auto-killing origin, functionality working --- .gitignore | 1 + src/app/index.html | 12 +++ src/index.js | 75 ++++++++++------- src/lang/en.json | 3 + src/utils.js | 239 +++++++++++++++++++++++++++-------------------------- 5 files changed, 182 insertions(+), 148 deletions(-) (limited to 'src/index.js') diff --git a/.gitignore b/.gitignore index 1eae0cf..ec12b74 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ dist/ node_modules/ +.vscode/ \ No newline at end of file diff --git a/src/app/index.html b/src/app/index.html index 2305e4c..0255dd6 100644 --- a/src/app/index.html +++ b/src/app/index.html @@ -107,6 +107,18 @@ +

%%gui.settings.title.misc%%

+
+
+ %%gui.settings.originkill.title%% +
+ %%gui.settings.originkill.desc%% +
+
+
+ +
+
diff --git a/src/index.js b/src/index.js index 96976b7..ddaa928 100644 --- a/src/index.js +++ b/src/index.js @@ -6,6 +6,7 @@ const { app, ipcMain, BrowserWindow, dialog } = require("electron"); const utils = require("./utils"); const cli = require("./cli"); const requests = require("./extras/requests"); +const { settings } = require("./utils"); var log = console.log; @@ -33,10 +34,10 @@ function start() { nodeIntegration: true, contextIsolation: false, }, - }); + }); // when --debug is added it'll open the dev tools - if (cli.hasParam("debug")) {win.openDevTools()} + if (cli.hasParam("debug")) { win.openDevTools() } // general setup win.removeMenu(); @@ -46,23 +47,35 @@ function start() { 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()}); + ipcMain.on("exit", () => { + if (settings.originkill) { + utils.isOriginRunning().then((running) => { + if (running) { + utils.killOrigin().then(process.exit(0)) + } else { + process.exit(0) + } + }) + } else { + 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("win-log", (event, ...args) => { send("log", ...args) }); + ipcMain.on("win-alert", (event, ...args) => { send("alert", ...args) }); // 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)}); + 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)}); + 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()); @@ -71,18 +84,18 @@ function start() { // ensures gamepath still exists and is valid on startup let gamepathlost = false; ipcMain.on("gamepath-lost", (event, ...args) => { - if (! gamepathlost) { + if (!gamepathlost) { gamepathlost = true; send("gamepath-lost"); } }); - ipcMain.on("save-settings", (event, obj) => {utils.saveSettings(obj)}); + ipcMain.on("save-settings", (event, obj) => { utils.saveSettings(obj) }); // allows renderer to check for updates - ipcMain.on("ns-update-event", (event) => {send("ns-update-event", event)}); + ipcMain.on("ns-update-event", (event) => { send("ns-update-event", event) }); ipcMain.on("can-autoupdate", () => { - if (! autoUpdater.isUpdaterActive() || cli.hasParam("no-vp-updates")) { + if (!autoUpdater.isUpdaterActive() || cli.hasParam("no-vp-updates")) { send("cant-autoupdate"); } }) @@ -115,34 +128,34 @@ ipcMain.on("install-mod", () => { if (cli.hasArgs()) { utils.mods.install(cli.param("installmod")); } else { - dialog.showOpenDialog({properties: ["openFile"]}).then(res => { + dialog.showOpenDialog({ properties: ["openFile"] }).then(res => { if (res.filePaths.length != 0) { utils.mods.install(res.filePaths[0]); } else { send("set-buttons", true); } - }).catch(err => {error(err)}); + }).catch(err => { error(err) }); } }) -ipcMain.on("remove-mod", (event, mod) => {utils.mods.remove(mod)}); -ipcMain.on("toggle-mod", (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-ns", () => { utils.launch() }); +ipcMain.on("launch-vanilla", () => { utils.launch("vanilla") }); -ipcMain.on("setlang", (event, lang) => {utils.setlang(lang)}); +ipcMain.on("setlang", (event, lang) => { utils.setlang(lang) }); -ipcMain.on("update", () => {utils.update()}) -ipcMain.on("setpath-cli", () => {utils.setpath()}); +ipcMain.on("update", () => { utils.update() }) +ipcMain.on("setpath-cli", () => { utils.setpath() }); ipcMain.on("setpath", (event, value) => { - if (! value) { - if (! win.isVisible()) { + if (!value) { + if (!win.isVisible()) { utils.setpath(win); } else { utils.setpath(win, true); } - } else if (! win.isVisible()) { + } else if (!win.isVisible()) { win.show(); } }); @@ -157,7 +170,7 @@ function sendVersionsInfo() { } // sends the version info back to the renderer -ipcMain.on("get-version", () => {sendVersionsInfo()}); +ipcMain.on("get-version", () => { sendVersionsInfo() }); // prints out version info for the CLI ipcMain.on("version-cli", () => { @@ -194,7 +207,7 @@ ipcMain.on("getmods", () => { // allows renderer to set a new renderer ipcMain.on("newpath", (event, newpath) => { - if (newpath === false && ! win.isVisible()) { + if (newpath === false && !win.isVisible()) { win.send("no-path-selected"); } else { _sendVersionsInfo(); diff --git a/src/lang/en.json b/src/lang/en.json index 34c4619..3ae6ff1 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -91,6 +91,7 @@ "gui.settings.title.ns": "Northstar", "gui.settings.title.language": "Language", "gui.settings.title.updates": "Updates", + "gui.settings.title.misc": "Miscellaneous", "gui.settings.nsargs.title": "Launch options", "gui.settings.nsargs.desc": "Here you can add launch options for Northstar/Titanfall.", "gui.settings.autolang.title": "Auto-Detect Language", @@ -103,6 +104,8 @@ "gui.settings.nsupdate.desc": "Viper will automatically keep Northstar up-to-date, however it can still manually be updated through the Northstar page.", "gui.settings.excludes.title": "Retain files on update", "gui.settings.excludes.desc": "When Northstar is updated, files specified here will not be overwritten by files from the new Northstar update, unless you know what you're changing, you should probably not change anything here. Each file is separated with a space.", + "gui.settings.originkill.title": "Automatically quit Origin", + "gui.settings.originkill.desc": "When Viper exits, automatically quit Origin client too. Mirrors behavior of launching the game through Steam.", "gui.update.downloading": "Downloading...", "gui.update.extracting": "Extracting update...", diff --git a/src/utils.js b/src/utils.js index 061c3f1..2e81bdf 100644 --- a/src/utils.js +++ b/src/utils.js @@ -37,7 +37,9 @@ var settings = { excludes: [ "ns_startup_args.txt", "ns_startup_args_dedi.txt" - ] + ], + + originkill: false } // Logs into the dev tools of the renderer @@ -58,11 +60,11 @@ if (fs.existsSync("viper.json")) { // Validates viper.json try { json = JSON.parse(conf); - }catch (e) { + } catch (e) { invalidsettings = true; } - settings = {...settings, ...json}; + settings = { ...settings, ...json }; settings.zip = path.join(settings.gamepath + "/northstar.zip"); let args = path.join(settings.gamepath, "ns_startup_args.txt"); @@ -95,7 +97,7 @@ async function isGameRunning() { break } - if (i == procs.length - 1) {resolve(false)} + if (i == procs.length - 1) { resolve(false) } } }); }); @@ -103,44 +105,44 @@ async function isGameRunning() { //Check if origin client is running async function isOriginRunning() { - return new Promise(resolve => { - let procs = ["origin.exe"]; //check this, probably not right - let cmd = (() => { - switch (process.platform) { - case "linux": return "ps -A"; - case "win32": return "tasklist"; - } - })(); - - exec(cmd, (err, stdout) => { - procs.forEach( proc => { - if (stdout.includes(proc)) { - resolve(true); - return; - } - resolve(false); - }); + return new Promise(resolve => { + let procs = ["Origin.exe", "OriginClientService.exe"]; + let cmd = (() => { + switch (process.platform) { + case "linux": return "ps -A"; + case "win32": return "tasklist"; + } + })(); + + exec(cmd, (err, stdout) => { + procs.forEach(proc => { + if (stdout.includes(proc)) { + resolve(true); + return; + } + resolve(false); + }); + }); }); - }); } //Kill origin client async function killOrigin() { - return Promise(resolve => { - let proc = "origin.exe" ; //need to match above - let cmd = (() => { - switch (process.platform) { - case "linux": return "killall " + proc; - case "win32": return "taskkill /IM " + proc + " /F"; - } - })(); - - exec(cmd, (err, stdout) => { - //do some checking here maybe? idk we're going to be exiting so maybe we should - //just try and fail silently if we don't find shit - resolve(true); + return new Promise(resolve => { + let proc = "Origin.exe"; //I'm pretty sure we only have to kill this one + let cmd = (() => { + switch (process.platform) { + case "linux": return "killall " + proc; + case "win32": return "taskkill /IM " + proc + " /F"; + } + })(); + + exec(cmd, (err, stdout) => { + //do some checking here maybe? idk we're going to be exiting so maybe we should + //just try and fail silently if we don't find shit + resolve(true); + }); }); - }); } // Handles auto updating Northstar. @@ -148,7 +150,7 @@ async function killOrigin() { // It uses isGameRunning() to ensure it doesn't run while the game is // running, as that may have all kinds of issues. function handleNorthstarUpdating() { - if (! settings.nsupdate || ! fs.existsSync("viper.json") || settings.gamepath.length === 0) { + if (!settings.nsupdate || !fs.existsSync("viper.json") || settings.gamepath.length === 0) { return; } @@ -163,7 +165,7 @@ function handleNorthstarUpdating() { if (await isGameRunning()) { console.log(lang("cli.autoupdates.gamerunning")); new Notification({ - title: lang("gui.nsupdate.gaming.title"), + title: lang("gui.nsupdate.gaming.title"), body: lang("gui.nsupdate.gaming.body") }).show(); } else { @@ -175,7 +177,7 @@ function handleNorthstarUpdating() { } setTimeout( - _checkForUpdates, + _checkForUpdates, 15 * 60 * 1000 // interval in between each update check // by default 15 minutes. @@ -201,10 +203,10 @@ async function setpath(win, forcedialog) { modpath = path.join(settings.gamepath, "R2Northstar/mods"); } - if (! win) { // CLI + if (!win) { // CLI setGamepath(cli.param("setpath")); } else { // GUI - if (! forcedialog) { + if (!forcedialog) { function setGamepath(folder, forcedialog) { settings.gamepath = folder; settings.zip = path.join(settings.gamepath + "/northstar.zip"); @@ -223,12 +225,12 @@ async function setpath(win, forcedialog) { } // Fallback to manual selection - dialog.showOpenDialog({properties: ["openDirectory"]}).then(res => { + dialog.showOpenDialog({ properties: ["openDirectory"] }).then(res => { if (res.canceled) { ipcMain.emit("newpath", null, false); return; } - if (! fs.existsSync(path.join(res.filePaths[0], "Titanfall2.exe"))) { + if (!fs.existsSync(path.join(res.filePaths[0], "Titanfall2.exe"))) { ipcMain.emit("wrong-path"); return; } @@ -237,7 +239,7 @@ async function setpath(win, forcedialog) { cli.exit(); return; - }).catch(err => {console.error(err)}) + }).catch(err => { console.error(err) }) } } @@ -248,15 +250,15 @@ async function setpath(win, forcedialog) { // 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} + if (invalidsettings) { return false } - settings = {...settings, ...obj}; + 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})); + fs.writeFileSync(app.getPath("appData") + "/viper.json", JSON.stringify({ ...settings, ...obj })); } // Returns the current Northstar version @@ -313,7 +315,7 @@ restoreExcludedFiles(); // .excluded, then rename them back after the extraction. The // unzip module does not support excluding files directly. async function update() { - if (! gamepathExists()) {return} + if (!gamepathExists()) { return } ipcMain.emit("ns-update-event", "cli.update.checking"); console.log(lang("cli.update.checking")); @@ -332,7 +334,7 @@ async function update() { } else { if (version != "unknown") { console.log(lang("cli.update.current"), version); - }; + }; console.log(lang("cli.update.downloading") + ":", latestAvailableVersion); ipcMain.emit("ns-update-event", "cli.update.downloading"); } @@ -365,19 +367,19 @@ async function update() { console.log(lang("cli.update.downloaddone")); // Extracts the zip, this is the part where we're actually // installing Northstar. - fs.createReadStream(settings.zip).pipe(unzip.Extract({path: settings.gamepath})) - .on("finish", () => { - fs.writeFileSync(path.join(settings.gamepath, "ns_version.txt"), latestAvailableVersion); - ipcMain.emit("get-version"); - - restoreExcludedFiles(); - - ipcMain.emit("gui-getmods"); - ipcMain.emit("ns-update-event", "cli.update.uptodate.short"); - winLog(lang("gui.update.finished")); - console.log(lang("cli.update.finished")); - cli.exit(); - }) + fs.createReadStream(settings.zip).pipe(unzip.Extract({ path: settings.gamepath })) + .on("finish", () => { + fs.writeFileSync(path.join(settings.gamepath, "ns_version.txt"), latestAvailableVersion); + ipcMain.emit("get-version"); + + restoreExcludedFiles(); + + ipcMain.emit("gui-getmods"); + ipcMain.emit("ns-update-event", "cli.update.uptodate.short"); + winLog(lang("gui.update.finished")); + console.log(lang("cli.update.finished")); + cli.exit(); + }) }) }) } @@ -390,7 +392,7 @@ async function update() { function updatevp(autoinstall) { const { autoUpdater } = require("electron-updater"); - if (! autoUpdater.isUpdaterActive()) { + if (!autoUpdater.isUpdaterActive()) { if (settings.nsupdate) { handleNorthstarUpdating(); } @@ -403,7 +405,7 @@ function updatevp(autoinstall) { }); } - autoUpdater.on("error", (info) => {cli.exit(1)}); + autoUpdater.on("error", (info) => { cli.exit(1) }); autoUpdater.on("update-not-available", (info) => { // only check for NS updates if Viper itself has no updates and // if NS auto updates is enabled. @@ -429,7 +431,7 @@ function launch(version) { } process.chdir(settings.gamepath); - switch(version) { + switch (version) { case "vanilla": console.log(lang("general.launching"), "Vanilla..."); run(path.join(settings.gamepath + "/Titanfall2.exe")); @@ -471,8 +473,8 @@ const mods = { let enabled = []; let disabled = []; - if (! fs.existsSync(modpath)) { - fs.mkdirSync(path.join(modpath), {recursive: true}); + if (!fs.existsSync(modpath)) { + fs.mkdirSync(path.join(modpath), { recursive: true }); return { enabled: [], disabled: [], @@ -491,9 +493,10 @@ const mods = { Version: "unknown", Name: "unknown", FolderName: file, - ...mod} + ...mod + } - obj.Disabled = ! mods.modfile().get(obj.Name); + obj.Disabled = !mods.modfile().get(obj.Name); let manifestfile = path.join(modpath, file, "manifest.json"); if (fs.existsSync(manifestfile)) { @@ -542,7 +545,7 @@ const mods = { for (let i = 0; i < list.length; i++) { if (list[i].Name == mod) { return list[i]; - } else {continue} + } else { continue } } return false; @@ -556,11 +559,11 @@ const mods = { let modpath = path.join(settings.gamepath, "R2Northstar/mods"); let file = path.join(modpath, "..", "enabledmods.json"); - if (! fs.existsSync(modpath)) { - fs.mkdirSync(path.join(modpath), {recursive: true}); + if (!fs.existsSync(modpath)) { + fs.mkdirSync(path.join(modpath), { recursive: true }); } - if (! fs.existsSync(file)) { + if (!fs.existsSync(file)) { fs.writeFileSync(file, "{}"); } @@ -587,7 +590,7 @@ const mods = { toggle: (mod) => { let data = JSON.parse(repair(fs.readFileSync(file, "utf8"))); if (data[mod] != undefined) { - data[mod] = ! data[mod]; + data[mod] = !data[mod]; } else { data[mod] = false; } @@ -653,19 +656,19 @@ const mods = { return true; } - if (! fs.existsSync(mod)) {return notamod()} + if (!fs.existsSync(mod)) { return notamod() } if (fs.statSync(mod).isDirectory()) { winLog(lang("gui.mods.installing")); files = fs.readdirSync(mod); - if (fs.existsSync(path.join(mod, "mod.json")) && + if (fs.existsSync(path.join(mod, "mod.json")) && fs.statSync(path.join(mod, "mod.json")).isFile()) { if (fs.existsSync(path.join(modpath, modname))) { - fs.rmSync(path.join(modpath, modname), {recursive: true}); + fs.rmSync(path.join(modpath, modname), { recursive: true }); } let copydest = path.join(modpath, modname); - if (typeof destname == "string") {copydest = path.join(modpath, destname)} + if (typeof destname == "string") { copydest = path.join(modpath, destname) } copy(mod, copydest); copy(manifestfile, path.join(copydest, "manifest.json")); @@ -679,7 +682,7 @@ const mods = { fs.statSync(path.join(mod, files[i], "mod.json")).isFile()) { mods.install(path.join(mod, files[i])); - if (mods.install(path.join(mod, files[i]))) {return true}; + if (mods.install(path.join(mod, files[i]))) { return true }; } } } @@ -692,52 +695,52 @@ const mods = { winLog(lang("gui.mods.extracting")); let cache = path.join(app.getPath("userData"), "Archives"); if (fs.existsSync(cache)) { - fs.rmSync(cache, {recursive: true}); - fs.mkdirSync(path.join(cache, "mods"), {recursive: true}); + fs.rmSync(cache, { recursive: true }); + fs.mkdirSync(path.join(cache, "mods"), { recursive: true }); } else { - fs.mkdirSync(path.join(cache, "mods"), {recursive: true}); + fs.mkdirSync(path.join(cache, "mods"), { recursive: true }); } try { if (mod.replace(/.*\./, "").toLowerCase() == "zip") { - fs.createReadStream(mod).pipe(unzip.Extract({path: cache})) - .on("finish", () => { - setTimeout(() => { - let manifest = path.join(cache, "manifest.json"); - if (fs.existsSync(manifest)) { - files = fs.readdirSync(path.join(cache, "mods")); - if (fs.existsSync(path.join(cache, "mods/mod.json"))) { - if (mods.install(path.join(cache, "mods"), require(manifest).name, manifest, true)) { - return true; - } - } else { - for (let i = 0; i < files.length; i++) { - let mod = path.join(cache, "mods", files[i]); - if (fs.statSync(mod).isDirectory()) { - setTimeout(() => { - if (mods.install(mod, false, manifest)) {return true}; - }, 1000) + fs.createReadStream(mod).pipe(unzip.Extract({ path: cache })) + .on("finish", () => { + setTimeout(() => { + let manifest = path.join(cache, "manifest.json"); + if (fs.existsSync(manifest)) { + files = fs.readdirSync(path.join(cache, "mods")); + if (fs.existsSync(path.join(cache, "mods/mod.json"))) { + if (mods.install(path.join(cache, "mods"), require(manifest).name, manifest, true)) { + return true; + } + } else { + for (let i = 0; i < files.length; i++) { + let mod = path.join(cache, "mods", files[i]); + if (fs.statSync(mod).isDirectory()) { + setTimeout(() => { + if (mods.install(mod, false, manifest)) { return true }; + }, 1000) + } } - } - if (files.length == 0) { - ipcMain.emit("failed-mod"); - return notamod(); + if (files.length == 0) { + ipcMain.emit("failed-mod"); + return notamod(); + } } - } - return notamod(); - } + return notamod(); + } - if (mods.install(cache)) { - installed(); - } else {return notamod()} - }, 1000) - }); + if (mods.install(cache)) { + installed(); + } else { return notamod() } + }, 1000) + }); } else { return notamod(); } - }catch(err) {return notamod()} + } catch (err) { return notamod() } } }, @@ -751,7 +754,7 @@ const mods = { let modlocation = path.join(tmp, "/mod.zip"); if (fs.existsSync(tmp)) { - if (! fs.statSync(tmp).isDirectory()) { + if (!fs.statSync(tmp).isDirectory()) { fs.rmSync(tmp); } } else { @@ -794,12 +797,12 @@ const mods = { } let disabled = path.join(modpath, "disabled"); - if (! fs.existsSync(disabled)) { + if (!fs.existsSync(disabled)) { fs.mkdirSync(disabled); } let modName = mods.get(mod).FolderName; - if (! modName) { + if (!modName) { console.log("error: " + lang("cli.mods.cantfind")); cli.exit(1); return; @@ -817,7 +820,7 @@ const mods = { manifestname = require(path.join(modPath, "manifest.json")).name; } - fs.rmSync(modPath, {recursive: true}); + fs.rmSync(modPath, { recursive: true }); console.log(lang("cli.mods.removed")); cli.exit(); ipcMain.emit("gui-getmods"); @@ -856,7 +859,7 @@ const mods = { } mods.modfile().toggle(mod); - if (! fork) { + if (!fork) { console.log(lang("cli.mods.toggled")); cli.exit(); } @@ -889,6 +892,8 @@ module.exports = { getNSVersion, getTF2Version, isGameRunning, + isOriginRunning, + killOrigin, gamepathExists, handleNorthstarUpdating, setlang: (lang) => { -- cgit v1.2.3 From 0944356fb64512396c46b374197fdfb2606aeb07 Mon Sep 17 00:00:00 2001 From: B Date: Sun, 5 Jun 2022 02:13:33 -0600 Subject: removed auto formatting missed a few one last one --- src/index.js | 54 ++++++++++---------- src/utils.js | 164 +++++++++++++++++++++++++++++------------------------------ 2 files changed, 108 insertions(+), 110 deletions(-) (limited to 'src/index.js') diff --git a/src/index.js b/src/index.js index ddaa928..a566b57 100644 --- a/src/index.js +++ b/src/index.js @@ -37,7 +37,7 @@ function start() { }); // when --debug is added it'll open the dev tools - if (cli.hasParam("debug")) { win.openDevTools() } + if (cli.hasParam("debug")) {win.openDevTools()} // general setup win.removeMenu(); @@ -64,18 +64,18 @@ function start() { 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("win-log", (event, ...args) => {send("log", ...args)}); + ipcMain.on("win-alert", (event, ...args) => {send("alert", ...args)}); // 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) }); + 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) }); + 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()); @@ -84,18 +84,18 @@ function start() { // ensures gamepath still exists and is valid on startup let gamepathlost = false; ipcMain.on("gamepath-lost", (event, ...args) => { - if (!gamepathlost) { + if (! gamepathlost) { gamepathlost = true; send("gamepath-lost"); } }); - ipcMain.on("save-settings", (event, obj) => { utils.saveSettings(obj) }); + ipcMain.on("save-settings", (event, obj) => {utils.saveSettings(obj)}); // allows renderer to check for updates - ipcMain.on("ns-update-event", (event) => { send("ns-update-event", event) }); + ipcMain.on("ns-update-event", (event) => {send("ns-update-event", event)}); ipcMain.on("can-autoupdate", () => { - if (!autoUpdater.isUpdaterActive() || cli.hasParam("no-vp-updates")) { + if (! autoUpdater.isUpdaterActive() || cli.hasParam("no-vp-updates")) { send("cant-autoupdate"); } }) @@ -128,34 +128,34 @@ ipcMain.on("install-mod", () => { if (cli.hasArgs()) { utils.mods.install(cli.param("installmod")); } else { - dialog.showOpenDialog({ properties: ["openFile"] }).then(res => { + dialog.showOpenDialog({properties: ["openFile"]}).then(res => { if (res.filePaths.length != 0) { utils.mods.install(res.filePaths[0]); } else { send("set-buttons", true); } - }).catch(err => { error(err) }); + }).catch(err => {error(err)}); } }) -ipcMain.on("remove-mod", (event, mod) => { utils.mods.remove(mod) }); -ipcMain.on("toggle-mod", (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-ns", () => {utils.launch()}); +ipcMain.on("launch-vanilla", () => {utils.launch("vanilla")}); -ipcMain.on("setlang", (event, lang) => { utils.setlang(lang) }); +ipcMain.on("setlang", (event, lang) => {utils.setlang(lang)}); -ipcMain.on("update", () => { utils.update() }) -ipcMain.on("setpath-cli", () => { utils.setpath() }); +ipcMain.on("update", () => {utils.update()}) +ipcMain.on("setpath-cli", () => {utils.setpath()}); ipcMain.on("setpath", (event, value) => { - if (!value) { - if (!win.isVisible()) { + if (! value) { + if (! win.isVisible()) { utils.setpath(win); } else { utils.setpath(win, true); } - } else if (!win.isVisible()) { + } else if (! win.isVisible()) { win.show(); } }); @@ -170,7 +170,7 @@ function sendVersionsInfo() { } // sends the version info back to the renderer -ipcMain.on("get-version", () => { sendVersionsInfo() }); +ipcMain.on("get-version", () => {sendVersionsInfo()}); // prints out version info for the CLI ipcMain.on("version-cli", () => { @@ -207,7 +207,7 @@ ipcMain.on("getmods", () => { // allows renderer to set a new renderer ipcMain.on("newpath", (event, newpath) => { - if (newpath === false && !win.isVisible()) { + if (newpath === false && ! win.isVisible()) { win.send("no-path-selected"); } else { _sendVersionsInfo(); diff --git a/src/utils.js b/src/utils.js index 2e81bdf..2fea3ea 100644 --- a/src/utils.js +++ b/src/utils.js @@ -60,11 +60,11 @@ if (fs.existsSync("viper.json")) { // Validates viper.json try { json = JSON.parse(conf); - } catch (e) { + }catch (e) { invalidsettings = true; } - settings = { ...settings, ...json }; + settings = {...settings, ...json}; settings.zip = path.join(settings.gamepath + "/northstar.zip"); let args = path.join(settings.gamepath, "ns_startup_args.txt"); @@ -97,7 +97,7 @@ async function isGameRunning() { break } - if (i == procs.length - 1) { resolve(false) } + if (i == procs.length - 1) {resolve(false)} } }); }); @@ -138,8 +138,7 @@ async function killOrigin() { })(); exec(cmd, (err, stdout) => { - //do some checking here maybe? idk we're going to be exiting so maybe we should - //just try and fail silently if we don't find shit + //just try and fail silently if we don't find it w/e resolve(true); }); }); @@ -150,7 +149,7 @@ async function killOrigin() { // It uses isGameRunning() to ensure it doesn't run while the game is // running, as that may have all kinds of issues. function handleNorthstarUpdating() { - if (!settings.nsupdate || !fs.existsSync("viper.json") || settings.gamepath.length === 0) { + if (! settings.nsupdate || ! fs.existsSync("viper.json") || settings.gamepath.length === 0) { return; } @@ -203,10 +202,10 @@ async function setpath(win, forcedialog) { modpath = path.join(settings.gamepath, "R2Northstar/mods"); } - if (!win) { // CLI + if (! win) { // CLI setGamepath(cli.param("setpath")); } else { // GUI - if (!forcedialog) { + if (! forcedialog) { function setGamepath(folder, forcedialog) { settings.gamepath = folder; settings.zip = path.join(settings.gamepath + "/northstar.zip"); @@ -225,12 +224,12 @@ async function setpath(win, forcedialog) { } // Fallback to manual selection - dialog.showOpenDialog({ properties: ["openDirectory"] }).then(res => { + dialog.showOpenDialog({properties: ["openDirectory"]}).then(res => { if (res.canceled) { ipcMain.emit("newpath", null, false); return; } - if (!fs.existsSync(path.join(res.filePaths[0], "Titanfall2.exe"))) { + if (! fs.existsSync(path.join(res.filePaths[0], "Titanfall2.exe"))) { ipcMain.emit("wrong-path"); return; } @@ -239,7 +238,7 @@ async function setpath(win, forcedialog) { cli.exit(); return; - }).catch(err => { console.error(err) }) + }).catch(err => {console.error(err)}) } } @@ -250,15 +249,15 @@ async function setpath(win, forcedialog) { // 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 } + if (invalidsettings) {return false} - settings = { ...settings, ...obj }; + 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 })); + fs.writeFileSync(app.getPath("appData") + "/viper.json", JSON.stringify({...settings, ...obj})); } // Returns the current Northstar version @@ -315,7 +314,7 @@ restoreExcludedFiles(); // .excluded, then rename them back after the extraction. The // unzip module does not support excluding files directly. async function update() { - if (!gamepathExists()) { return } + if (! gamepathExists()) {return} ipcMain.emit("ns-update-event", "cli.update.checking"); console.log(lang("cli.update.checking")); @@ -367,19 +366,19 @@ async function update() { console.log(lang("cli.update.downloaddone")); // Extracts the zip, this is the part where we're actually // installing Northstar. - fs.createReadStream(settings.zip).pipe(unzip.Extract({ path: settings.gamepath })) - .on("finish", () => { - fs.writeFileSync(path.join(settings.gamepath, "ns_version.txt"), latestAvailableVersion); - ipcMain.emit("get-version"); - - restoreExcludedFiles(); - - ipcMain.emit("gui-getmods"); - ipcMain.emit("ns-update-event", "cli.update.uptodate.short"); - winLog(lang("gui.update.finished")); - console.log(lang("cli.update.finished")); - cli.exit(); - }) + fs.createReadStream(settings.zip).pipe(unzip.Extract({path: settings.gamepath})) + .on("finish", () => { + fs.writeFileSync(path.join(settings.gamepath, "ns_version.txt"), latestAvailableVersion); + ipcMain.emit("get-version"); + + restoreExcludedFiles(); + + ipcMain.emit("gui-getmods"); + ipcMain.emit("ns-update-event", "cli.update.uptodate.short"); + winLog(lang("gui.update.finished")); + console.log(lang("cli.update.finished")); + cli.exit(); + }) }) }) } @@ -392,7 +391,7 @@ async function update() { function updatevp(autoinstall) { const { autoUpdater } = require("electron-updater"); - if (!autoUpdater.isUpdaterActive()) { + if (! autoUpdater.isUpdaterActive()) { if (settings.nsupdate) { handleNorthstarUpdating(); } @@ -405,7 +404,7 @@ function updatevp(autoinstall) { }); } - autoUpdater.on("error", (info) => { cli.exit(1) }); + autoUpdater.on("error", (info) => {cli.exit(1)}); autoUpdater.on("update-not-available", (info) => { // only check for NS updates if Viper itself has no updates and // if NS auto updates is enabled. @@ -431,7 +430,7 @@ function launch(version) { } process.chdir(settings.gamepath); - switch (version) { + switch(version) { case "vanilla": console.log(lang("general.launching"), "Vanilla..."); run(path.join(settings.gamepath + "/Titanfall2.exe")); @@ -473,8 +472,8 @@ const mods = { let enabled = []; let disabled = []; - if (!fs.existsSync(modpath)) { - fs.mkdirSync(path.join(modpath), { recursive: true }); + if (! fs.existsSync(modpath)) { + fs.mkdirSync(path.join(modpath), {recursive: true}); return { enabled: [], disabled: [], @@ -493,10 +492,9 @@ const mods = { Version: "unknown", Name: "unknown", FolderName: file, - ...mod - } + ...mod} - obj.Disabled = !mods.modfile().get(obj.Name); + obj.Disabled = ! mods.modfile().get(obj.Name); let manifestfile = path.join(modpath, file, "manifest.json"); if (fs.existsSync(manifestfile)) { @@ -545,7 +543,7 @@ const mods = { for (let i = 0; i < list.length; i++) { if (list[i].Name == mod) { return list[i]; - } else { continue } + } else {continue} } return false; @@ -559,11 +557,11 @@ const mods = { let modpath = path.join(settings.gamepath, "R2Northstar/mods"); let file = path.join(modpath, "..", "enabledmods.json"); - if (!fs.existsSync(modpath)) { - fs.mkdirSync(path.join(modpath), { recursive: true }); + if (! fs.existsSync(modpath)) { + fs.mkdirSync(path.join(modpath), {recursive: true}); } - if (!fs.existsSync(file)) { + if (! fs.existsSync(file)) { fs.writeFileSync(file, "{}"); } @@ -590,7 +588,7 @@ const mods = { toggle: (mod) => { let data = JSON.parse(repair(fs.readFileSync(file, "utf8"))); if (data[mod] != undefined) { - data[mod] = !data[mod]; + data[mod] = ! data[mod]; } else { data[mod] = false; } @@ -656,7 +654,7 @@ const mods = { return true; } - if (!fs.existsSync(mod)) { return notamod() } + if (! fs.existsSync(mod)) {return notamod()} if (fs.statSync(mod).isDirectory()) { winLog(lang("gui.mods.installing")); @@ -665,10 +663,10 @@ const mods = { fs.statSync(path.join(mod, "mod.json")).isFile()) { if (fs.existsSync(path.join(modpath, modname))) { - fs.rmSync(path.join(modpath, modname), { recursive: true }); + fs.rmSync(path.join(modpath, modname), {recursive: true}); } let copydest = path.join(modpath, modname); - if (typeof destname == "string") { copydest = path.join(modpath, destname) } + if (typeof destname == "string") {copydest = path.join(modpath, destname)} copy(mod, copydest); copy(manifestfile, path.join(copydest, "manifest.json")); @@ -682,7 +680,7 @@ const mods = { fs.statSync(path.join(mod, files[i], "mod.json")).isFile()) { mods.install(path.join(mod, files[i])); - if (mods.install(path.join(mod, files[i]))) { return true }; + if (mods.install(path.join(mod, files[i]))) {return true}; } } } @@ -695,52 +693,52 @@ const mods = { winLog(lang("gui.mods.extracting")); let cache = path.join(app.getPath("userData"), "Archives"); if (fs.existsSync(cache)) { - fs.rmSync(cache, { recursive: true }); - fs.mkdirSync(path.join(cache, "mods"), { recursive: true }); + fs.rmSync(cache, {recursive: true}); + fs.mkdirSync(path.join(cache, "mods"), {recursive: true}); } else { - fs.mkdirSync(path.join(cache, "mods"), { recursive: true }); + fs.mkdirSync(path.join(cache, "mods"), {recursive: true}); } try { if (mod.replace(/.*\./, "").toLowerCase() == "zip") { - fs.createReadStream(mod).pipe(unzip.Extract({ path: cache })) - .on("finish", () => { - setTimeout(() => { - let manifest = path.join(cache, "manifest.json"); - if (fs.existsSync(manifest)) { - files = fs.readdirSync(path.join(cache, "mods")); - if (fs.existsSync(path.join(cache, "mods/mod.json"))) { - if (mods.install(path.join(cache, "mods"), require(manifest).name, manifest, true)) { - return true; - } - } else { - for (let i = 0; i < files.length; i++) { - let mod = path.join(cache, "mods", files[i]); - if (fs.statSync(mod).isDirectory()) { - setTimeout(() => { - if (mods.install(mod, false, manifest)) { return true }; - }, 1000) - } - } - - if (files.length == 0) { - ipcMain.emit("failed-mod"); - return notamod(); + fs.createReadStream(mod).pipe(unzip.Extract({path: cache})) + .on("finish", () => { + setTimeout(() => { + let manifest = path.join(cache, "manifest.json"); + if (fs.existsSync(manifest)) { + files = fs.readdirSync(path.join(cache, "mods")); + if (fs.existsSync(path.join(cache, "mods/mod.json"))) { + if (mods.install(path.join(cache, "mods"), require(manifest).name, manifest, true)) { + return true; + } + } else { + for (let i = 0; i < files.length; i++) { + let mod = path.join(cache, "mods", files[i]); + if (fs.statSync(mod).isDirectory()) { + setTimeout(() => { + if (mods.install(mod, false, manifest)) {return true}; + }, 1000) } } - return notamod(); + if (files.length == 0) { + ipcMain.emit("failed-mod"); + return notamod(); + } } - if (mods.install(cache)) { - installed(); - } else { return notamod() } - }, 1000) - }); + return notamod(); + } + + if (mods.install(cache)) { + installed(); + } else {return notamod()} + }, 1000) + }); } else { return notamod(); } - } catch (err) { return notamod() } + }catch(err) {return notamod()} } }, @@ -754,7 +752,7 @@ const mods = { let modlocation = path.join(tmp, "/mod.zip"); if (fs.existsSync(tmp)) { - if (!fs.statSync(tmp).isDirectory()) { + if (! fs.statSync(tmp).isDirectory()) { fs.rmSync(tmp); } } else { @@ -797,12 +795,12 @@ const mods = { } let disabled = path.join(modpath, "disabled"); - if (!fs.existsSync(disabled)) { + if (! fs.existsSync(disabled)) { fs.mkdirSync(disabled); } let modName = mods.get(mod).FolderName; - if (!modName) { + if (! modName) { console.log("error: " + lang("cli.mods.cantfind")); cli.exit(1); return; @@ -820,7 +818,7 @@ const mods = { manifestname = require(path.join(modPath, "manifest.json")).name; } - fs.rmSync(modPath, { recursive: true }); + fs.rmSync(modPath, {recursive: true}); console.log(lang("cli.mods.removed")); cli.exit(); ipcMain.emit("gui-getmods"); @@ -859,7 +857,7 @@ const mods = { } mods.modfile().toggle(mod); - if (!fork) { + if (! fork) { console.log(lang("cli.mods.toggled")); cli.exit(); } -- cgit v1.2.3 From 59aa33289cebd63432904ac69fa7d9f4f2bc1b9c Mon Sep 17 00:00:00 2001 From: 0neGal Date: Tue, 7 Jun 2022 17:23:33 +0200 Subject: minor changes Mostly syntax, but also a few fixes with how the settings system work, and also a change in localization strings. --- .gitignore | 3 ++- src/app/main.js | 1 + src/index.js | 7 +++---- src/lang/en.json | 2 +- src/utils.js | 31 ++++++++++++++++++------------- 5 files changed, 25 insertions(+), 19 deletions(-) (limited to 'src/index.js') diff --git a/.gitignore b/.gitignore index ec12b74..32e527a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.vscode/ + dist/ node_modules/ -.vscode/ \ No newline at end of file diff --git a/src/app/main.js b/src/app/main.js index 647e5cd..9fb3191 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -14,6 +14,7 @@ var settings = { autolang: true, forcedlang: "en", autoupdate: true, + originkill: false, zip: "/northstar.zip", lang: navigator.language, excludes: [ diff --git a/src/index.js b/src/index.js index a566b57..2c03529 100644 --- a/src/index.js +++ b/src/index.js @@ -6,7 +6,6 @@ const { app, ipcMain, BrowserWindow, dialog } = require("electron"); const utils = require("./utils"); const cli = require("./cli"); const requests = require("./extras/requests"); -const { settings } = require("./utils"); var log = console.log; @@ -48,7 +47,7 @@ function start() { }; send = win.send; ipcMain.on("exit", () => { - if (settings.originkill) { + if (utils.settings.originkill) { utils.isOriginRunning().then((running) => { if (running) { utils.killOrigin().then(process.exit(0)) @@ -60,8 +59,8 @@ function start() { process.exit(0) } }); - ipcMain.on("minimize", () => { win.minimize() }); - ipcMain.on("relaunch", () => { app.relaunch(); app.exit() }); + 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)}); diff --git a/src/lang/en.json b/src/lang/en.json index 3ae6ff1..72711b8 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -105,7 +105,7 @@ "gui.settings.excludes.title": "Retain files on update", "gui.settings.excludes.desc": "When Northstar is updated, files specified here will not be overwritten by files from the new Northstar update, unless you know what you're changing, you should probably not change anything here. Each file is separated with a space.", "gui.settings.originkill.title": "Automatically quit Origin", - "gui.settings.originkill.desc": "When Viper exits, automatically quit Origin client too. Mirrors behavior of launching the game through Steam.", + "gui.settings.originkill.desc": "When Viper exits, automatically quit Origin client too.", "gui.update.downloading": "Downloading...", "gui.update.extracting": "Extracting update...", diff --git a/src/utils.js b/src/utils.js index 2fea3ea..6114ef1 100644 --- a/src/utils.js +++ b/src/utils.js @@ -29,6 +29,7 @@ var settings = { autolang: true, forcedlang: "en", autoupdate: true, + originkill: false, nsargs: "-multiple", zip: "/northstar.zip", @@ -37,9 +38,7 @@ var settings = { excludes: [ "ns_startup_args.txt", "ns_startup_args_dedi.txt" - ], - - originkill: false + ] } // Logs into the dev tools of the renderer @@ -103,7 +102,7 @@ async function isGameRunning() { }); } -//Check if origin client is running +// checks if any origin processes are running async function isOriginRunning() { return new Promise(resolve => { let procs = ["Origin.exe", "OriginClientService.exe"]; @@ -126,7 +125,7 @@ async function isOriginRunning() { }); } -//Kill origin client +// kill origin processes async function killOrigin() { return new Promise(resolve => { let proc = "Origin.exe"; //I'm pretty sure we only have to kill this one @@ -138,7 +137,7 @@ async function killOrigin() { })(); exec(cmd, (err, stdout) => { - //just try and fail silently if we don't find it w/e + // just try and fail silently if we don't find it w/e resolve(true); }); }); @@ -879,21 +878,27 @@ setInterval(() => { module.exports = { mods, - lang, winLog, - launch, + update, - setpath, updatevp, - settings, - saveSettings, getNSVersion, getTF2Version, + handleNorthstarUpdating, + + launch, + killOrigin, isGameRunning, isOriginRunning, - killOrigin, + + + settings, + saveSettings, + + setpath, gamepathExists, - handleNorthstarUpdating, + + lang, setlang: (lang) => { settings.lang = lang; saveSettings(); -- cgit v1.2.3