From 4f8884ea68c1360a7b0adec2386c60c7dc65c281 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Sun, 23 Jan 2022 01:56:27 +0100 Subject: moved modules into extras/ I moved requests.js into extras and made the function for finding the potentional gamepath into it's own module. I also made the exec() called Promise based. --- src/extras/findgame.js | 64 +++++++++++++++++++++ src/extras/requests.js | 153 +++++++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 2 +- src/requests.js | 153 ------------------------------------------------- src/utils.js | 74 ++++++------------------ 5 files changed, 234 insertions(+), 212 deletions(-) create mode 100644 src/extras/findgame.js create mode 100644 src/extras/requests.js delete mode 100644 src/requests.js (limited to 'src') diff --git a/src/extras/findgame.js b/src/extras/findgame.js new file mode 100644 index 0000000..029914a --- /dev/null +++ b/src/extras/findgame.js @@ -0,0 +1,64 @@ +const fs = require("fs"); +const path = require("path"); +const vdf = require("simple-vdf"); + +const util = require("util"); +const exec = util.promisify(require("child_process").exec); + +module.exports = () => { + let gamepath = ""; + + // Autodetect path + // Windows only using powershell and windows registery + // Get-Item -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Respawn\Titanfall2\ + if (process.platform == "win32") { + try { + exec("Get-Item -Path Registry::HKEY_LOCAL_MACHINE\\SOFTWARE\\Respawn\\Titanfall2\\", {"shell":"powershell.exe"}, (err, stdout) => { + gamepath = stdout.split('\n') + .filter(r => r.indexOf("Install Dir") !== -1)[0] + .replace(/\s+/g,' ') + .trim() + .replace("Install Dir : ",""); + }); + + if (gamepath) {return gamepath} + } catch (err) {} + } + + // Detect using Steam VDF + function readvdf(data) { + // Parse read_data + data = vdf.parse(data); + + //data['libraryfolders'] + // `.length - 1` This is because the last value is `contentstatsid` + for (let pathIterator = 0; pathIterator < Object.values(data["libraryfolders"]).length - 1; pathIterator++) { + let data_array = Object.values(data["libraryfolders"][pathIterator]) + + if (fs.existsSync(data_array[0] + "/steamapps/common/Titanfall2/Titanfall2.exe")) { + return data_array[0] + "/steamapps/common/Titanfall2"; + } + } + } + + switch (process.platform) { + case "win32": + if (fs.existsSync("C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf")) { + let data = fs.readFileSync("C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf") + if (readvdf(data.toString())) {return data.toString()} + } + break; + case "linux": + if (fs.existsSync(path.join(app.getPath("home"), "/.steam/steam/steamapps/libraryfolders.vdf"))) { + let data = fs.readFileSync(os.homedir() + "/.steam/steam/steamapps/libraryfolders.vdf") + if (readvdf(data.toString())) {return data.toString()} + } + break; + } + + if (gamepath) { + return gamepath; + } else { + return false; + } +} diff --git a/src/extras/requests.js b/src/extras/requests.js new file mode 100644 index 0000000..16b1330 --- /dev/null +++ b/src/extras/requests.js @@ -0,0 +1,153 @@ +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"; +const NORTHSTAR_RELEASE_NOTES_KEY = "nsReleaseNotes"; +const VIPER_RELEASE_NOTES_KEY = "vpReleaseNotes"; + + +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) { + 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; +} + +// Returns and caches the Northstar release notes +async function getNsReleaseNotes() { + return new Promise(resolve => { + let cache = _getRequestsCache(); + + if (cache[NORTHSTAR_RELEASE_NOTES_KEY] && (Date.now() - cache[NORTHSTAR_RELEASE_NOTES_KEY]["time"]) < 5 * 60 * 1000) { + resolve( cache[NORTHSTAR_RELEASE_NOTES_KEY]["body"] ); + } else { + https.get({ + host: "api.github.com", + port: 443, + path: "/repos/R2Northstar/Northstar/releases", + method: "GET", + headers: { "User-Agent": "viper" } + }, + + response => { + response.setEncoding("utf8"); + let responseData = ""; + + response.on("data", data => { + responseData += data; + }); + + response.on("end", _ => { + cache[NORTHSTAR_RELEASE_NOTES_KEY] = { + "time": Date.now(), + "body": JSON.parse(responseData) + }; + _saveCache(cache); + resolve( cache[NORTHSTAR_RELEASE_NOTES_KEY]["body"] ); + }); + }); + } + }); +} + +// Returns and caches the Viper release notes +async function getVpReleaseNotes() { + return new Promise(resolve => { + let cache = _getRequestsCache(); + + if (cache[VIPER_RELEASE_NOTES_KEY] && (Date.now() - cache[VIPER_RELEASE_NOTES_KEY]["time"]) < 5 * 60 * 1000) { + resolve( cache[VIPER_RELEASE_NOTES_KEY]["body"] ); + } else { + https.get({ + host: "api.github.com", + port: 443, + path: "/repos/0negal/viper/releases", + method: "GET", + headers: { "User-Agent": "viper" } + }, + + response => { + response.setEncoding("utf8"); + let responseData = ""; + + response.on("data", data => { + responseData += data; + }); + + response.on("end", _ => { + cache[VIPER_RELEASE_NOTES_KEY] = { + "time": Date.now(), + "body": JSON.parse(responseData) + }; + _saveCache(cache); + resolve( cache[VIPER_RELEASE_NOTES_KEY]["body"] ); + }); + }); + } + }); +} + +module.exports = { + getLatestNsVersion, + getLatestNsVersionLink, + getNsReleaseNotes, + getVpReleaseNotes +}; diff --git a/src/index.js b/src/index.js index 3f50840..f4207e7 100644 --- a/src/index.js +++ b/src/index.js @@ -8,7 +8,7 @@ const events = new Emitter(); const utils = require("./utils"); const cli = require("./cli"); -const requests = require("./requests"); +const requests = require("./extras/requests"); // Starts the actual BrowserWindow, which is only run when using the // GUI, for the CLI this function is never called. diff --git a/src/requests.js b/src/requests.js deleted file mode 100644 index 16b1330..0000000 --- a/src/requests.js +++ /dev/null @@ -1,153 +0,0 @@ -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"; -const NORTHSTAR_RELEASE_NOTES_KEY = "nsReleaseNotes"; -const VIPER_RELEASE_NOTES_KEY = "vpReleaseNotes"; - - -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) { - 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; -} - -// Returns and caches the Northstar release notes -async function getNsReleaseNotes() { - return new Promise(resolve => { - let cache = _getRequestsCache(); - - if (cache[NORTHSTAR_RELEASE_NOTES_KEY] && (Date.now() - cache[NORTHSTAR_RELEASE_NOTES_KEY]["time"]) < 5 * 60 * 1000) { - resolve( cache[NORTHSTAR_RELEASE_NOTES_KEY]["body"] ); - } else { - https.get({ - host: "api.github.com", - port: 443, - path: "/repos/R2Northstar/Northstar/releases", - method: "GET", - headers: { "User-Agent": "viper" } - }, - - response => { - response.setEncoding("utf8"); - let responseData = ""; - - response.on("data", data => { - responseData += data; - }); - - response.on("end", _ => { - cache[NORTHSTAR_RELEASE_NOTES_KEY] = { - "time": Date.now(), - "body": JSON.parse(responseData) - }; - _saveCache(cache); - resolve( cache[NORTHSTAR_RELEASE_NOTES_KEY]["body"] ); - }); - }); - } - }); -} - -// Returns and caches the Viper release notes -async function getVpReleaseNotes() { - return new Promise(resolve => { - let cache = _getRequestsCache(); - - if (cache[VIPER_RELEASE_NOTES_KEY] && (Date.now() - cache[VIPER_RELEASE_NOTES_KEY]["time"]) < 5 * 60 * 1000) { - resolve( cache[VIPER_RELEASE_NOTES_KEY]["body"] ); - } else { - https.get({ - host: "api.github.com", - port: 443, - path: "/repos/0negal/viper/releases", - method: "GET", - headers: { "User-Agent": "viper" } - }, - - response => { - response.setEncoding("utf8"); - let responseData = ""; - - response.on("data", data => { - responseData += data; - }); - - response.on("end", _ => { - cache[VIPER_RELEASE_NOTES_KEY] = { - "time": Date.now(), - "body": JSON.parse(responseData) - }; - _saveCache(cache); - resolve( cache[VIPER_RELEASE_NOTES_KEY]["body"] ); - }); - }); - } - }); -} - -module.exports = { - getLatestNsVersion, - getLatestNsVersionLink, - getNsReleaseNotes, - getVpReleaseNotes -}; diff --git a/src/utils.js b/src/utils.js index cf74a59..bc0f66c 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 requests = require("./extras/requests"); +const findgame = require("./extras/findgame"); -const vdf = require("simple-vdf"); const unzip = require("unzipper"); const run = require("child_process").spawn; const exec = require("child_process").exec; @@ -117,8 +117,16 @@ northstar_auto_updates: { // If running with CLI it takes in the --setpath argument otherwise it // open the systems file browser for the user to select a path. async function setpath(win) { + function setGamepath(folder) { + settings.gamepath = folder; + settings.zip = path.join(settings.gamepath + "/northstar.zip"); + saveSettings(); + win.webContents.send("newpath", settings.gamepath); + ipcMain.emit("newpath", null, settings.gamepath); + } + if (! win) { // CLI - settings.gamepath = cli.param("setpath"); + setGamepath(cli.param("setpath")); } else { // GUI function setGamepath(folder) { settings.gamepath = folder; @@ -128,56 +136,10 @@ async function setpath(win) { ipcMain.emit("newpath", null, settings.gamepath); } - // Autodetect path - // Windows only using powershell and windows registery - // Get-Item -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Respawn\Titanfall2\ - if (process.platform == "win32") { - try { - let {stdout} = await exec("Get-Item -Path Registry::HKEY_LOCAL_MACHINE\\SOFTWARE\\Respawn\\Titanfall2\\", {"shell":"powershell.exe"}); - let originPath = stdout.split('\n') - .filter(r => r.indexOf("Install Dir") !== -1)[0] - .replace(/\s+/g,' ') - .trim() - .replace("Install Dir : ",""); - setGamepath(originPath) - return - } catch (err) {} - } - - // Detect using Steam VDF - function readvdf(data) { - // Parse read_data - data = vdf.parse(data); - - //data['libraryfolders'] - // `.length - 1` This is because the last value is `contentstatsid` - for (let pathIterator = 0; pathIterator < Object.values(data["libraryfolders"]).length - 1; pathIterator++) { - let data_array = Object.values(data["libraryfolders"][pathIterator]) - - if (fs.existsSync(data_array[0] + "/steamapps/common/Titanfall2/Titanfall2.exe")) { - // Found the location - setGamepath(data_array[0] + "/steamapps/common/Titanfall2") - - saveSettings(); - cli.exit(); - return true; - } - } - } - - switch (process.platform) { - case "win32": - if (fs.existsSync("C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf")) { - let data = fs.readFileSync("C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf") - if (readvdf(data.toString())) {return} - } - break; - case "linux": - if (fs.existsSync(path.join(app.getPath("home"), "/.steam/steam/steamapps/libraryfolders.vdf"))) { - let data = fs.readFileSync(os.homedir() + "/.steam/steam/steamapps/libraryfolders.vdf") - if (readvdf(data.toString())) {return} - } - break; + let gamepath = findgame(); + if (findgame()) { + setGamepath(gamepath); + return; } // Fallback to manual selection @@ -191,11 +153,7 @@ async function setpath(win) { return; } - settings.gamepath = res.filePaths[0]; - settings.zip = path.join(settings.gamepath + "/northstar.zip"); - saveSettings(); - win.webContents.send("newpath", settings.gamepath); - ipcMain.emit("newpath", null, settings.gamepath); + setGamepath(res.filePaths[0]) cli.exit(); return; -- cgit v1.2.3