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

const Emitter = require("events");
const events = new Emitter();

const utils = require("./utils");
const cli = require("./cli");

function start() {
	let width = 600;
	win = new BrowserWindow({
		width: width,
		height: 115,
		show: false,
		title: "Viper",
		resizable: false,
		titleBarStyle: "hidden",
		icon: path.join(__dirname, "assets/icons/512x512.png"),
		webPreferences: {
			nodeIntegration: true,
			contextIsolation: false,
		},
	}); 

	if (cli.hasParam("debug")) {win.openDevTools()}

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

	ipcMain.on("exit", () => {process.exit(0)})
	ipcMain.on("setsize", (event, height) => {
		win.setSize(width, height);
	})

	ipcMain.on("ns-updated", () => {win.webContents.send("ns-updated")})
	ipcMain.on("ns-updating", () => {win.webContents.send("ns-updating")})
	ipcMain.on("winLog", (event, ...args) => {win.webContents.send("log", ...args)})
	ipcMain.on("winAlert", (event, ...args) => {win.webContents.send("alert", ...args)})
	ipcMain.on("guigetmods", (event, ...args) => {win.webContents.send("mods", utils.mods.list())})

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

	if (utils.settings.autoupdate) {utils.updatevp(false)}

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

	ipcMain.on("updatenow", () => {
		autoUpdater.quitAndInstall();
	})
}

ipcMain.on("installmod", () => {
	if (cli.hasArgs()) {
		utils.mods.install(cli.param("installmod"))
	} else {
		dialog.showOpenDialog({properties: ["openFile"]}).then(res => {
			utils.mods.install(res.filePaths[0]);
		}).catch(err => {console.error(err)})
	}
})

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

ipcMain.on("launch", (event) => {utils.launch()})
ipcMain.on("setlang", (event, lang) => {utils.setlang(lang)})
ipcMain.on("launchVanilla", (event) => {utils.launch("vanilla")})

ipcMain.on("update", (event) => {utils.update()})
ipcMain.on("setpathcli", (event) => {utils.setpath()});
ipcMain.on("setpath", (event, value) => {
	if (!value) {
		utils.setpath(win)
	} else if (!win.isVisible()) {
		win.show();
	}
});

ipcMain.on("newpath", (event, newpath) => {
	if (newpath === false && !win.isVisible()) {
		win.webContents.send("nopathselected");
	} else {
		if (!win.isVisible()) {
			win.show();
		}
	}
}); ipcMain.on("wrongpath", (event) => {
	win.webContents.send("wrongpath");
});

ipcMain.on("getversion", () => {
	win.webContents.send("version", {
		ns: utils.getNSVersion(),
		vp: "v" + require("../package.json").version
	});
});

ipcMain.on("versioncli", () => {
	console.log("Viper: v" + require("../package.json").version);
	console.log("Northstar: " + utils.getNSVersion());
	console.log("Node: " + process.version);
	console.log("Electron: v" + process.versions.electron);
	cli.exit();
})

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

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

process.chdir(app.getPath("appData"));

if (cli.hasArgs()) {
	if (cli.hasParam("updatevp")) {
		utils.updatevp(true);
	} else {
		cli.init();
	}
} else {
	app.on("ready", () => {
		app.setPath("userData", path.join(app.getPath("cache"), app.name));
		start();
	})
}