actionbinder.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. const cp = require('child_process')
  2. const path = require('path')
  3. const {AssetGuard} = require(path.join(__dirname, 'assets', 'js', 'assetguard.js'))
  4. const ProcessBuilder = require(path.join(__dirname, 'assets', 'js', 'processbuilder.js'))
  5. const ConfigManager = require(path.join(__dirname, 'assets', 'js', 'configmanager.js'))
  6. const DiscordWrapper = require(path.join(__dirname, 'assets', 'js', 'discordwrapper.js'))
  7. const Mojang = require(path.join(__dirname, 'assets', 'js', 'mojang.js'))
  8. const AuthManager = require(path.join(__dirname, 'assets', 'js', 'authmanager.js'))
  9. let mojangStatusListener
  10. // Launch Elements
  11. let launch_content, launch_details, launch_progress, launch_progress_label, launch_details_text
  12. // Synchronous Listener
  13. document.addEventListener('readystatechange', function(){
  14. if (document.readyState === 'complete'){
  15. if(ConfigManager.isFirstLaunch()){
  16. $('#welcomeContainer').fadeIn(500)
  17. } else {
  18. $('#landingContainer').fadeIn(500)
  19. }
  20. }
  21. if (document.readyState === 'interactive'){
  22. // Save a reference to the launch elements.
  23. launch_content = document.getElementById('launch_content')
  24. launch_details = document.getElementById('launch_details')
  25. launch_progress = document.getElementById('launch_progress')
  26. launch_progress_label = document.getElementById('launch_progress_label')
  27. launch_details_text = document.getElementById('launch_details_text')
  28. // Bind launch button
  29. document.getElementById('launch_button').addEventListener('click', function(e){
  30. console.log('Launching game..')
  31. //testdownloads()
  32. dlAsync()
  33. })
  34. // TODO convert this to dropdown menu.
  35. // Bind selected server
  36. document.getElementById('server_selection').innerHTML = '\u2022 ' + AssetGuard.getServerById(ConfigManager.getGameDirectory(), ConfigManager.getSelectedServer()).name
  37. // Update Mojang Status Color
  38. const refreshMojangStatuses = async function(){
  39. console.log('Refreshing Mojang Statuses..')
  40. try {
  41. let status = 'grey'
  42. const statuses = await Mojang.status()
  43. greenCount = 0
  44. for(let i=0; i<statuses.length; i++){
  45. if(statuses[i].status === 'yellow' && status !== 'red'){
  46. status = 'yellow'
  47. continue
  48. } else if(statuses[i].status === 'red'){
  49. status = 'red'
  50. break
  51. }
  52. ++greenCount
  53. }
  54. if(greenCount == statuses.length){
  55. status = 'green'
  56. }
  57. document.getElementById('mojang_status_icon').style.color = Mojang.statusToHex(status)
  58. } catch (err) {
  59. console.error('Unable to refresh Mojang service status..', err)
  60. }
  61. }
  62. refreshMojangStatuses()
  63. // Set refresh rate to once every 5 minutes.
  64. mojangStatusListener = setInterval(refreshMojangStatuses, 300000)
  65. }
  66. }, false)
  67. // Keep reference to Minecraft Process
  68. let proc
  69. // Is DiscordRPC enabled
  70. let hasRPC = false
  71. // Joined server regex
  72. const servJoined = /[[0-2][0-9]:[0-6][0-9]:[0-6][0-9]\] \[Client thread\/INFO\]: \[CHAT\] [a-zA-Z0-9_]{1,16} joined the game/g
  73. const gameJoined = /\[[0-2][0-9]:[0-6][0-9]:[0-6][0-9]\] \[Client thread\/WARN\]: Skipping bad option: lastServer:/g
  74. const gameJoined2 = /\[[0-2][0-9]:[0-6][0-9]:[0-6][0-9]\] \[Client thread\/INFO\]: Created: \d+x\d+ textures-atlas/g
  75. let aEx
  76. let currentProc
  77. let serv
  78. let versionData
  79. let forgeData
  80. function dlAsync(login = true){
  81. // Login parameter is temporary for debug purposes. Allows testing the validation/downloads without
  82. // launching the game.
  83. if(login) {
  84. if(ConfigManager.getSelectedAccount() == null){
  85. console.error('login first.')
  86. //in devtools AuthManager.addAccount(username, pass)
  87. return
  88. }
  89. }
  90. launch_details_text.innerHTML = 'Please wait..'
  91. launch_progress.setAttribute('max', '100')
  92. launch_details.style.display = 'flex'
  93. launch_content.style.display = 'none'
  94. aEx = cp.fork(path.join(__dirname, 'assets', 'js', 'assetexec.js'), [
  95. ConfigManager.getGameDirectory(),
  96. ConfigManager.getJavaExecutable()
  97. ])
  98. aEx.on('message', (m) => {
  99. if(currentProc === 'validateDistribution'){
  100. launch_progress.setAttribute('value', 20)
  101. launch_progress_label.innerHTML = '20%'
  102. serv = m.result
  103. console.log('forge stuff done')
  104. // Begin version load.
  105. launch_details_text.innerHTML = 'Loading version information..'
  106. currentProc = 'loadVersionData'
  107. aEx.send({task: 0, content: currentProc, argsArr: [serv.mc_version]})
  108. } else if(currentProc === 'loadVersionData'){
  109. launch_progress.setAttribute('value', 40)
  110. launch_progress_label.innerHTML = '40%'
  111. versionData = m.result
  112. // Begin asset validation.
  113. launch_details_text.innerHTML = 'Validating asset integrity..'
  114. currentProc = 'validateAssets'
  115. aEx.send({task: 0, content: currentProc, argsArr: [versionData]})
  116. } else if(currentProc === 'validateAssets'){
  117. launch_progress.setAttribute('value', 60)
  118. launch_progress_label.innerHTML = '60%'
  119. console.log('assets done')
  120. // Begin library validation.
  121. launch_details_text.innerHTML = 'Validating library integrity..'
  122. currentProc = 'validateLibraries'
  123. aEx.send({task: 0, content: currentProc, argsArr: [versionData]})
  124. } else if(currentProc === 'validateLibraries'){
  125. launch_progress.setAttribute('value', 80)
  126. launch_progress_label.innerHTML = '80%'
  127. console.log('libs done')
  128. // Begin miscellaneous validation.
  129. launch_details_text.innerHTML = 'Validating miscellaneous file integrity..'
  130. currentProc = 'validateMiscellaneous'
  131. aEx.send({task: 0, content: currentProc, argsArr: [versionData]})
  132. } else if(currentProc === 'validateMiscellaneous'){
  133. launch_progress.setAttribute('value', 100)
  134. launch_progress_label.innerHTML = '100%'
  135. console.log('files done')
  136. launch_details_text.innerHTML = 'Downloading files..'
  137. currentProc = 'processDlQueues'
  138. aEx.send({task: 0, content: currentProc})
  139. } else if(currentProc === 'processDlQueues'){
  140. if(m.task === 0){
  141. remote.getCurrentWindow().setProgressBar(m.value/m.total)
  142. launch_progress.setAttribute('max', m.total)
  143. launch_progress.setAttribute('value', m.value)
  144. launch_progress_label.innerHTML = m.percent + '%'
  145. } else if(m.task === 1){
  146. remote.getCurrentWindow().setProgressBar(-1)
  147. launch_details_text.innerHTML = 'Preparing to launch..'
  148. currentProc = 'loadForgeData'
  149. aEx.send({task: 0, content: currentProc, argsArr: [serv.id]})
  150. } else {
  151. console.error('Unknown download data type.', m)
  152. }
  153. } else if(currentProc === 'loadForgeData'){
  154. forgeData = m.result
  155. if(login) {
  156. //if(!(await AuthManager.validateSelected())){
  157. //
  158. //}
  159. const authUser = ConfigManager.getSelectedAccount();
  160. console.log('authu', authUser)
  161. let pb = new ProcessBuilder(ConfigManager.getGameDirectory(), serv, versionData, forgeData, authUser)
  162. launch_details_text.innerHTML = 'Launching game..'
  163. try{
  164. proc = pb.build()
  165. launch_details_text.innerHTML = 'Done. Enjoy the server!'
  166. const tempListener = function(data){
  167. if(data.indexOf('[Client thread/INFO]: -- System Details --') > -1){
  168. launch_details.style.display = 'none'
  169. launch_content.style.display = 'inline-flex'
  170. if(hasRPC){
  171. DiscordWrapper.updateDetails('Loading game..')
  172. }
  173. proc.stdout.removeListener('data', tempListener)
  174. }
  175. }
  176. const gameStateChange = function(data){
  177. if(servJoined.test(data)){
  178. DiscordWrapper.updateDetails('Exploring the Realm!')
  179. } else if(gameJoined.test(data)){
  180. DiscordWrapper.updateDetails('Idling on Main Menu')
  181. }
  182. }
  183. proc.stdout.on('data', tempListener)
  184. proc.stdout.on('data', gameStateChange)
  185. // Init Discord Hook (Untested)
  186. const distro = AssetGuard.retrieveDistributionDataSync(ConfigManager.getGameDirectory)
  187. if(distro.discord != null && serv.discord != null){
  188. DiscordWrapper.initRPC(distro.discord, serv.discord)
  189. hasRPC = true
  190. proc.on('close', (code, signal) => {
  191. console.log('Shutting down Discord Rich Presence..')
  192. DiscordWrapper.shutdownRPC()
  193. hasRPC = false
  194. proc = null
  195. })
  196. }
  197. } catch(err) {
  198. //launch_details_text.innerHTML = 'Error: ' + err.message;
  199. launch_details_text.innerHTML = 'Error: See log for details..';
  200. console.log(err)
  201. setTimeout(function(){
  202. launch_details.style.display = 'none'
  203. launch_content.style.display = 'inline-flex'
  204. }, 5000)
  205. }
  206. }
  207. // Disconnect from AssetExec
  208. aEx.disconnect()
  209. }
  210. })
  211. launch_details_text.innerHTML = 'Loading server information..'
  212. currentProc = 'validateDistribution'
  213. aEx.send({task: 0, content: currentProc, argsArr: [ConfigManager.getSelectedServer()]})
  214. }