aboutsummaryrefslogtreecommitdiff
path: root/src/modules/settings.js
blob: d513ec98cd9c536b5a47d7f1648f4476e92b2f05 (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
const fs = require("fs");
const path = require("path");
const { app, ipcMain } = require("electron");

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

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

var invalid_settings = false;

ipcMain.on("save-settings", (event, obj) => {
	save(obj, false);
})

ipcMain.on("reset-config", async () => {
	let confirmation = await win().confirm(
		lang("gui.settings.miscbuttons.reset_config_alert")
	)

	if (confirmation) {
		fs.rmSync("viper.json");

		app.relaunch({
			args: process.argv.slice(1)
		})

		app.exit(0);
	}
})

ipcMain.on("setlang", (event, lang) => {
	set("lang", lang);
	save();
})

// base settings
var settings = {
	gamepath: "",
	lang: "en-US",
	nsupdate: true,
	autolang: true,
	forcedlang: "en",
	autoupdate: true,
	originkill: false,
	nsargs: "-multiple",
	zip: "/northstar.zip",

	// these files won't be overwritten when installing/updating
	// Northstar, useful for config files
	excludes: [
		"ns_startup_args.txt",
		"ns_startup_args_dedi.txt"
	]
}

// creates the settings file with the base settings if it doesn't exist.
if (fs.existsSync("viper.json")) {
	let conf = json("viper.json");

	// validates viper.json
	if (! conf) {
		invalid_settings = true;
	}

	settings = {
		...settings, ...conf
	}

	settings.zip = path.join(app.getPath("userData"), "Temp/northstar.zip");

	let args = path.join(settings.gamepath, "ns_startup_args.txt");
	if (! settings.nsargs && fs.existsSync(args)) {
		settings.nsargs = fs.readFileSync(args, "utf8");
	}
} else {
	console.error(lang("general.missing_path"));
}

// as to not have to do the same one liner a million times, this
// function exists, as the name suggests, it simply writes the current
// settings to the disk.
//
// you can also pass a settings object to the function and it'll try and
// merge it together with the already existing settings
let save = (obj = {}, notify_renderer = true) => {
	// refuse to save if settings aren't valid
	if (invalid_settings) {
		settings = {};
	}

	let settings_content = {
		...settings, ...obj
	}

	let stringified_settings = JSON.stringify({
		...settings, ...obj
	})

	let settings_file = app.getPath("appData") + "/viper.json";

	// write the settings file
	fs.writeFileSync(settings_file, stringified_settings);

	// set the settings obj for the main process
	settings = settings_content;
	
	if (notify_renderer) {
		send("changed-settings", obj);
	}
}

// sets `key` in `settings` to `value`
let set = (key, value) => {
	settings[key] = value;
}

// returns up-to-date `settings`, along with `set()` and `save()`
let export_func = () => {
	return {
		...settings,

		set,
		save
	}
}

// add properties from `settings` to `export_func`, this is just for
// backwards compatibility, and they really shouldn't be relied on,
// this'll likely be removed at some point
for (let i in settings) {
	export_func[i] = settings[i];
}

module.exports = export_func;