OpenAsar/src/bootstrap.js

117 lines
3.3 KiB
JavaScript
Raw Normal View History

const { app } = 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
const Constants = require('./Constants');
process.env.PULSE_LATENCY_MSEC = process.env.PULSE_LATENCY_MSEC ?? 30;
app.setAppUserModelId(Constants.APP_ID);
app.name = 'discord'; // Force name as sometimes breaks
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);
const { fatal, handled, init: EHInit } = require('./errorHandler');
EHInit();
const splash = require('./splash');
2021-12-09 16:25:14 +00:00
const updater = require('./updater/updater');
const moduleUpdater = require('./updater/moduleUpdater');
if (!settings.get('enableHardwareAcceleration', true)) app.disableHardwareAcceleration();
const autoStart = require('./autoStart');
2021-12-09 16:25:14 +00:00
let desktopCore;
const startCore = () => {
app.on('browser-window-created', (e, bw) => { // Main window injection
bw.webContents.on('dom-ready', () => {
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('<channel>', channel)
.replaceAll('<hash>', hash || 'custom')
);
});
});
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,
appSettings: require('./appSettings'),
paths: require('./paths'),
GPUSettings: require('./GPUSettings'),
autoStart,
crashReporterSetup: require('./crashReporterSetup'),
2021-12-09 16:25:14 +00:00
});
};
const startUpdate = async () => {
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', handled);
require('./firstRun').do(inst);
} 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
2022-03-13 20:12:42 +00:00
const config = require('./config');
if (oaConfig.setup !== true || process.argv.includes('--config')) config.open();
if (oaConfig.autoupdate !== false) { // If autoupdate disabled, don't update
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 = () => {
if (!app.requestSingleInstanceLock() && !(process.argv.includes('--multi-instance') || oaConfig.multiInstance === true)) {
2022-02-12 14:35:46 +00:00
log('Bootstrap', 'Non-first instance');
return app.quit();
}
2021-12-09 16:25:14 +00:00
if (app.isReady()) {
startUpdate();
} else {
app.once('ready', startUpdate);
}
};