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(){
|
2018-04-02 20:05:48 +00:00
|
|
|
|
|
|
|
if (document.readyState === 'complete'){
|
|
|
|
if(ConfigManager.isFirstLaunch()){
|
|
|
|
$('#welcomeContainer').fadeIn(500)
|
|
|
|
} else {
|
|
|
|
$('#landingContainer').fadeIn(500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-02 22:40:32 +00:00
|
|
|
const jExe = ConfigManager.getJavaExecutable()
|
|
|
|
if(jExe == null){
|
|
|
|
asyncSystemScan()
|
|
|
|
} else {
|
|
|
|
AssetGuard._validateJavaBinary(jExe).then((v) => {
|
|
|
|
if(v){
|
|
|
|
dlAsync()
|
|
|
|
} else {
|
|
|
|
asyncSystemScan()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
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-04-02 22:40:32 +00:00
|
|
|
/* Launch Progress Wrapper Functions */
|
|
|
|
|
|
|
|
function toggleLaunchArea(loading){
|
|
|
|
if(loading){
|
|
|
|
launch_details.style.display = 'flex'
|
|
|
|
launch_content.style.display = 'none'
|
|
|
|
} else {
|
|
|
|
launch_details.style.display = 'none'
|
|
|
|
launch_content.style.display = 'inline-flex'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setLaunchDetails(details){
|
|
|
|
launch_details_text.innerHTML = details
|
|
|
|
}
|
|
|
|
|
|
|
|
function setLaunchPercentage(value, max, percent = ((value/max)*100)){
|
|
|
|
launch_progress.setAttribute('max', max)
|
|
|
|
launch_progress.setAttribute('value', value)
|
|
|
|
launch_progress_label.innerHTML = percent + '%'
|
|
|
|
}
|
|
|
|
|
|
|
|
function setDownloadPercentage(value, max, percent = ((value/max)*100)){
|
|
|
|
remote.getCurrentWindow().setProgressBar(value/max)
|
|
|
|
setLaunchPercentage(value, max, percent)
|
|
|
|
}
|
|
|
|
|
|
|
|
let sysAEx
|
|
|
|
let scanAt
|
|
|
|
|
|
|
|
function asyncSystemScan(){
|
|
|
|
|
|
|
|
setLaunchDetails('Please wait..')
|
|
|
|
toggleLaunchArea(true)
|
|
|
|
setLaunchPercentage(0, 100)
|
|
|
|
|
|
|
|
sysAEx = cp.fork(path.join(__dirname, 'assets', 'js', 'assetexec.js'), [
|
|
|
|
ConfigManager.getGameDirectory(),
|
|
|
|
ConfigManager.getJavaExecutable()
|
|
|
|
])
|
|
|
|
|
|
|
|
sysAEx.on('message', (m) => {
|
|
|
|
if(m.content === 'validateJava'){
|
|
|
|
jPath = m.result
|
|
|
|
console.log(m.result)
|
|
|
|
sysAEx.disconnect()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
setLaunchDetails('Checking system info..')
|
|
|
|
sysAEx.send({task: 0, content: 'validateJava', argsArr: [ConfigManager.getLauncherDirectory()]})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function overlayError(){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 22:40:32 +00:00
|
|
|
setLaunchDetails('Please wait..')
|
|
|
|
toggleLaunchArea(true)
|
|
|
|
setLaunchPercentage(0, 100)
|
2017-11-30 06:40:56 +00:00
|
|
|
|
2018-04-02 22:40:32 +00:00
|
|
|
// Start AssetExec to run validations and downloads in a forked process.
|
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-04-02 22:40:32 +00:00
|
|
|
// Establish communications between the AssetExec and current process.
|
2018-01-19 04:45:50 +00:00
|
|
|
aEx.on('message', (m) => {
|
2018-04-02 22:40:32 +00:00
|
|
|
if(m.content === 'validateDistribution'){
|
2017-12-03 13:12:55 +00:00
|
|
|
|
2018-04-02 22:40:32 +00:00
|
|
|
setLaunchPercentage(20, 100)
|
2018-01-19 04:45:50 +00:00
|
|
|
serv = m.result
|
2018-04-02 22:40:32 +00:00
|
|
|
console.log('Forge Validation Complete.')
|
2017-11-30 06:40:56 +00:00
|
|
|
|
2018-01-19 04:45:50 +00:00
|
|
|
// Begin version load.
|
2018-04-02 22:40:32 +00:00
|
|
|
setLaunchDetails('Loading version information..')
|
|
|
|
aEx.send({task: 0, content: 'loadVersionData', argsArr: [serv.mc_version]})
|
2018-01-19 04:45:50 +00:00
|
|
|
|
2018-04-02 22:40:32 +00:00
|
|
|
} else if(m.content === 'loadVersionData'){
|
2018-01-19 04:45:50 +00:00
|
|
|
|
2018-04-02 22:40:32 +00:00
|
|
|
setLaunchPercentage(40, 100)
|
2018-01-19 04:45:50 +00:00
|
|
|
versionData = m.result
|
2018-04-02 22:40:32 +00:00
|
|
|
console.log('Version data loaded.')
|
2018-01-19 04:45:50 +00:00
|
|
|
|
|
|
|
// Begin asset validation.
|
2018-04-02 22:40:32 +00:00
|
|
|
setLaunchDetails('Validating asset integrity..')
|
|
|
|
aEx.send({task: 0, content: 'validateAssets', argsArr: [versionData]})
|
2018-01-19 04:45:50 +00:00
|
|
|
|
2018-04-02 22:40:32 +00:00
|
|
|
} else if(m.content === 'validateAssets'){
|
2018-01-19 04:45:50 +00:00
|
|
|
|
2018-04-02 22:40:32 +00:00
|
|
|
setLaunchPercentage(60, 100)
|
|
|
|
console.log('Asset Validation Complete')
|
2018-01-19 04:45:50 +00:00
|
|
|
|
|
|
|
// Begin library validation.
|
2018-04-02 22:40:32 +00:00
|
|
|
setLaunchDetails('Validating library integrity..')
|
|
|
|
aEx.send({task: 0, content: 'validateLibraries', argsArr: [versionData]})
|
2018-01-19 04:45:50 +00:00
|
|
|
|
2018-04-02 22:40:32 +00:00
|
|
|
} else if(m.content === 'validateLibraries'){
|
2018-01-19 04:45:50 +00:00
|
|
|
|
2018-04-02 22:40:32 +00:00
|
|
|
setLaunchPercentage(80, 100)
|
|
|
|
console.log('Library validation complete.')
|
2018-01-19 04:45:50 +00:00
|
|
|
|
|
|
|
// Begin miscellaneous validation.
|
2018-04-02 22:40:32 +00:00
|
|
|
setLaunchDetails('Validating miscellaneous file integrity..')
|
|
|
|
aEx.send({task: 0, content: 'validateMiscellaneous', argsArr: [versionData]})
|
2018-01-19 04:45:50 +00:00
|
|
|
|
2018-04-02 22:40:32 +00:00
|
|
|
} else if(m.content === 'validateMiscellaneous'){
|
2018-01-19 04:45:50 +00:00
|
|
|
|
2018-04-02 22:40:32 +00:00
|
|
|
setLaunchPercentage(100, 100)
|
|
|
|
console.log('File validation complete.')
|
2018-01-19 04:45:50 +00:00
|
|
|
|
2018-04-02 22:40:32 +00:00
|
|
|
// Download queued files.
|
|
|
|
setLaunchDetails('Downloading files..')
|
|
|
|
aEx.send({task: 0, content: 'processDlQueues'})
|
|
|
|
|
|
|
|
} else if(m.content === 'dl'){
|
2018-01-19 04:45:50 +00:00
|
|
|
|
|
|
|
if(m.task === 0){
|
2018-04-02 22:40:32 +00:00
|
|
|
|
|
|
|
setDownloadPercentage(m.value, m.total, m.percent)
|
|
|
|
|
2018-01-19 04:45:50 +00:00
|
|
|
} else if(m.task === 1){
|
2018-04-02 22:40:32 +00:00
|
|
|
|
|
|
|
// Download will be at 100%, remove the loading from the OS progress bar.
|
2018-01-19 04:45:50 +00:00
|
|
|
remote.getCurrentWindow().setProgressBar(-1)
|
|
|
|
|
2018-04-02 22:40:32 +00:00
|
|
|
setLaunchDetails('Preparing to launch..')
|
|
|
|
aEx.send({task: 0, content: 'loadForgeData', argsArr: [serv.id]})
|
2018-01-19 04:45:50 +00:00
|
|
|
|
|
|
|
} else {
|
2018-04-02 22:40:32 +00:00
|
|
|
|
2018-01-19 04:45:50 +00:00
|
|
|
console.error('Unknown download data type.', m)
|
2018-04-02 22:40:32 +00:00
|
|
|
|
2017-11-30 06:40:56 +00:00
|
|
|
}
|
2018-04-02 22:40:32 +00:00
|
|
|
|
|
|
|
} else if(m.content === 'loadForgeData'){
|
2018-01-19 04:45:50 +00:00
|
|
|
|
|
|
|
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)
|
2018-04-02 22:40:32 +00:00
|
|
|
setLaunchDetails('Launching game..')
|
|
|
|
try {
|
|
|
|
// Build Minecraft process.
|
2018-01-19 04:45:50 +00:00
|
|
|
proc = pb.build()
|
2018-04-02 22:40:32 +00:00
|
|
|
setLaunchDetails('Done. Enjoy the server!')
|
|
|
|
|
|
|
|
// Attach a temporary listener to the client output.
|
|
|
|
// Will wait for a certain bit of text meaning that
|
|
|
|
// the client application has started, and we can hide
|
|
|
|
// the progress bar stuff.
|
2018-01-19 04:45:50 +00:00
|
|
|
const tempListener = function(data){
|
|
|
|
if(data.indexOf('[Client thread/INFO]: -- System Details --') > -1){
|
2018-04-02 22:40:32 +00:00
|
|
|
toggleLaunchArea(false)
|
2018-01-19 04:45:50 +00:00
|
|
|
if(hasRPC){
|
|
|
|
DiscordWrapper.updateDetails('Loading game..')
|
|
|
|
}
|
|
|
|
proc.stdout.removeListener('data', tempListener)
|
|
|
|
}
|
|
|
|
}
|
2018-04-02 22:40:32 +00:00
|
|
|
|
|
|
|
// Listener for Discord RPC.
|
2018-01-19 04:45:50 +00:00
|
|
|
const gameStateChange = function(data){
|
|
|
|
if(servJoined.test(data)){
|
|
|
|
DiscordWrapper.updateDetails('Exploring the Realm!')
|
|
|
|
} else if(gameJoined.test(data)){
|
|
|
|
DiscordWrapper.updateDetails('Idling on Main Menu')
|
|
|
|
}
|
|
|
|
}
|
2018-04-02 22:40:32 +00:00
|
|
|
|
|
|
|
// Bind listeners to stdout.
|
2018-01-19 04:45:50 +00:00
|
|
|
proc.stdout.on('data', tempListener)
|
|
|
|
proc.stdout.on('data', gameStateChange)
|
2018-04-02 22:40:32 +00:00
|
|
|
|
|
|
|
// Init Discord Hook
|
2018-01-19 04:45:50 +00:00
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
2018-04-02 22:40:32 +00:00
|
|
|
|
2018-01-19 04:45:50 +00:00
|
|
|
} catch(err) {
|
2018-04-02 22:40:32 +00:00
|
|
|
|
|
|
|
// Show that there was an error then hide the
|
|
|
|
// progress area. Maybe switch this to an error
|
|
|
|
// alert in the future. TODO
|
|
|
|
setLaunchDetails('Error: See log for details..')
|
2018-01-19 04:45:50 +00:00
|
|
|
console.log(err)
|
|
|
|
setTimeout(function(){
|
2018-04-02 22:40:32 +00:00
|
|
|
toggleLaunchArea(false)
|
2018-01-19 04:45:50 +00:00
|
|
|
}, 5000)
|
2018-04-02 22:40:32 +00:00
|
|
|
|
2018-01-19 04:45:50 +00:00
|
|
|
}
|
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
|
|
|
|
2018-04-02 22:40:32 +00:00
|
|
|
// Begin Validations
|
|
|
|
|
|
|
|
// Validate Forge files.
|
|
|
|
setLaunchDetails('Loading server information..')
|
|
|
|
aEx.send({task: 0, content: 'validateDistribution', argsArr: [ConfigManager.getSelectedServer()]})
|
2017-11-30 06:40:56 +00:00
|
|
|
}
|