actionbinder.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. const jExe = ConfigManager.getJavaExecutable()
  32. if(jExe == null){
  33. asyncSystemScan()
  34. } else {
  35. AssetGuard._validateJavaBinary(jExe).then((v) => {
  36. if(v){
  37. dlAsync()
  38. } else {
  39. asyncSystemScan()
  40. }
  41. })
  42. }
  43. })
  44. // TODO convert this to dropdown menu.
  45. // Bind selected server
  46. document.getElementById('server_selection').innerHTML = '\u2022 ' + AssetGuard.getServerById(ConfigManager.getGameDirectory(), ConfigManager.getSelectedServer()).name
  47. // Update Mojang Status Color
  48. const refreshMojangStatuses = async function(){
  49. console.log('Refreshing Mojang Statuses..')
  50. try {
  51. let status = 'grey'
  52. const statuses = await Mojang.status()
  53. greenCount = 0
  54. for(let i=0; i<statuses.length; i++){
  55. if(statuses[i].status === 'yellow' && status !== 'red'){
  56. status = 'yellow'
  57. continue
  58. } else if(statuses[i].status === 'red'){
  59. status = 'red'
  60. break
  61. }
  62. ++greenCount
  63. }
  64. if(greenCount == statuses.length){
  65. status = 'green'
  66. }
  67. document.getElementById('mojang_status_icon').style.color = Mojang.statusToHex(status)
  68. } catch (err) {
  69. console.error('Unable to refresh Mojang service status..', err)
  70. }
  71. }
  72. refreshMojangStatuses()
  73. // Set refresh rate to once every 5 minutes.
  74. mojangStatusListener = setInterval(refreshMojangStatuses, 300000)
  75. }
  76. }, false)
  77. /* Launch Progress Wrapper Functions */
  78. function toggleLaunchArea(loading){
  79. if(loading){
  80. launch_details.style.display = 'flex'
  81. launch_content.style.display = 'none'
  82. } else {
  83. launch_details.style.display = 'none'
  84. launch_content.style.display = 'inline-flex'
  85. }
  86. }
  87. function setLaunchDetails(details){
  88. launch_details_text.innerHTML = details
  89. }
  90. function setLaunchPercentage(value, max, percent = ((value/max)*100)){
  91. launch_progress.setAttribute('max', max)
  92. launch_progress.setAttribute('value', value)
  93. launch_progress_label.innerHTML = percent + '%'
  94. }
  95. function setDownloadPercentage(value, max, percent = ((value/max)*100)){
  96. remote.getCurrentWindow().setProgressBar(value/max)
  97. setLaunchPercentage(value, max, percent)
  98. }
  99. let sysAEx
  100. let scanAt
  101. function asyncSystemScan(){
  102. setLaunchDetails('Please wait..')
  103. toggleLaunchArea(true)
  104. setLaunchPercentage(0, 100)
  105. sysAEx = cp.fork(path.join(__dirname, 'assets', 'js', 'assetexec.js'), [
  106. ConfigManager.getGameDirectory(),
  107. ConfigManager.getJavaExecutable()
  108. ])
  109. sysAEx.on('message', (m) => {
  110. if(m.content === 'validateJava'){
  111. jPath = m.result
  112. console.log(m.result)
  113. sysAEx.disconnect()
  114. }
  115. })
  116. setLaunchDetails('Checking system info..')
  117. sysAEx.send({task: 0, content: 'validateJava', argsArr: [ConfigManager.getLauncherDirectory()]})
  118. }
  119. function overlayError(){
  120. }
  121. // Keep reference to Minecraft Process
  122. let proc
  123. // Is DiscordRPC enabled
  124. let hasRPC = false
  125. // Joined server regex
  126. 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
  127. const gameJoined = /\[[0-2][0-9]:[0-6][0-9]:[0-6][0-9]\] \[Client thread\/WARN\]: Skipping bad option: lastServer:/g
  128. const gameJoined2 = /\[[0-2][0-9]:[0-6][0-9]:[0-6][0-9]\] \[Client thread\/INFO\]: Created: \d+x\d+ textures-atlas/g
  129. let aEx
  130. let serv
  131. let versionData
  132. let forgeData
  133. function dlAsync(login = true){
  134. // Login parameter is temporary for debug purposes. Allows testing the validation/downloads without
  135. // launching the game.
  136. if(login) {
  137. if(ConfigManager.getSelectedAccount() == null){
  138. console.error('login first.')
  139. //in devtools AuthManager.addAccount(username, pass)
  140. return
  141. }
  142. }
  143. setLaunchDetails('Please wait..')
  144. toggleLaunchArea(true)
  145. setLaunchPercentage(0, 100)
  146. // Start AssetExec to run validations and downloads in a forked process.
  147. aEx = cp.fork(path.join(__dirname, 'assets', 'js', 'assetexec.js'), [
  148. ConfigManager.getGameDirectory(),
  149. ConfigManager.getJavaExecutable()
  150. ])
  151. // Establish communications between the AssetExec and current process.
  152. aEx.on('message', (m) => {
  153. if(m.content === 'validateDistribution'){
  154. setLaunchPercentage(20, 100)
  155. serv = m.result
  156. console.log('Forge Validation Complete.')
  157. // Begin version load.
  158. setLaunchDetails('Loading version information..')
  159. aEx.send({task: 0, content: 'loadVersionData', argsArr: [serv.mc_version]})
  160. } else if(m.content === 'loadVersionData'){
  161. setLaunchPercentage(40, 100)
  162. versionData = m.result
  163. console.log('Version data loaded.')
  164. // Begin asset validation.
  165. setLaunchDetails('Validating asset integrity..')
  166. aEx.send({task: 0, content: 'validateAssets', argsArr: [versionData]})
  167. } else if(m.content === 'validateAssets'){
  168. setLaunchPercentage(60, 100)
  169. console.log('Asset Validation Complete')
  170. // Begin library validation.
  171. setLaunchDetails('Validating library integrity..')
  172. aEx.send({task: 0, content: 'validateLibraries', argsArr: [versionData]})
  173. } else if(m.content === 'validateLibraries'){
  174. setLaunchPercentage(80, 100)
  175. console.log('Library validation complete.')
  176. // Begin miscellaneous validation.
  177. setLaunchDetails('Validating miscellaneous file integrity..')
  178. aEx.send({task: 0, content: 'validateMiscellaneous', argsArr: [versionData]})
  179. } else if(m.content === 'validateMiscellaneous'){
  180. setLaunchPercentage(100, 100)
  181. console.log('File validation complete.')
  182. // Download queued files.
  183. setLaunchDetails('Downloading files..')
  184. aEx.send({task: 0, content: 'processDlQueues'})
  185. } else if(m.content === 'dl'){
  186. if(m.task === 0){
  187. setDownloadPercentage(m.value, m.total, m.percent)
  188. } else if(m.task === 1){
  189. // Download will be at 100%, remove the loading from the OS progress bar.
  190. remote.getCurrentWindow().setProgressBar(-1)
  191. setLaunchDetails('Preparing to launch..')
  192. aEx.send({task: 0, content: 'loadForgeData', argsArr: [serv.id]})
  193. } else {
  194. console.error('Unknown download data type.', m)
  195. }
  196. } else if(m.content === 'loadForgeData'){
  197. forgeData = m.result
  198. if(login) {
  199. //if(!(await AuthManager.validateSelected())){
  200. //
  201. //}
  202. const authUser = ConfigManager.getSelectedAccount();
  203. console.log('authu', authUser)
  204. let pb = new ProcessBuilder(ConfigManager.getGameDirectory(), serv, versionData, forgeData, authUser)
  205. setLaunchDetails('Launching game..')
  206. try {
  207. // Build Minecraft process.
  208. proc = pb.build()
  209. setLaunchDetails('Done. Enjoy the server!')
  210. // Attach a temporary listener to the client output.
  211. // Will wait for a certain bit of text meaning that
  212. // the client application has started, and we can hide
  213. // the progress bar stuff.
  214. const tempListener = function(data){
  215. if(data.indexOf('[Client thread/INFO]: -- System Details --') > -1){
  216. toggleLaunchArea(false)
  217. if(hasRPC){
  218. DiscordWrapper.updateDetails('Loading game..')
  219. }
  220. proc.stdout.removeListener('data', tempListener)
  221. }
  222. }
  223. // Listener for Discord RPC.
  224. const gameStateChange = function(data){
  225. if(servJoined.test(data)){
  226. DiscordWrapper.updateDetails('Exploring the Realm!')
  227. } else if(gameJoined.test(data)){
  228. DiscordWrapper.updateDetails('Idling on Main Menu')
  229. }
  230. }
  231. // Bind listeners to stdout.
  232. proc.stdout.on('data', tempListener)
  233. proc.stdout.on('data', gameStateChange)
  234. // Init Discord Hook
  235. const distro = AssetGuard.retrieveDistributionDataSync(ConfigManager.getGameDirectory)
  236. if(distro.discord != null && serv.discord != null){
  237. DiscordWrapper.initRPC(distro.discord, serv.discord)
  238. hasRPC = true
  239. proc.on('close', (code, signal) => {
  240. console.log('Shutting down Discord Rich Presence..')
  241. DiscordWrapper.shutdownRPC()
  242. hasRPC = false
  243. proc = null
  244. })
  245. }
  246. } catch(err) {
  247. // Show that there was an error then hide the
  248. // progress area. Maybe switch this to an error
  249. // alert in the future. TODO
  250. setLaunchDetails('Error: See log for details..')
  251. console.log(err)
  252. setTimeout(function(){
  253. toggleLaunchArea(false)
  254. }, 5000)
  255. }
  256. }
  257. // Disconnect from AssetExec
  258. aEx.disconnect()
  259. }
  260. })
  261. // Begin Validations
  262. // Validate Forge files.
  263. setLaunchDetails('Loading server information..')
  264. aEx.send({task: 0, content: 'validateDistribution', argsArr: [ConfigManager.getSelectedServer()]})
  265. }