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 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 217 insertions(+) create mode 100644 src/extras/findgame.js create mode 100644 src/extras/requests.js (limited to 'src/extras') 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 +}; -- cgit v1.2.3 From 77c03b45a5dd6861f23043221315c3016b615753 Mon Sep 17 00:00:00 2001 From: 3top1a <3top1a.official@gmail.com> Date: Sun, 23 Jan 2022 11:43:28 +0100 Subject: Repaired vdf reading - os nor app was imported - returned the entire vdf file instead --- src/extras/findgame.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/extras') diff --git a/src/extras/findgame.js b/src/extras/findgame.js index 029914a..426d2e3 100644 --- a/src/extras/findgame.js +++ b/src/extras/findgame.js @@ -1,6 +1,7 @@ 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); @@ -30,7 +31,6 @@ module.exports = () => { // 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]) @@ -45,13 +45,15 @@ module.exports = () => { 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()} + let read_vdf = readvdf(data.toString()) + if (read_vdf ) {return read_vdf} } 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()} + let data = fs.readFileSync(path.join(app.getPath("home"), "/.steam/steam/steamapps/libraryfolders.vdf")) + let read_vdf = readvdf(data.toString()) + if (read_vdf ) {return read_vdf} } break; } -- cgit v1.2.3 From c92db733034a549e53ebf2c83aaea828ca61697a Mon Sep 17 00:00:00 2001 From: Rémy Raes Date: Sun, 23 Jan 2022 20:05:37 +0100 Subject: fix: Finding Windows game folder (#55) --- src/extras/findgame.js | 16 ++++++++-------- src/utils.js | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) (limited to 'src/extras') diff --git a/src/extras/findgame.js b/src/extras/findgame.js index 426d2e3..f2d7be1 100644 --- a/src/extras/findgame.js +++ b/src/extras/findgame.js @@ -6,7 +6,7 @@ const { app } = require("electron"); const util = require("util"); const exec = util.promisify(require("child_process").exec); -module.exports = () => { +module.exports = async () => { let gamepath = ""; // Autodetect path @@ -14,13 +14,13 @@ module.exports = () => { // 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 : ",""); - }); + 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) {} diff --git a/src/utils.js b/src/utils.js index bc0f66c..a2c61d4 100644 --- a/src/utils.js +++ b/src/utils.js @@ -136,8 +136,8 @@ async function setpath(win) { ipcMain.emit("newpath", null, settings.gamepath); } - let gamepath = findgame(); - if (findgame()) { + let gamepath = await findgame(); + if (gamepath) { setGamepath(gamepath); return; } -- cgit v1.2.3 From 1362df77adf25a941cc31d3a82378f6911728f10 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Sun, 23 Jan 2022 21:20:43 +0100 Subject: simplify VDF code --- src/extras/findgame.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'src/extras') diff --git a/src/extras/findgame.js b/src/extras/findgame.js index f2d7be1..f7da1c0 100644 --- a/src/extras/findgame.js +++ b/src/extras/findgame.js @@ -41,21 +41,20 @@ module.exports = async () => { } } + let folder = ""; 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") - let read_vdf = readvdf(data.toString()) - if (read_vdf ) {return read_vdf} - } - break; + folder = "C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf"; + break case "linux": - if (fs.existsSync(path.join(app.getPath("home"), "/.steam/steam/steamapps/libraryfolders.vdf"))) { - let data = fs.readFileSync(path.join(app.getPath("home"), "/.steam/steam/steamapps/libraryfolders.vdf")) - let read_vdf = readvdf(data.toString()) - if (read_vdf ) {return read_vdf} - } - break; + folder = path.join(app.getPath("home"), "/.steam/steam/steamapps/libraryfolders.vdf"); + break + } + + if (fs.existsSync(folder)) { + let data = fs.readFileSync(folder) + let read_vdf = readvdf(data.toString()) + if (read_vdf ) {return read_vdf} } if (gamepath) { -- cgit v1.2.3 From 18d1166b6b8beef422590607cd02d613dd02d2d0 Mon Sep 17 00:00:00 2001 From: 3top1a <57371001+3top1a@users.noreply.github.com> Date: Mon, 24 Jan 2022 14:27:29 +0100 Subject: Edge case in vdf reading (#56) * Edge case * Alert user only when automatic detection failed * extra info on not found message * support for [Free/Open]BSD Co-authored-by: 0neGal --- .github/FUNDING.yml | 2 +- src/app/main.js | 1 - src/extras/findgame.js | 6 ++++-- src/lang/en.json | 2 +- src/utils.js | 2 ++ 5 files changed, 8 insertions(+), 5 deletions(-) (limited to 'src/extras') diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 96f466a..8991fb3 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,2 +1,2 @@ liberapay: 0neGal -custom: ["https://github.com/R2Northstar"] +custom: ["github.com/R2Northstar"] diff --git a/src/app/main.js b/src/app/main.js index 5f0cc9a..dab584e 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -32,7 +32,6 @@ if (fs.existsSync("viper.json")) { setpath(true); } } else { - alert(lang("general.missingpath")); setpath(); } diff --git a/src/extras/findgame.js b/src/extras/findgame.js index f7da1c0..42c9b85 100644 --- a/src/extras/findgame.js +++ b/src/extras/findgame.js @@ -41,17 +41,19 @@ module.exports = async () => { } } - let folder = ""; + 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)) { + if (fs.existsSync(folder) && folder) { let data = fs.readFileSync(folder) let read_vdf = readvdf(data.toString()) if (read_vdf ) {return read_vdf} diff --git a/src/lang/en.json b/src/lang/en.json index c2b6f98..8d4daeb 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -96,7 +96,7 @@ "general.mods.enabled": "Enabled mods:", "general.mods.disabled": "Disabled mods:", "general.mods.installed": "Installed mods:", - "general.missingpath": "Game path is not set!", + "general.missingpath": "Game location could not be found automatically! Please select it manually!", "general.notinstalled": "Northstar is not installed!", "general.launching": "Launching" } diff --git a/src/utils.js b/src/utils.js index a2c61d4..ef647fe 100644 --- a/src/utils.js +++ b/src/utils.js @@ -142,6 +142,8 @@ async function setpath(win) { return; } + alert(lang("general.missingpath")); + // Fallback to manual selection dialog.showOpenDialog({properties: ["openDirectory"]}).then(res => { if (res.canceled) { -- cgit v1.2.3