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
|
const path = require("path");
const fs = require("fs-extra");
const { ipcMain, Notification } = require("electron");
const cli = require("../cli");
const lang = require("../lang");
const win = require("./window");
const version = require("./version");
const settings = require("./settings");
const releases = require("./releases");
const gamepath = require("./gamepath");
const is_running = require("./is_running");
console = require("./console");
const unzip = require("unzipper");
const { https } = require("follow-redirects");
let update = {};
// renames excluded files to their original name
function restore_excluded_files() {
if (! gamepath.exists()) {return}
for (let i = 0; i < settings().excludes.length; i++) {
let exclude = path.join(settings().gamepath + "/" + settings().excludes[i]);
if (fs.existsSync(exclude + ".excluded")) {
fs.renameSync(exclude + ".excluded", exclude);
}
}
}; restore_excluded_files();
// renames excluded files to <file>.excluded, the list of files to be
// exluded is set in the settings (settings().excludes)
function exclude_files() {
for (let i = 0; i < settings().excludes.length; i++) {
let exclude = path.join(settings().gamepath + "/" + settings().excludes[i]);
if (fs.existsSync(exclude)) {
fs.renameSync(exclude, exclude + ".excluded");
}
}
}
// whether update.northstar_auto_update() has already been run before
let is_auto_updating = false;
// handles auto updating Northstar.
//
// it uses isGameRunning() to ensure it doesn't run while the game is
// running, as that may have all kinds of issues.
update.northstar_autoupdate = () => {
if (! settings().nsupdate || ! fs.existsSync("viper.json") || settings().gamepath.length === 0) {
return;
}
if (is_auto_updating) {return}
async function _checkForUpdates() {
is_auto_updating = true;
console.info(lang("cli.auto_updates.checking"));
// checks if NS is outdated
if (await northstar_update_available()) {
console.ok(lang("cli.auto_updates.available"));
if (await is_running.game()) {
console.error(lang("general.auto_updates.game_running"));
new Notification({
title: lang("gui.nsupdate.gaming.title"),
body: lang("gui.nsupdate.gaming.body")
}).show();
} else {
console.info(lang("cli.auto_updates.updating_ns"));
update.northstar();
}
} else {
console.info(lang("cli.auto_updates.no_update"));
}
setTimeout(
_checkForUpdates,
15 * 60 * 1000
// interval in between each update check
// by default 15 minutes.
);
}
_checkForUpdates();
}
// returns whether an update is available for Northstar
async function northstar_update_available() {
let local = version.northstar();
let distant = (await releases.latest.northstar()).version;
if (distant == false) {
return false;
}
// checks if NS is outdated
if (local !== distant) {
return true;
} else {
return false;
}
}
// updates Viper itself
//
// this uses electron updater to easily update and publish releases, it
// simply fetches it from GitHub and updates if it's outdated, very
// useful. Not much we have to do on our side.
update.viper = (autoinstall) => {
// stop if we're already in the process of updating
if (update.viper.updating) {
return;
}
update.viper.updating = true;
const { autoUpdater } = require("electron-updater");
if (! autoUpdater.isUpdaterActive()) {
update.viper.updating = false;
if (settings().nsupdate) {
update.northstar_autoupdate();
}
return cli.exit();
}
if (autoinstall) {
autoUpdater.on("update-downloaded", (info) => {
autoUpdater.quitAndInstall();
update.viper.updating = false;
});
}
autoUpdater.on("error", (info) => {
update.viper.updating = false;
cli.exit(1)
});
autoUpdater.on("update-not-available", (info) => {
update.viper.updating = false;
// only check for NS updates if Viper itself has no updates and
// if NS auto updates is enabled.
if (settings().nsupdate || cli.hasArgs()) {
update.northstar_autoupdate();
}
cli.exit();
});
autoUpdater.checkForUpdatesAndNotify();
}
update.viper.updating = false;
// removes all mods in "R2Northstar/mods" starting with "Northstar."
function remove_core_mods() {
if (! gamepath.exists()) {return}
// make sure the "R2Northstar/mods" folder exists, on top of making
// sure that, it is in fact a folder
let mod_dir = path.join(settings().gamepath, "R2Northstar/mods");
if (! fs.existsSync(mod_dir) && ! fs.statSync(mod_dir).isFile()) {
return
}
// get list of items in `mod_dir`
let mods = fs.readdirSync(mod_dir);
// run through list
for (let i = 0; i < mods.length; i++) {
// does the item starts with "Northstar."?
if (mods[i].match("Northstar\..*")) {
// remove the item
fs.rmSync(path.join(mod_dir, mods[i]), {
recursive: true
})
}
}
console.ok("Removed existing core mods!");
}
// installs/Updates Northstar
//
// if Northstar is already installed it'll be an update, otherwise it'll
// install it. It simply downloads the Northstar archive from GitHub, if
// it's outdated, then extracts it into the game path.
//
// as to handle not overwriting files we rename certain files to
// <file>.excluded, then rename them back after the extraction. The
// unzip module does not support excluding files directly.
//
// `force_install` makes this function not care about whether or not
// we're already up-to-date, forcing the install
update.northstar = async (force_install) => {
// stop if we're already in the process of updating
if (update.northstar.updating) {
return;
}
update.northstar.updating = true;
if (await is_running.game()) {
update.northstar.updating = false;
console.error(lang("general.auto_updates.game_running"));
return false;
}
if (! gamepath.exists()) {
update.northstar.updating = false;
return;
}
ipcMain.emit("ns-update-event", "cli.update.checking");
console.info(lang("cli.update.checking"));
let ns_version = version.northstar();
let latest = await releases.latest.northstar();
if (latest && latest.version == false) {
update.northstar.updating = false;
ipcMain.emit("ns-update-event", "cli.update.noInternet");
return;
}
// Makes sure it is not already the latest version
if (! force_install && ! await northstar_update_available()) {
ipcMain.emit("ns-update-event", "cli.update.uptodate_short");
console.ok(lang("cli.update.uptodate").replace("%s", ns_version));
win.log(lang("gui.update.uptodate"));
update.northstar.updating = false;
cli.exit();
return;
} else {
if (ns_version != "unknown") {
console.info(lang("cli.update.current"), ns_version);
}
}
exclude_files();
// start the download of the zip
https.get(latest.download_link, (res) => {
// cancel out if zip can't be retrieved and or found
if (res.statusCode !== 200) {
ipcMain.emit("ns-update-event", "cli.update.uptodate_short");
console.ok(lang("cli.update.uptodate"), ns_version);
update.northstar.updating = false;
return false;
}
let unknown_size = false;
let content_length_mb = 0.0;
let content_length = res.headers["content-length"];
// if content_length is `undefined`, we can't get the complete
// size (not all servers send the content-length)
if (content_length == undefined) {
unknown_size = true;
} else {
content_length = parseInt(content_length);
if (isNaN(content_length)) {
unknown_size = true;
} else {
content_length_mb = (content_length / 1024 / 1024).toFixed(1);
}
}
console.info(lang("cli.update.downloading") + ":", latest.version);
ipcMain.emit("ns-update-event", {
progress: 0,
btn_text: "1/2",
key: "cli.update.downloading",
});
let tmp = path.dirname(settings().zip);
if (fs.existsSync(tmp)) {
if (! fs.statSync(tmp).isDirectory()) {
fs.rmSync(tmp);
}
} else {
fs.mkdirSync(tmp);
if (fs.existsSync(settings().zip)) {
fs.rmSync(settings().zip);
}
}
let stream = fs.createWriteStream(settings().zip);
res.pipe(stream);
let received = 0;
res.on("data", (chunk) => {
received += chunk.length;
let received_mb = (received / 1024 / 1024).toFixed(1);
let percentage_str = "";
let current_percentage = 0;
let key = lang("gui.update.downloading") + " " + received_mb + "mb";
if (unknown_size === false) {
key += " / " + content_length_mb + "mb";
current_percentage = Math.floor(received_mb / content_length_mb * 100);
percentage_str = " - " + current_percentage + "%";
}
ipcMain.emit("ns-update-event", {
key: key,
progress: current_percentage,
btn_text: "1/2" + percentage_str
});
})
stream.on("finish", () => {
remove_core_mods();
stream.close();
let extract = fs.createReadStream(settings().zip);
win.log(lang("gui.update.extracting"));
ipcMain.emit("ns-update-event", {
progress: 0,
btn_text: "2/2 - 0%",
key: lang("gui.update.extracting")
});
console.ok(lang("cli.update.download_done"));
// extracts the zip, this is the part where we're actually
// installing Northstar.
extract.pipe(unzip.Extract({path: settings().gamepath}))
let extracted = 0;
let size = received;
let size_mb = (size / 1024 / 1024).toFixed(1);
extract.on("data", (chunk) => {
extracted += chunk.length;
let percent = Math.floor(extracted / size * 100);
let extracted_mb = (extracted / 1024 / 1024).toFixed(1);
ipcMain.emit("ns-update-event", {
progress: percent,
btn_text: "2/2 - " + percent + "%",
key: lang("gui.update.extracting") +
" " + extracted_mb + "mb / " + size_mb + "mb"
});
})
extract.on("end", () => {
extract.close();
ipcMain.emit("getversion");
restore_excluded_files();
ipcMain.emit("gui-getmods");
ipcMain.emit("get-version");
ipcMain.emit("ns-update-event", "cli.update.uptodate_short");
win.log(lang("gui.update.finished"));
console.ok(lang("cli.update.finished"));
update.northstar.updating = false;
cli.exit();
})
})
})
}
update.northstar.updating = false;
module.exports = update;
|