aboutsummaryrefslogtreecommitdiff
path: root/src/index.js
blob: c3d35569c8c8609fe8ca782cbf3035cec171b244 (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
const fs = require("fs");
const path = require("path");
const { autoUpdater } = require("electron-updater");
const { app, ipcMain, BrowserWindow, dialog } = require("electron");

// ensures PWD/CWD is the config folder where viper.json is located
process.chdir(app.getPath("appData"));

const utils = require("./utils");
const cli = require("./cli");
const settings = require("./modules/settings");
const requests = require("./modules/requests");

var log = console.log;

// Starts the actual BrowserWindow, which is only run when using the
// GUI, for the CLI this function is never called.
function start() {
	win = new BrowserWindow({
		width: 1000,
		height: 600,
		title: "Viper",

		// Hides the window initially, it'll be shown when the DOM is
		// loaded, as to not cause visual issues.
		show: false,

		// In the future we may want to allow the user to resize the window,
		// as it's fairly responsive, but for now we won't allow that.
		resizable: false,

		frame: false,
		titleBarStyle: "hidden",
		icon: path.join(__dirname, "assets/icons/512x512.png"),
		webPreferences: {
			webviewTag: true,
			nodeIntegration: true,
			contextIsolation: false,
		},
	});

	// when --devtools is added it'll open the dev tools
	if (cli.hasParam("devtools")) {win.openDevTools()}

	// general setup
	win.removeMenu();
	win.loadFile(__dirname + "/app/index.html");

	win.send = (channel, data) => {
		win.webContents.send(channel, data);
	}; send = win.send;

	ipcMain.on("exit", () => {
		if (settings.originkill) {
			utils.isOriginRunning().then((running) => {
				if (running) {
					utils.killOrigin().then(process.exit(0))
				} else {
					process.exit(0)	
				}
			})
		} else {
			process.exit(0)
		}
	});
	ipcMain.on("minimize", () => {win.minimize()});
	ipcMain.on("relaunch", () => {app.relaunch(); app.exit()});

	// passthrough to renderer from main
	ipcMain.on("win-log", (event, ...args) => {send("log", ...args)});
	ipcMain.on("win-alert", (event, ...args) => {send("alert", ...args)});

	// mod states
	ipcMain.on("duped-mod", (event, modname) => {send("duped-mod", modname)});
	ipcMain.on("failed-mod", (event, modname) => {send("failed-mod", modname)});
	ipcMain.on("removed-mod", (event, modname) => {send("removed-mod", modname)});
	ipcMain.on("gui-getmods", (event, ...args) => {send("mods", utils.mods.list())});
	ipcMain.on("installed-mod", (event, modname) => {send("installed-mod", modname)});
	ipcMain.on("no-internet", () => {send("no-internet")});

	process.on("uncaughtException", (err) => {
		send("unknown-error", err);
		console.error(err);
	});

	// install calls
	ipcMain.on("install-from-path", (event, path) => {utils.mods.install(path)});
	ipcMain.on("install-from-url", (event, url, author) => {utils.mods.installFromURL(url, author)});

	win.webContents.on("dom-ready", () => {
		send("mods", utils.mods.list());
	});

	// ensures gamepath still exists and is valid on startup
	let gamepathlost = false;
	ipcMain.on("gamepath-lost", (event, ...args) => {
		if (! gamepathlost) {
			gamepathlost = true;
			send("gamepath-lost");
		}
	});

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

	// allows renderer to check for updates
	ipcMain.on("ns-update-event", (event) => {send("ns-update-event", event)});
	ipcMain.on("can-autoupdate", () => {
		if (! autoUpdater.isUpdaterActive() || cli.hasParam("no-vp-updates")) {
			send("cant-autoupdate");
		}
	})

	// start auto-update process
	if (settings.autoupdate) {
		if (cli.hasParam("no-vp-updates")) {
			utils.handleNorthstarUpdating();
		} else {
			utils.updateViper(false)
		}
	} else {
		utils.handleNorthstarUpdating();
	}

	autoUpdater.on("update-downloaded", () => {
		send("update-available");
	});

	// updates and restarts Viper, if user says yes to do so.
	// otherwise it'll do it on the next start up.
	ipcMain.on("update-now", () => {
		autoUpdater.quitAndInstall();
	})
}

// General events used to handle utils.js stuff without requiring the
// module inside the file that sent the event. {
ipcMain.on("install-mod", () => {
	if (cli.hasArgs()) {
		utils.mods.install(cli.param("installmod"));
	} else {
		dialog.showOpenDialog({properties: ["openFile"]}).then(res => {
			if (res.filePaths.length != 0) {
				utils.mods.install(res.filePaths[0]);
			} else {
				send("set-buttons", true);
			}
		}).catch(err => {error(err)});
	}
})

ipcMain.on("remove-mod", (event, mod) => {utils.mods.remove(mod)});
ipcMain.on("toggle-mod", (event, mod) => {utils.mods.toggle(mod)});

ipcMain.on("launch-ns", () => {utils.launch()});
ipcMain.on("launch-vanilla", () => {utils.launch("vanilla")});

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

ipcMain.on("update-northstar", () => {utils.updateNorthstar()})
ipcMain.on("setpath-cli", () => {utils.setpath()});
ipcMain.on("setpath", (event, value) => {
	if (! value) {
		if (! win.isVisible()) {
			utils.setpath(win);
		} else {
			utils.setpath(win, true);
		}
	} else if (! win.isVisible()) {
		win.show();
	}
});

// retrieves various local version numbers
function sendVersionsInfo() {
	send("version", {
		ns: utils.getNSVersion(),
		tf2: utils.getTF2Version(),
		vp: "v" + require("../package.json").version
	});
}

// sends the version info back to the renderer
ipcMain.on("get-version", () => {sendVersionsInfo()});

// prints out version info for the CLI
ipcMain.on("version-cli", () => {
	log("Viper: v" + require("../package.json").version);
	log("Northstar: " + utils.getNSVersion());
	log("Node: " + process.version);
	log("Electron: v" + process.versions.electron);
	cli.exit();
})

// sends installed mods info to renderer
ipcMain.on("getmods", () => {
	let mods = utils.mods.list();
	if (mods.all.length > 0) {
		log(`${utils.lang("general.mods.installed")} ${mods.all.length}`);
		log(`${utils.lang("general.mods.enabled")} ${mods.enabled.length}`);
		for (let i = 0; i < mods.enabled.length; i++) {
			log(`  ${mods.enabled[i].Name} ${mods.enabled[i].Version}`);
		}

		if (mods.disabled.length > 0) {
			log(`${utils.lang("general.mods.disabled")} ${mods.disabled.length}`);
			for (let i = 0; i < mods.disabled.length; i++) {
				log(`  ${mods.disabled[i].Name} ${mods.disabled[i].Version}`);
			}
		}
		cli.exit(0);
	} else {
		log("No mods installed");
		cli.exit(0);
	}
})
// }

// allows renderer to set a new renderer
ipcMain.on("newpath", (event, newpath) => {
	if (newpath === false && ! win.isVisible()) {
		win.send("no-path-selected");
	} else {
		sendVersionsInfo();
		if (!win.isVisible()) {
			win.show();
		}
	}
}); ipcMain.on("wrong-path", () => {
	win.send("wrong-path");
});

// starts the GUI or CLI
if (cli.hasArgs()) {
	if (cli.hasParam("update-viper")) {
		utils.updateViper(true);
	} else {
		cli.init();
	}
} else {
	app.on("ready", () => {
		app.setPath("userData", path.join(app.getPath("cache"), app.name));
		start();
	})
}

// returns cached requests
ipcMain.on("get-ns-notes", async () => {
	win.send("ns-notes", await requests.getNsReleaseNotes());
});

ipcMain.on("get-vp-notes", async () => {
	win.send("vp-notes", await requests.getVpReleaseNotes());
});