-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathApplication.ts
1130 lines (1047 loc) · 43 KB
/
Application.ts
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {setApplicationVersion, setInstallType, setInstanceId, setWarnedAdmin} from '../actions/app';
import { NEXUS_DOMAIN } from '../extensions/nexus_integration/constants';
import { STATE_BACKUP_PATH } from '../reducers/index';
import { ThunkStore } from '../types/IExtensionContext';
import type { IPresetStep, IPresetStepHydrateState } from '../types/IPreset';
import {IState} from '../types/IState';
import { getApplication } from '../util/application';
import commandLine, {IParameters, ISetItem, relaunch} from '../util/commandLine';
import { DataInvalid, DocumentsPathMissing, ProcessCanceled,
UserCanceled } from '../util/CustomErrors';
import * as develT from '../util/devel';
import { didIgnoreError, disableErrorReport, getVisibleWindow, setOutdated, setWindow,
terminate, toError } from '../util/errorHandling';
import ExtensionManagerT from '../util/ExtensionManager';
import { validateFiles } from '../util/fileValidation';
import * as fs from '../util/fs';
import getVortexPath, { setVortexPath } from '../util/getVortexPath';
import lazyRequire from '../util/lazyRequire';
import LevelPersist, { DatabaseLocked } from '../util/LevelPersist';
import {log, setLogPath, setupLogging} from '../util/log';
import { prettifyNodeErrorMessage, showError } from '../util/message';
import migrate from '../util/migrate';
import presetManager from '../util/PresetManager';
import { StateError } from '../util/reduxSanity';
import startupSettings from '../util/startupSettings';
import { allHives, createFullStateBackup, createVortexStore, currentStatePath, extendStore,
finalizeStoreWrite,
importState, insertPersistor, markImported, querySanitize } from '../util/store';
import {} from '../util/storeHelper';
import SubPersistor from '../util/SubPersistor';
import { isMajorDowngrade, replaceRecursive, spawnSelf, timeout, truthy } from '../util/util';
import { addNotification, setCommandLine, showDialog } from '../actions';
import MainWindowT from './MainWindow';
import SplashScreenT from './SplashScreen';
import TrayIconT from './TrayIcon';
import * as msgpackT from '@msgpack/msgpack';
import Promise from 'bluebird';
import crashDumpT from 'crash-dump';
import {app, crashReporter as crashReporterT, dialog, ipcMain, protocol, shell} from 'electron';
import contextMenu from 'electron-context-menu';
import isAdmin = require('is-admin');
import * as _ from 'lodash';
import * as os from 'os';
import * as path from 'path';
import * as permissionsT from 'permissions';
import * as semver from 'semver';
import * as uuidT from 'uuid';
import * as winapiT from 'winapi-bindings';
const uuid = lazyRequire<typeof uuidT>(() => require('uuid'));
const permissions = lazyRequire<typeof permissionsT>(() => require('permissions'));
const winapi = lazyRequire<typeof winapiT>(() => require('winapi-bindings'));
const STATE_CHUNK_SIZE = 128 * 1024;
function last(array: any[]): any {
if (array.length === 0) {
return undefined;
}
return array[array.length - 1];
}
class Application {
public static shouldIgnoreError(error: any, promise?: any): boolean {
if (error instanceof UserCanceled) {
return true;
}
if (!truthy(error)) {
log('error', 'empty error unhandled', { wasPromise: promise !== undefined });
return true;
}
if (error.message === 'Object has been destroyed') {
// This happens when Vortex crashed because of something else so there is no point
// reporting this, it might otherwise obfuscate the actual problem
return true;
}
// this error message appears to happen as the result of some other problem crashing the
// renderer process, so all this may do is obfuscate what's actually going on.
if (error.message.includes('Error processing argument at index 0, conversion failure from')) {
return true;
}
if (['net::ERR_CONNECTION_RESET',
'net::ERR_CONNECTION_ABORTED',
'net::ERR_ABORTED',
'net::ERR_CONTENT_LENGTH_MISMATCH',
'net::ERR_SSL_PROTOCOL_ERROR',
'net::ERR_HTTP2_PROTOCOL_ERROR',
'net::ERR_INCOMPLETE_CHUNKED_ENCODING'].includes(error.message)
|| ['ETIMEDOUT', 'ECONNRESET', 'EPIPE'].includes(error.code)) {
log('warn', 'network error unhandled', error.stack);
return true;
}
if (['EACCES', 'EPERM'].includes(error.errno)
&& (error.path !== undefined)
&& (error.path.indexOf('vortex-setup') !== -1)) {
// It's wonderous how electron-builder finds new ways to be more shit without even being
// updated. Probably caused by node update
log('warn', 'suppressing error message', { message: error.message, stack: error.stack });
return true;
}
return false;
}
private mBasePath: string;
private mStore: ThunkStore<IState>;
private mLevelPersistors: LevelPersist[] = [];
private mArgs: IParameters;
private mMainWindow: MainWindowT;
private mExtensions: ExtensionManagerT;
private mTray: TrayIconT;
private mFirstStart: boolean = false;
private mStartupLogPath: string;
private mDeinitCrashDump: () => void;
constructor(args: IParameters) {
this.mArgs = args;
ipcMain.on('show-window', () => this.showMainWindow(args?.startMinimized));
process.env['UV_THREADPOOL_SIZE'] = (os.cpus().length * 1.5).toString();
app.commandLine.appendSwitch('js-flags', `--max-old-space-size=${args.maxMemory || 4096}`);
this.mBasePath = app.getPath('userData');
fs.ensureDirSync(this.mBasePath);
setVortexPath('temp', () => path.join(getVortexPath('userData'), 'temp'));
const tempPath = getVortexPath('temp');
fs.ensureDirSync(path.join(tempPath, 'dumps'));
this.mStartupLogPath = path.join(tempPath, 'startup.log');
try {
fs.statSync(this.mStartupLogPath);
process.env.CRASH_REPORTING = Math.random() > 0.5 ? 'vortex' : 'electron';
} catch (err) {
// nop, this is the expected case
}
if (process.env.CRASH_REPORTING === 'electron') {
const crashReporter: typeof crashReporterT = require('electron').crashReporter;
crashReporter.start({
productName: 'Vortex',
uploadToServer: false,
submitURL: '',
});
app.setPath('crashDumps', path.join(tempPath, 'dumps'));
} else if (process.env.CRASH_REPORTING === 'vortex') {
const crashDump: typeof crashDumpT = require('crash-dump').default;
this.mDeinitCrashDump =
crashDump(path.join(tempPath, 'dumps', `crash-main-${Date.now()}.dmp`));
}
setupLogging(app.getPath('userData'), process.env.NODE_ENV === 'development');
this.setupAppEvents(args);
}
private setupContextMenu() {
contextMenu({
showCopyImage: false,
showLookUpSelection: false,
showSaveImageAs: false,
showInspectElement: false,
showSearchWithGoogle: false,
shouldShowMenu: (event: Electron.Event, params: Electron.ContextMenuParams) => {
// currently only offer menu on selected text
return params.selectionText.length > 0;
},
});
}
private startUi(): Promise<void> {
const MainWindow = require('./MainWindow').default;
this.mMainWindow = new MainWindow(this.mStore, this.mArgs.inspector);
log('debug', 'creating main window');
return this.mMainWindow.create(this.mStore).then(webContents => {
log('debug', 'window created');
this.mExtensions.setupApiMain(this.mStore, webContents);
setOutdated(this.mExtensions.getApi());
// in the past we would process some command line arguments the same as we do when
// they get passed in from a second instance but that was inconsistent
// because we don't use most arguments from secondary instances and the
// rest get handled by the extension they are intended for.
// so now "applyArguments()" is only intended for forwarding messages from
// secondary instances
if (didIgnoreError()) {
webContents.send('did-ignore-error', true);
}
return Promise.resolve();
});
}
private startSplash(): Promise<SplashScreenT> {
const SplashScreen = require('./SplashScreen').default;
const splash: SplashScreenT = new SplashScreen();
return splash.create(this.mArgs.disableGPU)
.then(() => {
setWindow(splash.getHandle());
return splash;
});
}
private setupAppEvents(args: IParameters) {
app.on('window-all-closed', () => {
log('info', 'Vortex closing');
finalizeStoreWrite()
.then(() => {
log('info', 'clean application end');
if (this.mTray !== undefined) {
this.mTray.close();
}
if (this.mDeinitCrashDump !== undefined) {
this.mDeinitCrashDump();
}
if (process.platform !== 'darwin') {
app.quit();
}
});
});
app.on('activate', () => {
if (this.mMainWindow !== undefined) {
this.mMainWindow.create(this.mStore);
}
});
app.on('second-instance', (event: Event, secondaryArgv: string[]) => {
log('debug', 'getting arguments from second instance', secondaryArgv);
this.applyArguments(commandLine(secondaryArgv, true));
});
app.whenReady().then(() => {
const vortexPath = process.env.NODE_ENV === 'development'
? 'vortex_devel'
: 'vortex';
// if userData specified, use it
let userData = args.userData
// (only on windows) use ProgramData from environment
?? ((args.shared && process.platform === 'win32')
? path.join(process.env.ProgramData, 'vortex')
// this allows the development build to access data from the
// production version and vice versa
: path.resolve(app.getPath('userData'), '..', vortexPath));
userData = path.join(userData, currentStatePath);
// handle nxm:// internally
protocol.registerHttpProtocol('nxm', (request, callback) => {
const cfgFile: IParameters = {download: request.url};
this.applyArguments(cfgFile);
});
let startupMode: Promise<void>;
if (args.get) {
startupMode = this.handleGet(args.get, userData);
} else if (args.set) {
startupMode = this.handleSet(args.set, userData);
} else if (args.del) {
startupMode = this.handleDel(args.del, userData);
}
if (startupMode !== undefined) {
startupMode.then(() => {
app.quit();
});
} else {
this.regularStart(args);
}
});
app.on('web-contents-created', (event: Electron.Event, contents: Electron.WebContents) => {
// tslint:disable-next-line:no-submodule-imports
require('@electron/remote/main').enable(contents);
contents.on('will-attach-webview', this.attachWebView);
});
}
private attachWebView = (event: Electron.Event,
webPreferences: Electron.WebPreferences & { preloadURL: string },
params) => {
// disallow creation of insecure webviews
delete webPreferences.preload;
delete webPreferences.preloadURL;
webPreferences.nodeIntegration = false;
}
private genHandleError() {
return (error: any, promise?: any) => {
if (Application.shouldIgnoreError(error, promise)) {
return;
}
terminate(toError(error), this.mStore.getState());
};
}
private regularStart(args: IParameters): Promise<void> {
let splash: SplashScreenT;
return fs.writeFileAsync(this.mStartupLogPath, (new Date()).toUTCString())
.catch(() => null)
.tap(() => {
log('info', '--------------------------');
log('info', 'Vortex Version', getApplication().version);
log('info', 'Parameters', process.argv.join(' '));
})
.then(() => this.testUserEnvironment())
.then(() => this.validateFiles())
.then(() => (args?.startMinimized === true)
? Promise.resolve(undefined)
: this.startSplash())
// start initialization
.tap(splashIn => (splashIn !== undefined)
? log('debug', 'showing splash screen')
: log('debug', 'starting without splash screen'))
.then(splashIn => {
splash = splashIn;
return this.createStore(args.restore, args.merge)
.catch(DataInvalid, err => {
log('error', 'store data invalid', err.message);
dialog.showMessageBox(getVisibleWindow(), {
type: 'error',
buttons: ['Continue'],
title: 'Error',
message: 'Data corrupted',
detail: 'The application state which contains things like your Vortex '
+ 'settings, meta data about mods and other important data is '
+ 'corrupted and can\'t be read. This could be a result of '
+ 'hard disk corruption, a power outage or something similar. '
+ 'Vortex will now try to repair the database, usually this '
+ 'should work fine but please check that settings, mod list and so '
+ 'on are ok before you deploy anything. '
+ 'If not, you can go to settings->workarounds and restore a backup '
+ 'which shouldn\'t lose you more than an hour of progress.',
})
.then(() => this.createStore(args.restore, args.merge, true));
});
})
.tap(() => log('debug', 'checking admin rights'))
.then(() => this.warnAdmin())
.tap(() => log('debug', 'checking how Vortex was installed'))
.then(() => this.identifyInstallType())
.tap(() => log('debug', 'checking if migration is required'))
.then(() => this.checkUpgrade())
.tap(() => log('debug', 'setting up error handlers'))
.then(() => {
// as soon as we have a store, install an extended error handler that has
// access to application state
const handleError = this.genHandleError();
process.removeAllListeners('uncaughtException');
process.removeAllListeners('unhandledRejection');
process.on('uncaughtException', handleError);
process.on('unhandledRejection', handleError);
})
.then(() => {
this.mStore.dispatch(setCommandLine(args));
})
.then(() => this.initDevel())
.tap(() => log('debug', 'starting user interface'))
.then(() => {
this.setupContextMenu();
return Promise.resolve();
})
.then(() => this.startUi())
.tap(() => log('debug', 'setting up tray icon'))
.then(() => this.createTray())
// end initialization
.tap(() => {
if (splash !== undefined) {
log('debug', 'removing splash screen');
}
})
.then(() => {
this.connectTrayAndWindow();
return (splash !== undefined)
? splash.fadeOut()
: Promise.resolve();
})
.tapCatch((err) => log('debug', 'quitting with exception', err.message))
.catch(UserCanceled, () => app.exit())
.catch(ProcessCanceled, () => {
app.quit();
})
.catch(DocumentsPathMissing, () =>
dialog.showMessageBox(getVisibleWindow(), {
type: 'error',
buttons: ['Close', 'More info'],
defaultId: 1,
title: 'Error',
message: 'Startup failed',
detail: 'Your "My Documents" folder is missing or is '
+ 'misconfigured. Please ensure that the folder is properly '
+ 'configured and accessible, then try again.',
}).then(response => {
if (response.response === 1) {
shell.openExternal(
`https://wiki.${NEXUS_DOMAIN}/index.php/Misconfigured_Documents_Folder`);
}
app.quit();
}))
.catch(DatabaseLocked, () => {
dialog.showErrorBox('Startup failed', 'Vortex seems to be running already. '
+ 'If you can\'t see it, please check the task manager.');
app.quit();
})
.catch({ code: 'ENOSPC' }, () => {
dialog.showErrorBox('Startup failed', 'Your system drive is full. '
+ 'You should always ensure your system drive has some space free (ideally '
+ 'at least 10% of the total capacity, especially on SSDs). '
+ 'Vortex can\'t start until you have freed up some space.');
app.quit();
})
.catch((err) => {
try {
if (err instanceof Error) {
const pretty = prettifyNodeErrorMessage(err);
const details = pretty.message
.replace(/{{ *([a-zA-Z]+) *}}/g, (m, key) => pretty.replace?.[key] || key);
terminate({
message: 'Startup failed',
details,
code: pretty.code,
stack: err.stack,
}, this.mStore !== undefined ? this.mStore.getState() : {},
pretty.allowReport);
} else {
terminate({
message: 'Startup failed',
details: err.message,
stack: err.stack,
}, this.mStore !== undefined ? this.mStore.getState() : {});
}
} catch (err) {
// nop
}
})
.finally(() => fs.removeAsync(this.mStartupLogPath).catch(() => null));
}
private isUACEnabled(): Promise<boolean> {
if (process.platform !== 'win32') {
return Promise.resolve(true);
}
const getSystemPolicyValue = (key: string) => {
try {
const res = winapi.RegGetValue('HKEY_LOCAL_MACHINE',
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System',
key);
return Promise.resolve({ key, type: res.type, value: res.value});
} catch (err) {
// We couldn't retrieve the value, log this and resolve positively
// as the user might have a version of Windows that does not use
// the key we're looking for.
log('debug', 'failed to check UAC settings', err);
return Promise.resolve(undefined);
}
};
return Promise.all([getSystemPolicyValue('ConsentPromptBehaviorAdmin'),
getSystemPolicyValue('ConsentPromptBehaviorUser')])
.then(res => {
res.forEach(value => {
if (value !== undefined) {
log('debug', 'UAC settings found', `${value.key}: ${value.value}`);
}
});
const adminConsent = res[0];
return ((adminConsent.type === 'REG_DWORD') && (adminConsent.value === 0))
? Promise.resolve(false)
: Promise.resolve(true);
})
// Perfectly ok not to have the registry keys.
.catch(err => Promise.resolve(true));
}
private identifyInstallType(): Promise<void> {
/**
* we are checking to see if an uninstaller exists as if it does, it means it was installed via our installer.
* if it doesn't, then something else installed it. Maybe GOG, or EPIC, or something.
*
* TODO: we want to further check managed types to distiguish between anything that isn't us.
* Quick research says we need to file pattern match the install directory to see what files gog or epic adds etc.
* This should determine where it's from
*
* GOG
*
* Maybe the existance of: (the number being the gog product id)
* 'goggame-galaxyFileList.ini'
* 'goggame-2053394557.info'
* 'goggame-2053394557.hashdb'
*
* EPIC
*
*
*/
return fs.statAsync(path.join(getVortexPath('application'), 'Uninstall Vortex.exe'))
.then(() => {
this.mStore.dispatch(setInstallType('regular'));
})
.catch(() => {
this.mStore.dispatch(setInstallType('managed'));
});
}
private warnAdmin(): Promise<void> {
const state: IState = this.mStore.getState();
return timeout(Promise.resolve(isAdmin()), 1000)
.then(admin => {
if ((admin === undefined) || !admin) {
return Promise.resolve();
}
log('warn', 'running as administrator');
if (state.app.warnedAdmin > 0) {
return Promise.resolve();
}
return this.isUACEnabled().then(uacEnabled => dialog.showMessageBox(getVisibleWindow(), {
title: 'Admin rights detected',
message:
`Vortex has detected that it is being run with administrator rights. It is strongly
advised to not run any application with admin rights as adverse effects may include
permission issues or even security risks. Continue at your own risk`
+ (!uacEnabled
? `\n\nPlease note: User Account Control (UAC) notifications are disabled in your
operating system. We strongly recommend you re-enable these to avoid file permissions
issues and potential security risks.`
: ''),
buttons: [
'Quit',
'Ignore',
],
noLink: true,
}).then(result => {
if (result.response === 0) {
app.quit();
} else {
this.mStore.dispatch(setWarnedAdmin(1));
return Promise.resolve();
}
}));
});
}
private checkUpgrade(): Promise<void> {
const currentVersion = getApplication().version;
return this.migrateIfNecessary(currentVersion)
.then(() => {
this.mStore.dispatch(setApplicationVersion(currentVersion));
return Promise.resolve();
});
}
private migrateIfNecessary(currentVersion: string): Promise<void> {
const state: IState = this.mStore.getState();
const lastVersion = state.app.appVersion || '0.0.0';
if (this.mFirstStart || (process.env.NODE_ENV === 'development')) {
// don't check version change in development builds or on first start
return Promise.resolve();
}
if (isMajorDowngrade(lastVersion, currentVersion)) {
if (dialog.showMessageBoxSync(getVisibleWindow(), {
type: 'warning',
title: 'Downgrade detected',
message: `You're using a version of Vortex that is older than the version you ran previously.
Active version: (${currentVersion}) Previously run: (${lastVersion}). Continuing to run this
older version may cause irreversible damage to your application state and setup. Continue at your own risk. `,
buttons: [
'Quit',
'Continue at your own risk',
],
noLink: true,
}) === 0) {
app.quit();
return Promise.reject(new UserCanceled());
}
} else if (semver.gt(currentVersion, lastVersion)) {
log('info', 'Vortex was updated, checking for necessary migrations');
return migrate(this.mStore, getVisibleWindow())
.then(() => {
return Promise.resolve();
})
.catch(err => !(err instanceof UserCanceled)
&& !(err instanceof ProcessCanceled), (err: Error) => {
dialog.showErrorBox(
'Migration failed',
'The migration from the previous Vortex release failed. '
+ 'Please resolve the errors you got, then try again.');
app.exit(1);
return Promise.reject(new ProcessCanceled('Migration failed'));
});
}
return Promise.resolve();
}
private splitPath(statePath: string): string[] {
return statePath.match(/(\\.|[^.])+/g).map(input => input.replace(/\\(.)/g, '$1'));
}
private handleGet(getPaths: string[] | boolean, dbpath: string): Promise<void> {
if (typeof(getPaths) === 'boolean') {
fs.writeSync(1, 'Usage: vortex --get <path>\n');
return;
}
let persist: LevelPersist;
return LevelPersist.create(dbpath)
.then(persistIn => {
persist = persistIn;
return persist.getAllKeys();
})
.then(keys => {
return Promise.all(getPaths.map(getPath => {
const pathArray = this.splitPath(getPath);
const matches = keys
.filter(key => _.isEqual(key.slice(0, pathArray.length), pathArray));
return Promise.all(matches.map(match => persist.getItem(match)
.then(value => `${match.join('.')} = ${value}`)))
.then(output => { process.stdout.write(output.join('\n') + '\n'); })
.catch(err => { process.stderr.write(err.message + '\n'); });
}))
.then(() => null);
})
.catch(err => {
process.stderr.write(err.message + '\n');
})
.finally(() => {
persist.close();
});
}
private handleSet(setParameters: ISetItem[], dbpath: string): Promise<void> {
let persist: LevelPersist;
return LevelPersist.create(dbpath)
.then(persistIn => {
persist = persistIn;
return Promise.all(setParameters.map((setParameter: ISetItem) => {
const pathArray = this.splitPath(setParameter.key);
return persist.getItem(pathArray)
.catch(() => undefined)
.then(oldValue => {
const newValue = setParameter.value.length === 0
? undefined
: (oldValue === undefined) || (typeof (oldValue) === 'object')
? JSON.parse(setParameter.value)
: oldValue.constructor(setParameter.value);
return persist.setItem(pathArray, newValue);
})
.then(() => { process.stdout.write('changed\n'); })
.catch(err => {
process.stderr.write(err.message + '\n');
})
}))
.then(() => null);
})
.catch(err => {
process.stderr.write(err.message + '\n');
})
.finally(() => {
persist.close();
});
}
private handleDel(delPaths: string[], dbpath: string): Promise<void> {
let persist: LevelPersist;
return LevelPersist.create(dbpath)
.then(persistIn => {
persist = persistIn;
return persist.getAllKeys();
})
.then(keys => {
return Promise.all(delPaths.map(delPath => {
const pathArray = this.splitPath(delPath);
const matches = keys
.filter(key => _.isEqual(key.slice(0, pathArray.length), pathArray));
return Promise.all(matches.map(match => persist.removeItem(match)
.then(() => process.stdout.write(`removed ${match.join('.')}\n`))
.catch(err => { process.stderr.write(err.message + '\n'); })));
}))
.then(() => null);
})
.catch(err => {
process.stderr.write(err.message + '\n')
})
.finally(() => {
persist.close();
});
}
private createTray(): Promise<void> {
const TrayIcon = require('./TrayIcon').default;
this.mTray = new TrayIcon(this.mExtensions.getApi());
return Promise.resolve();
}
private connectTrayAndWindow() {
if (this.mTray.initialized) {
this.mMainWindow.connectToTray(this.mTray);
}
}
private multiUserPath() {
if (process.platform === 'win32') {
const muPath = path.join(process.env.ProgramData, 'vortex');
try {
fs.ensureDirSync(muPath);
} catch (err) {
// not sure why this would happen, ensureDir isn't supposed to report a problem if
// the directory exists, but there was a single report of EEXIST in this place.
// Probably a bug related to the filesystem used in C:\ProgramData, we had similar
// problems with OneDrive paths
if (err.code !== 'EEXIST') {
throw err;
}
}
return muPath;
} else {
log('error', 'Multi-User mode not implemented outside windows');
return app.getPath('userData');
}
}
private createStore(restoreBackup?: string, mergeBackup?: string,
repair?: boolean): Promise<void> {
const newStore = createVortexStore(this.sanityCheckCB);
const backupPath = path.join(app.getPath('temp'), STATE_BACKUP_PATH);
let backups: string[];
const updateBackups = () => fs.ensureDirAsync(backupPath)
.then(() => fs.readdirAsync(backupPath))
.filter((fileName: string) =>
fileName.startsWith('backup') && path.extname(fileName) === '.json')
.then(backupsIn => { backups = backupsIn; })
.catch(err => {
log('error', 'failed to read backups', err.message);
backups = [];
});
const deleteBackups = () => Promise.map(backups, backupName =>
fs.removeAsync(path.join(backupPath, backupName))
.catch(() => undefined))
.then(() => null);
// storing the last version that ran in the startup.json settings file.
// We have that same information in the leveldb store but what if we need
// to react to an upgrade before the state is loaded?
// In development of 1.4 I assumed we had a case where this was necessary.
// Turned out it wasn't, still feel it's sensible to have this
// information available asap
startupSettings.storeVersion = getApplication().version;
// 1. load only user settings to determine if we're in multi-user mode
// 2. load app settings to determine which extensions to load
// 3. load extensions, then load all settings, including extensions
return LevelPersist.create(path.join(this.mBasePath, currentStatePath),
undefined,
repair ?? false)
.then(levelPersistor => {
this.mLevelPersistors.push(levelPersistor);
return insertPersistor(
'user', new SubPersistor(levelPersistor, 'user'));
})
.catch(DataInvalid, err => {
const failedPersistor = this.mLevelPersistors.pop();
return failedPersistor.close()
.then(() => Promise.reject(err));
})
.then(() => {
let dataPath = app.getPath('userData');
const { multiUser } = newStore.getState().user;
if (this.mArgs.userData !== undefined) {
dataPath = this.mArgs.userData;
} else if (multiUser) {
dataPath = this.multiUserPath();
}
setVortexPath('userData', dataPath);
this.mBasePath = dataPath;
let created = false;
try {
fs.statSync(dataPath);
} catch (err) {
fs.ensureDirSync(dataPath);
created = true;
}
if (multiUser && created) {
permissions.allow(dataPath, 'group', 'rwx');
}
fs.ensureDirSync(path.join(dataPath, 'temp'));
log('info', `using ${dataPath} as the storage directory`);
if (multiUser || (this.mArgs.userData !== undefined)) {
log('info', 'all further logging will happen in', path.join(dataPath, 'vortex.log'));
setLogPath(dataPath);
log('info', '--------------------------');
log('info', 'Vortex Version', getApplication().version);
return LevelPersist.create(
path.join(dataPath, currentStatePath),
undefined,
repair ?? false,
)
.then(levelPersistor => {
this.mLevelPersistors.push(levelPersistor);
});
} else {
return Promise.resolve();
}
})
.then(() => {
log('debug', 'reading app state');
return insertPersistor('app', new SubPersistor(last(this.mLevelPersistors), 'app'));
})
.then(() => {
if (newStore.getState().app.instanceId === undefined) {
this.mFirstStart = true;
const newId = uuid.v4();
log('debug', 'first startup, generated instance id', { instanceId: newId });
newStore.dispatch(setInstanceId(newId));
} else {
log('debug', 'startup instance', { instanceId: newStore.getState().app.instanceId });
}
const ExtensionManager = require('../util/ExtensionManager').default;
this.mExtensions = new ExtensionManager(newStore);
if (this.mExtensions.hasOutdatedExtensions) {
log('debug', 'relaunching to remove outdated extensions');
finalizeStoreWrite().then(() => relaunch());
// relaunching the process happens asynchronously but we don't want to any further work
// before that
return new Promise(() => null);
}
const reducer = require('../reducers/index').default;
newStore.replaceReducer(reducer(this.mExtensions.getReducers(), querySanitize));
return Promise.mapSeries(allHives(this.mExtensions), hive =>
insertPersistor(hive, new SubPersistor(last(this.mLevelPersistors), hive)));
})
.then(() => {
log('debug', 'checking if state db needs to be upgraded');
return importState(this.mBasePath);
})
.then(oldState => {
// mark as imported first, otherwise we risk importing again, overwriting data.
// this way we risk not importing but since the old state is still there, that
// can be repaired
return oldState !== undefined ?
markImported(this.mBasePath)
.then(() => {
newStore.dispatch({
type: '__hydrate',
payload: oldState,
});
}) :
Promise.resolve();
})
.then(() => {
log('debug', 'updating state backups');
return updateBackups();
})
.then(() => {
if (restoreBackup !== undefined) {
log('info', 'restoring state backup', restoreBackup);
return fs.readFileAsync(restoreBackup, { encoding: 'utf-8' })
.then(backupState => {
newStore.dispatch({
type: '__hydrate_replace',
payload: JSON.parse(backupState),
});
})
.then(() => deleteBackups())
.then(() => updateBackups())
.catch(err => {
if (err instanceof UserCanceled) {
return Promise.reject(err);
}
terminate({
message: 'Failed to restore backup',
details: (err.code !== 'ENOENT')
? err.message
: 'Specified backup file doesn\'t exist',
path: restoreBackup,
}, {}, false);
});
} else if (mergeBackup !== undefined) {
log('info', 'merging state backup', mergeBackup);
return fs.readFileAsync(mergeBackup, { encoding: 'utf-8' })
.then(backupState => {
newStore.dispatch({
type: '__hydrate',
payload: JSON.parse(backupState),
});
})
.catch(err => {
if (err instanceof UserCanceled) {
return Promise.reject(err);
}
terminate({
message: 'Failed to merge backup',
details: (err.code !== 'ENOENT')
? err.message
: 'Specified backup file doesn\'t exist',
path: mergeBackup,
}, {}, false);
});
} else {
return Promise.resolve();
}
})
.then(() => {
const hydrateHandler = (stepIn: IPresetStep): Promise<void> => {
newStore.dispatch({
type: '__hydrate',
payload: (stepIn as IPresetStepHydrateState).state,
});
return Promise.resolve();
}
presetManager.on('hydrate', hydrateHandler);
presetManager.now('hydrate', hydrateHandler);
})
.then(() => {
this.mStore = newStore;
let sendState: Buffer;
(global as any).getReduxStateMsgpack = (idx: number) => {
const msgpack: typeof msgpackT = require('@msgpack/msgpack');
if ((sendState === undefined) || (idx === 0)) {
sendState = Buffer.from(msgpack.encode(
replaceRecursive(this.mStore.getState(), undefined, '__UNDEFINED__')));
}
const res = sendState.slice(idx * STATE_CHUNK_SIZE, (idx + 1) * STATE_CHUNK_SIZE);
return res.toString('base64');
};
this.mExtensions.setStore(newStore);
log('debug', 'setting up extended store');
return extendStore(newStore, this.mExtensions);
})
.then(() => {
if (backups.length > 0) {
const sorted = backups.sort((lhs, rhs) => rhs.localeCompare(lhs));
const mostRecent = sorted[0];
const timestamp = path.basename(mostRecent, '.json').replace('backup_', '');
const date = new Date(+timestamp);
const dateString = `${date.toDateString()} `
+ `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
const replace = { date: dateString };
this.mStore.dispatch(addNotification({
type: 'info',
message: 'Found an application state backup. Created on: {{date}}',
actions: [
{ title: 'Restore', action: () => {
this.mStore.dispatch(showDialog('question', 'Restoring Application State', {
bbcode: 'You are attempting to restore an application state backup which will revert any '
+ 'state changes you have made since the backup was created.[br][/br][br][/br]'
+ 'Please note that this operation will NOT uninstall/remove any mods you '
+ 'may have downloaded/installed since the backup was created, however Vortex '
+ 'may "forget" some changes:[list]'
+ '[*] Which download archive belongs to which mod installation, exhibiting '
+ 'itself as "duplicate" entries of the same mod (archive entry and installed mod entry).'
+ '[*] The state of an installed mod - reverting it to a disabled state.'
+ '[*] Any conflict rules you had defined after the state backup.'
+ '[*] Any other configuration changes you may have made.'
+ '[/list][br][/br]'
+ 'Are you sure you wish to restore the backed up state ?',
}, [
{ label: 'Cancel' },
{ label: 'Restore', action: () => {
log('info', 'sorted backups', sorted);
spawnSelf(['--restore', path.join(backupPath, mostRecent)]);
app.exit();
} },
]));
} },
{ title: 'Delete', action: dismiss => {
deleteBackups();
dismiss();
} },
],
replace,
}));
} else if (!repair) {
// we started without any problems, save this application state
return createFullStateBackup('startup', this.mStore)
.then(() => Promise.resolve())