preloader.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const {ipcRenderer} = require('electron')
  2. const os = require('os')
  3. const path = require('path')
  4. const rimraf = require('rimraf')
  5. const ConfigManager = require('./configmanager')
  6. const DistroManager = require('./distromanager')
  7. console.log('%c[Preloader]', 'color: #a02d2a; font-weight: bold', 'Loading..')
  8. // Load ConfigManager
  9. ConfigManager.load()
  10. function onDistroLoad(data){
  11. if(data != null){
  12. // Resolve the selected server if its value has yet to be set.
  13. if(ConfigManager.getSelectedServer() == null || data.getServer(ConfigManager.getSelectedServer()) == null){
  14. console.log('%c[Preloader]', 'color: #a02d2a; font-weight: bold', 'Determining default selected server..')
  15. ConfigManager.setSelectedServer(data.getMainServer().getID())
  16. ConfigManager.save()
  17. }
  18. }
  19. ipcRenderer.send('distributionIndexDone', data != null)
  20. }
  21. // Ensure Distribution is downloaded and cached.
  22. DistroManager.pullRemote().then((data) => {
  23. console.log('%c[Preloader]', 'color: #a02d2a; font-weight: bold', 'Loaded distribution index.')
  24. onDistroLoad(data)
  25. }).catch((err) => {
  26. console.log('%c[Preloader]', 'color: #a02d2a; font-weight: bold', 'Failed to load distribution index.')
  27. console.error(err)
  28. console.log('%c[Preloader]', 'color: #a02d2a; font-weight: bold', 'Attempting to load an older version of the distribution index.')
  29. // Try getting a local copy, better than nothing.
  30. DistroManager.pullLocal().then((data) => {
  31. console.log('%c[Preloader]', 'color: #a02d2a; font-weight: bold', 'Successfully loaded an older version of the distribution index.')
  32. onDistroLoad(data)
  33. }).catch((err) => {
  34. console.log('%c[Preloader]', 'color: #a02d2a; font-weight: bold', 'Failed to load an older version of the distribution index.')
  35. console.log('%c[Preloader]', 'color: #a02d2a; font-weight: bold', 'Application cannot run.')
  36. console.error(err)
  37. onDistroLoad(null)
  38. })
  39. })
  40. // Clean up temp dir incase previous launches ended unexpectedly.
  41. rimraf(path.join(os.tmpdir(), ConfigManager.getTempNativeFolder()), (err) => {
  42. if(err){
  43. console.warn('%c[Preloader]', 'color: #a02d2a; font-weight: bold', 'Error while cleaning natives directory', err)
  44. } else {
  45. console.log('%c[Preloader]', 'color: #a02d2a; font-weight: bold', 'Cleaned natives directory.')
  46. }
  47. })