From f81558d552e5e3f98438a3fe94290a96041a443a Mon Sep 17 00:00:00 2001 From: 0neGal Date: Thu, 12 Jan 2023 18:57:21 +0100 Subject: src/extras/ is now src/modules/ --- src/extras/findgame.js | 85 ---------------------- src/extras/requests.js | 187 ------------------------------------------------ src/index.js | 2 +- src/modules/findgame.js | 85 ++++++++++++++++++++++ src/modules/requests.js | 187 ++++++++++++++++++++++++++++++++++++++++++++++++ src/utils.js | 4 +- 6 files changed, 275 insertions(+), 275 deletions(-) delete mode 100644 src/extras/findgame.js delete mode 100644 src/extras/requests.js create mode 100644 src/modules/findgame.js create mode 100644 src/modules/requests.js diff --git a/src/extras/findgame.js b/src/extras/findgame.js deleted file mode 100644 index 615c5b4..0000000 --- a/src/extras/findgame.js +++ /dev/null @@ -1,85 +0,0 @@ -const fs = require("fs"); -const path = require("path"); -const vdf = require("simple-vdf"); -const { app } = require("electron"); - -const util = require("util"); -const exec = util.promisify(require("child_process").exec); - -module.exports = async () => { - 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 { - const {stdout} = await exec("Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\\SOFTWARE\\Respawn\\Titanfall2\\ -Name \"Install Dir\"", {"shell":"powershell.exe"}); - - const 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); - - let values = Object.values(data["libraryfolders"]); - if (typeof values[values.length - 1] != "object") { - values.pop(1); - } - - // `.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]); - - if (fs.existsSync(data_array[0] + "/steamapps/common/Titanfall2/Titanfall2.exe")) { - console.log("Found game in:", data_array[0]); - return data_array[0] + "/steamapps/common/Titanfall2"; - } else { - console.log("Game not in:", data_array[0]); - } - } - } - - let folders = []; - switch (process.platform) { - case "win32": - folders = ["C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf"]; - break - case "linux": - case "openbsd": - case "freebsd": - let home = app.getPath("home"); - folders = [ - path.join(home, "/.steam/steam/steamapps/libraryfolders.vdf"), - path.join(home, ".var/app/com.valvesoftware.Steam/.steam/steam/steamapps/libraryfolders.vdf"), - path.join(home, ".var/app/com.valvesoftware.Steam/.local/share/Steam/steamapps/libraryfolders.vdf") - ] - break - } - - 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]); - - let data = fs.readFileSync(folders[i]); - let read_vdf = readvdf(data.toString()); - if (read_vdf) {return read_vdf} - } - } - - if (gamepath) { - return gamepath; - } else { - return false; - } -} diff --git a/src/extras/requests.js b/src/extras/requests.js deleted file mode 100644 index a5f3121..0000000 --- a/src/extras/requests.js +++ /dev/null @@ -1,187 +0,0 @@ -const { app } = require("electron"); -const path = require("path"); -const fs = require("fs"); -const { https } = require("follow-redirects"); -const lang = require("../lang"); - - -// all requests results are stored in this file -const cachePath = path.join(app.getPath("cache"), "viper-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"] ); - }); - }) - - .on('error', () => { - console.error('Failed to get latest Northstar version.'); - resolve( false ); - }); - } - }); -} - -// 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"] ); - }); - }) - - // When GitHub cannot be reached (when user doesn't have Internet - // access for instance), we return latest cache content even if - // it's not up-to-date, or display an error message if cache - // is empty. - .on('error', () => { - if ( cache[NORTHSTAR_RELEASE_NOTES_KEY] ) { - console.warn("Couldn't fetch Northstar release notes, returning data from cache."); - resolve( cache[NORTHSTAR_RELEASE_NOTES_KEY]["body"] ); - } else { - console.error("Couldn't fetch Northstar release notes, cache is empty."); - resolve( [lang("request.northstar.noReleaseNotes")] ); - } - }); - } - }); -} - -// 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"] ); - }); - }) - - // When GitHub cannot be reached (when user doesn't have Internet - // access for instance), we return latest cache content even if - // it's not up-to-date, or display an error message if cache - // is empty. - .on('error', () => { - if ( cache[VIPER_RELEASE_NOTES_KEY] ) { - console.warn("Couldn't fetch Viper release notes, returning data from cache."); - resolve( cache[VIPER_RELEASE_NOTES_KEY]["body"] ); - } else { - console.error("Couldn't fetch Viper release notes, cache is empty."); - resolve( [lang("request.viper.noReleaseNotes")] ); - } - }); - } - }); -} - -module.exports = { - getLatestNsVersion, - getLatestNsVersionLink, - getNsReleaseNotes, - getVpReleaseNotes -}; diff --git a/src/index.js b/src/index.js index 1f4cc87..4ff774a 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,7 @@ const { app, ipcMain, BrowserWindow, dialog } = require("electron"); const utils = require("./utils"); const cli = require("./cli"); -const requests = require("./extras/requests"); +const requests = require("./modules/requests"); var log = console.log; diff --git a/src/modules/findgame.js b/src/modules/findgame.js new file mode 100644 index 0000000..615c5b4 --- /dev/null +++ b/src/modules/findgame.js @@ -0,0 +1,85 @@ +const fs = require("fs"); +const path = require("path"); +const vdf = require("simple-vdf"); +const { app } = require("electron"); + +const util = require("util"); +const exec = util.promisify(require("child_process").exec); + +module.exports = async () => { + 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 { + const {stdout} = await exec("Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\\SOFTWARE\\Respawn\\Titanfall2\\ -Name \"Install Dir\"", {"shell":"powershell.exe"}); + + const 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); + + let values = Object.values(data["libraryfolders"]); + if (typeof values[values.length - 1] != "object") { + values.pop(1); + } + + // `.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]); + + if (fs.existsSync(data_array[0] + "/steamapps/common/Titanfall2/Titanfall2.exe")) { + console.log("Found game in:", data_array[0]); + return data_array[0] + "/steamapps/common/Titanfall2"; + } else { + console.log("Game not in:", data_array[0]); + } + } + } + + let folders = []; + switch (process.platform) { + case "win32": + folders = ["C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf"]; + break + case "linux": + case "openbsd": + case "freebsd": + let home = app.getPath("home"); + folders = [ + path.join(home, "/.steam/steam/steamapps/libraryfolders.vdf"), + path.join(home, ".var/app/com.valvesoftware.Steam/.steam/steam/steamapps/libraryfolders.vdf"), + path.join(home, ".var/app/com.valvesoftware.Steam/.local/share/Steam/steamapps/libraryfolders.vdf") + ] + break + } + + 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]); + + let data = fs.readFileSync(folders[i]); + let read_vdf = readvdf(data.toString()); + if (read_vdf) {return read_vdf} + } + } + + if (gamepath) { + return gamepath; + } else { + return false; + } +} diff --git a/src/modules/requests.js b/src/modules/requests.js new file mode 100644 index 0000000..a5f3121 --- /dev/null +++ b/src/modules/requests.js @@ -0,0 +1,187 @@ +const { app } = require("electron"); +const path = require("path"); +const fs = require("fs"); +const { https } = require("follow-redirects"); +const lang = require("../lang"); + + +// all requests results are stored in this file +const cachePath = path.join(app.getPath("cache"), "viper-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"] ); + }); + }) + + .on('error', () => { + console.error('Failed to get latest Northstar version.'); + resolve( false ); + }); + } + }); +} + +// 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"] ); + }); + }) + + // When GitHub cannot be reached (when user doesn't have Internet + // access for instance), we return latest cache content even if + // it's not up-to-date, or display an error message if cache + // is empty. + .on('error', () => { + if ( cache[NORTHSTAR_RELEASE_NOTES_KEY] ) { + console.warn("Couldn't fetch Northstar release notes, returning data from cache."); + resolve( cache[NORTHSTAR_RELEASE_NOTES_KEY]["body"] ); + } else { + console.error("Couldn't fetch Northstar release notes, cache is empty."); + resolve( [lang("request.northstar.noReleaseNotes")] ); + } + }); + } + }); +} + +// 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"] ); + }); + }) + + // When GitHub cannot be reached (when user doesn't have Internet + // access for instance), we return latest cache content even if + // it's not up-to-date, or display an error message if cache + // is empty. + .on('error', () => { + if ( cache[VIPER_RELEASE_NOTES_KEY] ) { + console.warn("Couldn't fetch Viper release notes, returning data from cache."); + resolve( cache[VIPER_RELEASE_NOTES_KEY]["body"] ); + } else { + console.error("Couldn't fetch Viper release notes, cache is empty."); + resolve( [lang("request.viper.noReleaseNotes")] ); + } + }); + } + }); +} + +module.exports = { + getLatestNsVersion, + getLatestNsVersionLink, + getNsReleaseNotes, + getVpReleaseNotes +}; diff --git a/src/utils.js b/src/utils.js index 514d8d2..647e089 100644 --- a/src/utils.js +++ b/src/utils.js @@ -8,8 +8,8 @@ const events = new Emitter(); const cli = require("./cli"); const lang = require("./lang"); -const requests = require("./extras/requests"); -const findgame = require("./extras/findgame"); +const requests = require("./modules/requests"); +const findgame = require("./modules/findgame"); const unzip = require("unzipper"); const repair = require("jsonrepair"); -- cgit v1.2.3