diff options
author | 0neGal <mail@0negal.com> | 2022-10-23 03:26:54 +0200 |
---|---|---|
committer | 0neGal <mail@0negal.com> | 2022-10-23 03:29:23 +0200 |
commit | 0862c82996f11822fa851eeebc247d12f88dcd7d (patch) | |
tree | 2084b900a35eeccce2d0a027df3b49dc8d1e29d1 /src/extras | |
parent | efb5761268d1ced56cc633347a5143343aa957c6 (diff) | |
parent | 096c54e746def93eb6ee1b2a8d91e1e62555d610 (diff) | |
download | Viper-0862c82996f11822fa851eeebc247d12f88dcd7d.tar.gz Viper-0862c82996f11822fa851eeebc247d12f88dcd7d.zip |
Merge branch 'main' into linux-launch
Diffstat (limited to 'src/extras')
-rw-r--r-- | src/extras/findgame.js | 85 | ||||
-rw-r--r-- | src/extras/requests.js | 36 |
2 files changed, 120 insertions, 1 deletions
diff --git a/src/extras/findgame.js b/src/extras/findgame.js new file mode 100644 index 0000000..615c5b4 --- /dev/null +++ b/src/extras/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/extras/requests.js b/src/extras/requests.js index 16b1330..a5f3121 100644 --- a/src/extras/requests.js +++ b/src/extras/requests.js @@ -2,10 +2,11 @@ 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"), "requests.json"); +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"; @@ -58,6 +59,11 @@ async function getLatestNsVersion() { _saveCache(cache); resolve( cache[NORTHSTAR_LATEST_RELEASE_KEY]["body"]["tag_name"] ); }); + }) + + .on('error', () => { + console.error('Failed to get latest Northstar version.'); + resolve( false ); }); } }); @@ -103,6 +109,20 @@ async function getNsReleaseNotes() { _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")] ); + } }); } }); @@ -140,6 +160,20 @@ async function getVpReleaseNotes() { _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")] ); + } }); } }); |