script.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. const $ = require('jquery');
  2. const remote = require('electron').remote
  3. const shell = require('electron').shell
  4. const path = require('path')
  5. const os = require('os');
  6. const ag = require(path.join(__dirname, 'assets', 'js', 'assetguard.js'))
  7. const ProcessBuilder = require(path.join(__dirname, 'assets', 'js', 'processbuilder.js'))
  8. const mojang = require('mojang')
  9. const {GAME_DIRECTORY, DEFAULT_CONFIG} = require(path.join(__dirname, 'assets', 'js', 'constants.js'))
  10. $(document).on('ready', function(){
  11. console.log('okay');
  12. })
  13. document.onreadystatechange = function () {
  14. if (document.readyState == "complete") {
  15. // Bind close button.
  16. document.getElementById("frame_btn_close").addEventListener("click", function (e) {
  17. const window = remote.getCurrentWindow()
  18. window.close()
  19. })
  20. // Bind restore down button.
  21. document.getElementById("frame_btn_restoredown").addEventListener("click", function (e) {
  22. const window = remote.getCurrentWindow()
  23. if(window.isMaximized()){
  24. window.unmaximize();
  25. } else {
  26. window.maximize()
  27. }
  28. })
  29. // Bind minimize button.
  30. document.getElementById("frame_btn_minimize").addEventListener("click", function (e) {
  31. const window = remote.getCurrentWindow()
  32. window.minimize()
  33. })
  34. // Bind launch button
  35. document.getElementById("launch_button").addEventListener('click', function(e){
  36. console.log('Launching game..')
  37. testdownloads()
  38. })
  39. // Bind progress bar length to length of bot wrapper
  40. const targetWidth = document.getElementById("launch_content").getBoundingClientRect().width
  41. const targetWidth2 = document.getElementById("server_selection").getBoundingClientRect().width
  42. const targetWidth3 = document.getElementById("launch_button").getBoundingClientRect().width
  43. document.getElementById("launch_details").style.maxWidth = targetWidth
  44. document.getElementById("launch_progress").style.width = targetWidth2
  45. document.getElementById("launch_details_right").style.maxWidth = targetWidth2
  46. document.getElementById("launch_progress_label").style.width = targetWidth3
  47. }
  48. }
  49. // Open web links in the user's default browser.
  50. $(document).on('click', 'a[href^="http"]', function(event) {
  51. event.preventDefault();
  52. //console.log(os.homedir())
  53. shell.openExternal(this.href)
  54. })
  55. testdownloads = async function(){
  56. const content = document.getElementById("launch_content")
  57. const details = document.getElementById("launch_details")
  58. const progress = document.getElementById("launch_progress")
  59. const progress_text = document.getElementById("launch_progress_label")
  60. const det_text = document.getElementById("launch_details_text")
  61. det_text.innerHTML = 'Please wait..'
  62. progress.setAttribute('max', '100')
  63. details.style.display = 'flex'
  64. content.style.display = 'none'
  65. det_text.innerHTML = 'Loading version information..'
  66. const versionData = await ag.loadVersionData('1.11.2', GAME_DIRECTORY)
  67. progress.setAttribute('value', 20)
  68. progress_text.innerHTML = '20%'
  69. det_text.innerHTML = 'Validating asset integrity..'
  70. await ag.validateAssets(versionData, GAME_DIRECTORY)
  71. progress.setAttribute('value', 40)
  72. progress_text.innerHTML = '40%'
  73. console.log('assets done')
  74. det_text.innerHTML = 'Validating library integrity..'
  75. await ag.validateLibraries(versionData, GAME_DIRECTORY)
  76. progress.setAttribute('value', 60)
  77. progress_text.innerHTML = '60%'
  78. console.log('libs done')
  79. det_text.innerHTML = 'Validating miscellaneous file integrity..'
  80. await ag.validateMiscellaneous(versionData, GAME_DIRECTORY)
  81. progress.setAttribute('value', 80)
  82. progress_text.innerHTML = '80%'
  83. console.log('files done')
  84. det_text.innerHTML = 'Validating server distribution files..'
  85. const serv = await ag.validateDistribution('WesterosCraft-1.11.2', GAME_DIRECTORY)
  86. progress.setAttribute('value', 100)
  87. progress_text.innerHTML = '100%'
  88. console.log('forge stuff done')
  89. det_text.innerHTML = 'Downloading files..'
  90. ag.instance.on('totaldlprogress', function(data){
  91. progress.setAttribute('max', data.total)
  92. progress.setAttribute('value', data.acc)
  93. progress_text.innerHTML = parseInt((data.acc/data.total)*100) + '%'
  94. })
  95. ag.instance.on('dlcomplete', async function(){
  96. det_text.innerHTML = 'Preparing to launch..'
  97. const forgeData = await ag.loadForgeData('WesterosCraft-1.11.2', GAME_DIRECTORY)
  98. const authUser = await mojang.auth('EMAIL', 'PASS', DEFAULT_CONFIG.getClientToken(), {
  99. name: 'Minecraft',
  100. version: 1
  101. })
  102. let pb = new ProcessBuilder(GAME_DIRECTORY, serv, versionData, forgeData, authUser)
  103. det_text.innerHTML = 'Launching game..'
  104. let proc;
  105. try{
  106. proc = pb.build()
  107. det_text.innerHTML = 'Done. Enjoy the server!'
  108. } catch(err) {
  109. //det_text.innerHTML = 'Error: ' + err.message;
  110. det_text.innerHTML = 'Error: See log for details..';
  111. }
  112. setTimeout(function(){
  113. details.style.display = 'none'
  114. content.style.display = 'inline-flex'
  115. }, 5000)
  116. })
  117. ag.processDlQueues()
  118. }
  119. /**
  120. * Opens DevTools window if you type "wcdev" in sequence.
  121. * This will crash the program if you are using multiple
  122. * DevTools, for example the chrome debugger in VS Code.
  123. */
  124. const match = [87, 67, 68, 69, 86]
  125. let at = 0;
  126. document.addEventListener('keydown', function (e) {
  127. switch(e.keyCode){
  128. case match[0]:
  129. if(at === 0) ++at
  130. break
  131. case match[1]:
  132. if(at === 1) ++at
  133. break
  134. case match[2]:
  135. if(at === 2) ++at
  136. break
  137. case match[3]:
  138. if(at === 3) ++at
  139. break
  140. case match[4]:
  141. if(at === 4) ++at
  142. break
  143. default:
  144. at = 0
  145. }
  146. if(at === 5) {
  147. var window = remote.getCurrentWindow()
  148. window.toggleDevTools()
  149. at = 0
  150. }
  151. })