aboutsummaryrefslogtreecommitdiff
path: root/src/modules/launch.js
blob: fab80c709dd295fd6b0d9996f987d1f59ce49b38 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const { ipcMain, shell } = require("electron");
const { exec, execSync } = require("child_process");

const cli = require("../cli");
const win = require("../win");
const lang = require("../lang");

const in_path = require("./in_path");
const settings = require("./settings");

console = require("./console");

ipcMain.on("launch", (_, game_version) => {
	launch(game_version)
})

// launches the game
//
// either Northstar or Vanilla. Linux support is not currently a thing,
// however it'll be added at some point.
function launch(game_version, method = settings().linux_launch_method) {
	console.info(
		lang("general.launching"),
		game_version || "Northstar" + "..."
	)

	// change current directory to gamepath
	process.chdir(settings().gamepath);

	let launch_args = settings().nsargs || "";

	// add `-vanilla` or `-northstar` depending on `game_version`
	if (game_version == "vanilla") {
		launch_args += " -vanilla"
	} else {
		launch_args += " -northstar"
	}


	// Linux launch support
	if (process.platform == "linux") {
		let flatpak_id = "com.valvesoftware.Steam";
		let steam_run_str = `steam://run/1237970//${launch_args}/`;
		console.log(steam_run_str)

		// returns whether the Flatpak version of Steam is installed
		let flatpak_steam_installed = () => {
			// this will throw an error if the command fails,
			// either because of an error with the command, or
			// because it's not installed, either way,
			// indicating it's not installed
			try {
				execSync(
					`flatpak info ${flatpak_id}`
				)

				return true;
			}catch(err) {}

			return false;
		}

		switch(method) {
			case "steam_auto":
				// if a Steam executable is found, use that
				if (in_path("steam")) {
					return launch(game_version, "steam_executable");

				// is Flatpak (likely) installed?
				} else if (in_path("flatpak")) { 

					if (flatpak_steam_installed()) {
						return launch(game_version, "steam_flatpak");
					}
				}

				// fallback to Steam protocol
				return launch(game_version, "steam_protocol");

			case "steam_flatpak":
				// make sure Flatpak is installed, and show an error
				// toast if not
				if (! in_path("flatpak")) {
					win().toast({
						scheme: "error",
						title: lang("gui.toast.title.missing_flatpak"),
						description: lang(
							"gui.toast.desc.missing_flatpak"
						)
					})
					return;
				}

				// make sure the Flatpak version of Steam is installed,
				// and show an error toast if not
				if (! flatpak_steam_installed()) {
					win().toast({
						scheme: "error",
						title: lang(
							"gui.toast.title.missing_flatpak_steam"
						),
						description: lang(
							"gui.toast.desc.missing_flatpak_steam"
						)
					})
					return;
				}

				// attempt to launch the game with Flatpak Steam
				exec(`flatpak run ${flatpak_id} "${steam_run_str}"`, {

					cwd: settings().gamepath
				})

				return;

			case "steam_executable":
				// make sure Steam is installed, and show an error toast
				// if not
				if (! in_path("steam")) {
					win().toast({
						scheme: "error",
						title: lang("gui.toast.title.missing_steam"),
						description: lang(
							"gui.toast.desc.missing_steam"
						)
					})
					return;
				}

				// attempt to launch the game with the Steam executable
				exec(`steam "${steam_run_str}"`, {
					cwd: settings().gamepath
				})

				return;

			case "steam_protocol":
				// attempt to launch the game with the Steam protocol
				shell.openExternal(steam_run_str)

				return;
		}

		// launch Vanilla with custom command
		let command = settings().linux_launch_cmd_ns;
		if (game_version == "vanilla") {
			command = settings().linux_launch_cmd_vanilla;
		}

		// make sure the custom command is not just whitespace, and show
		// an error toast if it is
		if (! command.trim()) {
			win().toast({
				scheme: "error",
				title: lang("gui.toast.title.missing_launch_command"),
				description: lang(
					"gui.toast.desc.missing_launch_command"
				)
			})

			return;
		}

		// launch Northstar with custom command
		try {
			exec(command, {
				cwd: settings().gamepath,
				env: {
					...process.env,
					TF_ARGS: launch_args
				}
			})
		}catch(err) { 
			// show error if custom commands fails
			// this should basically never trigger, it should only
			// trigger if `command` isn't set to something valid
			win().toast({
				scheme: "error",
				title: lang("gui.toast.title.failed_launch_command"),
				description: lang(
					"gui.toast.desc.failed_launch_command"
				)
			})
		}

		return;
	}

	// default launches with Northstar
	let executable = "NorthstarLauncher.exe"

	// change over to using vanilla executable
	if (game_version == "vanilla") {
		executable = "Titanfall2.exe"
	}

	// launch executable/game
	exec(executable + " " + launch_args, {
		cwd: settings().gamepath
	})
}

module.exports = launch;