2022-04-21 21:49:48 +00:00
const { app , session } = require ( 'electron' ) ;
2021-12-16 16:52:25 +00:00
const { readFileSync } = require ( 'fs' ) ;
2022-04-21 19:48:19 +00:00
const get = require ( 'request' ) ;
2021-12-16 16:52:25 +00:00
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 ( ) ;
2022-02-15 21:52:32 +00:00
process . env . PULSE _LATENCY _MSEC = process . env . PULSE _LATENCY _MSEC ? ? 30 ;
2021-12-10 21:31:40 +00:00
2021-12-09 16:25:14 +00:00
const buildInfo = require ( './utils/buildInfo' ) ;
2021-12-10 18:37:50 +00:00
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 ) ;
2021-12-09 19:26:22 +00:00
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
2022-04-21 21:49:48 +00:00
const fatal = e => log ( 'Fatal' , e ) ;
2022-04-17 17:55:37 +00:00
process . on ( 'uncaughtException' , console . error ) ;
2022-04-17 17:31:39 +00:00
2021-12-11 19:51:36 +00:00
2022-04-05 12:13:55 +00:00
const splash = require ( './splash' ) ;
2021-12-09 16:25:14 +00:00
const updater = require ( './updater/updater' ) ;
const moduleUpdater = require ( './updater/moduleUpdater' ) ;
2022-04-05 12:13:55 +00:00
const autoStart = require ( './autoStart' ) ;
2021-12-09 16:25:14 +00:00
let desktopCore ;
const startCore = ( ) => {
2022-04-21 19:53:20 +00:00
if ( oaConfig . js ) session . defaultSession . webRequest . onHeadersReceived ( ( d , cb ) => {
delete d . responseHeaders [ 'content-security-policy' ] ;
cb ( d ) ;
} ) ;
2022-04-06 19:49:42 +00:00
app . on ( 'browser-window-created' , ( e , bw ) => { // Main window injection
bw . webContents . on ( 'dom-ready' , ( ) => {
2022-04-21 21:46:08 +00:00
if ( ! bw . resizable ) return ; // Main window only
2022-04-06 19:49:42 +00:00
splash . pageReady ( ) ; // Override Core's pageReady with our own on dom-ready to show main window earlier
const [ channel , hash ] = oaVersion . split ( '-' ) ; // Split via -
2022-04-21 20:22:48 +00:00
bw . webContents . executeJavaScript ( readFileSync ( join ( _ _dirname , 'mainWindow.js' ) , 'utf8' )
2022-04-21 19:53:20 +00:00
. replaceAll ( '<channel>' , channel )
. replaceAll ( '<hash>' , hash || 'custom' ) ) ;
2022-04-21 20:22:48 +00:00
if ( oaConfig . js ) bw . webContents . executeJavaScript ( oaConfig . js ) ;
2022-04-06 19:49:42 +00:00
} ) ;
} ) ;
2022-04-02 21:23:19 +00:00
desktopCore = require ( './utils/requireNative' ) ( 'discord_desktop_core' ) ;
2021-12-09 16:25:14 +00:00
desktopCore . startup ( {
2022-04-05 12:13:55 +00:00
splashScreen : splash ,
2021-12-09 16:25:14 +00:00
moduleUpdater ,
buildInfo ,
Constants ,
updater ,
2022-04-19 13:37:31 +00:00
autoStart ,
2022-04-19 20:47:09 +00:00
// Just requires
2022-04-05 07:35:57 +00:00
appSettings : require ( './appSettings' ) ,
2022-03-29 17:14:18 +00:00
paths : require ( './paths' ) ,
2022-04-19 20:47:09 +00:00
// Stubs
GPUSettings : {
replace : ( ) => { }
} ,
2022-04-19 13:37:31 +00:00
crashReporterSetup : {
isInitialized : ( ) => true ,
metadata : { }
}
2021-12-09 16:25:14 +00:00
} ) ;
} ;
2022-04-21 20:22:48 +00:00
const startUpdate = ( ) => {
2022-04-21 19:48:19 +00:00
if ( oaConfig . noTrack !== false ) {
const bl = { cancel : true } ; // Standard block callback response
let sentry ;
session . defaultSession . webRequest . onBeforeRequest ( {
urls : [
'https://*.discord.com/assets/*.js' ,
'https://*/api/*/science'
]
} , async ( { url } , cb ) => {
if ( url . endsWith ( '/science' ) ) return cb ( bl ) ;
if ( ! sentry && ( await new Promise ( ( res ) => get ( url , ( e , r , b ) => res ( b ) ) ) ) . includes ( 'RecipeWebview' ) ) sentry = url ;
if ( sentry === url ) return cb ( bl ) ;
cb ( { } ) ;
} ) ;
}
2022-04-08 21:31:08 +00:00
2022-04-05 12:13:55 +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 ) ;
2022-04-17 17:31:39 +00:00
inst . on ( 'update-error' , console . error ) ;
2022-04-05 12:13:55 +00:00
require ( './firstRun' ) . do ( inst ) ;
} else {
moduleUpdater . init ( Constants . UPDATE _ENDPOINT , buildInfo ) ;
}
2021-12-26 17:48:09 +00:00
2022-04-05 12:13:55 +00:00
splash . events . once ( 'APP_SHOULD_LAUNCH' , ( ) => {
if ( ! process . env . OPENASAR _NOSTART ) startCore ( ) ;
} ) ;
2021-12-12 11:45:25 +00:00
2022-04-06 19:54:04 +00:00
let done ;
2022-04-05 12:13:55 +00:00
splash . events . once ( 'APP_SHOULD_SHOW' , ( ) => {
2022-04-06 19:54:04 +00:00
if ( done ) return ;
done = true ;
2022-04-05 12:13:55 +00:00
desktopCore . setMainWindowVisible ( ! startMin ) ;
2021-12-11 09:59:39 +00:00
setTimeout ( ( ) => { // Try to update our asar
2022-04-09 07:25:09 +00:00
const config = require ( './config' ) ;
if ( oaConfig . setup !== true || process . argv . includes ( '--config' ) ) config . open ( ) ;
2022-03-13 20:12:42 +00:00
if ( oaConfig . autoupdate !== false ) { // If autoupdate disabled, don't update
try {
require ( './asarUpdate' ) ( ) ;
} catch ( e ) {
2022-04-02 21:23:19 +00:00
log ( 'AsarUpdate' , e ) ;
2022-03-13 20:12:42 +00:00
}
2021-12-11 09:59:39 +00:00
}
2022-03-13 20:12:42 +00:00
} , 3000 ) ;
2021-12-09 16:25:14 +00:00
} ) ;
2022-04-06 21:45:43 +00:00
splash . initSplash ( startMin ) ;
2021-12-09 16:25:14 +00:00
} ;
2021-12-11 10:37:43 +00:00
2021-12-09 16:25:14 +00:00
module . exports = ( ) => {
2022-04-22 16:04:37 +00:00
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-04-22 16:08:04 +00:00
if ( ! app . requestSingleInstanceLock ( ) && ! ( process . argv . includes ( '--multi-instance' ) || oaConfig . multiInstance === true ) ) return app . quit ( ) ;
2021-12-11 10:37:43 +00:00
2022-04-22 16:08:04 +00:00
if ( app . isReady ( ) ) startUpdate ( ) ;
else app . once ( 'ready' , startUpdate ) ;
2022-04-26 11:37:50 +00:00
} ;