/** * Script for landing.ejs */ (() => { // Requirements require('./assets/js/vanillapatch') const path = require('path') const { URL } = require('url') const { MojangRestAPI } = require('helios-core/mojang') const { RestResponseStatus, isDisplayableError, validateLocalFile } = require('helios-core/common') const { FullRepair, DistributionIndexProcessor, MojangIndexProcessor, downloadFile } = require('helios-core/dl') const { validateSelectedJvm, ensureJavaDirIsRoot, javaExecFromRoot, discoverBestJvmInstallation, latestOpenJDK, extractJdk } = require('helios-core/java') // Internal Requirements const AuthManager = require('./assets/js/authmanager') const CatalogManager = require('./assets/js/catalogmanager') const DiscordWrapper = require('./assets/js/discordwrapper') const PortManager = require('./assets/js/portmanager') const ProcessBuilder = require('./assets/js/processbuilder') const ServerRuntime = require('./assets/js/serverruntime') // Launch Elements const launch_content = document.getElementById('launch_content') const launch_details = document.getElementById('launch_details') const launch_progress = document.getElementById('launch_progress') const launch_progress_label = document.getElementById('launch_progress_label') const launch_details_text = document.getElementById('launch_details_text') const server_selection_button = document.getElementById('server_selection_button') const accountPreviewName = document.getElementById('accountPreviewName') const avatarMenuButton = document.getElementById('avatarMenuButton') const avatarContainer = document.getElementById('avatarContainer') const accountMenu = document.getElementById('accountMenu') const accountMenuName = document.getElementById('accountMenuName') const accountMenuLogoutButton = document.getElementById('accountMenuLogoutButton') const portStatusTooltip = document.getElementById('portStatusTooltip') const loggerLanding = LoggerUtil.getLogger('Landing') function setAccountMenuOpen(open){ if(open && ConfigManager.getSelectedAccount() == null){ return } avatarMenuButton.setAttribute('aria-expanded', open ? 'true' : 'false') if(open){ accountMenu.removeAttribute('hidden') } else { accountMenu.setAttribute('hidden', '') } } function showAccountError(message){ if(typeof setOverlayContent === 'function'){ setOverlayContent( Lang.queryJS('settings.msftLogout.errorTitle'), message, Lang.queryJS('landing.launch.okay') ) setOverlayHandler(() => toggleOverlay(false)) toggleOverlay(true) } } async function logoutSelectedAccount(){ const selectedAccount = ConfigManager.getSelectedAccount() if(selectedAccount == null){ return } const accountCount = Object.keys(ConfigManager.getAuthAccounts()).length accountMenuLogoutButton.disabled = true try { if(selectedAccount.type === 'microsoft'){ await AuthManager.removeMicrosoftAccount(selectedAccount.uuid) } else { await AuthManager.removeMojangAccount(selectedAccount.uuid) } setAccountMenuOpen(false) const nextAccount = ConfigManager.getSelectedAccount() if(nextAccount != null){ updateSelectedAccount(nextAccount) validateSelectedAccount() } else if(accountCount === 1){ updateSelectedAccount(null) loginOptionsCancelEnabled(false) loginOptionsViewOnLoginSuccess = VIEWS.landing loginOptionsViewOnLoginCancel = VIEWS.loginOptions switchView(getCurrentView(), VIEWS.loginOptions) } } catch (error) { console.error(error) const message = selectedAccount.type === 'microsoft' ? Lang.queryJS('settings.msftLogout.errorMessage') : Lang.queryJS('landing.selectedAccount.logoutFailed') showAccountError(message) } finally { accountMenuLogoutButton.disabled = false } } /* Launch Progress Wrapper Functions */ /** * Show/hide the loading area. * * @param {boolean} loading True if the loading area should be shown, otherwise false. */ function toggleLaunchArea(loading){ if(typeof syncLaunchDetailWidths === 'function'){ syncLaunchDetailWidths() } if(loading){ launch_details.style.display = 'flex' launch_content.style.display = 'none' } else { launch_details.style.display = 'none' launch_content.style.display = 'inline-flex' } window.requestAnimationFrame(() => { if(typeof syncLaunchDetailWidths === 'function'){ syncLaunchDetailWidths() } }) } /** * Set the details text of the loading area. * * @param {string} details The new text for the loading details. */ function setLaunchDetails(details){ launch_details_text.innerHTML = details } /** * Set the value of the loading progress bar and display that value. * * @param {number} percent Percentage (0-100) */ function setLaunchPercentage(percent){ launch_progress.setAttribute('max', 100) launch_progress.setAttribute('value', percent) launch_progress_label.innerHTML = percent + '%' } /** * Set the value of the OS progress bar and display that on the UI. * * @param {number} percent Percentage (0-100) */ function setDownloadPercentage(percent){ remote.getCurrentWindow().setProgressBar(percent/100) setLaunchPercentage(percent) } /** * Enable or disable the launch button. * * @param {boolean} val True to enable, false to disable. */ function setLaunchEnabled(val){ document.getElementById('launch_button').disabled = !val } function refreshSelectedProfileButton(){ const selectedProfile = CatalogManager.getSelectedProfileSync() if(selectedProfile == null){ server_selection_button.innerHTML = '• ' + Lang.queryJS('landing.selectedProfile.noSelection') setLaunchEnabled(false) return } const label = selectedProfile.name?.trim().length > 0 ? selectedProfile.name.trim() : selectedProfile.id server_selection_button.innerHTML = '• ' + label setLaunchEnabled(selectedProfile.configured !== false) } function isSelectedMapReady(profile){ if(profile == null || profile.serverEnabled === true){ return false } const assetState = ConfigManager.getLibraryProfileAssetState(profile.id) return profile.launchReady && ( assetState.worldInstalledAt != null || assetState.prefetchedAt != null || profile.worldArchiveUrl == null ) } function updateLandingStatusDisplay(label, value, tone, tooltip, fade = false){ const applyValues = () => { document.getElementById('landingPlayerLabel').innerHTML = label document.getElementById('player_count').innerHTML = value document.getElementById('player_count').dataset.tone = tone portStatusTooltip.textContent = tooltip } if(fade){ $('#server_status_wrapper').fadeOut(250, () => { applyValues() $('#server_status_wrapper').fadeIn(500) }) } else { applyValues() } } // Bind launch button document.getElementById('launch_button').addEventListener('click', async e => { loggerLanding.info('Launching game..') try { const selectedProfile = CatalogManager.getSelectedProfileSync() if(selectedProfile == null){ if(typeof refreshLibraryView === 'function'){ await refreshLibraryView() } switchView(getCurrentView(), VIEWS.library) return } CatalogManager.applyConfiguredProfile() const distro = await DistroAPI.refreshDistributionOrFallback() if(distro == null){ throw new Error('Distribution refresh returned null.') } let server = distro.getServerById(ConfigManager.getSelectedServer()) if(server == null && typeof distro.getMainServer === 'function'){ const mainServer = distro.getMainServer() if(mainServer != null){ ConfigManager.setSelectedServer(mainServer.rawServer.id) ConfigManager.save() server = mainServer } } onDistroRefresh(distro) refreshSelectedProfileButton() if(server == null){ throw new Error('No server available for selected profile.') } const jExe = ConfigManager.getJavaExecutable(ConfigManager.getSelectedServer()) if(jExe == null){ await asyncSystemScan(server.effectiveJavaOptions) } else { setLaunchDetails(Lang.queryJS('landing.launch.pleaseWait')) toggleLaunchArea(true) setLaunchPercentage(0, 100) const details = await validateSelectedJvm(ensureJavaDirIsRoot(jExe), server.effectiveJavaOptions.supported) if(details != null){ loggerLanding.info('Jvm Details', details) await dlAsync() } else { await asyncSystemScan(server.effectiveJavaOptions) } } } catch(err) { loggerLanding.error('Unhandled error in during launch process.', err) showLaunchFailure(Lang.queryJS('landing.launch.failureTitle'), Lang.queryJS('landing.launch.failureText')) } }) // Bind settings button document.getElementById('settingsMediaButton').onclick = async e => { await prepareSettings() switchView(getCurrentView(), VIEWS.settings) } avatarMenuButton.addEventListener('click', (e) => { e.stopPropagation() setAccountMenuOpen(accountMenu.hasAttribute('hidden')) }) accountMenu.addEventListener('click', (e) => { e.stopPropagation() }) accountMenuLogoutButton.addEventListener('click', async () => { await logoutSelectedAccount() }) document.addEventListener('click', () => { if(!accountMenu.hasAttribute('hidden')){ setAccountMenuOpen(false) } }) // Bind selected account function updateSelectedAccount(authUser){ let username = Lang.queryJS('landing.selectedAccount.noAccountSelected') if(authUser != null){ if(authUser.displayName != null){ username = authUser.displayName } if(authUser.uuid != null){ avatarContainer.style.backgroundImage = `url('https://mc-heads.net/avatar/${authUser.uuid}/64'), url('assets/images/Icon.png')` } } else { avatarContainer.style.backgroundImage = `url('assets/images/Icon.png')` } accountPreviewName.textContent = username accountMenuName.textContent = username avatarMenuButton.disabled = authUser == null if(authUser == null){ setAccountMenuOpen(false) } } updateSelectedAccount(ConfigManager.getSelectedAccount()) // Bind selected server function updateSelectedServer(serv){ if(getCurrentView() === VIEWS.settings){ fullSettingsSave() } ConfigManager.setSelectedServer(serv != null ? serv.rawServer.id : null) ConfigManager.save() if(getCurrentView() === VIEWS.settings){ animateSettingsTabRefresh() } refreshSelectedProfileButton() } // Real text is set based on the selected library profile. server_selection_button.innerHTML = '• ' + Lang.queryJS('landing.selectedProfile.loading') server_selection_button.onclick = async e => { e.target.blur() if(typeof refreshLibraryView === 'function'){ await refreshLibraryView() } switchView(getCurrentView(), VIEWS.library) } // Update Mojang Status Color const refreshMojangStatuses = async function(){ loggerLanding.info('Refreshing Mojang Statuses..') let status = 'grey' let tooltipEssentialHTML = '' let tooltipNonEssentialHTML = '' const response = await MojangRestAPI.status() let statuses if(response.responseStatus === RestResponseStatus.SUCCESS) { statuses = response.data } else { loggerLanding.warn('Unable to refresh Mojang service status.') statuses = MojangRestAPI.getDefaultStatuses() } greenCount = 0 greyCount = 0 for(let i=0; i ${service.name} ` if(service.essential){ tooltipEssentialHTML += tooltipHTML } else { tooltipNonEssentialHTML += tooltipHTML } if(service.status === 'yellow' && status !== 'red'){ status = 'yellow' } else if(service.status === 'red'){ status = 'red' } else { if(service.status === 'grey'){ ++greyCount } ++greenCount } } if(greenCount === statuses.length){ if(greyCount === statuses.length){ status = 'grey' } else { status = 'green' } } document.getElementById('mojangStatusEssentialContainer').innerHTML = tooltipEssentialHTML document.getElementById('mojangStatusNonEssentialContainer').innerHTML = tooltipNonEssentialHTML document.getElementById('mojang_status_icon').style.color = MojangRestAPI.statusToHex(status) } const refreshServerStatus = async (fade = false) => { loggerLanding.info('Refreshing Server Status') const selectedProfile = CatalogManager.getSelectedProfileSync() let pLabel = Lang.queryJS('landing.profileStatus.label') let pVal = Lang.queryJS('landing.selectedProfile.noSelection') let pTone = 'info' let tooltip = '라이브러리에서 실행할 프로필을 먼저 선택하세요.' if(selectedProfile == null){ updateLandingStatusDisplay(pLabel, pVal, pTone, tooltip, fade) return } if(selectedProfile.serverEnabled !== true){ pLabel = Lang.queryJS('landing.mapStatus.label') pVal = isSelectedMapReady(selectedProfile) ? Lang.queryJS('landing.mapStatus.ready') : Lang.queryJS('landing.mapStatus.notReady') pTone = selectedProfile.launchReady ? 'success' : 'error' tooltip = selectedProfile.launchReady ? '이 프로필은 맵 실행 준비가 끝났습니다.' : (selectedProfile.launchIssues?.join(' / ') || '맵 실행 준비가 아직 끝나지 않았습니다.') updateLandingStatusDisplay(pLabel, pVal, pTone, tooltip, fade) return } try { pLabel = Lang.queryJS('landing.portStatus.label') const portState = await PortManager.ensurePortAvailability(selectedProfile) pVal = portState.summary pTone = portState.tone tooltip = selectedProfile.hostIssues?.length > 0 ? `${portState.message} / ${selectedProfile.hostIssues.join(' / ')}` : portState.message } catch (err) { loggerLanding.warn('Unable to refresh port status.') loggerLanding.debug(err) pLabel = Lang.queryJS('landing.portStatus.label') pVal = Lang.queryJS('landing.portStatus.failed') pTone = 'error' tooltip = err instanceof Error ? err.message : '자동 포트 개방 상태를 확인하지 못했습니다.' } updateLandingStatusDisplay(pLabel, pVal, pTone, tooltip, fade) } refreshMojangStatuses() // Server Status is refreshed in uibinder.js on distributionIndexDone. // Refresh statuses every hour. The status page itself refreshes every day so... let mojangStatusListener = setInterval(() => refreshMojangStatuses(true), 60*60*1000) // Set refresh rate to once every 5 minutes. let serverStatusListener = setInterval(() => refreshServerStatus(true), 300000) window.addEventListener('beforeunload', () => { PortManager.cleanupAll().catch(() => {}) }) /** * Shows an error overlay, toggles off the launch area. * * @param {string} title The overlay title. * @param {string} desc The overlay description. */ function showLaunchFailure(title, desc){ setOverlayContent( title, desc, Lang.queryJS('landing.launch.okay') ) setOverlayHandler(null) toggleOverlay(true) toggleLaunchArea(false) } /* System (Java) Scan */ /** * Asynchronously scan the system for valid Java installations. * * @param {boolean} launchAfter Whether we should begin to launch after scanning. */ async function asyncSystemScan(effectiveJavaOptions, launchAfter = true){ setLaunchDetails(Lang.queryJS('landing.systemScan.checking')) toggleLaunchArea(true) setLaunchPercentage(0, 100) const jvmDetails = await discoverBestJvmInstallation( ConfigManager.getDataDirectory(), effectiveJavaOptions.supported ) if(jvmDetails == null) { // If the result is null, no valid Java installation was found. // Show this information to the user. setOverlayContent( Lang.queryJS('landing.systemScan.noCompatibleJava'), Lang.queryJS('landing.systemScan.installJavaMessage', { 'major': effectiveJavaOptions.suggestedMajor }), Lang.queryJS('landing.systemScan.installJava'), Lang.queryJS('landing.systemScan.installJavaManually') ) setOverlayHandler(() => { setLaunchDetails(Lang.queryJS('landing.systemScan.javaDownloadPrepare')) toggleOverlay(false) try { downloadJava(effectiveJavaOptions, launchAfter) } catch(err) { loggerLanding.error('Unhandled error in Java Download', err) showLaunchFailure(Lang.queryJS('landing.systemScan.javaDownloadFailureTitle'), Lang.queryJS('landing.systemScan.javaDownloadFailureText')) } }) setDismissHandler(() => { $('#overlayContent').fadeOut(250, () => { //$('#overlayDismiss').toggle(false) setOverlayContent( Lang.queryJS('landing.systemScan.javaRequired', { 'major': effectiveJavaOptions.suggestedMajor }), Lang.queryJS('landing.systemScan.javaRequiredMessage', { 'major': effectiveJavaOptions.suggestedMajor }), Lang.queryJS('landing.systemScan.javaRequiredDismiss'), Lang.queryJS('landing.systemScan.javaRequiredCancel') ) setOverlayHandler(() => { toggleLaunchArea(false) toggleOverlay(false) }) setDismissHandler(() => { toggleOverlay(false, true) asyncSystemScan(effectiveJavaOptions, launchAfter) }) $('#overlayContent').fadeIn(250) }) }) toggleOverlay(true, true) } else { // Java installation found, use this to launch the game. const javaExec = javaExecFromRoot(jvmDetails.path) ConfigManager.setJavaExecutable(ConfigManager.getSelectedServer(), javaExec) ConfigManager.save() // We need to make sure that the updated value is on the settings UI. // Just incase the settings UI is already open. settingsJavaExecVal.value = javaExec await populateJavaExecDetails(settingsJavaExecVal.value) // TODO Callback hell, refactor // TODO Move this out, separate concerns. if(launchAfter){ await dlAsync() } } } async function downloadJava(effectiveJavaOptions, launchAfter = true) { // TODO Error handling. // asset can be null. const asset = await latestOpenJDK( effectiveJavaOptions.suggestedMajor, ConfigManager.getDataDirectory(), effectiveJavaOptions.distribution) if(asset == null) { throw new Error(Lang.queryJS('landing.downloadJava.findJdkFailure')) } let received = 0 await downloadFile(asset.url, asset.path, ({ transferred }) => { received = transferred setDownloadPercentage(Math.trunc((transferred/asset.size)*100)) }) setDownloadPercentage(100) if(received != asset.size) { loggerLanding.warn(`Java Download: Expected ${asset.size} bytes but received ${received}`) if(!await validateLocalFile(asset.path, asset.algo, asset.hash)) { log.error(`Hashes do not match, ${asset.id} may be corrupted.`) // Don't know how this could happen, but report it. throw new Error(Lang.queryJS('landing.downloadJava.javaDownloadCorruptedError')) } } // Extract // Show installing progress bar. remote.getCurrentWindow().setProgressBar(2) // Wait for extration to complete. const eLStr = Lang.queryJS('landing.downloadJava.extractingJava') let dotStr = '' setLaunchDetails(eLStr) const extractListener = setInterval(() => { if(dotStr.length >= 3){ dotStr = '' } else { dotStr += '.' } setLaunchDetails(eLStr + dotStr) }, 750) const newJavaExec = await extractJdk(asset.path) // Extraction complete, remove the loading from the OS progress bar. remote.getCurrentWindow().setProgressBar(-1) // Extraction completed successfully. ConfigManager.setJavaExecutable(ConfigManager.getSelectedServer(), newJavaExec) ConfigManager.save() clearInterval(extractListener) setLaunchDetails(Lang.queryJS('landing.downloadJava.javaInstalled')) // TODO Callback hell // Refactor the launch functions asyncSystemScan(effectiveJavaOptions, launchAfter) } // Keep reference to Minecraft Process let proc // Is DiscordRPC enabled let hasRPC = false // Joined server regex // Change this if your server uses something different. const GAME_JOINED_REGEX = /\[.+\]: Sound engine started/ const GAME_LAUNCH_REGEX = /^\[.+\]: (?:MinecraftForge .+ Initialized|ModLauncher .+ starting: .+|Loading Minecraft .+ with Fabric Loader .+)$/ const MIN_LINGER = 5000 async function dlAsync(login = true) { // Login parameter is temporary for debug purposes. Allows testing the validation/downloads without // launching the game. const loggerLaunchSuite = LoggerUtil.getLogger('LaunchSuite') setLaunchDetails(Lang.queryJS('landing.dlAsync.loadingServerInfo')) let distro try { distro = await DistroAPI.refreshDistributionOrFallback() onDistroRefresh(distro) } catch(err) { loggerLaunchSuite.error('Unable to refresh distribution index.', err) showLaunchFailure(Lang.queryJS('landing.dlAsync.fatalError'), Lang.queryJS('landing.dlAsync.unableToLoadDistributionIndex')) return } const serv = distro.getServerById(ConfigManager.getSelectedServer()) if(login) { if(ConfigManager.getSelectedAccount() == null){ loggerLanding.error('You must be logged into an account.') return } } setLaunchDetails(Lang.queryJS('landing.dlAsync.pleaseWait')) toggleLaunchArea(true) setLaunchPercentage(0, 100) const fullRepairModule = new FullRepair( ConfigManager.getCommonDirectory(), ConfigManager.getInstanceDirectory(), ConfigManager.getLauncherDirectory(), ConfigManager.getSelectedServer(), DistroAPI.isDevMode() ) const receiverPatchPath = path.join(__dirname, '..', 'vanillapatch.js') const existingNodeOptions = process.env.NODE_OPTIONS?.trim() const receiverNodeOptions = `--require=\"${receiverPatchPath}\"` fullRepairModule.spawnReceiver({ NODE_OPTIONS: existingNodeOptions != null && existingNodeOptions.length > 0 ? `${existingNodeOptions} ${receiverNodeOptions}` : receiverNodeOptions }) fullRepairModule.childProcess.on('error', (err) => { loggerLaunchSuite.error('Error during launch', err) showLaunchFailure(Lang.queryJS('landing.dlAsync.errorDuringLaunchTitle'), err.message || Lang.queryJS('landing.dlAsync.errorDuringLaunchText')) }) fullRepairModule.childProcess.on('close', (code, _signal) => { if(code !== 0){ loggerLaunchSuite.error(`Full Repair Module exited with code ${code}, assuming error.`) showLaunchFailure(Lang.queryJS('landing.dlAsync.errorDuringLaunchTitle'), Lang.queryJS('landing.dlAsync.seeConsoleForDetails')) } }) loggerLaunchSuite.info('Validating files.') setLaunchDetails(Lang.queryJS('landing.dlAsync.validatingFileIntegrity')) let invalidFileCount = 0 try { invalidFileCount = await fullRepairModule.verifyFiles(percent => { setLaunchPercentage(percent) }) setLaunchPercentage(100) } catch (err) { loggerLaunchSuite.error('Error during file validation.') showLaunchFailure(Lang.queryJS('landing.dlAsync.errorDuringFileVerificationTitle'), err.displayable || Lang.queryJS('landing.dlAsync.seeConsoleForDetails')) return } if(invalidFileCount > 0) { loggerLaunchSuite.info('Downloading files.') setLaunchDetails(Lang.queryJS('landing.dlAsync.downloadingFiles')) setLaunchPercentage(0) try { await fullRepairModule.download(percent => { setDownloadPercentage(percent) }) setDownloadPercentage(100) } catch(err) { loggerLaunchSuite.error('Error during file download.') showLaunchFailure(Lang.queryJS('landing.dlAsync.errorDuringFileDownloadTitle'), err.displayable || Lang.queryJS('landing.dlAsync.seeConsoleForDetails')) return } } else { loggerLaunchSuite.info('No invalid files, skipping download.') } // Remove download bar. remote.getCurrentWindow().setProgressBar(-1) fullRepairModule.destroyReceiver() setLaunchDetails(Lang.queryJS('landing.dlAsync.preparingToLaunch')) const mojangIndexProcessor = new MojangIndexProcessor( ConfigManager.getCommonDirectory(), serv.rawServer.minecraftVersion) const distributionIndexProcessor = new DistributionIndexProcessor( ConfigManager.getCommonDirectory(), distro, serv.rawServer.id ) const modLoaderData = await distributionIndexProcessor.loadModLoaderVersionJson() const versionData = await mojangIndexProcessor.getVersionJson() if(login) { const authUser = ConfigManager.getSelectedAccount() const selectedProfile = CatalogManager.getSelectedProfileSync() loggerLaunchSuite.info(`Sending selected account (${authUser.displayName}) to ProcessBuilder.`) let pb = new ProcessBuilder(serv, versionData, modLoaderData, authUser, remote.app.getVersion()) setLaunchDetails(Lang.queryJS('landing.dlAsync.launchingGame')) if(selectedProfile?.serverEnabled === true && CatalogManager.shouldHostLocally(selectedProfile)){ if(!selectedProfile.hostReady){ showLaunchFailure( Lang.queryJS('landing.localServer.missingJarTitle'), Lang.queryJS('landing.localServer.missingJarText') ) return } setLaunchDetails(Lang.queryJS('landing.localServer.starting')) try { await ServerRuntime.startHostedProfile(selectedProfile) refreshServerStatus(true) } catch (error) { loggerLaunchSuite.error('Failed to start local server.', error) showLaunchFailure( Lang.queryJS('landing.localServer.failureTitle'), error instanceof Error ? error.message : Lang.queryJS('landing.localServer.failureText') ) return } } // 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 onLoadComplete = () => { toggleLaunchArea(false) if(hasRPC){ DiscordWrapper.updateDetails(Lang.queryJS('landing.discord.loading')) proc.stdout.on('data', gameStateChange) } proc.stdout.removeListener('data', tempListener) proc.stderr.removeListener('data', gameErrorListener) } const start = Date.now() // 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){ if(GAME_LAUNCH_REGEX.test(data.trim())){ const diff = Date.now()-start if(diff < MIN_LINGER) { setTimeout(onLoadComplete, MIN_LINGER-diff) } else { onLoadComplete() } } } // Listener for Discord RPC. const gameStateChange = function(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')) } } const gameErrorListener = function(data){ data = data.trim() if(data.indexOf('Could not find or load main class net.minecraft.launchwrapper.Launch') > -1){ loggerLaunchSuite.error('Game launch failed, LaunchWrapper was not downloaded properly.') showLaunchFailure(Lang.queryJS('landing.dlAsync.errorDuringLaunchTitle'), Lang.queryJS('landing.dlAsync.launchWrapperNotDownloaded')) } } try { // Build Minecraft process. proc = pb.build() // Bind listeners to stdout. 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 }) } } catch(err) { loggerLaunchSuite.error('Error during launch', err) showLaunchFailure(Lang.queryJS('landing.dlAsync.errorDuringLaunchTitle'), Lang.queryJS('landing.dlAsync.checkConsoleForDetails')) } } } /** * News Loading Functions */ // DOM Cache const newsContent = document.getElementById('newsContent') const newsArticleTitle = document.getElementById('newsArticleTitle') const newsArticleDate = document.getElementById('newsArticleDate') const newsArticleAuthor = document.getElementById('newsArticleAuthor') const newsArticleComments = document.getElementById('newsArticleComments') const newsNavigationStatus = document.getElementById('newsNavigationStatus') const newsArticleContentScrollable = document.getElementById('newsArticleContentScrollable') const nELoadSpan = document.getElementById('nELoadSpan') // News slide caches. let newsActive = false let newsGlideCount = 0 /** * Show the news UI via a slide animation. * * @param {boolean} up True to slide up, otherwise false. */ function slide_(up){ const lCUpper = document.querySelector('#landingContainer > #upper') const lCLLeft = document.querySelector('#landingContainer > #lower > #left') const lCLCenter = document.querySelector('#landingContainer > #lower > #center') const lCLRight = document.querySelector('#landingContainer > #lower > #right') const newsBtn = document.querySelector('#landingContainer > #lower > #center #content') const landingContainer = document.getElementById('landingContainer') const newsContainer = document.querySelector('#landingContainer > #newsContainer') newsGlideCount++ if(up){ lCUpper.style.top = '-200vh' lCLLeft.style.top = '-200vh' lCLCenter.style.top = '-200vh' lCLRight.style.top = '-200vh' newsBtn.style.top = '130vh' newsContainer.style.top = '0px' //date.toLocaleDateString('en-US', {month: 'short', day: 'numeric', year: 'numeric', hour: 'numeric', minute: 'numeric'}) //landingContainer.style.background = 'rgba(29, 29, 29, 0.55)' landingContainer.style.background = 'rgba(0, 0, 0, 0.50)' setTimeout(() => { if(newsGlideCount === 1){ lCLCenter.style.transition = 'none' newsBtn.style.transition = 'none' } newsGlideCount-- }, 2000) } else { setTimeout(() => { newsGlideCount-- }, 2000) landingContainer.style.background = null lCLCenter.style.transition = null newsBtn.style.transition = null newsContainer.style.top = '100%' lCUpper.style.top = '0px' lCLLeft.style.top = '0px' lCLCenter.style.top = '0px' lCLRight.style.top = '0px' newsBtn.style.top = '10px' } } // Bind install button on the landing footer. document.getElementById('newsButton').onclick = () => { if(typeof refreshInstallView === 'function'){ refreshInstallView() } switchView(getCurrentView(), VIEWS.install) } // Array to store article meta. let newsArr = null // News load animation listener. let newsLoadingListener = null /** * Set the news loading animation. * * @param {boolean} val True to set loading animation, otherwise false. */ function setNewsLoading(val){ if(val){ const nLStr = Lang.queryJS('landing.news.checking') let dotStr = '..' nELoadSpan.innerHTML = nLStr + dotStr newsLoadingListener = setInterval(() => { if(dotStr.length >= 3){ dotStr = '' } else { dotStr += '.' } nELoadSpan.innerHTML = nLStr + dotStr }, 750) } else { if(newsLoadingListener != null){ clearInterval(newsLoadingListener) newsLoadingListener = null } } } // Bind retry button. newsErrorRetry.onclick = () => { $('#newsErrorFailed').fadeOut(250, () => { initNews() $('#newsErrorLoading').fadeIn(250) }) } newsArticleContentScrollable.onscroll = (e) => { if(e.target.scrollTop > Number.parseFloat($('.newsArticleSpacerTop').css('height'))){ newsContent.setAttribute('scrolled', '') } else { newsContent.removeAttribute('scrolled') } } /** * Reload the news without restarting. * * @returns {Promise.} A promise which resolves when the news * content has finished loading and transitioning. */ function reloadNews(){ return new Promise((resolve, reject) => { $('#newsContent').fadeOut(250, () => { $('#newsErrorLoading').fadeIn(250) initNews().then(() => { resolve() }) }) }) } let newsAlertShown = false /** * Show the news alert indicating there is new news. */ function showNewsAlert(){ newsAlertShown = true $(newsButtonAlert).fadeIn(250) } async function digestMessage(str) { const msgUint8 = new TextEncoder().encode(str) const hashBuffer = await crypto.subtle.digest('SHA-1', msgUint8) const hashArray = Array.from(new Uint8Array(hashBuffer)) const hashHex = hashArray .map((b) => b.toString(16).padStart(2, '0')) .join('') return hashHex } /** * Initialize News UI. This will load the news and prepare * the UI accordingly. * * @returns {Promise.} A promise which resolves when the news * content has finished loading and transitioning. */ async function initNews(){ setNewsLoading(true) const news = await loadNews() newsArr = news?.articles || null if(newsArr == null){ // News Loading Failed setNewsLoading(false) await $('#newsErrorLoading').fadeOut(250).promise() await $('#newsErrorFailed').fadeIn(250).promise() } else if(newsArr.length === 0) { // No News Articles setNewsLoading(false) ConfigManager.setNewsCache({ date: null, content: null, dismissed: false }) ConfigManager.save() await $('#newsErrorLoading').fadeOut(250).promise() await $('#newsErrorNone').fadeIn(250).promise() } else { // Success setNewsLoading(false) const lN = newsArr[0] const cached = ConfigManager.getNewsCache() let newHash = await digestMessage(lN.content) let newDate = new Date(lN.date) let isNew = false if(cached.date != null && cached.content != null){ if(new Date(cached.date) >= newDate){ // Compare Content if(cached.content !== newHash){ isNew = true showNewsAlert() } else { if(!cached.dismissed){ isNew = true showNewsAlert() } } } else { isNew = true showNewsAlert() } } else { isNew = true showNewsAlert() } if(isNew){ ConfigManager.setNewsCache({ date: newDate.getTime(), content: newHash, dismissed: false }) ConfigManager.save() } const switchHandler = (forward) => { let cArt = parseInt(newsContent.getAttribute('article')) let nxtArt = forward ? (cArt >= newsArr.length-1 ? 0 : cArt + 1) : (cArt <= 0 ? newsArr.length-1 : cArt - 1) displayArticle(newsArr[nxtArt], nxtArt+1) } document.getElementById('newsNavigateRight').onclick = () => { switchHandler(true) } document.getElementById('newsNavigateLeft').onclick = () => { switchHandler(false) } await $('#newsErrorContainer').fadeOut(250).promise() displayArticle(newsArr[0], 1) await $('#newsContent').fadeIn(250).promise() } } /** * Add keyboard controls to the news UI. Left and right arrows toggle * between articles. If you are on the landing page, the up arrow will * open the news UI. */ document.addEventListener('keydown', (e) => { if(newsActive){ if(e.key === 'ArrowRight' || e.key === 'ArrowLeft'){ document.getElementById(e.key === 'ArrowRight' ? 'newsNavigateRight' : 'newsNavigateLeft').click() } // Interferes with scrolling an article using the down arrow. // Not sure of a straight forward solution at this point. // if(e.key === 'ArrowDown'){ // document.getElementById('newsButton').click() // } } }) /** * Display a news article on the UI. * * @param {Object} articleObject The article meta object. * @param {number} index The article index. */ function displayArticle(articleObject, index){ newsArticleTitle.innerHTML = articleObject.title newsArticleTitle.href = articleObject.link newsArticleAuthor.innerHTML = 'by ' + articleObject.author newsArticleDate.innerHTML = articleObject.date newsArticleComments.innerHTML = articleObject.comments newsArticleComments.href = articleObject.commentsLink newsArticleContentScrollable.innerHTML = '
' + articleObject.content + '
' Array.from(newsArticleContentScrollable.getElementsByClassName('bbCodeSpoilerButton')).forEach(v => { v.onclick = () => { const text = v.parentElement.getElementsByClassName('bbCodeSpoilerText')[0] text.style.display = text.style.display === 'block' ? 'none' : 'block' } }) newsNavigationStatus.innerHTML = Lang.query('ejs.landing.newsNavigationStatus', {currentPage: index, totalPages: newsArr.length}) newsContent.setAttribute('article', index-1) } /** * Load news information from the RSS feed specified in the * distribution index. */ async function loadNews(){ const distroData = await DistroAPI.getDistribution() if(!distroData.rawDistribution.rss) { loggerLanding.debug('No RSS feed provided.') return null } const promise = new Promise((resolve, reject) => { const newsFeed = distroData.rawDistribution.rss const newsHost = new URL(newsFeed).origin + '/' $.ajax({ url: newsFeed, success: (data) => { const items = $(data).find('item') const articles = [] for(let i=0; i { resolve({ articles: null }) }) }) return await promise } window.updateSelectedAccount = updateSelectedAccount window.updateSelectedServer = updateSelectedServer window.refreshServerStatus = refreshServerStatus window.initNews = initNews window.refreshSelectedProfileButton = refreshSelectedProfileButton })()