2018-01-19 04:45:50 +00:00
|
|
|
const cp = require('child_process')
|
2017-11-30 06:40:56 +00:00
|
|
|
const path = require('path')
|
2017-11-30 08:00:06 +00:00
|
|
|
const {AssetGuard} = require(path.join(__dirname, 'assets', 'js', 'assetguard.js'))
|
2017-11-30 06:40:56 +00:00
|
|
|
const ProcessBuilder = require(path.join(__dirname, 'assets', 'js', 'processbuilder.js'))
|
2017-12-03 05:38:22 +00:00
|
|
|
const ConfigManager = require(path.join(__dirname, 'assets', 'js', 'configmanager.js'))
|
2017-12-03 07:13:42 +00:00
|
|
|
const DiscordWrapper = require(path.join(__dirname, 'assets', 'js', 'discordwrapper.js'))
|
2017-12-03 13:12:55 +00:00
|
|
|
const Mojang = require(path.join(__dirname, 'assets', 'js', 'mojang.js'))
|
|
|
|
const AuthManager = require(path.join(__dirname, 'assets', 'js', 'authmanager.js'))
|
2017-11-30 06:40:56 +00:00
|
|
|
|
2017-12-03 12:05:13 +00:00
|
|
|
let mojangStatusListener
|
|
|
|
|
2018-01-19 04:45:50 +00:00
|
|
|
// Launch Elements
|
|
|
|
let launch_content, launch_details, launch_progress, launch_progress_label, launch_details_text
|
|
|
|
|
2017-12-03 12:05:13 +00:00
|
|
|
// Synchronous Listener
|
2017-11-30 08:00:06 +00:00
|
|
|
document.addEventListener('readystatechange', function(){
|
2017-11-30 06:40:56 +00:00
|
|
|
if (document.readyState === 'interactive'){
|
|
|
|
|
2018-01-19 04:45:50 +00:00
|
|
|
// Save a reference to the launch elements.
|
|
|
|
launch_content = document.getElementById('launch_content')
|
|
|
|
launch_details = document.getElementById('launch_details')
|
|
|
|
launch_progress = document.getElementById('launch_progress')
|
|
|
|
launch_progress_label = document.getElementById('launch_progress_label')
|
|
|
|
launch_details_text = document.getElementById('launch_details_text')
|
|
|
|
|
2017-11-30 06:40:56 +00:00
|
|
|
// Bind launch button
|
2017-12-02 07:59:25 +00:00
|
|
|
document.getElementById('launch_button').addEventListener('click', function(e){
|
2017-11-30 06:40:56 +00:00
|
|
|
console.log('Launching game..')
|
2018-01-19 04:45:50 +00:00
|
|
|
//testdownloads()
|
|
|
|
dlAsync()
|
2017-11-30 06:40:56 +00:00
|
|
|
})
|
2017-12-02 07:59:25 +00:00
|
|
|
|
|
|
|
// TODO convert this to dropdown menu.
|
|
|
|
// Bind selected server
|
2017-12-03 05:38:22 +00:00
|
|
|
document.getElementById('server_selection').innerHTML = '\u2022 ' + AssetGuard.getServerById(ConfigManager.getGameDirectory(), ConfigManager.getSelectedServer()).name
|
2017-11-30 06:40:56 +00:00
|
|
|
|
2017-12-03 12:05:13 +00:00
|
|
|
|
|
|
|
// Update Mojang Status Color
|
|
|
|
const refreshMojangStatuses = async function(){
|
|
|
|
console.log('Refreshing Mojang Statuses..')
|
|
|
|
try {
|
|
|
|
let status = 'grey'
|
2017-12-03 13:12:55 +00:00
|
|
|
const statuses = await Mojang.status()
|
2017-12-03 12:05:13 +00:00
|
|
|
greenCount = 0
|
|
|
|
for(let i=0; i<statuses.length; i++){
|
|
|
|
if(statuses[i].status === 'yellow' && status !== 'red'){
|
|
|
|
status = 'yellow'
|
|
|
|
continue
|
|
|
|
} else if(statuses[i].status === 'red'){
|
|
|
|
status = 'red'
|
|
|
|
break
|
|
|
|
}
|
|
|
|
++greenCount
|
|
|
|
}
|
|
|
|
if(greenCount == statuses.length){
|
|
|
|
status = 'green'
|
|
|
|
}
|
|
|
|
|
2017-12-03 13:12:55 +00:00
|
|
|
document.getElementById('mojang_status_icon').style.color = Mojang.statusToHex(status)
|
2017-12-03 12:05:13 +00:00
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Unable to refresh Mojang service status..', err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshMojangStatuses()
|
|
|
|
// Set refresh rate to once every 5 minutes.
|
|
|
|
mojangStatusListener = setInterval(refreshMojangStatuses, 300000)
|
|
|
|
|
2017-11-30 06:40:56 +00:00
|
|
|
}
|
2017-11-30 08:00:06 +00:00
|
|
|
}, false)
|
|
|
|
|
2018-01-19 04:45:50 +00:00
|
|
|
// Keep reference to Minecraft Process
|
|
|
|
let proc
|
|
|
|
// Is DiscordRPC enabled
|
|
|
|
let hasRPC = false
|
|
|
|
// Joined server regex
|
|
|
|
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
|
|
|
|
const gameJoined = /\[[0-2][0-9]:[0-6][0-9]:[0-6][0-9]\] \[Client thread\/WARN\]: Skipping bad option: lastServer:/g
|
|
|
|
const gameJoined2 = /\[[0-2][0-9]:[0-6][0-9]:[0-6][0-9]\] \[Client thread\/INFO\]: Created: \d+x\d+ textures-atlas/g
|
|
|
|
|
|
|
|
let aEx
|
|
|
|
let currentProc
|
|
|
|
let serv
|
|
|
|
let versionData
|
|
|
|
let forgeData
|
|
|
|
|
|
|
|
function dlAsync(login = true){
|
|
|
|
|
|
|
|
// Login parameter is temporary for debug purposes. Allows testing the validation/downloads without
|
|
|
|
// launching the game.
|
|
|
|
|
|
|
|
if(login) {
|
|
|
|
if(ConfigManager.getSelectedAccount() == null){
|
|
|
|
console.error('login first.')
|
|
|
|
//in devtools AuthManager.addAccount(username, pass)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
launch_details_text.innerHTML = 'Please wait..'
|
|
|
|
launch_progress.setAttribute('max', '100')
|
|
|
|
launch_details.style.display = 'flex'
|
|
|
|
launch_content.style.display = 'none'
|
2017-11-30 06:40:56 +00:00
|
|
|
|
2018-01-19 04:45:50 +00:00
|
|
|
aEx = cp.fork(path.join(__dirname, 'assets', 'js', 'assetexec.js'), [
|
|
|
|
ConfigManager.getGameDirectory(),
|
|
|
|
ConfigManager.getJavaExecutable()
|
|
|
|
])
|
2017-12-03 13:12:55 +00:00
|
|
|
|
2018-01-19 04:45:50 +00:00
|
|
|
aEx.on('message', (m) => {
|
|
|
|
if(currentProc === 'validateDistribution'){
|
2017-12-03 13:12:55 +00:00
|
|
|
|
2018-01-19 04:45:50 +00:00
|
|
|
launch_progress.setAttribute('value', 20)
|
|
|
|
launch_progress_label.innerHTML = '20%'
|
|
|
|
serv = m.result
|
|
|
|
console.log('forge stuff done')
|
2017-11-30 06:40:56 +00:00
|
|
|
|
2018-01-19 04:45:50 +00:00
|
|
|
// Begin version load.
|
|
|
|
launch_details_text.innerHTML = 'Loading version information..'
|
|
|
|
currentProc = 'loadVersionData'
|
|
|
|
aEx.send({task: 0, content: currentProc, argsArr: [serv.mc_version]})
|
|
|
|
|
|
|
|
} else if(currentProc === 'loadVersionData'){
|
|
|
|
|
|
|
|
launch_progress.setAttribute('value', 40)
|
|
|
|
launch_progress_label.innerHTML = '40%'
|
|
|
|
versionData = m.result
|
|
|
|
|
|
|
|
// Begin asset validation.
|
|
|
|
launch_details_text.innerHTML = 'Validating asset integrity..'
|
|
|
|
currentProc = 'validateAssets'
|
|
|
|
aEx.send({task: 0, content: currentProc, argsArr: [versionData]})
|
|
|
|
|
|
|
|
} else if(currentProc === 'validateAssets'){
|
|
|
|
|
|
|
|
launch_progress.setAttribute('value', 60)
|
|
|
|
launch_progress_label.innerHTML = '60%'
|
|
|
|
console.log('assets done')
|
|
|
|
|
|
|
|
// Begin library validation.
|
|
|
|
launch_details_text.innerHTML = 'Validating library integrity..'
|
|
|
|
currentProc = 'validateLibraries'
|
|
|
|
aEx.send({task: 0, content: currentProc, argsArr: [versionData]})
|
|
|
|
|
|
|
|
} else if(currentProc === 'validateLibraries'){
|
|
|
|
|
|
|
|
launch_progress.setAttribute('value', 80)
|
|
|
|
launch_progress_label.innerHTML = '80%'
|
|
|
|
console.log('libs done')
|
|
|
|
|
|
|
|
// Begin miscellaneous validation.
|
|
|
|
launch_details_text.innerHTML = 'Validating miscellaneous file integrity..'
|
|
|
|
currentProc = 'validateMiscellaneous'
|
|
|
|
aEx.send({task: 0, content: currentProc, argsArr: [versionData]})
|
|
|
|
|
|
|
|
} else if(currentProc === 'validateMiscellaneous'){
|
|
|
|
|
|
|
|
launch_progress.setAttribute('value', 100)
|
|
|
|
launch_progress_label.innerHTML = '100%'
|
|
|
|
console.log('files done')
|
|
|
|
|
|
|
|
launch_details_text.innerHTML = 'Downloading files..'
|
|
|
|
currentProc = 'processDlQueues'
|
|
|
|
aEx.send({task: 0, content: currentProc})
|
|
|
|
|
|
|
|
} else if(currentProc === 'processDlQueues'){
|
|
|
|
if(m.task === 0){
|
|
|
|
remote.getCurrentWindow().setProgressBar(m.value/m.total)
|
|
|
|
launch_progress.setAttribute('max', m.total)
|
|
|
|
launch_progress.setAttribute('value', m.value)
|
|
|
|
launch_progress_label.innerHTML = m.percent + '%'
|
|
|
|
} else if(m.task === 1){
|
|
|
|
remote.getCurrentWindow().setProgressBar(-1)
|
|
|
|
|
|
|
|
launch_details_text.innerHTML = 'Preparing to launch..'
|
|
|
|
currentProc = 'loadForgeData'
|
|
|
|
aEx.send({task: 0, content: currentProc, argsArr: [serv.id]})
|
|
|
|
|
|
|
|
} else {
|
|
|
|
console.error('Unknown download data type.', m)
|
2017-11-30 06:40:56 +00:00
|
|
|
}
|
2018-01-19 04:45:50 +00:00
|
|
|
} else if(currentProc === 'loadForgeData'){
|
|
|
|
|
|
|
|
forgeData = m.result
|
|
|
|
|
|
|
|
if(login) {
|
|
|
|
//if(!(await AuthManager.validateSelected())){
|
|
|
|
//
|
|
|
|
//}
|
|
|
|
const authUser = ConfigManager.getSelectedAccount();
|
|
|
|
console.log('authu', authUser)
|
|
|
|
let pb = new ProcessBuilder(ConfigManager.getGameDirectory(), serv, versionData, forgeData, authUser)
|
|
|
|
launch_details_text.innerHTML = 'Launching game..'
|
|
|
|
try{
|
|
|
|
proc = pb.build()
|
|
|
|
launch_details_text.innerHTML = 'Done. Enjoy the server!'
|
|
|
|
const tempListener = function(data){
|
|
|
|
if(data.indexOf('[Client thread/INFO]: -- System Details --') > -1){
|
|
|
|
launch_details.style.display = 'none'
|
|
|
|
launch_content.style.display = 'inline-flex'
|
|
|
|
if(hasRPC){
|
|
|
|
DiscordWrapper.updateDetails('Loading game..')
|
|
|
|
}
|
|
|
|
proc.stdout.removeListener('data', tempListener)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const gameStateChange = function(data){
|
|
|
|
if(servJoined.test(data)){
|
|
|
|
DiscordWrapper.updateDetails('Exploring the Realm!')
|
|
|
|
} else if(gameJoined.test(data)){
|
|
|
|
DiscordWrapper.updateDetails('Idling on Main Menu')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
proc.stdout.on('data', tempListener)
|
|
|
|
proc.stdout.on('data', gameStateChange)
|
|
|
|
// Init Discord Hook (Untested)
|
|
|
|
const distro = AssetGuard.retrieveDistributionDataSync(ConfigManager.getGameDirectory)
|
|
|
|
if(distro.discord != null && serv.discord != null){
|
|
|
|
DiscordWrapper.initRPC(distro.discord, serv.discord)
|
|
|
|
hasRPC = true
|
|
|
|
proc.on('close', (code, signal) => {
|
|
|
|
console.log('Shutting down Discord Rich Presence..')
|
|
|
|
DiscordWrapper.shutdownRPC()
|
|
|
|
hasRPC = false
|
|
|
|
proc = null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
//launch_details_text.innerHTML = 'Error: ' + err.message;
|
|
|
|
launch_details_text.innerHTML = 'Error: See log for details..';
|
|
|
|
console.log(err)
|
|
|
|
setTimeout(function(){
|
|
|
|
launch_details.style.display = 'none'
|
|
|
|
launch_content.style.display = 'inline-flex'
|
|
|
|
}, 5000)
|
|
|
|
}
|
2017-12-03 07:13:42 +00:00
|
|
|
}
|
2018-01-19 04:45:50 +00:00
|
|
|
|
|
|
|
// Disconnect from AssetExec
|
|
|
|
aEx.disconnect()
|
|
|
|
|
2017-11-30 06:40:56 +00:00
|
|
|
}
|
|
|
|
})
|
2018-01-19 04:45:50 +00:00
|
|
|
|
|
|
|
launch_details_text.innerHTML = 'Loading server information..'
|
|
|
|
currentProc = 'validateDistribution'
|
|
|
|
aEx.send({task: 0, content: currentProc, argsArr: [ConfigManager.getSelectedServer()]})
|
2017-11-30 06:40:56 +00:00
|
|
|
}
|