OpenAsar/src/bootstrap.js

137 lines
4.1 KiB
JavaScript
Raw Normal View History

const { app, session } = require('electron');
2021-12-16 16:52:25 +00:00
const { readFileSync } = require('fs');
const { join } = require('path');
2021-12-09 16:25:14 +00:00
2022-04-21 21:57:31 +00:00
if (!settings.get('enableHardwareAcceleration', true)) app.disableHardwareAcceleration();
process.env.PULSE_LATENCY_MSEC = process.env.PULSE_LATENCY_MSEC ?? 30;
2021-12-09 16:25:14 +00:00
const buildInfo = require('./utils/buildInfo');
app.setVersion(buildInfo.version); // More global because discord / electron
2021-12-11 11:11:42 +00:00
global.releaseChannel = buildInfo.releaseChannel;
2021-12-09 16:25:14 +00:00
2022-03-19 11:10:13 +00:00
log('BuildInfo', buildInfo);
2022-04-21 21:57:31 +00:00
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');
2021-12-09 16:25:14 +00:00
const updater = require('./updater/updater');
const moduleUpdater = require('./updater/moduleUpdater');
const autoStart = require('./autoStart');
2021-12-09 16:25:14 +00:00
let desktopCore;
const startCore = () => {
2022-09-28 15:21:30 +00:00
if (oaConfig.js || oaConfig.css) session.defaultSession.webRequest.onHeadersReceived((d, cb) => {
2022-04-21 19:53:20 +00:00
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('<notrack>', oaConfig.noTrack)
2022-10-10 15:41:39 +00:00
.replace('<css>', (oaConfig.css ?? '').replaceAll('\\', '\\\\').replaceAll('`', '\\`')));
2022-04-21 19:53:20 +00:00
if (oaConfig.js) bw.webContents.executeJavaScript(oaConfig.js);
});
});
desktopCore = require('./utils/requireNative')('discord_desktop_core');
2021-12-09 16:25:14 +00:00
desktopCore.startup({
splashScreen: splash,
2021-12-09 16:25:14 +00:00
moduleUpdater,
buildInfo,
Constants,
updater,
autoStart,
// Just requires
appSettings: require('./appSettings'),
paths: require('./paths'),
// Stubs
GPUSettings: {
replace: () => {}
},
crashReporterSetup: {
isInitialized: () => true,
metadata: {}
}
2021-12-09 16:25:14 +00:00
});
};
const startUpdate = () => {
2022-09-29 16:07:54 +00:00
const urls = [
2022-09-29 16:04:13 +00:00
oaConfig.noTrack !== false ? 'https://*/api/v9/science' : '',
oaConfig.noTyping === true ? 'https://*/api/*/typing' : ''
2022-09-29 16:07:54 +00:00
].filter(x => x);
if (urls.length > 0) session.defaultSession.webRequest.onBeforeRequest({ urls }, (e, cb) => cb({ cancel: true }));
2022-04-08 21:31:08 +00:00
2022-05-07 19:08:40 +00:00
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');
2022-05-07 19:08:40 +00:00
if (oaConfig.setup !== true) config.open();
2022-03-13 20:12:42 +00:00
2022-09-29 16:07:54 +00:00
if (oaConfig.autoupdate !== false) {
2022-03-13 20:12:42 +00:00
try {
require('./asarUpdate')();
} catch (e) {
log('AsarUpdate', e);
2022-03-13 20:12:42 +00:00
}
}
2022-03-13 20:12:42 +00:00
}, 3000);
2021-12-09 16:25:14 +00:00
});
splash.initSplash(startMin);
2021-12-09 16:25:14 +00:00
};
2021-12-09 16:25:14 +00:00
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")
});
2022-05-07 19:08:40 +00:00
if (!app.requestSingleInstanceLock() && !(process.argv?.includes?.('--multi-instance') || oaConfig.multiInstance === true)) return app.quit();
2022-04-22 16:08:04 +00:00
if (app.isReady()) startUpdate();
else app.once('ready', startUpdate);
};