Update launch process with Discord RPC enhancements

Refined the logic for initializing and shutting down Discord Rich Presence (RPC). Added more detailed logging and error handling to the game launch process, enhancing the user experience and debugging capabilities.
This commit is contained in:
Sandro642 2024-10-26 17:39:40 +02:00
parent a8a00e46ba
commit aab9ff5c3e

View File

@ -702,72 +702,83 @@ async function dlAsync(login = true) {
if(login) { if(login) {
const authUser = ConfigManager.getSelectedAccount() const authUser = ConfigManager.getSelectedAccount()
loggerLaunchSuite.info(Lang.queryJS('landing.dlAsync.accountToProcessBuilder', {'userDisplayName': authUser.displayName})) loggerLaunchSuite.info(`Sending selected account (${authUser.displayName}) to ProcessBuilder.`)
let pb = new ProcessBuilder(serv, versionData, modLoaderData, authUser, remote.app.getVersion()) let pb = new ProcessBuilder(serv, versionData, modLoaderData, authUser, remote.app.getVersion())
setLaunchDetails(Lang.queryJS('landing.dlAsync.launchingGame')) setLaunchDetails(Lang.queryJS('landing.dlAsync.launchingGame'))
// const SERVER_JOINED_REGEX = /\[.+\]: \[CHAT\] [a-zA-Z0-9_]{1,16} joined the game/
const SERVER_JOINED_REGEX = new RegExp(`\\[.+\\]: \\[CHAT\\] ${authUser.displayName} joined the game`) const SERVER_JOINED_REGEX = new RegExp(`\\[.+\\]: \\[CHAT\\] ${authUser.displayName} joined the game`)
const onLoadComplete = () => { const onLoadComplete = () => {
toggleLaunchArea(false) toggleLaunchArea(false)
if(hasRPC){
DiscordWrapper.updateDetails(Lang.queryJS('landing.discord.loading'))
proc.stdout.on('data', gameStateChange)
}
proc.stdout.removeListener('data', tempListener) proc.stdout.removeListener('data', tempListener)
proc.stderr.removeListener('data', gameErrorListener) proc.stderr.removeListener('data', gameErrorListener)
} }
const start = Date.now() const start = Date.now()
// Attach a temporary listener to the client output. // 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.
const tempListener = function(data){ const tempListener = function(data){
if(GAME_LAUNCH_REGEX.test(data.trim())){ if(GAME_LAUNCH_REGEX.test(data.trim())){
const diff = Date.now()-start const diff = Date.now() - start
if(diff < MIN_LINGER) { if(diff < MIN_LINGER) {
setTimeout(onLoadComplete, MIN_LINGER-diff) setTimeout(onLoadComplete, MIN_LINGER - diff)
} else { } else {
onLoadComplete() onLoadComplete()
} }
} }
} }
const gameErrorListener = function(data){ // Listener for Discord RPC.
if(data.trim().toLowerCase().includes('error')){ const gameStateChange = function(data) {
loggerLaunchSuite.error(Lang.queryJS('landing.dlAsync.gameError', {'data': data})) data = data.trim()
if(SERVER_JOINED_REGEX.test(data)) {
DiscordWrapper.updateDetails(Lang.queryJS('landing.discord.joined'))
} else if(GAME_JOINED_REGEX.test(data)) {
DiscordWrapper.updateDetails(Lang.queryJS('landing.discord.joining'))
} }
} }
proc = pb.build() const gameErrorListener = function(data) {
data = data.trim()
proc.stdout.on('data', tempListener) if(data.indexOf('Could not find or load main class net.minecraft.launchwrapper.Launch') > -1) {
proc.stderr.on('data', gameErrorListener) loggerLaunchSuite.error('Game launch failed, LaunchWrapper was not downloaded properly.')
showLaunchFailure(Lang.queryJS('landing.dlAsync.errorDuringLaunchTitle'), Lang.queryJS('landing.dlAsync.launchWrapperNotDownloaded'))
proc.stdout.on('data', function(data){
if(SERVER_JOINED_REGEX.test(data.trim())){
DiscordWrapper.updateDetails('Exploring the World')
} else if(GAME_JOINED_REGEX.test(data.trim())) {
DiscordWrapper.updateDetails('Main Menu')
} }
}) }
proc.on('close', (code, _signal) => { try {
if (hasRPC) { // Build Minecraft process.
DiscordWrapper.shutdownRPC() proc = pb.build()
hasRPC = false
// Bind listeners to stdout and stderr.
proc.stdout.on('data', tempListener)
proc.stderr.on('data', gameErrorListener)
setLaunchDetails(Lang.queryJS('landing.dlAsync.doneEnjoyServer'))
// Init Discord Hook
if(distro.rawDistribution.discord != null && serv.rawServer.discord != null) {
DiscordWrapper.initRPC(distro.rawDistribution.discord, serv.rawServer.discord)
hasRPC = true
proc.on('close', (code, signal) => {
loggerLaunchSuite.info('Shutting down Discord Rich Presence..')
DiscordWrapper.shutdownRPC()
hasRPC = false
proc = null
})
} }
loggerLaunchSuite.info(Lang.queryJS('landing.dlAsync.gameExited', {'code': code}))
if(code !== 0){
showLaunchFailure(Lang.queryJS('landing.dlAsync.gameExitedAbnormal'), Lang.queryJS('landing.dlAsync.seeConsoleForDetails'))
}
proc = null
})
proc.on('error', (err) => { } catch(err) {
loggerLaunchSuite.error(Lang.queryJS('landing.dlAsync.gameErrorDuringLaunch', {'error': err})) loggerLaunchSuite.error('Error during launch', err)
showLaunchFailure(Lang.queryJS('landing.dlAsync.errorDuringLaunchTitle'), err.message || Lang.queryJS('landing.dlAsync.errorDuringLaunchText')) showLaunchFailure(Lang.queryJS('landing.dlAsync.errorDuringLaunchTitle'), Lang.queryJS('landing.dlAsync.checkConsoleForDetails'))
proc = null }
})
setTimeout(() => {
loggerLaunchSuite.info(Lang.queryJS('landing.dlAsync.waintingLaunchingGame'))
}, MIN_LINGER)
} }
} }