2018-05-06 22:34:57 -07:00
|
|
|
const {ipcRenderer} = require('electron')
|
2018-08-22 11:21:49 -07:00
|
|
|
const os = require('os')
|
|
|
|
const path = require('path')
|
|
|
|
const rimraf = require('rimraf')
|
2017-12-01 23:59:25 -08:00
|
|
|
|
2018-07-22 08:40:15 -07:00
|
|
|
const ConfigManager = require('./configmanager')
|
|
|
|
const DistroManager = require('./distromanager')
|
2018-08-22 11:21:49 -07:00
|
|
|
const logger = require('./loggerutil')('%c[Preloader]', 'color: #a02d2a; font-weight: bold')
|
2018-07-22 08:40:15 -07:00
|
|
|
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.log('Loading..')
|
2017-12-01 23:59:25 -08:00
|
|
|
|
2017-12-02 21:38:22 -08:00
|
|
|
// Load ConfigManager
|
|
|
|
ConfigManager.load()
|
2017-12-02 19:41:47 -08:00
|
|
|
|
2018-05-08 03:34:16 -07:00
|
|
|
function onDistroLoad(data){
|
|
|
|
if(data != null){
|
|
|
|
|
2018-07-22 10:31:15 -07:00
|
|
|
// Resolve the selected server if its value has yet to be set.
|
2018-07-22 08:40:15 -07:00
|
|
|
if(ConfigManager.getSelectedServer() == null || data.getServer(ConfigManager.getSelectedServer()) == null){
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.log('Determining default selected server..')
|
2018-07-22 08:40:15 -07:00
|
|
|
ConfigManager.setSelectedServer(data.getMainServer().getID())
|
2018-05-08 03:34:16 -07:00
|
|
|
ConfigManager.save()
|
|
|
|
}
|
|
|
|
}
|
2018-07-22 08:40:15 -07:00
|
|
|
ipcRenderer.send('distributionIndexDone', data != null)
|
2018-05-08 03:34:16 -07:00
|
|
|
}
|
|
|
|
|
2017-12-02 21:38:22 -08:00
|
|
|
// Ensure Distribution is downloaded and cached.
|
2018-07-22 08:40:15 -07:00
|
|
|
DistroManager.pullRemote().then((data) => {
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.log('Loaded distribution index.')
|
2017-12-02 19:41:47 -08:00
|
|
|
|
2018-07-22 10:31:15 -07:00
|
|
|
onDistroLoad(data)
|
2018-05-06 22:34:57 -07:00
|
|
|
|
2018-05-08 03:34:16 -07:00
|
|
|
}).catch((err) => {
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.log('Failed to load distribution index.')
|
|
|
|
logger.error(err)
|
2018-05-08 03:34:16 -07:00
|
|
|
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.log('Attempting to load an older version of the distribution index.')
|
2018-05-08 03:34:16 -07:00
|
|
|
// Try getting a local copy, better than nothing.
|
2018-07-22 08:40:15 -07:00
|
|
|
DistroManager.pullLocal().then((data) => {
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.log('Successfully loaded an older version of the distribution index.')
|
2018-05-08 03:34:16 -07:00
|
|
|
|
|
|
|
onDistroLoad(data)
|
|
|
|
|
|
|
|
|
|
|
|
}).catch((err) => {
|
|
|
|
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.log('Failed to load an older version of the distribution index.')
|
|
|
|
logger.log('Application cannot run.')
|
|
|
|
logger.error(err)
|
2018-05-08 03:34:16 -07:00
|
|
|
|
|
|
|
onDistroLoad(null)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2018-05-06 22:34:57 -07:00
|
|
|
})
|
2018-04-14 21:00:08 -07:00
|
|
|
|
2018-04-14 21:49:20 -07:00
|
|
|
// Clean up temp dir incase previous launches ended unexpectedly.
|
2018-04-14 21:00:08 -07:00
|
|
|
rimraf(path.join(os.tmpdir(), ConfigManager.getTempNativeFolder()), (err) => {
|
|
|
|
if(err){
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.warn('Error while cleaning natives directory', err)
|
2018-04-14 21:00:08 -07:00
|
|
|
} else {
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.log('Cleaned natives directory.')
|
2018-04-14 21:00:08 -07:00
|
|
|
}
|
|
|
|
})
|