From b1b7d260a1a333618c045d3c43b7af5573c274a3 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Sat, 22 Jan 2022 19:10:08 +0100 Subject: initial review of VDF parsing --- src/utils.js | 46 +++++++++++++++------------------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/src/utils.js b/src/utils.js index 86ea5ae..521931d 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,6 +1,5 @@ const path = require("path"); const fs = require("fs-extra"); -const os = require("os") const copy = require("copy-dir"); const { app, dialog, ipcMain, Notification } = require("electron"); @@ -11,11 +10,11 @@ const cli = require("./cli"); const lang = require("./lang"); const requests = require("./requests"); +const vdf = require("simple-vdf"); const unzip = require("unzipper"); const run = require("child_process").spawn; const exec = require("child_process").exec; const { https } = require("follow-redirects"); -const vdf = require('simple-vdf'); process.chdir(app.getPath("appData")); @@ -122,30 +121,19 @@ function setpath(win) { settings.gamepath = cli.param("setpath"); } else { // GUI // Autodetect path - - // Windows only using powershell and windows registery // Get-Item -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Respawn\Titanfall2\ - // https://nodejs.org/api/os.html#osplatform - if (os.platform() == "win32") - { - // TODO - } - - // Detect using - // Windows: C:\Program Files (x86)\Steam\steamapps - // Linux: ~/.steam/steam/steamapps/ - // Mac: ~/Library/Application\ Support/Steam/steamapps + if (process.platform == "win32") {/* TODO */} - function read_libraryfolders(read_data) - { + // Detect using Steam VDF + function readvdf(data) { // Parse read_data - data = vdf.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]) + 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 @@ -163,26 +151,22 @@ function setpath(win) { } } - switch (os.platform()) { + switch (process.platform) { case "win32": - if (fs.existsSync( "C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf")) - { - let read_data = fs.readFileSync("C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf") - if (read_libraryfolders(read_data.toString()) == 1) - return + 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()) == 1) {return} } break; case "linux": - if (fs.existsSync( os.homedir() + "/.steam/steam/steamapps/libraryfolders.vdf")) - { - let read_data = fs.readFileSync(os.homedir() + "/.steam/steam/steamapps/libraryfolders.vdf") - if (read_libraryfolders(read_data.toString()) == 1) - return + 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()) == 1) {return} } break; } - // Let user choose manually + // Fallback to manual selection dialog.showOpenDialog({properties: ["openDirectory"]}).then(res => { if (res.canceled) { ipcMain.emit("newpath", null, false); -- cgit v1.2.3