OpenAsar/src/bootstrap.js

110 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-12-16 16:52:25 +00:00
const { app, BrowserWindow } = require('electron');
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 errorHandler = require('./errorHandler');
errorHandler.init();
2022-01-28 20:47:31 +00:00
const splashScreen = require('./splash');
const appSettings = require('./appSettings');
2021-12-09 16:25:14 +00:00
const updater = require('./updater/updater');
const moduleUpdater = require('./updater/moduleUpdater');
const appUpdater = require('./updater/appUpdater');
const settings = appSettings.getSettings();
if (!settings.get('enableHardwareAcceleration', true)) app.disableHardwareAcceleration();
2021-12-09 16:25:14 +00:00
let desktopCore;
const startCore = () => {
desktopCore = require('discord_desktop_core');
log('Bootstrap', 'Required core');
2021-12-09 16:25:14 +00:00
desktopCore.startup({
splashScreen,
moduleUpdater,
buildInfo,
appSettings,
Constants,
updater,
paths: require('./paths'),
GPUSettings: require('./GPUSettings'),
autoStart: require('./autoStart'),
crashReporterSetup: require('./crashReporterSetup'),
2021-12-09 16:25:14 +00:00
});
2021-12-16 16:52:25 +00:00
2022-03-29 18:13:01 +00:00
setImmediate(() => {
2021-12-16 16:52:25 +00:00
if (!global.mainWindowId) return;
const bw = BrowserWindow.fromId(global.mainWindowId);
2022-03-29 17:21:36 +00:00
let done = false;
2021-12-16 16:52:25 +00:00
bw.webContents.on('dom-ready', () => {
2022-03-29 17:21:36 +00:00
if (!done) { // Only run once
splashScreen.pageReady(); // Override Core's pageReady with our own on dom-ready to show main window earlier
2022-03-13 20:12:42 +00:00
2022-03-29 17:21:36 +00:00
done = true;
}
2022-03-10 21:27:02 +00:00
const [ channel, hash ] = oaVersion.split('-'); // Split via -
2022-03-10 21:27:02 +00:00
bw.webContents.executeJavaScript(
2022-03-29 17:26:41 +00:00
readFileSync(join(__dirname, 'mainWindow.js'), 'utf8')
2022-03-10 21:27:02 +00:00
.replaceAll('<channel>', channel)
.replaceAll('<hash>', hash || 'custom')
);
2021-12-16 16:52:25 +00:00
});
});
2021-12-09 16:25:14 +00:00
};
const startUpdate = async () => {
2022-01-28 20:52:45 +00:00
const startMinimized = process.argv.includes('--start-minimized');
appUpdater.update(startMinimized, () => {
2022-02-12 14:35:46 +00:00
if (process.env.OPENASAR_NOSTART) return;
2021-12-09 16:25:14 +00:00
startCore();
}, () => {
log('Bootstrap', 'Main window visible');
desktopCore.setMainWindowVisible(!startMinimized);
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', 'Failed', e);
}
}
2022-03-13 20:12:42 +00:00
}, 3000);
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);
}
};