2022-04-22 09:11:57 +00:00
const { app , ipcMain } = require ( 'electron' ) ;
2022-02-11 22:41:40 +00:00
const moduleUpdater = require ( "../updater/moduleUpdater" ) ;
const updater = require ( "../updater/updater" ) ;
2022-04-23 13:26:59 +00:00
let launched , win ;
2022-02-11 22:41:40 +00:00
2022-04-20 12:56:52 +00:00
exports . initSplash = ( startMin ) => {
const inst = updater . getUpdater ( ) ;
if ( inst ) initNew ( inst ) ;
else initOld ( ) ;
2022-02-06 17:48:27 +00:00
2022-04-04 14:06:05 +00:00
launchSplash ( startMin ) ;
2022-02-06 17:48:27 +00:00
2022-02-11 22:41:40 +00:00
if ( process . env . OPENASAR _QUICKSTART || oaConfig . quickstart ) setTimeout ( ( ) => {
destroySplash ( ) ;
2022-04-04 14:06:05 +00:00
launchMain ( ) ;
2022-06-08 19:10:38 +00:00
2022-02-11 22:41:40 +00:00
setTimeout ( ( ) => {
2022-03-13 15:53:33 +00:00
events . emit ( 'APP_SHOULD_SHOW' ) ;
2022-02-11 22:41:40 +00:00
} , 100 ) ;
} , 300 ) ;
} ;
2022-02-06 17:48:27 +00:00
2022-04-07 15:51:00 +00:00
exports . focusWindow = ( ) => win ? . focus ? . ( ) ;
2022-04-07 13:52:07 +00:00
exports . pageReady = ( ) => destroySplash ( ) || process . nextTick ( ( ) => events . emit ( 'APP_SHOULD_SHOW' ) ) ;
2022-02-06 17:48:27 +00:00
2022-02-11 22:41:40 +00:00
const destroySplash = ( ) => {
2022-04-07 15:51:00 +00:00
win ? . setSkipTaskbar ? . ( true ) ;
2022-02-06 17:48:27 +00:00
2022-02-11 22:41:40 +00:00
setTimeout ( ( ) => {
2022-04-07 15:51:00 +00:00
if ( ! win ) return ;
2022-02-06 17:48:27 +00:00
2022-04-07 15:51:00 +00:00
win . hide ( ) ;
win . close ( ) ;
win = null ;
2022-02-11 22:41:40 +00:00
} , 100 ) ;
} ;
2022-02-06 17:48:27 +00:00
2022-04-04 14:06:05 +00:00
const launchMain = ( ) => {
2022-04-20 10:45:03 +00:00
moduleUpdater . events . removeAllListeners ( ) ; // Remove updater v1 listeners
2022-02-06 17:48:27 +00:00
2022-04-20 10:45:03 +00:00
if ( ! launched && win != null ) {
2022-04-07 13:52:07 +00:00
sendState ( 'starting' ) ;
2022-02-16 16:02:58 +00:00
2022-04-20 10:45:03 +00:00
launched = true ;
2022-04-07 13:52:07 +00:00
events . emit ( 'APP_SHOULD_LAUNCH' ) ;
}
2022-02-11 22:41:40 +00:00
} ;
2022-02-06 17:48:27 +00:00
2022-04-23 13:26:59 +00:00
const sendState = ( status , s = { } ) => {
2022-02-17 20:47:15 +00:00
try {
2022-04-23 13:26:59 +00:00
win . webContents . send ( 'state' , { status , ... s } ) ;
} catch { }
2022-02-17 20:47:15 +00:00
} ;
2022-02-06 17:48:27 +00:00
2022-04-04 14:06:05 +00:00
const launchSplash = ( startMin ) => {
2022-04-17 21:46:38 +00:00
win = require ( '../utils/win' ) ( {
2022-02-11 22:41:40 +00:00
width : 300 ,
2022-04-17 21:46:38 +00:00
height : process . platform === 'darwin' ? 300 : 350
2022-04-19 22:06:34 +00:00
} , 'splash' ) ;
2022-02-11 22:41:40 +00:00
2022-04-20 10:45:03 +00:00
if ( process . platform !== 'darwin' ) win . on ( 'closed' , ( ) => ! launched && app . quit ( ) ) ;
2022-02-11 22:41:40 +00:00
2022-04-22 09:11:57 +00:00
ipcMain . on ( 'ss' , launchMain ) ;
ipcMain . on ( 'sq' , app . quit ) ;
2022-04-17 21:58:03 +00:00
if ( ! startMin ) win . once ( 'ready-to-show' , win . show ) ;
2022-02-11 22:41:40 +00:00
} ;
2022-03-13 15:53:33 +00:00
const events = exports . events = new ( require ( 'events' ) . EventEmitter ) ( ) ;
2022-04-18 21:51:54 +00:00
let toSend = 0 ; // Progress state to send for ModuleUpdater (0 = downloading, 1 = installing)
2022-02-15 13:57:13 +00:00
class UIProgress { // Generic class to track updating and sent states to splash
2022-03-13 15:53:33 +00:00
constructor ( st ) {
2022-04-18 21:51:54 +00:00
this . st = st ;
2022-03-13 15:53:33 +00:00
2022-02-16 10:21:56 +00:00
this . reset ( ) ;
}
reset ( ) {
2022-02-15 13:57:13 +00:00
Object . assign ( this , {
progress : new Map ( ) ,
done : new Set ( ) ,
total : new Set ( )
} ) ;
2021-12-09 16:25:14 +00:00
}
2022-02-06 17:48:27 +00:00
2022-03-15 19:11:54 +00:00
record ( id , state , current , outOf ) {
this . total . add ( id ) ;
if ( current ) this . progress . set ( id , [ current , outOf ? ? 100 ] ) ;
if ( state === 'Complete' ) this . done . add ( id ) ;
this . send ( ) ;
}
send ( ) {
2022-04-20 12:56:52 +00:00
if ( ( toSend === - 1 && this . progress . size > 0 && this . progress . size > this . done . size ) || toSend === this . st ) {
2022-04-26 19:25:13 +00:00
const progress = Math . min ( 100 , [ ... this . progress . values ( ) ] . reduce ( ( a , x ) => a + x [ 0 ] , 0 ) / [ ... this . progress . values ( ) ] . reduce ( ( a , x ) => a + x [ 1 ] , 0 ) * 100 ) ; // Clamp progress to 0-100
2022-03-15 19:11:54 +00:00
2022-04-23 13:26:59 +00:00
sendState ( this . st ? 'installing' : 'downloading' , {
2022-02-15 13:57:13 +00:00
current : this . done . size + 1 ,
total : this . total . size ,
2022-03-15 19:11:54 +00:00
progress
2022-04-23 13:26:59 +00:00
} ) ;
2022-02-15 13:57:13 +00:00
2021-12-09 16:25:14 +00:00
return true ;
}
}
}
2022-02-06 17:48:27 +00:00
2022-04-20 12:56:52 +00:00
const initNew = async ( inst ) => {
toSend = - 1 ;
2021-12-09 16:25:14 +00:00
const retryOptions = {
2022-12-11 12:35:40 +00:00
skip _host _delta : true ,
2021-12-09 16:25:14 +00:00
skip _module _delta : { }
} ;
2022-02-06 17:48:27 +00:00
2021-12-09 16:25:14 +00:00
while ( true ) {
2022-04-28 21:37:04 +00:00
sendState ( 'checking-for-updates' ) ;
2022-02-06 17:48:27 +00:00
2021-12-09 16:25:14 +00:00
try {
let installedAnything = false ;
2022-03-13 15:53:33 +00:00
const downloads = new UIProgress ( 0 ) ;
const installs = new UIProgress ( 1 ) ;
2022-02-15 13:57:13 +00:00
2022-04-20 12:56:52 +00:00
await inst . updateToLatestWithOptions ( retryOptions , ( { task , state , percent } ) => {
2022-02-15 13:57:13 +00:00
const download = task . HostDownload || task . ModuleDownload ;
const install = task . HostInstall || task . ModuleInstall ;
2021-12-09 16:25:14 +00:00
installedAnything = true ;
2022-02-06 17:48:27 +00:00
2022-02-16 16:02:58 +00:00
const simpleRecord = ( tracker , x ) => tracker . record ( x . package _sha256 , state , percent ) ;
if ( download != null ) simpleRecord ( downloads , download ) ;
2022-02-06 17:48:27 +00:00
2022-02-16 10:34:23 +00:00
if ( ! downloads . send ( ) ) installs . send ( ) ;
2022-02-06 17:48:27 +00:00
2022-02-15 13:57:13 +00:00
if ( install == null ) return ;
2022-02-16 16:02:58 +00:00
simpleRecord ( installs , install ) ;
2022-02-06 17:48:27 +00:00
2022-02-15 13:57:13 +00:00
if ( task . HostInstall != null ) {
retryOptions . skip _host _delta = true ;
} else if ( task . ModuleInstall != null ) {
retryOptions . skip _module _delta [ install . version . module . name ] = true ;
2021-12-09 16:25:14 +00:00
}
} ) ;
2022-02-06 17:48:27 +00:00
2021-12-09 16:25:14 +00:00
if ( ! installedAnything ) {
2022-04-20 12:56:52 +00:00
await inst . startCurrentVersion ( ) ;
inst . collectGarbage ( ) ;
2022-02-15 13:57:13 +00:00
2022-04-04 14:06:05 +00:00
return launchMain ( ) ;
2021-12-09 16:25:14 +00:00
}
} catch ( e ) {
2022-04-02 21:23:19 +00:00
log ( 'Splash' , e ) ;
2022-04-20 11:17:12 +00:00
await new Promise ( r => fail ( r ) ) ;
2021-12-09 16:25:14 +00:00
}
}
2022-02-15 19:15:09 +00:00
} ;
2022-02-06 17:48:27 +00:00
2022-04-20 12:56:52 +00:00
const initOld = ( ) => { // "Old" (not v2 / new, win32 only)
2022-04-20 10:45:03 +00:00
const on = ( k , v ) => moduleUpdater . events . on ( k , v ) ;
2022-02-15 13:57:13 +00:00
2022-04-20 10:45:03 +00:00
const check = ( ) => moduleUpdater . checkForUpdates ( ) ;
2022-02-15 13:57:13 +00:00
2022-03-15 19:11:54 +00:00
const downloads = new UIProgress ( 0 ) , installs = new UIProgress ( 1 ) ;
2022-02-15 13:57:13 +00:00
const handleFail = ( ) => {
2022-04-20 11:17:12 +00:00
fail ( check ) ;
2022-02-15 13:57:13 +00:00
} ;
2022-04-22 20:51:35 +00:00
on ( 'checked' , ( { failed , count } ) => { // Finished check
2022-02-16 10:21:56 +00:00
installs . reset ( ) ;
downloads . reset ( ) ;
2022-04-22 20:51:35 +00:00
if ( failed ) handleFail ( ) ;
2022-04-28 21:37:04 +00:00
else if ( ! count ) launchMain ( ) ; // Count is 0 / undefined
2021-12-09 16:25:14 +00:00
} ) ;
2022-02-06 17:48:27 +00:00
2022-04-22 20:51:35 +00:00
on ( 'downloaded' , ( { failed } ) => { // Downloaded all modules
2022-04-18 21:51:54 +00:00
toSend = 1 ;
2022-03-15 20:16:09 +00:00
if ( failed > 0 ) handleFail ( ) ;
2021-12-09 16:25:14 +00:00
} ) ;
2022-04-22 20:51:35 +00:00
on ( 'installed' , check ) ; // Installed all modules
on ( 'downloading-module' , ( { name , cur , total } ) => {
downloads . record ( name , '' , cur , total ) ;
installs . record ( name , 'Waiting' ) ;
} ) ;
2022-06-08 19:10:38 +00:00
2022-04-21 17:37:31 +00:00
on ( 'installing-module' , ( { name , cur , total } ) => {
installs . record ( name , '' , cur , total ) ;
2022-02-16 10:21:56 +00:00
} ) ;
2022-04-21 17:45:37 +00:00
const segment = ( tracker ) => ( ( { name } ) => {
2022-03-15 19:11:54 +00:00
tracker . record ( name , 'Complete' ) ;
2021-12-09 16:25:14 +00:00
} ) ;
2022-02-15 13:57:13 +00:00
2022-04-21 17:45:37 +00:00
on ( 'downloaded-module' , segment ( downloads ) ) ;
on ( 'installed-module' , segment ( installs ) ) ;
2022-02-15 13:57:13 +00:00
2022-06-08 19:10:38 +00:00
on ( 'manual' , ( e ) => sendState ( 'manual' , { details : e } ) ) ; // Host manual update required
2022-02-15 13:57:13 +00:00
2022-04-28 21:37:04 +00:00
sendState ( 'checking-for-updates' ) ;
2022-04-17 17:15:40 +00:00
2022-04-20 10:45:03 +00:00
check ( ) ;
2022-04-05 21:31:55 +00:00
} ;
2022-02-15 13:57:13 +00:00
2022-04-20 11:17:12 +00:00
const fail = ( c ) => {
2022-04-23 13:26:59 +00:00
sendState ( 'fail' , { seconds : 10 } ) ;
2022-02-15 13:57:13 +00:00
2022-04-23 13:26:59 +00:00
setTimeout ( c , 10000 ) ;
2022-02-16 10:21:56 +00:00
} ;