preloader.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const {AssetGuard} = require('./assetguard.js')
  2. const ConfigManager = require('./configmanager.js')
  3. const {ipcRenderer} = require('electron')
  4. const os = require('os')
  5. const path = require('path')
  6. const rimraf = require('rimraf')
  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 || AssetGuard.getServerById(ConfigManager.getLauncherDirectory(), ConfigManager.getSelectedServer()) == null){
  14. console.log('Determining default selected server..')
  15. ConfigManager.setSelectedServer(AssetGuard.resolveSelectedServer(ConfigManager.getLauncherDirectory()).id)
  16. ConfigManager.save()
  17. }
  18. }
  19. ipcRenderer.send('distributionIndexDone', data)
  20. }
  21. // Ensure Distribution is downloaded and cached.
  22. AssetGuard.retrieveDistributionDataFresh(ConfigManager.getLauncherDirectory()).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. AssetGuard.retrieveDistributionData(ConfigManager.getLauncherDirectory(), false).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. })