-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathbootstrap.js
139 lines (106 loc) · 4.24 KB
/
bootstrap.js
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
const { app, session } = require('electron');
const { readFileSync } = require('fs');
const { join } = require('path');
if (!settings.get('enableHardwareAcceleration', true)) app.disableHardwareAcceleration();
process.env.PULSE_LATENCY_MSEC = process.env.PULSE_LATENCY_MSEC ?? 30;
const buildInfo = require('./utils/buildInfo');
app.setVersion(buildInfo.version); // More global because discord / electron
global.releaseChannel = buildInfo.releaseChannel;
log('BuildInfo', buildInfo);
const Constants = require('./Constants');
app.setAppUserModelId(Constants.APP_ID);
app.name = 'discord'; // Force name as sometimes breaks
const fatal = e => log('Fatal', e);
process.on('uncaughtException', console.error);
const splash = require('./splash');
const updater = require('./updater/updater');
const moduleUpdater = require('./updater/moduleUpdater');
const autoStart = require('./autoStart');
let desktopCore;
const startCore = () => {
if (oaConfig.js || oaConfig.css) session.defaultSession.webRequest.onHeadersReceived((d, cb) => {
delete d.responseHeaders['content-security-policy'];
cb(d);
});
app.on('browser-window-created', (e, bw) => { // Main window injection
bw.webContents.on('dom-ready', () => {
if (!bw.resizable) return; // Main window only
splash.pageReady(); // Override Core's pageReady with our own on dom-ready to show main window earlier
const [ channel = '', hash = '' ] = oaVersion.split('-'); // Split via -
bw.webContents.executeJavaScript(readFileSync(join(__dirname, 'mainWindow.js'), 'utf8')
.replaceAll('<hash>', hash).replaceAll('<channel>', channel)
.replaceAll('<notrack>', oaConfig.noTrack !== false)
.replaceAll('<domopt>', oaConfig.domOptimizer !== false)
.replace('<css>', (oaConfig.css ?? '').replaceAll('\\', '\\\\').replaceAll('`', '\\`')));
if (oaConfig.js) bw.webContents.executeJavaScript(oaConfig.js);
});
});
desktopCore = require('discord_desktop_core');
desktopCore.startup({
splashScreen: splash,
moduleUpdater,
buildInfo,
Constants,
updater,
autoStart,
// Just requires
appSettings: require('./appSettings'),
paths: require('./paths'),
// Stubs
GPUSettings: {
replace: () => {}
},
crashReporterSetup: {
isInitialized: () => true,
getGlobalSentry: () => null,
metadata: {}
}
});
};
const startUpdate = () => {
const urls = [
oaConfig.noTrack !== false ? 'https://*/api/*/science' : '',
oaConfig.noTrack !== false ? 'https://*/api/*/metrics' : '',
oaConfig.noTyping === true ? 'https://*/api/*/typing' : ''
].filter(x => x);
if (urls.length > 0) session.defaultSession.webRequest.onBeforeRequest({ urls }, (e, cb) => cb({ cancel: true }));
const startMin = process.argv?.includes?.('--start-minimized');
if (updater.tryInitUpdater(buildInfo, Constants.NEW_UPDATE_ENDPOINT)) {
const inst = updater.getUpdater();
inst.on('host-updated', () => autoStart.update(() => {}));
inst.on('unhandled-exception', fatal);
inst.on('InconsistentInstallerState', fatal);
inst.on('update-error', console.error);
require('./winFirst').do();
} else {
moduleUpdater.init(Constants.UPDATE_ENDPOINT, buildInfo);
}
splash.events.once('APP_SHOULD_LAUNCH', () => {
if (!process.env.OPENASAR_NOSTART) startCore();
});
let done;
splash.events.once('APP_SHOULD_SHOW', () => {
if (done) return;
done = true;
desktopCore.setMainWindowVisible(!startMin);
setTimeout(() => { // Try to update our asar
const config = require('./config');
if (oaConfig.setup !== true) config.open();
if (oaConfig.autoupdate !== false) {
try {
require('./asarUpdate')();
} catch (e) {
log('AsarUpdate', e);
}
}
}, 3000);
});
splash.initSplash(startMin);
};
module.exports = () => {
app.on('second-instance', (e, a) => {
desktopCore?.handleOpenUrl?.(a.includes('--url') && a[a.indexOf('--') + 1]); // Change url of main window if protocol is used (uses like "discord --url -- discord://example")
});
if (!app.requestSingleInstanceLock() && !(process.argv?.includes?.('--multi-instance') || oaConfig.multiInstance === true)) return app.quit();
app.whenReady().then(startUpdate);
};