diff options
author | 0neGal <mail@0negal.com> | 2022-02-08 21:13:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-08 21:13:20 +0100 |
commit | 1bd83cf1a72332e142bb63acab47ac3877061875 (patch) | |
tree | 5670e225378ebcb0e73efb0c84265357f74f00cb /src/extras | |
parent | d55f84536814238d8ea79acdae47dbdf269953f1 (diff) | |
parent | e2aa874f48dc585f0d67c5655df234bdf356cc53 (diff) | |
download | Viper-1bd83cf1a72332e142bb63acab47ac3877061875.tar.gz Viper-1bd83cf1a72332e142bb63acab47ac3877061875.zip |
Merge branch 'main' into thunderstore
Diffstat (limited to 'src/extras')
-rw-r--r-- | src/extras/findgame.js | 67 | ||||
-rw-r--r-- | src/extras/requests.js | 153 |
2 files changed, 220 insertions, 0 deletions
diff --git a/src/extras/findgame.js b/src/extras/findgame.js new file mode 100644 index 0000000..42c9b85 --- /dev/null +++ b/src/extras/findgame.js @@ -0,0 +1,67 @@ +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); + + // `.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"; + } + } + } + + let folder = null; + switch (process.platform) { + case "win32": + folder = "C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf"; + break + case "linux": + case "openbsd": + case "freebsd": + folder = path.join(app.getPath("home"), "/.steam/steam/steamapps/libraryfolders.vdf"); + break + } + + if (fs.existsSync(folder) && folder) { + let data = fs.readFileSync(folder) + 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 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 +}; |