diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/app/main.js | 5 | ||||
-rw-r--r-- | src/index.js | 2 | ||||
-rw-r--r-- | src/lang/en.json | 1 | ||||
-rw-r--r-- | src/lang/fr.json | 1 | ||||
-rw-r--r-- | src/requests.js | 77 | ||||
-rw-r--r-- | src/utils.js | 88 |
6 files changed, 130 insertions, 44 deletions
diff --git a/src/app/main.js b/src/app/main.js index a5c0a37..1678b86 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -164,6 +164,11 @@ ipcRenderer.on("nopathselected", () => { exit(); }); +ipcRenderer.on("wrongpath", () => { + alert(lang("gui.gamepath.wrong")); + setpath(false); +}); + setlang(); setInterval(() => { ipcRenderer.send("setsize", document.querySelector(".lines").offsetHeight + 20); diff --git a/src/index.js b/src/index.js index 1a9f660..c03326c 100644 --- a/src/index.js +++ b/src/index.js @@ -85,6 +85,8 @@ ipcMain.on("newpath", (event, newpath) => { win.show(); } } +}); ipcMain.on("wrongpath", (event) => { + win.webContents.send("wrongpath"); }); ipcMain.on("getversion", () => { diff --git a/src/lang/en.json b/src/lang/en.json index 406ee79..3f025a8 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -55,6 +55,7 @@ "gui.selectpath": "Please select the path!", "gui.gamepath.must": "The game path must be set to start Viper.", + "gui.gamepath.wrong": "This folder is not a valid game path.", "general.launching": "Launching", diff --git a/src/lang/fr.json b/src/lang/fr.json index 870fbfe..0cd9bfb 100644 --- a/src/lang/fr.json +++ b/src/lang/fr.json @@ -37,6 +37,7 @@ "gui.selectpath": "Veuillez sélectionner le dossier où se trouve le client Titanfall 2.", "gui.gamepath.must": "Vous devez sélectionner le chemin du dossier du jeu Titanfall 2 pour pouvoir lancer Viper.", + "gui.gamepath.wrong": "Ce dossier ne contient pas le jeu Titanfall 2, et n'est donc pas valide.", "general.launching": "Lancement", diff --git a/src/requests.js b/src/requests.js new file mode 100644 index 0000000..06c5577 --- /dev/null +++ b/src/requests.js @@ -0,0 +1,77 @@ +const { app } = require("electron"); +const path = require("path"); +const fs = require("fs"); +const { https } = require("follow-redirects"); + + +// all requests results are stored in this file +const cachePath = path.join(app.getPath("cache"), "requests.json"); +const NORTHSTAR_LATEST_RELEASE_KEY = "nsLatestRelease"; + + +function _saveCache(data) { + fs.writeFileSync(cachePath, JSON.stringify(data)); +} + +function _getRequestsCache() { + if (fs.existsSync(cachePath)) { + return JSON.parse(fs.readFileSync(cachePath, "utf8")); + } else { + fs.writeFileSync(cachePath, "{}"); + return {}; + } +} + +// Returns latest Northstar version available from GitHub releases. If +// there's no cache result for this request, or if cache exists but is +// old, refreshes cache with new data. +async function getLatestNsVersion() { + return new Promise((resolve, reject) => { + let cache = _getRequestsCache(); + + if (cache[NORTHSTAR_LATEST_RELEASE_KEY] && (Date.now() - cache[NORTHSTAR_LATEST_RELEASE_KEY]["time"]) < 5 * 60 * 1000) { + console.log(`returning ${NORTHSTAR_LATEST_RELEASE_KEY} data from cache`); + resolve( cache[NORTHSTAR_LATEST_RELEASE_KEY]["body"]["tag_name"] ); + } else { + https.get({ + host: "api.github.com", + port: 443, + path: "/repos/R2Northstar/Northstar/releases/latest", + method: "GET", + headers: { "User-Agent": "viper" } + }, + + response => { + response.setEncoding("utf8"); + let responseData = ""; + + response.on("data", data => { + responseData += data; + }); + + response.on("end", _ => { + cache[NORTHSTAR_LATEST_RELEASE_KEY] = { + "time": Date.now(), + "body": JSON.parse(responseData) + }; + _saveCache(cache); + resolve( cache[NORTHSTAR_LATEST_RELEASE_KEY]["body"]["tag_name"] ); + }); + }); + } + }); +} + +// Returns the download link to latest Northstar version. Should always +// be called after getLatestNsVersion, as it refreshes cache data (if +// needed). +function getLatestNsVersionLink() { + const cache = _getRequestsCache(); + return cache[NORTHSTAR_LATEST_RELEASE_KEY]["body"].assets[0].browser_download_url; +} + + +module.exports = { + getLatestNsVersion, + getLatestNsVersionLink +}; diff --git a/src/utils.js b/src/utils.js index fc62f01..9243546 100644 --- a/src/utils.js +++ b/src/utils.js @@ -8,9 +8,9 @@ const events = new Emitter(); const cli = require("./cli"); const lang = require("./lang"); +const requests = require("./requests"); const unzip = require("unzipper"); -const request = require("request"); const exec = require("child_process").spawn; const { https } = require("follow-redirects"); @@ -43,6 +43,11 @@ function setpath(win) { ipcMain.emit("newpath", null, false); return; } + if (! fs.existsSync(path.join(res.filePaths[0], "Titanfall2.exe"))) { + ipcMain.emit("wrongpath"); + return; + } + settings.gamepath = res.filePaths[0]; settings.zip = path.join(settings.gamepath + "/northstar.zip"); saveSettings(); @@ -51,7 +56,7 @@ function setpath(win) { }).catch(err => {console.error(err)}) } - fs.writeFileSync(app.getPath("appData") + "/viper.json", JSON.stringify(settings)); + saveSettings(); cli.exit(); } @@ -70,7 +75,7 @@ function getNSVersion() { } } -function update() { +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)) { @@ -82,52 +87,47 @@ function update() { console.log(lang("cli.update.checking")); var version = getNSVersion(); - request({ - json: true, - headers: {"User-Agent": "Viper"}, - url: "https://api.github.com/repos/R2Northstar/Northstar/releases/latest", - }, (error, response, body) => { - var tag = body["tag_name"]; + const latestAvailableVersion = await requests.getLatestNsVersion(); - if (version === tag) { - ipcMain.emit("ns-updated"); - console.log(lang("cli.update.uptodate"), version); + if (version === latestAvailableVersion) { + ipcMain.emit("ns-updated"); + console.log(lang("cli.update.uptodate"), version); - winLog(lang("gui.update.uptodate")); - return; - } else { - if (version != "unknown") { - console.log(lang("cli.update.current"), version); - }; console.log(lang("cli.update.downloading") + ":", tag); + winLog(lang("gui.update.uptodate")); + return; + } else { + if (version != "unknown") { + console.log(lang("cli.update.current"), version); + }; console.log(lang("cli.update.downloading") + ":", latestAvailableVersion); - winLog(lang("gui.update.downloading")); - } + winLog(lang("gui.update.downloading")); + } - https.get(body.assets[0].browser_download_url, (res) => { - let stream = fs.createWriteStream(settings.zip); - res.pipe(stream); - - let received = 0; - res.on("data", (chunk) => { - received += chunk.length; - winLog(lang("gui.update.downloading") + " " + (received / 1024 / 1024).toFixed(1) + "mb"); - }) - - stream.on("finish", () => { - stream.close(); - winLog(lang("gui.update.extracting")); - console.log(lang("cli.update.downloaddone")); - fs.createReadStream(settings.zip).pipe(unzip.Extract({path: settings.gamepath})) - .on("finish", () => { - fs.writeFileSync(path.join(settings.gamepath, "ns_version.txt"), tag); - ipcMain.emit("getversion"); + https.get(requests.getLatestNsVersionLink(), (res) => { + let stream = fs.createWriteStream(settings.zip); + res.pipe(stream); - 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) - } + let received = 0; + res.on("data", (chunk) => { + received += chunk.length; + winLog(lang("gui.update.downloading") + " " + (received / 1024 / 1024).toFixed(1) + "mb"); + }) + + stream.on("finish", () => { + stream.close(); + winLog(lang("gui.update.extracting")); + console.log(lang("cli.update.downloaddone")); + 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"); + + 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) } + } ipcMain.emit("guigetmods"); ipcMain.emit("ns-updated"); @@ -135,7 +135,7 @@ function update() { console.log(lang("cli.update.finished")); cli.exit(); }); - }) + }); }) }) } |