aboutsummaryrefslogtreecommitdiff
path: root/src/app/main.js
blob: f4c162411d97e76a01fde7a65c38b365eb008dda (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
const fs = require("fs");
const path = require("path");
const Fuse = require("fuse.js");
const { app, ipcRenderer, shell } = require("electron");

const json = require("../modules/json");

const lang = require("../lang");
var modsobj = {
	all: [],
	enabled: [],
	disabled: []
}

let shouldInstallNorthstar = false;

// Base settings
//
// TODO: dont duplicate this, instead just use the one in
// `src/modules/settings.js` automatically
var settings = {
	nsargs: "",
	gamepath: "",
	nsupdate: true,
	autolang: true,
	forcedlang: "en",
	autoupdate: true,
	originkill: false,
	zip: "/northstar.zip",
	lang: navigator.language,

	linux_launch_cmd_ns: "",
	linux_launch_cmd_vanilla: "",
	linux_launch_method: "steam_auto",

	excludes: [
		"ns_startup_args.txt",
		"ns_startup_args_dedi.txt"
	]
}

// invokes `requests.get()` from `src/modules/requests.js` through the
// main process, and returns the output
async function request(...args) {
	return await ipcRenderer.invoke("request", ...args);
}

// Sets the lang to the system default
ipcRenderer.send("setlang", settings.lang);

// Loads the settings
if (fs.existsSync("viper.json")) {
	let iteration = 0;

	// loads the config file, it's loaded in an interval like this in
	// case the config file is currently being written to, if we were to
	// read from the file during that, then it'd be empty or similar.
	//
	// and because of that, we check if the file is empty when loading
	// it, if so we wait 100ms, then check again, and we keep doing that
	// hoping it'll become normal again. unless we've checked it 50
	// times, then we simply give up, and force the user to re-select
	// the gamepath, as this'll make the config file readable again.
	//
	// ideally it'll never have to check those 50 times, it's only in
	// case something terrible has gone awry, like if a write got
	// interrupted, or a user messed with the file.
	//
	// were it to truly be broken, then it'd take up to 5 seconds to
	// then reset, this is basically nothing, especially considering
	// this should only happen in very rare cases... hopefully!
	let config_interval = setInterval(() => {
		iteration++;

		config = json("viper.json") || {};

		// checks whether `settings.gamepath` is set, and if so,
		// it'll attempt to load ns_startup_args.txt
		let parse_settings = () => {
			// if gamepath is not set, attempt to set it
			if (settings.gamepath.length === 0) {
				setpath(false);
			} else {
				// if the gamepath is set, we'll simply tell the main
				// process about it, and it'll then show the main
				// renderer BrowserWindow
				setpath(true);
			}

			// filepath to Northstar's startup args file
			let args = path.join(settings.gamepath, "ns_startup_args.txt");

			// check file exists, and that no `nsargs` setting was set
			if (! settings.nsargs && fs.existsSync(args)) {
				// load arguments from file into `settings`
				settings.nsargs = fs.readFileSync(args, "utf8");
			}
		}

		// make sure config isn't empty
		if (Object.keys(config).length !== 0) {
			// add `config` to `settings`
			settings = {
				...settings,
				...config
			}; parse_settings();

			clearInterval(config_interval);
			return;
		}

		// we've attempted to load the config 50 times now, give up
		if (iteration >= 50) {
			// request a new gamepath be set
			setpath(false);
			clearInterval(config_interval);
		}
	}, 100)
} else {
	setpath();
}

ipcRenderer.on("changed-settings", (e, new_settings) => {
	// attempt to set `settings` to `new_settings`
	try {
		settings = {
			...settings,
			...new_settings
		}
	}catch(e) {}
})

// Show a toast message if no Internet connection has been detected.
if (! navigator.onLine) {
	ipcRenderer.send("no-internet");
}

function exit() {ipcRenderer.send("exit")}

let checked_for_ns_update = new Date().getTime();

function updateNorthstar() {
	checked_for_ns_update = new Date().getTime();
	ipcRenderer.send("update-northstar")
}

function force_update_ns() {
	ipcRenderer.send("update-northstar", true);
}

function reset_config() {
	ipcRenderer.send("reset-config");
}

function open_gamepath() {
	let open_path = require("electron").shell.openPath;
	if (settings.gamepath) {
		open_path(settings.gamepath);
	} else {
		alert(lang("gui.settings.miscbuttons.open_gamepath_alert"));
	}
}

function relaunch() {
	ipcRenderer.send("relaunch");
}

// Reports to the main process about game path status.
// @param {boolean} value is game path loaded
function setpath(value = false) {
	ipcRenderer.send("setpath", value);
}

// Tells the main process to launch or install Northstar
function launch() {
	if (shouldInstallNorthstar) {
		updateNorthstar();
		shouldInstallNorthstar = false;
	} else {
		ipcRenderer.send("launch-ns");
	}
}

// Tells the main process to launch the vanilla game
function launchVanilla() {ipcRenderer.send("launch-vanilla")}

let log = console.log;

// Disables or enables certain buttons when for example
// updating/installing Northstar.
function setButtons(state, enable_gamepath_btns) {
	playNsBtn.disabled = !state;

	let disablearray = (array) => {
		for (let i = 0; i < array.length; i++) {
			array[i].disabled = ! state;

			if (state) {
				array[i].classList.remove("disabled")
			} else {
				array[i].classList.add("disabled")
			}
		}
	}

	disablearray(document.querySelectorAll("#modsdiv .el button"));
	disablearray(document.querySelectorAll(".disable-when-installing"));
	disablearray(document.querySelectorAll(".playBtnContainer .playBtn"));
	disablearray(document.querySelectorAll("#nsMods .buttons.modbtns button"));
	disablearray(document.querySelectorAll("#browser #browserEntries .text button"));

	if (enable_gamepath_btns) {
		let gamepath_btns = 
			document.querySelectorAll('*[onclick="setpath()"]');

		for (let i = 0; i < gamepath_btns.length; i++) {
			gamepath_btns[i].disabled = false;
			gamepath_btns[i].classList.remove("disabled");
		}
	}
}

// `percent` should be a number between 0 to 100, if it's `false` it'll
// reset it back to nothing instantly, with no animatino
function set_ns_progress(percent) {
	// reset button progress
	if (percent === false) {
		document.querySelector(".contentContainer #nsMain .playBtn")
			.style.setProperty("--progress", "unset");

		return;
	}

	percent = parseInt(percent);

	// make sure we're dealing with a number
	if (isNaN(percent) || typeof percent !== "number") {
		return false;
	}

	// limit percent, while this barely has a difference, if you were to
	// set a very high number, the CSS would then use a very high
	// number, not great.
	if (percent > 100) {
		percent = 100;
	} else if (percent < 0) {
		percent = 0;
	}

	// invert number to it works in the CSS
	percent = 100 - percent;

	// set the CSS progress variable
	document.querySelector(".contentContainer #nsMain .playBtn")
		.style.setProperty("--progress", percent + "%");
}

ipcRenderer.on("set-buttons", (event, state) => {
	setButtons(state);
})

ipcRenderer.on("gamepath-lost", (event, state) => {
	page(0);
	setButtons(false, true);
	alert(lang("gui.gamepath.lost"));
})

// Frontend part of updating Northstar
ipcRenderer.on("ns-update-event", (event, options) => {
	let key = options.key;
	if (typeof options == "string") {
		key = options;
	}

	// updates text in update button to `lang(key)`
	let update_btn = () => {
		document.getElementById("update")
			.innerText = `(${lang(key)})`;
	}

	switch(key) {
		case "cli.update.uptodate_short":
		case "cli.update.no_internet":
			// initial value
			let delay = 0;

			// get current time
			let now = new Date().getTime();

			// check if `checked_for_ns_update` was less than 500ms
			// since now, this variable is set when `updateNorthstar()`
			// is called
			if (now - checked_for_ns_update < 500) {
				// if less than 500ms has passed, set `delay` to the
				// amount of milliseconds missing until we've hit that
				// 500ms threshold
				delay = 500 - (now - checked_for_ns_update);
			}

			// wait `delay`ms
			setTimeout(() => {
				// set buttons accordingly
				update_btn();
				setButtons(true);
				set_ns_progress(false);
				playNsBtn.innerText = lang("gui.launch");
			}, delay)

			break;
		default:
			update_btn();

			if (options.progress) {
				set_ns_progress(options.progress);
			}

			if (options.btn_text) {
				playNsBtn.innerText = options.btn_text;
			}

			setButtons(false);
			break;
	}
});

ipcRenderer.on("unknown-error", (event, err) => {
	new Toast({
		timeout: 10000,
		scheme: "error",
		title: lang("gui.toast.title.unknown_error"),
		description: lang("gui.toast.desc.unknown_error"),
		callback: () => {
			new Toast({
				timeout: 15000,
				scheme: "error",
				title: "",
				description: err.stack.replaceAll("\n", "<br>")
			})
		}
	})

	console.error(err.stack)
})

let installqueue = [];

// Tells the main process to install a mod through the file selector
function installmod() {
	setButtons(false);
	ipcRenderer.send("install-mod");
}

// Tells the main process to directly install a mod from this path
function installFromPath(path) {
	setButtons(false);
	ipcRenderer.send("install-from-path", path);
}

// Tells the main process to install a mod from a URL
function installFromURL(url, dependencies, clearqueue, author, package_name, version) {
	if (clearqueue) {installqueue = []};

	let prettydepends = [];

	if (dependencies) {
		let newdepends = [];
		for (let i = 0; i < dependencies.length; i++) {
			let depend = dependencies[i].toLowerCase();
			if (! depend.match(/northstar-northstar-.*/)) {
				depend = dependencies[i].replaceAll("-", "/");
				let pkg = depend.split("/");
				if (! isModInstalled(pkg[1])) {
					newdepends.push({
						pkg: depend,
						author: pkg[0],
						version: pkg[2],
						package_name: pkg[1]
					});

					prettydepends.push(`${pkg[1]} v${pkg[2]} - ${lang("gui.browser.made_by")} ${pkg[0]}`);
				}
			}
		}

		dependencies = newdepends;
	} 

	if (dependencies && dependencies.length != 0) {
		let confirminstall = confirm(lang("gui.mods.confirm_dependencies") + prettydepends.join("\n"));
		if (! confirminstall) {
			return
		}
	}

	setButtons(false);
	ipcRenderer.send("install-from-url", url, author, package_name, version);

	if (dependencies) {
		installqueue = dependencies;
	}
}

function isModInstalled(modname) {
	for (let i = 0; i < modsobj.all.length; i++) {
		let mod = modsobj.all[i];
		if (mod.manifest_name) {
			if (mod.manifest_name.match(modname)) {
				return true;
			}
		} else if (mod.name.match(modname)) {
			return true;
		}
	}

	return false;
}

// Frontend part of settings a new game path
ipcRenderer.on("newpath", (event, newpath) => {
	setButtons(true);
	settings.gamepath = newpath;
	ipcRenderer.send("gui-getmods");
	ipcRenderer.send("save-settings", settings);
})

// Continuation of log()
ipcRenderer.on("log", (event, msg) => {log(msg)})
ipcRenderer.on("alert", (event, data) => {
	alert(data.message);
	ipcRenderer.send("alert-closed-" + data.id);
})

ipcRenderer.on("confirm", (event, data) => {
	let confirmed = confirm(data.message);
	ipcRenderer.send("confirm-closed-" + data.id, confirmed);
})

let is_running = false;
ipcRenderer.on("is-running", (event, running) => {
	let set_playbtns = (text) => {
		let playbtns = document.querySelectorAll(".playBtn");
		for (let i = 0; i < playbtns.length; i++) {
			playbtns[i].innerHTML = text;
		}
	}

	if (running && is_running != running) {
		setButtons(false);
		set_playbtns(lang("general.running"));

		is_running = running;

		// show force quit button in Titanfall tab
		tfquit.style.display = "inline-block";

		update.setAttribute("onclick", "kill_game()");
		update.innerHTML = "(" + lang("ns.menu.force_quit") + ")";
		return;
	}

	if (is_running != running) {
		setButtons(true);
		set_playbtns(lang("gui.launch"));

		is_running = running;

		// hide force quit button in Titanfall tab
		tfquit.style.display = "none";

		update.setAttribute("onclick", "updateNorthstar()");
		update.innerHTML = "(" + lang("gui.update.check") + ")";
	}
})

function kill_game() {
	ipcRenderer.send("kill-game");
}

function kill_origin() {
	ipcRenderer.send("kill-origin");
}

function delete_request_cache() {
	ipcRenderer.send("delete-request-cache");
}

function delete_install_cache() {
	ipcRenderer.send("delete-install-cache");
}

// Updates the installed mods
ipcRenderer.on("mods", (event, mods_obj) => {
	modsobj = mods_obj;
	if (! mods_obj) {return}

	mods.load(mods_obj);
})

// Updates version numbers
ipcRenderer.on("version", (event, versions) => {
	vpversion.innerText = versions.vp;
	nsversion.innerText = versions.ns;
	tf2Version.innerText = versions.tf2;

	if (versions.ns == "unknown") {
		let buttons = document.querySelectorAll(".modbtns button");

		for (let i = 0; i < buttons.length; i++) {
			buttons[i].disabled = true;
		}

		// Since Northstar is not installed, we cannot launch it
		shouldInstallNorthstar = true;
		playNsBtn.innerText = lang("gui.install");
	}
}); ipcRenderer.send("get-version");

// When an update is available it'll ask the user about it
ipcRenderer.on("update-available", () => {
	if (confirm(lang("gui.update.available"))) {
		ipcRenderer.send("update-now");
	}
})

// Error out when no game path is set
ipcRenderer.on("no-path-selected", () => {
	alert(lang("gui.gamepath.must"));
	exit();
});

// Error out when game path is wrong
ipcRenderer.on("wrong-path", () => {
	alert(lang("gui.gamepath.wrong"));
	setpath(false);
});

setlang();

let dragtimer;
document.addEventListener("dragover", (e) => {
	e.preventDefault();
	e.stopPropagation();
	dragUI.classList.add("shown");

	clearTimeout(dragtimer);
	dragtimer = setTimeout(() => {
		dragUI.classList.remove("shown");
	}, 5000)
});

document.addEventListener("mouseover", (e) => {
	clearTimeout(dragtimer);
	dragUI.classList.remove("shown");
});

document.addEventListener("drop", (e) => {
	event.preventDefault();
	event.stopPropagation();

	dragUI.classList.remove("shown");
	installFromPath(event.dataTransfer.files[0].path);
});

document.body.addEventListener("keyup", (e) => {
	if (e.key == "Escape") {
		Browser.toggle(false);
		Settings.toggle(false);
	}
})

document.body.addEventListener("click", event => {
	if (event.target.tagName.toLowerCase() === "a" && event.target.protocol != "file:") {
		event.preventDefault();
		shell.openExternal(event.target.href);
	}
});