aboutsummaryrefslogtreecommitdiff
path: root/src/modules/findgame.js
blob: f8d0126121e40e8402c8b56f4194f13fb36f5388 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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);

console = require("./console");

module.exports = async () => {
	let gamepath = "";
	
	// autodetect path through Powershell and Windows registry
	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"});

			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) {}
	}

	// reads, then parses VDF files, to search for Titanfall
	function readvdf(data) {
		data = vdf.parse(data); // parse read_data

		// verify VDF was parsed correctly
		if (! data || typeof data !== "object") {
			return;
		}

		// list of folders where the game could possibly be installed at
		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.ok("Found game in:", data_array[0]);
				return data_array[0] + "/steamapps/common/Titanfall2";
			} else {
				console.error("Game not in:", data_array[0]);
			}
		}
	}

	let vdf_files = [];

	// set `folders` to paths where the VDF file can be
	switch (process.platform) {
		case "win32":
			vdf_files = ["C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf"];
			break
		case "linux":
		case "openbsd":
		case "freebsd":
			let home = app.getPath("home");
			vdf_files = [
				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
	}

	// searches VDF files
	for (let i = 0; i < vdf_files.length; i++) {
		if (! fs.existsSync(vdf_files[i])) {continue}
		console.info("Searching VDF file at:", vdf_files[i]);

		let data = fs.readFileSync(vdf_files[i]);
		let read_vdf = readvdf(data.toString());
		if (read_vdf) {return read_vdf}
	}

	return gamepath || false;
}