aboutsummaryrefslogtreecommitdiff
path: root/src/extras/findgame.js
blob: 426d2e32513ccbcd2b8e075f85d7992754a66195 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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 = () => {
	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);

		// `.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")
				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(path.join(app.getPath("home"), "/.steam/steam/steamapps/libraryfolders.vdf"))
				let read_vdf = readvdf(data.toString())
				if (read_vdf ) {return read_vdf}
			}
			break;	
	}

	if (gamepath) {
		return gamepath;
	} else {
		return false;
	}
}