2018-04-26 00:11:10 +00:00
/ * *
* Initialize UI functions which depend on internal modules .
* Loaded after core UI functions are initialized in uicore . js .
* /
// Requirements
const path = require ( 'path' )
2018-05-10 02:23:37 +00:00
const AuthManager = require ( './assets/js/authmanager.js' )
2018-05-09 00:10:46 +00:00
const { AssetGuard } = require ( './assets/js/assetguard.js' )
2018-05-07 22:15:59 +00:00
const ConfigManager = require ( './assets/js/configmanager.js' )
2018-04-26 00:11:10 +00:00
2018-05-07 05:34:57 +00:00
let rscShouldLoad = false
2018-05-08 10:34:16 +00:00
let fatalStartupError = false
2018-05-10 02:23:37 +00:00
// Mapping of each view to their container IDs.
const VIEWS = {
2018-05-30 01:47:55 +00:00
landing : '#landingContainer' ,
login : '#loginContainer' ,
settings : '#settingsContainer' ,
welcome : '#welcomeContainer'
2018-05-10 02:23:37 +00:00
}
// The currently shown view container.
2018-06-22 00:54:07 +00:00
let currentView
2018-05-10 02:23:37 +00:00
/ * *
* Switch launcher views .
*
* @ param { string } current The ID of the current view container .
* @ param { * } next The ID of the next view container .
* @ param { * } currentFadeTime Optional . The fade out time for the current view .
* @ param { * } nextFadeTime Optional . The fade in time for the next view .
* @ param { * } onCurrentFade Optional . Callback function to execute when the current
* view fades out .
* @ param { * } onNextFade Optional . Callback function to execute when the next view
* fades in .
* /
function switchView ( current , next , currentFadeTime = 500 , nextFadeTime = 500 , onCurrentFade = ( ) => { } , onNextFade = ( ) => { } ) {
2018-05-30 01:47:55 +00:00
currentView = next
$ ( ` ${ current } ` ) . fadeOut ( currentFadeTime , ( ) => {
2018-05-10 02:23:37 +00:00
onCurrentFade ( )
2018-05-30 01:47:55 +00:00
$ ( ` ${ next } ` ) . fadeIn ( nextFadeTime , ( ) => {
2018-05-10 02:23:37 +00:00
onNextFade ( )
} )
} )
}
/ * *
* Get the currently shown view container .
*
* @ returns { string } The currently shown view container .
* /
function getCurrentView ( ) {
return currentView
}
2018-05-08 10:34:16 +00:00
function showMainUI ( ) {
2018-06-05 00:06:34 +00:00
if ( ! isDev ) {
console . log ( '%c[AutoUpdater]' , 'color: #a02d2a; font-weight: bold' , 'Initializing..' )
ipcRenderer . send ( 'autoUpdateAction' , 'initAutoUpdater' , ConfigManager . getAllowPrerelease ( ) )
}
2018-05-09 00:10:46 +00:00
updateSelectedServer ( AssetGuard . getServerById ( ConfigManager . getSelectedServer ( ) ) . name )
2018-05-08 10:34:16 +00:00
refreshServerStatus ( )
setTimeout ( ( ) => {
2018-06-22 00:54:07 +00:00
document . getElementById ( 'frameBar' ) . style . backgroundColor = 'rgba(0, 0, 0, 0.5)'
2018-05-08 10:34:16 +00:00
document . body . style . backgroundImage = ` url('assets/images/backgrounds/ ${ document . body . getAttribute ( 'bkid' ) } .jpg') `
$ ( '#main' ) . show ( )
2018-05-31 03:32:51 +00:00
const isLoggedIn = Object . keys ( ConfigManager . getAuthAccounts ( ) ) . length > 0
2018-05-30 01:47:55 +00:00
// If this is enabled in a development environment we'll get ratelimited.
// The relaunch frequency is usually far too high.
2018-05-31 03:32:51 +00:00
if ( ! isDev && isLoggedIn ) {
2018-05-30 01:47:55 +00:00
validateSelectedAccount ( ) . then ( ( v ) => {
if ( v ) {
console . log ( '%c[AuthManager]' , 'color: #209b07; font-weight: bold' , 'Account access token validated.' )
} else {
console . log ( '%c[AuthManager]' , 'color: #a02d2a; font-weight: bold' , 'Account access token is invalid.' )
}
} )
}
2018-05-10 02:23:37 +00:00
2018-05-08 10:34:16 +00:00
if ( ConfigManager . isFirstLaunch ( ) ) {
2018-06-22 00:54:07 +00:00
currentView = VIEWS . welcome
2018-05-30 01:47:55 +00:00
$ ( VIEWS . welcome ) . fadeIn ( 1000 )
2018-05-08 10:34:16 +00:00
} else {
2018-05-31 03:32:51 +00:00
if ( isLoggedIn ) {
2018-06-22 00:54:07 +00:00
currentView = VIEWS . landing
2018-05-31 03:32:51 +00:00
$ ( VIEWS . landing ) . fadeIn ( 1000 )
} else {
2018-06-22 00:54:07 +00:00
currentView = VIEWS . login
2018-05-31 03:32:51 +00:00
$ ( VIEWS . login ) . fadeIn ( 1000 )
}
2018-05-08 10:34:16 +00:00
}
setTimeout ( ( ) => {
2018-05-10 02:23:37 +00:00
$ ( '#loadingContainer' ) . fadeOut ( 500 , ( ) => {
2018-05-08 10:34:16 +00:00
$ ( '#loadSpinnerImage' ) . removeClass ( 'rotating' )
} )
2018-05-10 04:01:38 +00:00
} , 250 )
2018-05-08 10:34:16 +00:00
} , 750 )
2018-05-10 09:48:55 +00:00
// Disable tabbing to the news container.
initNews ( ) . then ( ( ) => {
$ ( "#newsContainer *" ) . attr ( 'tabindex' , '-1' )
} )
2018-05-08 10:34:16 +00:00
}
function showFatalStartupError ( ) {
setTimeout ( ( ) => {
$ ( '#loadingContainer' ) . fadeOut ( 250 , ( ) => {
document . getElementById ( 'overlayContainer' ) . style . background = 'none'
setOverlayContent (
'Fatal Error: Unable to Load Distribution Index' ,
'A connection could not be established to our servers to download the distribution index. No local copies were available to load. <br><br>The distribution index is an essential file which provides the latest server information. The launcher is unable to start without it. Ensure you are connected to the internet and relaunch the application.' ,
'Close'
)
setOverlayHandler ( ( ) => {
const window = remote . getCurrentWindow ( )
window . close ( )
} )
toggleOverlay ( true )
} )
} , 750 )
}
2018-05-07 05:34:57 +00:00
2018-06-23 19:17:26 +00:00
/ * *
* Common functions to perform after refreshing the distro index .
*
* @ param { Object } data The distro index object .
* /
2018-05-09 00:10:46 +00:00
function onDistroRefresh ( data ) {
updateSelectedServer ( AssetGuard . getServerById ( ConfigManager . getSelectedServer ( ) ) . name )
refreshServerStatus ( )
initNews ( )
2018-06-23 19:17:26 +00:00
syncModConfigurations ( data )
}
/ * *
* Sync the mod configurations with the distro index .
*
* @ param { Object } data The distro index object .
* /
function syncModConfigurations ( data ) {
const syncedCfgs = [ ]
const servers = data . servers
for ( let i = 0 ; i < servers . length ; i ++ ) {
const id = servers [ i ] . id
const mdls = servers [ i ] . modules
const cfg = ConfigManager . getModConfiguration ( servers [ i ] . id )
if ( cfg != null ) {
const modsOld = cfg . mods
const mods = { }
for ( let j = 0 ; j < mdls . length ; j ++ ) {
const mdl = mdls [ j ]
2018-06-24 01:03:49 +00:00
if ( mdl . type === 'forgemod' || mdl . type === 'litemod' || mdl . type === 'liteloader' ) {
if ( mdl . required != null && mdl . required . value != null && mdl . required . value === false ) {
const mdlID = AssetGuard . _resolveWithoutVersion ( mdl . id )
if ( modsOld [ mdlID ] == null ) {
mods [ mdlID ] = scanOptionalSubModules ( mdl . sub _modules , mdl )
} else {
mods [ mdlID ] = mergeModConfiguration ( modsOld [ mdlID ] , scanOptionalSubModules ( mdl . sub _modules , mdl ) )
2018-06-23 19:17:26 +00:00
}
}
}
}
syncedCfgs . push ( {
id ,
mods
} )
} else {
const mods = { }
for ( let j = 0 ; j < mdls . length ; j ++ ) {
const mdl = mdls [ j ]
2018-06-24 01:03:49 +00:00
if ( mdl . type === 'forgemod' || mdl . type === 'litemod' || mdl . type === 'liteloader' ) {
if ( mdl . required != null && mdl . required . value != null && mdl . required . value === false ) {
mods [ AssetGuard . _resolveWithoutVersion ( mdl . id ) ] = scanOptionalSubModules ( mdl . sub _modules , mdl )
2018-06-23 19:17:26 +00:00
}
}
}
syncedCfgs . push ( {
id ,
mods
} )
}
}
ConfigManager . setModConfigurations ( syncedCfgs )
ConfigManager . save ( )
2018-05-09 00:10:46 +00:00
}
2018-06-24 01:03:49 +00:00
/ * *
* Recursively scan for optional sub modules . If none are found ,
* this function returns a boolean . If optional sub modules do exist ,
* a recursive configuration object is returned .
*
* @ returns { boolean | Object } The resolved mod configuration .
* /
function scanOptionalSubModules ( mdls , origin ) {
if ( mdls != null ) {
const mods = { }
for ( let i = 0 ; i < mdls . length ; i ++ ) {
const mdl = mdls [ i ]
// Optional types.
if ( mdl . type === 'forgemod' || mdl . type === 'litemod' || mdl . type === 'liteloader' ) {
// It is optional.
if ( mdl . required != null && mdl . required . value != null && mdl . required . value === false ) {
mods [ AssetGuard . _resolveWithoutVersion ( mdl . id ) ] = scanOptionalSubModules ( mdl . sub _modules , mdl )
}
}
}
if ( Object . keys ( mods ) . length > 0 ) {
return {
value : origin . required != null && origin . required . def != null ? origin . required . def : true ,
mods
}
}
}
return origin . required != null && origin . required . def != null ? origin . required . def : true
}
/ * *
* Recursively merge an old configuration into a new configuration .
*
* @ param { boolean | Object } o The old configuration value .
* @ param { boolean | Object } n The new configuration value .
*
* @ returns { boolean | Object } The merged configuration .
* /
function mergeModConfiguration ( o , n ) {
if ( typeof o === 'boolean' ) {
if ( typeof n === 'boolean' ) return o
else if ( typeof n === 'object' ) {
n . value = o
return n
}
} else if ( typeof o === 'object' ) {
if ( typeof n === 'boolean' ) return o . value
else if ( typeof n === 'object' ) {
n . value = o . value
const newMods = Object . keys ( n . mods )
for ( let i = 0 ; i < newMods . length ; i ++ ) {
const mod = newMods [ i ]
if ( o . mods [ mod ] != null ) {
n . mods [ mod ] = mergeModConfiguration ( o . mods [ mod ] , n . mods [ mod ] )
}
}
return n
}
}
// If for some reason we haven't been able to merge,
// wipe the old value and use the new one. Just to be safe
return n
}
2018-05-09 00:10:46 +00:00
function refreshDistributionIndex ( remote , onSuccess , onError ) {
if ( remote ) {
AssetGuard . refreshDistributionDataRemote ( ConfigManager . getLauncherDirectory ( ) )
. then ( onSuccess )
. catch ( onError )
} else {
AssetGuard . refreshDistributionDataLocal ( ConfigManager . getLauncherDirectory ( ) )
. then ( onSuccess )
. catch ( onError )
}
}
2018-05-10 02:23:37 +00:00
async function validateSelectedAccount ( ) {
const selectedAcc = ConfigManager . getSelectedAccount ( )
if ( selectedAcc != null ) {
const val = await AuthManager . validateSelected ( )
if ( ! val ) {
2018-05-30 01:47:55 +00:00
ConfigManager . removeAuthAccount ( selectedAcc . uuid )
ConfigManager . save ( )
2018-05-10 02:23:37 +00:00
const accLen = Object . keys ( ConfigManager . getAuthAccounts ( ) ) . length
setOverlayContent (
'Failed to Refresh Login' ,
2018-05-30 01:47:55 +00:00
` We were unable to refresh the login for <strong> ${ selectedAcc . displayName } </strong>. Please ${ accLen > 0 ? 'select another account or ' : '' } login again. ` ,
2018-05-10 02:23:37 +00:00
'Login' ,
'Select Another Account'
)
setOverlayHandler ( ( ) => {
document . getElementById ( 'loginUsername' ) . value = selectedAcc . username
validateEmail ( selectedAcc . username )
2018-05-31 02:22:17 +00:00
loginViewOnSuccess = getCurrentView ( )
2018-05-10 02:23:37 +00:00
switchView ( getCurrentView ( ) , VIEWS . login )
toggleOverlay ( false )
} )
setDismissHandler ( ( ) => {
2018-05-30 01:47:55 +00:00
if ( accLen > 1 ) {
2018-05-10 02:23:37 +00:00
prepareAccountSelectionList ( )
$ ( '#overlayContent' ) . fadeOut ( 250 , ( ) => {
$ ( '#accountSelectContent' ) . fadeIn ( 250 )
} )
} else {
const accountsObj = ConfigManager . getAuthAccounts ( )
2018-05-30 01:47:55 +00:00
const accounts = Array . from ( Object . keys ( accountsObj ) , v => accountsObj [ v ] ) ;
// This function validates the account switch.
setSelectedAccount ( accounts [ 0 ] . uuid )
toggleOverlay ( false )
2018-05-10 02:23:37 +00:00
}
} )
2018-05-30 01:47:55 +00:00
toggleOverlay ( true , accLen > 0 )
2018-05-10 02:23:37 +00:00
} else {
return true
}
} else {
return true
}
}
2018-05-10 04:01:38 +00:00
/ * *
* Temporary function to update the selected account along
* with the relevent UI elements .
*
* @ param { string } uuid The UUID of the account .
* /
2018-05-10 02:23:37 +00:00
function setSelectedAccount ( uuid ) {
const authAcc = ConfigManager . setSelectedAccount ( uuid )
ConfigManager . save ( )
updateSelectedAccount ( authAcc )
2018-05-30 01:47:55 +00:00
validateSelectedAccount ( )
2018-05-10 02:23:37 +00:00
}
2018-04-26 00:11:10 +00:00
// Synchronous Listener
document . addEventListener ( 'readystatechange' , function ( ) {
if ( document . readyState === 'complete' ) {
2018-05-07 05:34:57 +00:00
if ( rscShouldLoad ) {
2018-05-08 10:34:16 +00:00
if ( ! fatalStartupError ) {
showMainUI ( )
2018-05-07 05:34:57 +00:00
} else {
2018-05-08 10:34:16 +00:00
showFatalStartupError ( )
2018-05-07 05:34:57 +00:00
}
2018-05-08 10:34:16 +00:00
}
} else if ( document . readyState === 'interactive' ) {
//toggleOverlay(true, 'loadingContent')
2018-04-26 00:11:10 +00:00
}
/ * i f ( d o c u m e n t . r e a d y S t a t e = = = ' i n t e r a c t i v e ' ) {
} * /
} , false )
2018-05-07 05:34:57 +00:00
// Actions that must be performed after the distribution index is downloaded.
2018-05-08 10:34:16 +00:00
ipcRenderer . on ( 'distributionIndexDone' , ( event , data ) => {
if ( data != null ) {
2018-06-23 19:17:26 +00:00
syncModConfigurations ( data )
2018-05-08 10:34:16 +00:00
if ( document . readyState === 'complete' ) {
showMainUI ( )
2018-05-07 05:34:57 +00:00
} else {
2018-05-08 10:34:16 +00:00
rscShouldLoad = true
2018-05-07 05:34:57 +00:00
}
} else {
2018-05-08 10:34:16 +00:00
fatalStartupError = true
if ( document . readyState === 'complete' ) {
showFatalStartupError ( )
} else {
rscShouldLoad = true
}
2018-05-07 05:34:57 +00:00
}
} )