2017-05-16 23:26:46 -07:00
|
|
|
// Requirements
|
2018-06-04 20:08:03 -07:00
|
|
|
const async = require('async')
|
2017-05-17 22:49:28 -07:00
|
|
|
const child_process = require('child_process')
|
2018-06-04 20:08:03 -07:00
|
|
|
const crypto = require('crypto')
|
|
|
|
const EventEmitter = require('events')
|
2018-12-01 05:20:42 -08:00
|
|
|
const fs = require('fs-extra')
|
2022-12-24 19:17:30 -08:00
|
|
|
const { LoggerUtil } = require('helios-core')
|
2022-11-27 15:13:50 -08:00
|
|
|
const nodeDiskInfo = require('node-disk-info')
|
2021-10-14 20:17:40 -07:00
|
|
|
const StreamZip = require('node-stream-zip')
|
2018-06-04 20:08:03 -07:00
|
|
|
const path = require('path')
|
|
|
|
const Registry = require('winreg')
|
|
|
|
const request = require('request')
|
|
|
|
const tar = require('tar-fs')
|
|
|
|
const zlib = require('zlib')
|
2017-05-16 23:26:46 -07:00
|
|
|
|
2018-09-22 23:19:16 -07:00
|
|
|
const isDev = require('./isdev')
|
2018-07-22 08:40:15 -07:00
|
|
|
|
2017-05-16 23:26:46 -07:00
|
|
|
// Classes
|
|
|
|
|
|
|
|
/** Class representing a base asset. */
|
2017-11-29 22:40:56 -08:00
|
|
|
class Asset {
|
2017-05-16 23:26:46 -07:00
|
|
|
/**
|
|
|
|
* Create an asset.
|
|
|
|
*
|
2018-03-28 13:42:10 -07:00
|
|
|
* @param {any} id The id of the asset.
|
|
|
|
* @param {string} hash The hash value of the asset.
|
|
|
|
* @param {number} size The size in bytes of the asset.
|
|
|
|
* @param {string} from The url where the asset can be found.
|
|
|
|
* @param {string} to The absolute local file path of the asset.
|
2017-05-16 23:26:46 -07:00
|
|
|
*/
|
|
|
|
constructor(id, hash, size, from, to){
|
|
|
|
this.id = id
|
|
|
|
this.hash = hash
|
|
|
|
this.size = size
|
|
|
|
this.from = from
|
|
|
|
this.to = to
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class representing a download tracker. This is used to store meta data
|
|
|
|
* about a download queue, including the queue itself.
|
|
|
|
*/
|
|
|
|
class DLTracker {
|
2017-11-30 00:00:06 -08:00
|
|
|
|
2017-05-16 23:26:46 -07:00
|
|
|
/**
|
|
|
|
* Create a DLTracker
|
|
|
|
*
|
2018-03-28 13:42:10 -07:00
|
|
|
* @param {Array.<Asset>} dlqueue An array containing assets queued for download.
|
|
|
|
* @param {number} dlsize The combined size of each asset in the download queue array.
|
|
|
|
* @param {function(Asset)} callback Optional callback which is called when an asset finishes downloading.
|
2017-05-16 23:26:46 -07:00
|
|
|
*/
|
2017-05-21 01:56:39 -07:00
|
|
|
constructor(dlqueue, dlsize, callback = null){
|
2017-05-16 23:26:46 -07:00
|
|
|
this.dlqueue = dlqueue
|
|
|
|
this.dlsize = dlsize
|
2017-05-21 01:56:39 -07:00
|
|
|
this.callback = callback
|
2017-05-16 23:26:46 -07:00
|
|
|
}
|
2017-11-30 00:00:06 -08:00
|
|
|
|
2017-05-16 23:26:46 -07:00
|
|
|
}
|
|
|
|
|
2019-03-05 20:05:01 -08:00
|
|
|
class Util {
|
2018-03-30 05:31:54 -07:00
|
|
|
|
|
|
|
/**
|
2019-03-05 20:05:01 -08:00
|
|
|
* Returns true if the actual version is greater than
|
|
|
|
* or equal to the desired version.
|
2018-03-30 05:31:54 -07:00
|
|
|
*
|
2019-03-05 20:05:01 -08:00
|
|
|
* @param {string} desired The desired version.
|
|
|
|
* @param {string} actual The actual version.
|
2018-03-30 05:31:54 -07:00
|
|
|
*/
|
2019-03-05 20:05:01 -08:00
|
|
|
static mcVersionAtLeast(desired, actual){
|
|
|
|
const des = desired.split('.')
|
|
|
|
const act = actual.split('.')
|
2018-03-30 05:31:54 -07:00
|
|
|
|
2019-03-05 20:05:01 -08:00
|
|
|
for(let i=0; i<des.length; i++){
|
|
|
|
if(!(parseInt(act[i]) >= parseInt(des[i]))){
|
2018-03-30 05:31:54 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-06-12 16:56:57 -07:00
|
|
|
static isAutoconnectBroken(forgeVersion) {
|
|
|
|
|
|
|
|
const minWorking = [31, 2, 15]
|
2020-06-29 07:31:05 -07:00
|
|
|
const verSplit = forgeVersion.split('.').map(v => Number(v))
|
2020-06-12 16:56:57 -07:00
|
|
|
|
|
|
|
if(verSplit[0] === 31) {
|
|
|
|
for(let i=0; i<minWorking.length; i++) {
|
|
|
|
if(verSplit[i] > minWorking[i]) {
|
|
|
|
return false
|
|
|
|
} else if(verSplit[i] < minWorking[i]) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:05:01 -08:00
|
|
|
}
|
2017-05-16 23:26:46 -07:00
|
|
|
|
2018-05-07 15:15:59 -07:00
|
|
|
|
2019-03-05 20:05:01 -08:00
|
|
|
class JavaGuard extends EventEmitter {
|
2018-05-07 15:15:59 -07:00
|
|
|
|
2019-03-05 20:05:01 -08:00
|
|
|
constructor(mcVersion){
|
|
|
|
super()
|
|
|
|
this.mcVersion = mcVersion
|
2022-12-24 19:17:30 -08:00
|
|
|
this.logger = LoggerUtil.getLogger('JavaGuard')
|
2017-11-30 00:00:06 -08:00
|
|
|
}
|
2017-05-21 15:06:48 -07:00
|
|
|
|
2018-03-31 10:05:05 -07:00
|
|
|
/**
|
2019-06-02 15:11:39 -07:00
|
|
|
* @typedef OpenJDKData
|
2018-03-31 10:05:05 -07:00
|
|
|
* @property {string} uri The base uri of the JRE.
|
2019-06-02 15:11:39 -07:00
|
|
|
* @property {number} size The size of the download.
|
|
|
|
* @property {string} name The name of the artifact.
|
2018-03-31 10:05:05 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2020-09-08 18:25:19 -07:00
|
|
|
* Fetch the last open JDK binary.
|
|
|
|
*
|
|
|
|
* HOTFIX: Uses Corretto 8 for macOS.
|
|
|
|
* See: https://github.com/dscalzi/HeliosLauncher/issues/70
|
|
|
|
* See: https://github.com/AdoptOpenJDK/openjdk-support/issues/101
|
2018-03-31 10:05:05 -07:00
|
|
|
*
|
2019-06-02 15:11:39 -07:00
|
|
|
* @param {string} major The major version of Java to fetch.
|
|
|
|
*
|
|
|
|
* @returns {Promise.<OpenJDKData>} Promise which resolved to an object containing the JRE download data.
|
2018-03-31 10:05:05 -07:00
|
|
|
*/
|
2019-06-02 15:11:39 -07:00
|
|
|
static _latestOpenJDK(major = '8'){
|
2018-03-31 10:05:05 -07:00
|
|
|
|
2020-09-08 18:25:19 -07:00
|
|
|
if(process.platform === 'darwin') {
|
|
|
|
return this._latestCorretto(major)
|
|
|
|
} else {
|
2021-10-14 20:17:40 -07:00
|
|
|
return this._latestAdoptium(major)
|
2020-09-08 18:25:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-14 20:17:40 -07:00
|
|
|
static _latestAdoptium(major) {
|
2020-09-08 18:25:19 -07:00
|
|
|
|
2021-10-14 20:17:40 -07:00
|
|
|
const majorNum = Number(major)
|
2019-06-02 15:11:39 -07:00
|
|
|
const sanitizedOS = process.platform === 'win32' ? 'windows' : (process.platform === 'darwin' ? 'mac' : process.platform)
|
2021-10-14 20:17:40 -07:00
|
|
|
const url = `https://api.adoptium.net/v3/assets/latest/${major}/hotspot?vendor=eclipse`
|
2019-06-02 15:11:39 -07:00
|
|
|
|
2018-03-31 10:05:05 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-06-02 15:11:39 -07:00
|
|
|
request({url, json: true}, (err, resp, body) => {
|
|
|
|
if(!err && body.length > 0){
|
2021-10-14 20:17:40 -07:00
|
|
|
|
|
|
|
const targetBinary = body.find(entry => {
|
|
|
|
return entry.version.major === majorNum
|
|
|
|
&& entry.binary.os === sanitizedOS
|
|
|
|
&& entry.binary.image_type === 'jdk'
|
|
|
|
&& entry.binary.architecture === 'x64'
|
2018-03-31 10:05:05 -07:00
|
|
|
})
|
2021-10-14 20:17:40 -07:00
|
|
|
|
|
|
|
if(targetBinary != null) {
|
|
|
|
resolve({
|
|
|
|
uri: targetBinary.binary.package.link,
|
|
|
|
size: targetBinary.binary.package.size,
|
|
|
|
name: targetBinary.binary.package.name
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
resolve(null)
|
|
|
|
}
|
2018-03-31 10:05:05 -07:00
|
|
|
} else {
|
|
|
|
resolve(null)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2020-09-08 18:25:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static _latestCorretto(major) {
|
|
|
|
|
|
|
|
let sanitizedOS, ext
|
|
|
|
|
|
|
|
switch(process.platform) {
|
|
|
|
case 'win32':
|
|
|
|
sanitizedOS = 'windows'
|
|
|
|
ext = 'zip'
|
|
|
|
break
|
|
|
|
case 'darwin':
|
|
|
|
sanitizedOS = 'macos'
|
|
|
|
ext = 'tar.gz'
|
|
|
|
break
|
|
|
|
case 'linux':
|
|
|
|
sanitizedOS = 'linux'
|
|
|
|
ext = 'tar.gz'
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
sanitizedOS = process.platform
|
|
|
|
ext = 'tar.gz'
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = `https://corretto.aws/downloads/latest/amazon-corretto-${major}-x64-${sanitizedOS}-jdk.${ext}`
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request.head({url, json: true}, (err, resp) => {
|
|
|
|
if(!err && resp.statusCode === 200){
|
|
|
|
resolve({
|
|
|
|
uri: url,
|
|
|
|
size: parseInt(resp.headers['content-length']),
|
|
|
|
name: url.substr(url.lastIndexOf('/')+1)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
resolve(null)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-03-31 10:05:05 -07:00
|
|
|
}
|
|
|
|
|
2018-04-07 15:06:49 -07:00
|
|
|
/**
|
|
|
|
* Returns the path of the OS-specific executable for the given Java
|
|
|
|
* installation. Supported OS's are win32, darwin, linux.
|
|
|
|
*
|
|
|
|
* @param {string} rootDir The root directory of the Java installation.
|
2018-04-12 13:12:45 -07:00
|
|
|
* @returns {string} The path to the Java executable.
|
2018-04-07 15:06:49 -07:00
|
|
|
*/
|
|
|
|
static javaExecFromRoot(rootDir){
|
|
|
|
if(process.platform === 'win32'){
|
|
|
|
return path.join(rootDir, 'bin', 'javaw.exe')
|
|
|
|
} else if(process.platform === 'darwin'){
|
2018-04-12 19:13:26 -07:00
|
|
|
return path.join(rootDir, 'Contents', 'Home', 'bin', 'java')
|
2018-04-07 15:06:49 -07:00
|
|
|
} else if(process.platform === 'linux'){
|
|
|
|
return path.join(rootDir, 'bin', 'java')
|
|
|
|
}
|
|
|
|
return rootDir
|
|
|
|
}
|
|
|
|
|
2018-04-12 13:12:45 -07:00
|
|
|
/**
|
|
|
|
* Check to see if the given path points to a Java executable.
|
|
|
|
*
|
|
|
|
* @param {string} pth The path to check against.
|
|
|
|
* @returns {boolean} True if the path points to a Java executable, otherwise false.
|
|
|
|
*/
|
|
|
|
static isJavaExecPath(pth){
|
2022-11-27 15:13:50 -08:00
|
|
|
if(pth == null) {
|
|
|
|
return false
|
|
|
|
}
|
2018-04-12 13:12:45 -07:00
|
|
|
if(process.platform === 'win32'){
|
|
|
|
return pth.endsWith(path.join('bin', 'javaw.exe'))
|
|
|
|
} else if(process.platform === 'darwin'){
|
|
|
|
return pth.endsWith(path.join('bin', 'java'))
|
|
|
|
} else if(process.platform === 'linux'){
|
|
|
|
return pth.endsWith(path.join('bin', 'java'))
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-03-31 17:45:24 -07:00
|
|
|
/**
|
|
|
|
* Load Mojang's launcher.json file.
|
|
|
|
*
|
|
|
|
* @returns {Promise.<Object>} Promise which resolves to Mojang's launcher.json object.
|
|
|
|
*/
|
|
|
|
static loadMojangLauncherData(){
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request.get('https://launchermeta.mojang.com/mc/launcher.json', (err, resp, body) => {
|
|
|
|
if(err){
|
|
|
|
resolve(null)
|
|
|
|
} else {
|
|
|
|
resolve(JSON.parse(body))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-12 13:12:45 -07:00
|
|
|
/**
|
2018-04-12 18:38:27 -07:00
|
|
|
* Parses a **full** Java Runtime version string and resolves
|
2018-12-31 07:39:27 -08:00
|
|
|
* the version information. Dynamically detects the formatting
|
|
|
|
* to use.
|
2018-04-12 18:38:27 -07:00
|
|
|
*
|
|
|
|
* @param {string} verString Full version string to parse.
|
|
|
|
* @returns Object containing the version information.
|
|
|
|
*/
|
|
|
|
static parseJavaRuntimeVersion(verString){
|
2018-12-31 07:39:27 -08:00
|
|
|
const major = verString.split('.')[0]
|
|
|
|
if(major == 1){
|
2019-03-05 20:05:01 -08:00
|
|
|
return JavaGuard._parseJavaRuntimeVersion_8(verString)
|
2018-12-31 07:39:27 -08:00
|
|
|
} else {
|
2019-03-05 20:05:01 -08:00
|
|
|
return JavaGuard._parseJavaRuntimeVersion_9(verString)
|
2018-12-31 07:39:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a **full** Java Runtime version string and resolves
|
|
|
|
* the version information. Uses Java 8 formatting.
|
|
|
|
*
|
|
|
|
* @param {string} verString Full version string to parse.
|
|
|
|
* @returns Object containing the version information.
|
|
|
|
*/
|
|
|
|
static _parseJavaRuntimeVersion_8(verString){
|
2018-04-12 18:38:27 -07:00
|
|
|
// 1.{major}.0_{update}-b{build}
|
|
|
|
// ex. 1.8.0_152-b16
|
|
|
|
const ret = {}
|
|
|
|
let pts = verString.split('-')
|
|
|
|
ret.build = parseInt(pts[1].substring(1))
|
2018-04-12 19:40:48 -07:00
|
|
|
pts = pts[0].split('_')
|
2018-04-12 18:38:27 -07:00
|
|
|
ret.update = parseInt(pts[1])
|
2018-04-12 19:40:48 -07:00
|
|
|
ret.major = parseInt(pts[0].split('.')[1])
|
2018-04-12 18:38:27 -07:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-12-31 07:39:27 -08:00
|
|
|
/**
|
|
|
|
* Parses a **full** Java Runtime version string and resolves
|
|
|
|
* the version information. Uses Java 9+ formatting.
|
|
|
|
*
|
|
|
|
* @param {string} verString Full version string to parse.
|
|
|
|
* @returns Object containing the version information.
|
|
|
|
*/
|
|
|
|
static _parseJavaRuntimeVersion_9(verString){
|
|
|
|
// {major}.{minor}.{revision}+{build}
|
|
|
|
// ex. 10.0.2+13
|
|
|
|
const ret = {}
|
|
|
|
let pts = verString.split('+')
|
|
|
|
ret.build = parseInt(pts[1])
|
|
|
|
pts = pts[0].split('.')
|
|
|
|
ret.major = parseInt(pts[0])
|
|
|
|
ret.minor = parseInt(pts[1])
|
|
|
|
ret.revision = parseInt(pts[2])
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-04-12 18:38:27 -07:00
|
|
|
/**
|
|
|
|
* Validates the output of a JVM's properties. Currently validates that a JRE is x64
|
|
|
|
* and that the major = 8, update > 52.
|
2018-04-12 13:12:45 -07:00
|
|
|
*
|
|
|
|
* @param {string} stderr The output to validate.
|
|
|
|
*
|
2018-05-14 22:05:10 -07:00
|
|
|
* @returns {Promise.<Object>} A promise which resolves to a meta object about the JVM.
|
|
|
|
* The validity is stored inside the `valid` property.
|
2018-04-12 13:12:45 -07:00
|
|
|
*/
|
2019-03-05 20:05:01 -08:00
|
|
|
_validateJVMProperties(stderr){
|
2018-04-12 13:12:45 -07:00
|
|
|
const res = stderr
|
|
|
|
const props = res.split('\n')
|
2018-04-12 18:38:27 -07:00
|
|
|
|
|
|
|
const goal = 2
|
|
|
|
let checksum = 0
|
|
|
|
|
2018-05-14 22:05:10 -07:00
|
|
|
const meta = {}
|
|
|
|
|
2018-04-12 13:12:45 -07:00
|
|
|
for(let i=0; i<props.length; i++){
|
|
|
|
if(props[i].indexOf('sun.arch.data.model') > -1){
|
|
|
|
let arch = props[i].split('=')[1].trim()
|
2018-05-14 22:05:10 -07:00
|
|
|
arch = parseInt(arch)
|
2022-12-24 19:17:30 -08:00
|
|
|
this.logger.debug(props[i].trim())
|
2018-05-14 22:05:10 -07:00
|
|
|
if(arch === 64){
|
|
|
|
meta.arch = arch
|
2018-04-12 18:38:27 -07:00
|
|
|
++checksum
|
|
|
|
if(checksum === goal){
|
2018-05-14 22:05:10 -07:00
|
|
|
break
|
2018-04-12 18:38:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if(props[i].indexOf('java.runtime.version') > -1){
|
|
|
|
let verString = props[i].split('=')[1].trim()
|
2022-12-24 19:17:30 -08:00
|
|
|
this.logger.debug(props[i].trim())
|
2019-03-05 20:05:01 -08:00
|
|
|
const verOb = JavaGuard.parseJavaRuntimeVersion(verString)
|
2022-11-27 15:13:50 -08:00
|
|
|
// TODO implement a support matrix eventually. Right now this is good enough
|
|
|
|
// 1.7-1.16 = Java 8
|
|
|
|
// 1.17+ = Java 17
|
|
|
|
// Actual support may vary, but we're going with this rule for simplicity.
|
2018-12-31 07:39:27 -08:00
|
|
|
if(verOb.major < 9){
|
|
|
|
// Java 8
|
2022-11-27 15:13:50 -08:00
|
|
|
if(!Util.mcVersionAtLeast('1.17', this.mcVersion)){
|
|
|
|
if(verOb.major === 8 && verOb.update > 52){
|
|
|
|
meta.version = verOb
|
|
|
|
++checksum
|
|
|
|
if(checksum === goal){
|
|
|
|
break
|
|
|
|
}
|
2018-12-31 07:39:27 -08:00
|
|
|
}
|
|
|
|
}
|
2022-11-27 15:13:50 -08:00
|
|
|
} else if(verOb.major >= 17) {
|
2018-12-31 07:39:27 -08:00
|
|
|
// Java 9+
|
2022-11-27 15:13:50 -08:00
|
|
|
if(Util.mcVersionAtLeast('1.17', this.mcVersion)){
|
|
|
|
meta.version = verOb
|
2018-12-31 07:39:27 -08:00
|
|
|
++checksum
|
|
|
|
if(checksum === goal){
|
|
|
|
break
|
2022-11-27 15:13:50 -08:00
|
|
|
}
|
2018-04-12 18:38:27 -07:00
|
|
|
}
|
|
|
|
}
|
2020-09-08 18:25:19 -07:00
|
|
|
// Space included so we get only the vendor.
|
|
|
|
} else if(props[i].lastIndexOf('java.vendor ') > -1) {
|
|
|
|
let vendorName = props[i].split('=')[1].trim()
|
2022-12-24 19:17:30 -08:00
|
|
|
this.logger.debug(props[i].trim())
|
2020-09-08 18:25:19 -07:00
|
|
|
meta.vendor = vendorName
|
2018-04-12 13:12:45 -07:00
|
|
|
}
|
|
|
|
}
|
2018-05-14 22:05:10 -07:00
|
|
|
|
|
|
|
meta.valid = checksum === goal
|
2018-04-12 18:38:27 -07:00
|
|
|
|
2018-05-14 22:05:10 -07:00
|
|
|
return meta
|
2018-04-12 13:12:45 -07:00
|
|
|
}
|
|
|
|
|
2017-11-30 00:00:06 -08:00
|
|
|
/**
|
2018-03-30 05:31:54 -07:00
|
|
|
* Validates that a Java binary is at least 64 bit. This makes use of the non-standard
|
|
|
|
* command line option -XshowSettings:properties. The output of this contains a property,
|
|
|
|
* sun.arch.data.model = ARCH, in which ARCH is either 32 or 64. This option is supported
|
|
|
|
* in Java 8 and 9. Since this is a non-standard option. This will resolve to true if
|
|
|
|
* the function's code throws errors. That would indicate that the option is changed or
|
|
|
|
* removed.
|
2017-11-30 00:00:06 -08:00
|
|
|
*
|
2018-04-07 15:06:49 -07:00
|
|
|
* @param {string} binaryExecPath Path to the java executable we wish to validate.
|
2018-03-30 05:31:54 -07:00
|
|
|
*
|
2018-05-14 22:05:10 -07:00
|
|
|
* @returns {Promise.<Object>} A promise which resolves to a meta object about the JVM.
|
|
|
|
* The validity is stored inside the `valid` property.
|
2017-11-30 00:00:06 -08:00
|
|
|
*/
|
2019-03-05 20:05:01 -08:00
|
|
|
_validateJavaBinary(binaryExecPath){
|
2018-03-30 05:31:54 -07:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-03-05 20:05:01 -08:00
|
|
|
if(!JavaGuard.isJavaExecPath(binaryExecPath)){
|
2018-06-14 00:49:55 -07:00
|
|
|
resolve({valid: false})
|
|
|
|
} else if(fs.existsSync(binaryExecPath)){
|
2019-06-02 15:11:39 -07:00
|
|
|
// Workaround (javaw.exe no longer outputs this information.)
|
2022-12-24 19:17:30 -08:00
|
|
|
this.logger.debug(typeof binaryExecPath)
|
2019-06-02 15:11:39 -07:00
|
|
|
if(binaryExecPath.indexOf('javaw.exe') > -1) {
|
|
|
|
binaryExecPath.replace('javaw.exe', 'java.exe')
|
|
|
|
}
|
2018-04-07 15:06:49 -07:00
|
|
|
child_process.exec('"' + binaryExecPath + '" -XshowSettings:properties', (err, stdout, stderr) => {
|
2018-03-30 05:31:54 -07:00
|
|
|
try {
|
|
|
|
// Output is stored in stderr?
|
2019-03-05 20:05:01 -08:00
|
|
|
resolve(this._validateJVMProperties(stderr))
|
2018-03-30 05:31:54 -07:00
|
|
|
} catch (err){
|
|
|
|
// Output format might have changed, validation cannot be completed.
|
2018-05-14 22:05:10 -07:00
|
|
|
resolve({valid: false})
|
2018-03-30 05:31:54 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
2018-05-14 22:05:10 -07:00
|
|
|
resolve({valid: false})
|
2018-03-30 05:31:54 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks for the presence of the environment variable JAVA_HOME. If it exits, we will check
|
|
|
|
* to see if the value points to a path which exists. If the path exits, the path is returned.
|
|
|
|
*
|
|
|
|
* @returns {string} The path defined by JAVA_HOME, if it exists. Otherwise null.
|
|
|
|
*/
|
|
|
|
static _scanJavaHome(){
|
|
|
|
const jHome = process.env.JAVA_HOME
|
|
|
|
try {
|
|
|
|
let res = fs.existsSync(jHome)
|
|
|
|
return res ? jHome : null
|
|
|
|
} catch (err) {
|
|
|
|
// Malformed JAVA_HOME property.
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scans the registry for 64-bit Java entries. The paths of each entry are added to
|
|
|
|
* a set and returned. Currently, only Java 8 (1.8) is supported.
|
|
|
|
*
|
|
|
|
* @returns {Promise.<Set.<string>>} A promise which resolves to a set of 64-bit Java root
|
|
|
|
* paths found in the registry.
|
|
|
|
*/
|
|
|
|
static _scanRegistry(){
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Keys for Java v9.0.0 and later:
|
|
|
|
// 'SOFTWARE\\JavaSoft\\JRE'
|
|
|
|
// 'SOFTWARE\\JavaSoft\\JDK'
|
|
|
|
// Forge does not yet support Java 9, therefore we do not.
|
|
|
|
|
|
|
|
// Keys for Java 1.8 and prior:
|
|
|
|
const regKeys = [
|
|
|
|
'\\SOFTWARE\\JavaSoft\\Java Runtime Environment',
|
|
|
|
'\\SOFTWARE\\JavaSoft\\Java Development Kit'
|
|
|
|
]
|
|
|
|
|
2018-05-10 01:36:52 -07:00
|
|
|
let keysDone = 0
|
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
const candidates = new Set()
|
|
|
|
|
|
|
|
for(let i=0; i<regKeys.length; i++){
|
|
|
|
const key = new Registry({
|
|
|
|
hive: Registry.HKLM,
|
|
|
|
key: regKeys[i],
|
|
|
|
arch: 'x64'
|
|
|
|
})
|
|
|
|
key.keyExists((err, exists) => {
|
|
|
|
if(exists) {
|
|
|
|
key.keys((err, javaVers) => {
|
|
|
|
if(err){
|
2018-05-10 01:36:52 -07:00
|
|
|
keysDone++
|
2018-03-30 05:31:54 -07:00
|
|
|
console.error(err)
|
2018-05-10 01:36:52 -07:00
|
|
|
|
|
|
|
// REG KEY DONE
|
|
|
|
// DUE TO ERROR
|
|
|
|
if(keysDone === regKeys.length){
|
2018-03-30 05:31:54 -07:00
|
|
|
resolve(candidates)
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-10 01:36:52 -07:00
|
|
|
if(javaVers.length === 0){
|
|
|
|
// REG KEY DONE
|
|
|
|
// NO SUBKEYS
|
|
|
|
keysDone++
|
|
|
|
if(keysDone === regKeys.length){
|
|
|
|
resolve(candidates)
|
|
|
|
}
|
2018-03-30 05:31:54 -07:00
|
|
|
} else {
|
2018-05-10 01:36:52 -07:00
|
|
|
|
|
|
|
let numDone = 0
|
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
for(let j=0; j<javaVers.length; j++){
|
|
|
|
const javaVer = javaVers[j]
|
|
|
|
const vKey = javaVer.key.substring(javaVer.key.lastIndexOf('\\')+1)
|
|
|
|
// Only Java 8 is supported currently.
|
|
|
|
if(parseFloat(vKey) === 1.8){
|
|
|
|
javaVer.get('JavaHome', (err, res) => {
|
|
|
|
const jHome = res.value
|
|
|
|
if(jHome.indexOf('(x86)') === -1){
|
|
|
|
candidates.add(jHome)
|
|
|
|
}
|
2018-05-10 01:36:52 -07:00
|
|
|
|
|
|
|
// SUBKEY DONE
|
|
|
|
|
|
|
|
numDone++
|
|
|
|
if(numDone === javaVers.length){
|
|
|
|
keysDone++
|
|
|
|
if(keysDone === regKeys.length){
|
|
|
|
resolve(candidates)
|
|
|
|
}
|
2018-03-30 05:31:54 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
2018-05-10 01:36:52 -07:00
|
|
|
|
|
|
|
// SUBKEY DONE
|
|
|
|
// NOT JAVA 8
|
|
|
|
|
|
|
|
numDone++
|
|
|
|
if(numDone === javaVers.length){
|
|
|
|
keysDone++
|
|
|
|
if(keysDone === regKeys.length){
|
|
|
|
resolve(candidates)
|
|
|
|
}
|
2018-03-30 05:31:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-30 00:00:06 -08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
2018-05-10 01:36:52 -07:00
|
|
|
|
|
|
|
// REG KEY DONE
|
|
|
|
// DUE TO NON-EXISTANCE
|
|
|
|
|
|
|
|
keysDone++
|
|
|
|
if(keysDone === regKeys.length){
|
2018-03-30 05:31:54 -07:00
|
|
|
resolve(candidates)
|
|
|
|
}
|
2017-11-30 00:00:06 -08:00
|
|
|
}
|
|
|
|
})
|
2018-03-30 05:31:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:05:10 -07:00
|
|
|
/**
|
|
|
|
* See if JRE exists in the Internet Plug-Ins folder.
|
|
|
|
*
|
|
|
|
* @returns {string} The path of the JRE if found, otherwise null.
|
|
|
|
*/
|
|
|
|
static _scanInternetPlugins(){
|
|
|
|
// /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java
|
|
|
|
const pth = '/Library/Internet Plug-Ins/JavaAppletPlugin.plugin'
|
2019-03-05 20:05:01 -08:00
|
|
|
const res = fs.existsSync(JavaGuard.javaExecFromRoot(pth))
|
2018-05-14 22:05:10 -07:00
|
|
|
return res ? pth : null
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scan a directory for root JVM folders.
|
|
|
|
*
|
|
|
|
* @param {string} scanDir The directory to scan.
|
|
|
|
* @returns {Promise.<Set.<string>>} A promise which resolves to a set of the discovered
|
|
|
|
* root JVM folders.
|
|
|
|
*/
|
2020-09-13 00:05:08 -07:00
|
|
|
static async _scanFileSystem(scanDir){
|
2018-05-14 22:05:10 -07:00
|
|
|
|
2020-09-13 00:05:08 -07:00
|
|
|
let res = new Set()
|
2018-05-14 22:05:10 -07:00
|
|
|
|
2020-09-13 00:05:08 -07:00
|
|
|
if(await fs.pathExists(scanDir)) {
|
2018-05-14 22:05:10 -07:00
|
|
|
|
2020-09-13 00:05:08 -07:00
|
|
|
const files = await fs.readdir(scanDir)
|
|
|
|
for(let i=0; i<files.length; i++){
|
2018-05-14 22:05:10 -07:00
|
|
|
|
2020-09-13 00:05:08 -07:00
|
|
|
const combinedPath = path.join(scanDir, files[i])
|
|
|
|
const execPath = JavaGuard.javaExecFromRoot(combinedPath)
|
2018-05-14 22:05:10 -07:00
|
|
|
|
2020-09-13 00:05:08 -07:00
|
|
|
if(await fs.pathExists(execPath)) {
|
|
|
|
res.add(combinedPath)
|
2018-05-14 22:05:10 -07:00
|
|
|
}
|
2020-09-13 00:05:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
2018-05-14 22:05:10 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Set.<string>} rootSet A set of JVM root strings to validate.
|
|
|
|
* @returns {Promise.<Object[]>} A promise which resolves to an array of meta objects
|
|
|
|
* for each valid JVM root directory.
|
|
|
|
*/
|
2019-03-05 20:05:01 -08:00
|
|
|
async _validateJavaRootSet(rootSet){
|
2018-05-14 22:05:10 -07:00
|
|
|
|
|
|
|
const rootArr = Array.from(rootSet)
|
|
|
|
const validArr = []
|
|
|
|
|
|
|
|
for(let i=0; i<rootArr.length; i++){
|
|
|
|
|
2019-03-05 20:05:01 -08:00
|
|
|
const execPath = JavaGuard.javaExecFromRoot(rootArr[i])
|
|
|
|
const metaOb = await this._validateJavaBinary(execPath)
|
2018-05-14 22:05:10 -07:00
|
|
|
|
|
|
|
if(metaOb.valid){
|
|
|
|
metaOb.execPath = execPath
|
|
|
|
validArr.push(metaOb)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return validArr
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort an array of JVM meta objects. Best candidates are placed before all others.
|
|
|
|
* Sorts based on version and gives priority to JREs over JDKs if versions match.
|
|
|
|
*
|
|
|
|
* @param {Object[]} validArr An array of JVM meta objects.
|
|
|
|
* @returns {Object[]} A sorted array of JVM meta objects.
|
|
|
|
*/
|
|
|
|
static _sortValidJavaArray(validArr){
|
|
|
|
const retArr = validArr.sort((a, b) => {
|
|
|
|
|
|
|
|
if(a.version.major === b.version.major){
|
2018-12-31 07:39:27 -08:00
|
|
|
|
|
|
|
if(a.version.major < 9){
|
|
|
|
// Java 8
|
|
|
|
if(a.version.update === b.version.update){
|
|
|
|
if(a.version.build === b.version.build){
|
|
|
|
|
|
|
|
// Same version, give priority to JRE.
|
|
|
|
if(a.execPath.toLowerCase().indexOf('jdk') > -1){
|
|
|
|
return b.execPath.toLowerCase().indexOf('jdk') > -1 ? 0 : 1
|
|
|
|
} else {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:05:10 -07:00
|
|
|
} else {
|
2018-12-31 07:39:27 -08:00
|
|
|
return a.version.build > b.version.build ? -1 : 1
|
2018-05-14 22:05:10 -07:00
|
|
|
}
|
|
|
|
} else {
|
2018-12-31 07:39:27 -08:00
|
|
|
return a.version.update > b.version.update ? -1 : 1
|
2018-05-14 22:05:10 -07:00
|
|
|
}
|
|
|
|
} else {
|
2018-12-31 07:39:27 -08:00
|
|
|
// Java 9+
|
|
|
|
if(a.version.minor === b.version.minor){
|
|
|
|
if(a.version.revision === b.version.revision){
|
|
|
|
|
|
|
|
// Same version, give priority to JRE.
|
|
|
|
if(a.execPath.toLowerCase().indexOf('jdk') > -1){
|
|
|
|
return b.execPath.toLowerCase().indexOf('jdk') > -1 ? 0 : 1
|
|
|
|
} else {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return a.version.revision > b.version.revision ? -1 : 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return a.version.minor > b.version.minor ? -1 : 1
|
|
|
|
}
|
2018-05-14 22:05:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2018-05-14 23:30:26 -07:00
|
|
|
return a.version.major > b.version.major ? -1 : 1
|
2018-05-14 22:05:10 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return retArr
|
|
|
|
}
|
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
/**
|
|
|
|
* Attempts to find a valid x64 installation of Java on Windows machines.
|
|
|
|
* Possible paths will be pulled from the registry and the JAVA_HOME environment
|
|
|
|
* variable. The paths will be sorted with higher versions preceeding lower, and
|
|
|
|
* JREs preceeding JDKs. The binaries at the sorted paths will then be validated.
|
|
|
|
* The first validated is returned.
|
|
|
|
*
|
|
|
|
* Higher versions > Lower versions
|
|
|
|
* If versions are equal, JRE > JDK.
|
|
|
|
*
|
2018-04-02 15:40:32 -07:00
|
|
|
* @param {string} dataDir The base launcher directory.
|
2018-04-07 15:06:49 -07:00
|
|
|
* @returns {Promise.<string>} A Promise which resolves to the executable path of a valid
|
2018-03-31 17:45:24 -07:00
|
|
|
* x64 Java installation. If none are found, null is returned.
|
2018-03-30 05:31:54 -07:00
|
|
|
*/
|
2019-03-05 20:05:01 -08:00
|
|
|
async _win32JavaValidate(dataDir){
|
2018-03-30 05:31:54 -07:00
|
|
|
|
|
|
|
// Get possible paths from the registry.
|
2019-03-05 20:05:01 -08:00
|
|
|
let pathSet1 = await JavaGuard._scanRegistry()
|
2020-09-13 00:05:08 -07:00
|
|
|
if(pathSet1.size === 0){
|
2022-11-27 15:13:50 -08:00
|
|
|
|
2018-05-14 22:05:10 -07:00
|
|
|
// Do a manual file system scan of program files.
|
2022-11-27 15:13:50 -08:00
|
|
|
// Check all drives
|
|
|
|
const driveMounts = nodeDiskInfo.getDiskInfoSync().map(({ mounted }) => mounted)
|
|
|
|
for(const mount of driveMounts) {
|
|
|
|
pathSet1 = new Set([
|
|
|
|
...pathSet1,
|
|
|
|
...(await JavaGuard._scanFileSystem(`${mount}\\Program Files\\Java`)),
|
|
|
|
...(await JavaGuard._scanFileSystem(`${mount}\\Program Files\\Eclipse Adoptium`)),
|
|
|
|
...(await JavaGuard._scanFileSystem(`${mount}\\Program Files\\Eclipse Foundation`)),
|
|
|
|
...(await JavaGuard._scanFileSystem(`${mount}\\Program Files\\AdoptOpenJDK`))
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:05:10 -07:00
|
|
|
}
|
2018-03-30 05:31:54 -07:00
|
|
|
|
2018-04-02 15:40:32 -07:00
|
|
|
// Get possible paths from the data directory.
|
2019-03-05 20:05:01 -08:00
|
|
|
const pathSet2 = await JavaGuard._scanFileSystem(path.join(dataDir, 'runtime', 'x64'))
|
2018-04-02 15:40:32 -07:00
|
|
|
|
2018-05-14 22:05:10 -07:00
|
|
|
// Merge the results.
|
|
|
|
const uberSet = new Set([...pathSet1, ...pathSet2])
|
|
|
|
|
|
|
|
// Validate JAVA_HOME.
|
2019-03-05 20:05:01 -08:00
|
|
|
const jHome = JavaGuard._scanJavaHome()
|
2018-03-30 05:31:54 -07:00
|
|
|
if(jHome != null && jHome.indexOf('(x86)') === -1){
|
2018-05-14 22:05:10 -07:00
|
|
|
uberSet.add(jHome)
|
2018-03-30 05:31:54 -07:00
|
|
|
}
|
|
|
|
|
2019-03-05 20:05:01 -08:00
|
|
|
let pathArr = await this._validateJavaRootSet(uberSet)
|
|
|
|
pathArr = JavaGuard._sortValidJavaArray(pathArr)
|
2018-03-30 05:31:54 -07:00
|
|
|
|
2018-05-14 22:05:10 -07:00
|
|
|
if(pathArr.length > 0){
|
|
|
|
return pathArr[0].execPath
|
|
|
|
} else {
|
|
|
|
return null
|
2017-11-30 00:00:06 -08:00
|
|
|
}
|
2018-03-30 05:31:54 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-12 13:12:45 -07:00
|
|
|
/**
|
2018-05-14 22:05:10 -07:00
|
|
|
* Attempts to find a valid x64 installation of Java on MacOS.
|
|
|
|
* The system JVM directory is scanned for possible installations.
|
|
|
|
* The JAVA_HOME enviroment variable and internet plugins directory
|
|
|
|
* are also scanned and validated.
|
2018-04-12 13:12:45 -07:00
|
|
|
*
|
2018-05-14 22:05:10 -07:00
|
|
|
* Higher versions > Lower versions
|
|
|
|
* If versions are equal, JRE > JDK.
|
|
|
|
*
|
|
|
|
* @param {string} dataDir The base launcher directory.
|
|
|
|
* @returns {Promise.<string>} A Promise which resolves to the executable path of a valid
|
|
|
|
* x64 Java installation. If none are found, null is returned.
|
2018-03-30 05:31:54 -07:00
|
|
|
*/
|
2019-03-05 20:05:01 -08:00
|
|
|
async _darwinJavaValidate(dataDir){
|
2018-04-12 13:12:45 -07:00
|
|
|
|
2019-03-05 20:05:01 -08:00
|
|
|
const pathSet1 = await JavaGuard._scanFileSystem('/Library/Java/JavaVirtualMachines')
|
|
|
|
const pathSet2 = await JavaGuard._scanFileSystem(path.join(dataDir, 'runtime', 'x64'))
|
2018-05-14 22:05:10 -07:00
|
|
|
|
|
|
|
const uberSet = new Set([...pathSet1, ...pathSet2])
|
2018-04-12 13:12:45 -07:00
|
|
|
|
2018-04-12 18:38:27 -07:00
|
|
|
// Check Internet Plugins folder.
|
2019-03-05 20:05:01 -08:00
|
|
|
const iPPath = JavaGuard._scanInternetPlugins()
|
2018-04-12 13:12:45 -07:00
|
|
|
if(iPPath != null){
|
2018-05-14 22:05:10 -07:00
|
|
|
uberSet.add(iPPath)
|
2018-04-12 13:12:45 -07:00
|
|
|
}
|
|
|
|
|
2018-04-12 18:38:27 -07:00
|
|
|
// Check the JAVA_HOME environment variable.
|
2019-03-05 20:05:01 -08:00
|
|
|
let jHome = JavaGuard._scanJavaHome()
|
2018-04-12 13:12:45 -07:00
|
|
|
if(jHome != null){
|
2018-04-12 19:13:26 -07:00
|
|
|
// Ensure we are at the absolute root.
|
|
|
|
if(jHome.contains('/Contents/Home')){
|
|
|
|
jHome = jHome.substring(0, jHome.indexOf('/Contents/Home'))
|
|
|
|
}
|
2018-05-14 22:05:10 -07:00
|
|
|
uberSet.add(jHome)
|
2018-04-12 13:12:45 -07:00
|
|
|
}
|
|
|
|
|
2019-03-05 20:05:01 -08:00
|
|
|
let pathArr = await this._validateJavaRootSet(uberSet)
|
|
|
|
pathArr = JavaGuard._sortValidJavaArray(pathArr)
|
2018-04-12 18:38:27 -07:00
|
|
|
|
2018-05-14 22:05:10 -07:00
|
|
|
if(pathArr.length > 0){
|
|
|
|
return pathArr[0].execPath
|
|
|
|
} else {
|
|
|
|
return null
|
2018-04-12 13:12:45 -07:00
|
|
|
}
|
2017-11-30 00:00:06 -08:00
|
|
|
}
|
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
/**
|
2018-05-14 22:05:10 -07:00
|
|
|
* Attempts to find a valid x64 installation of Java on Linux.
|
|
|
|
* The system JVM directory is scanned for possible installations.
|
|
|
|
* The JAVA_HOME enviroment variable is also scanned and validated.
|
|
|
|
*
|
|
|
|
* Higher versions > Lower versions
|
|
|
|
* If versions are equal, JRE > JDK.
|
|
|
|
*
|
|
|
|
* @param {string} dataDir The base launcher directory.
|
|
|
|
* @returns {Promise.<string>} A Promise which resolves to the executable path of a valid
|
|
|
|
* x64 Java installation. If none are found, null is returned.
|
2018-03-30 05:31:54 -07:00
|
|
|
*/
|
2019-03-05 20:05:01 -08:00
|
|
|
async _linuxJavaValidate(dataDir){
|
2018-05-14 22:05:10 -07:00
|
|
|
|
2019-03-05 20:05:01 -08:00
|
|
|
const pathSet1 = await JavaGuard._scanFileSystem('/usr/lib/jvm')
|
|
|
|
const pathSet2 = await JavaGuard._scanFileSystem(path.join(dataDir, 'runtime', 'x64'))
|
2018-05-14 22:05:10 -07:00
|
|
|
|
|
|
|
const uberSet = new Set([...pathSet1, ...pathSet2])
|
|
|
|
|
|
|
|
// Validate JAVA_HOME
|
2019-03-05 20:05:01 -08:00
|
|
|
const jHome = JavaGuard._scanJavaHome()
|
2018-05-14 22:05:10 -07:00
|
|
|
if(jHome != null){
|
|
|
|
uberSet.add(jHome)
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:05:01 -08:00
|
|
|
let pathArr = await this._validateJavaRootSet(uberSet)
|
|
|
|
pathArr = JavaGuard._sortValidJavaArray(pathArr)
|
2018-05-14 22:05:10 -07:00
|
|
|
|
|
|
|
if(pathArr.length > 0){
|
|
|
|
return pathArr[0].execPath
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
2018-03-30 05:31:54 -07:00
|
|
|
}
|
|
|
|
|
2018-03-31 17:45:24 -07:00
|
|
|
/**
|
|
|
|
* Retrieve the path of a valid x64 Java installation.
|
|
|
|
*
|
2018-04-02 15:40:32 -07:00
|
|
|
* @param {string} dataDir The base launcher directory.
|
2018-03-31 17:45:24 -07:00
|
|
|
* @returns {string} A path to a valid x64 Java installation, null if none found.
|
|
|
|
*/
|
2019-03-05 20:05:01 -08:00
|
|
|
async validateJava(dataDir){
|
|
|
|
return await this['_' + process.platform + 'JavaValidate'](dataDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Central object class used for control flow. This object stores data about
|
|
|
|
* categories of downloads. Each category is assigned an identifier with a
|
|
|
|
* DLTracker object as its value. Combined information is also stored, such as
|
|
|
|
* the total size of all the queued files in each category. This event is used
|
|
|
|
* to emit events so that external modules can listen into processing done in
|
|
|
|
* this module.
|
|
|
|
*/
|
|
|
|
class AssetGuard extends EventEmitter {
|
|
|
|
|
2022-12-24 19:17:30 -08:00
|
|
|
static logger = LoggerUtil.getLogger('AssetGuard')
|
|
|
|
|
2019-03-05 20:05:01 -08:00
|
|
|
/**
|
|
|
|
* Create an instance of AssetGuard.
|
|
|
|
* On creation the object's properties are never-null default
|
|
|
|
* values. Each identifier is resolved to an empty DLTracker.
|
|
|
|
*
|
|
|
|
* @param {string} commonPath The common path for shared game files.
|
|
|
|
* @param {string} javaexec The path to a java executable which will be used
|
|
|
|
* to finalize installation.
|
|
|
|
*/
|
|
|
|
constructor(commonPath, javaexec){
|
|
|
|
super()
|
|
|
|
this.totaldlsize = 0
|
|
|
|
this.progress = 0
|
|
|
|
this.assets = new DLTracker([], 0)
|
|
|
|
this.libraries = new DLTracker([], 0)
|
|
|
|
this.files = new DLTracker([], 0)
|
|
|
|
this.forge = new DLTracker([], 0)
|
|
|
|
this.java = new DLTracker([], 0)
|
|
|
|
this.extractQueue = []
|
|
|
|
this.commonPath = commonPath
|
|
|
|
this.javaexec = javaexec
|
|
|
|
}
|
|
|
|
|
|
|
|
// Static Utility Functions
|
|
|
|
// #region
|
|
|
|
|
|
|
|
// Static Hash Validation Functions
|
|
|
|
// #region
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the hash for a file using the specified algorithm.
|
|
|
|
*
|
|
|
|
* @param {Buffer} buf The buffer containing file data.
|
|
|
|
* @param {string} algo The hash algorithm.
|
|
|
|
* @returns {string} The calculated hash in hex.
|
|
|
|
*/
|
|
|
|
static _calculateHash(buf, algo){
|
|
|
|
return crypto.createHash(algo).update(buf).digest('hex')
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate that a file exists and matches a given hash value.
|
|
|
|
*
|
|
|
|
* @param {string} filePath The path of the file to validate.
|
|
|
|
* @param {string} algo The hash algorithm to check against.
|
|
|
|
* @param {string} hash The existing hash to check against.
|
|
|
|
* @returns {boolean} True if the file exists and calculated hash matches the given hash, otherwise false.
|
|
|
|
*/
|
|
|
|
static _validateLocal(filePath, algo, hash){
|
|
|
|
if(fs.existsSync(filePath)){
|
|
|
|
//No hash provided, have to assume it's good.
|
|
|
|
if(hash == null){
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
let buf = fs.readFileSync(filePath)
|
|
|
|
let calcdhash = AssetGuard._calculateHash(buf, algo)
|
2020-07-07 19:47:22 -07:00
|
|
|
return calcdhash === hash.toLowerCase()
|
2019-03-05 20:05:01 -08:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// Miscellaneous Static Functions
|
|
|
|
// #region
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts and unpacks a file from .pack.xz format.
|
|
|
|
*
|
|
|
|
* @param {Array.<string>} filePaths The paths of the files to be extracted and unpacked.
|
|
|
|
* @returns {Promise.<void>} An empty promise to indicate the extraction has completed.
|
|
|
|
*/
|
|
|
|
static _extractPackXZ(filePaths, javaExecutable){
|
2022-12-24 19:17:30 -08:00
|
|
|
const extractLogger = LoggerUtil.getLogger('PackXZExtract')
|
|
|
|
extractLogger.info('Starting')
|
2019-03-05 20:05:01 -08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
let libPath
|
|
|
|
if(isDev){
|
|
|
|
libPath = path.join(process.cwd(), 'libraries', 'java', 'PackXZExtract.jar')
|
|
|
|
} else {
|
|
|
|
if(process.platform === 'darwin'){
|
|
|
|
libPath = path.join(process.cwd(),'Contents', 'Resources', 'libraries', 'java', 'PackXZExtract.jar')
|
|
|
|
} else {
|
|
|
|
libPath = path.join(process.cwd(), 'resources', 'libraries', 'java', 'PackXZExtract.jar')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const filePath = filePaths.join(',')
|
|
|
|
const child = child_process.spawn(javaExecutable, ['-jar', libPath, '-packxz', filePath])
|
|
|
|
child.stdout.on('data', (data) => {
|
2022-12-24 19:17:30 -08:00
|
|
|
extractLogger.info(data.toString('utf8'))
|
2019-03-05 20:05:01 -08:00
|
|
|
})
|
|
|
|
child.stderr.on('data', (data) => {
|
2022-12-24 19:17:30 -08:00
|
|
|
extractLogger.info(data.toString('utf8'))
|
2019-03-05 20:05:01 -08:00
|
|
|
})
|
|
|
|
child.on('close', (code, signal) => {
|
2022-12-24 19:17:30 -08:00
|
|
|
extractLogger.info('Exited with code', code)
|
2019-03-05 20:05:01 -08:00
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
// #endregion
|
|
|
|
|
2018-03-31 10:05:05 -07:00
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// Java (Category=''') Validation (download) Functions
|
|
|
|
// #region
|
|
|
|
|
2022-11-27 15:13:50 -08:00
|
|
|
_enqueueOpenJDK(dataDir, mcVersion){
|
2018-03-31 17:45:24 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-11-27 15:13:50 -08:00
|
|
|
const major = Util.mcVersionAtLeast('1.17', mcVersion) ? '17' : '8'
|
|
|
|
JavaGuard._latestOpenJDK(major).then(verData => {
|
2018-03-31 17:45:24 -07:00
|
|
|
if(verData != null){
|
2018-03-31 10:05:05 -07:00
|
|
|
|
2019-06-02 15:11:39 -07:00
|
|
|
dataDir = path.join(dataDir, 'runtime', 'x64')
|
|
|
|
const fDir = path.join(dataDir, verData.name)
|
|
|
|
const jre = new Asset(verData.name, null, verData.size, verData.uri, fDir)
|
|
|
|
this.java = new DLTracker([jre], jre.size, (a, self) => {
|
|
|
|
if(verData.name.endsWith('zip')){
|
|
|
|
|
2021-10-14 20:17:40 -07:00
|
|
|
this._extractJdkZip(a.to, dataDir, self)
|
2019-06-02 15:11:39 -07:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// Tar.gz
|
|
|
|
let h = null
|
|
|
|
fs.createReadStream(a.to)
|
2022-12-24 19:17:30 -08:00
|
|
|
.on('error', err => AssetGuard.logger.error(err))
|
2019-06-02 15:11:39 -07:00
|
|
|
.pipe(zlib.createGunzip())
|
2022-12-24 19:17:30 -08:00
|
|
|
.on('error', err => AssetGuard.logger.error(err))
|
2019-06-02 15:11:39 -07:00
|
|
|
.pipe(tar.extract(dataDir, {
|
|
|
|
map: (header) => {
|
|
|
|
if(h == null){
|
|
|
|
h = header.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
2022-12-24 19:17:30 -08:00
|
|
|
.on('error', err => AssetGuard.logger.error(err))
|
2019-06-02 15:11:39 -07:00
|
|
|
.on('finish', () => {
|
|
|
|
fs.unlink(a.to, err => {
|
|
|
|
if(err){
|
2022-12-24 19:17:30 -08:00
|
|
|
AssetGuard.logger.error(err)
|
2019-06-02 15:11:39 -07:00
|
|
|
}
|
|
|
|
if(h.indexOf('/') > -1){
|
|
|
|
h = h.substring(0, h.indexOf('/'))
|
|
|
|
}
|
|
|
|
const pos = path.join(dataDir, h)
|
|
|
|
self.emit('complete', 'java', JavaGuard.javaExecFromRoot(pos))
|
|
|
|
})
|
|
|
|
})
|
2018-03-31 17:45:24 -07:00
|
|
|
}
|
|
|
|
})
|
2019-06-02 15:11:39 -07:00
|
|
|
resolve(true)
|
2018-03-31 10:05:05 -07:00
|
|
|
|
2018-03-31 17:45:24 -07:00
|
|
|
} else {
|
|
|
|
resolve(false)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2018-03-31 10:05:05 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-14 20:17:40 -07:00
|
|
|
async _extractJdkZip(zipPath, runtimeDir, self) {
|
|
|
|
|
|
|
|
const zip = new StreamZip.async({
|
|
|
|
file: zipPath,
|
|
|
|
storeEntries: true
|
|
|
|
})
|
|
|
|
|
|
|
|
let pos = ''
|
|
|
|
try {
|
|
|
|
const entries = await zip.entries()
|
|
|
|
pos = path.join(runtimeDir, Object.keys(entries)[0])
|
|
|
|
|
2022-12-24 19:17:30 -08:00
|
|
|
AssetGuard.logger.info('Extracting jdk..')
|
2021-10-14 20:17:40 -07:00
|
|
|
await zip.extract(null, runtimeDir)
|
2022-12-24 19:17:30 -08:00
|
|
|
AssetGuard.logger.info('Cleaning up..')
|
2021-10-14 20:17:40 -07:00
|
|
|
await fs.remove(zipPath)
|
2022-12-24 19:17:30 -08:00
|
|
|
AssetGuard.logger.info('Jdk extraction complete.')
|
2021-10-14 20:17:40 -07:00
|
|
|
|
|
|
|
} catch(err) {
|
2022-12-24 19:17:30 -08:00
|
|
|
AssetGuard.logger.error(err)
|
2021-10-14 20:17:40 -07:00
|
|
|
} finally {
|
|
|
|
zip.close()
|
|
|
|
self.emit('complete', 'java', JavaGuard.javaExecFromRoot(pos))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 10:05:05 -07:00
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// Control Flow Functions
|
|
|
|
// #region
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiate an async download process for an AssetGuard DLTracker.
|
|
|
|
*
|
|
|
|
* @param {string} identifier The identifier of the AssetGuard DLTracker.
|
|
|
|
* @param {number} limit Optional. The number of async processes to run in parallel.
|
|
|
|
* @returns {boolean} True if the process began, otherwise false.
|
|
|
|
*/
|
|
|
|
startAsyncProcess(identifier, limit = 5){
|
2018-07-22 08:40:15 -07:00
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
const self = this
|
2018-07-22 08:40:15 -07:00
|
|
|
const dlTracker = this[identifier]
|
|
|
|
const dlQueue = dlTracker.dlqueue
|
|
|
|
|
|
|
|
if(dlQueue.length > 0){
|
2022-12-24 19:17:30 -08:00
|
|
|
AssetGuard.logger.info('DLQueue', dlQueue)
|
2018-07-22 08:40:15 -07:00
|
|
|
|
|
|
|
async.eachLimit(dlQueue, limit, (asset, cb) => {
|
|
|
|
|
2018-12-01 05:20:42 -08:00
|
|
|
fs.ensureDirSync(path.join(asset.to, '..'))
|
2018-07-22 08:40:15 -07:00
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
let req = request(asset.from)
|
|
|
|
req.pause()
|
2018-07-22 08:40:15 -07:00
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
req.on('response', (resp) => {
|
2018-07-22 08:40:15 -07:00
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
if(resp.statusCode === 200){
|
2018-07-22 08:40:15 -07:00
|
|
|
|
|
|
|
let doHashCheck = false
|
|
|
|
const contentLength = parseInt(resp.headers['content-length'])
|
|
|
|
|
|
|
|
if(contentLength !== asset.size){
|
2022-12-24 19:17:30 -08:00
|
|
|
AssetGuard.logger.warn(`WARN: Got ${contentLength} bytes for ${asset.id}: Expected ${asset.size}`)
|
2018-07-22 08:40:15 -07:00
|
|
|
doHashCheck = true
|
|
|
|
|
|
|
|
// Adjust download
|
|
|
|
this.totaldlsize -= asset.size
|
|
|
|
this.totaldlsize += contentLength
|
|
|
|
}
|
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
let writeStream = fs.createWriteStream(asset.to)
|
|
|
|
writeStream.on('close', () => {
|
2018-07-22 08:40:15 -07:00
|
|
|
if(dlTracker.callback != null){
|
|
|
|
dlTracker.callback.apply(dlTracker, [asset, self])
|
2018-03-30 05:31:54 -07:00
|
|
|
}
|
2018-07-22 08:40:15 -07:00
|
|
|
|
|
|
|
if(doHashCheck){
|
|
|
|
const v = AssetGuard._validateLocal(asset.to, asset.type != null ? 'md5' : 'sha1', asset.hash)
|
|
|
|
if(v){
|
2022-12-24 19:17:30 -08:00
|
|
|
AssetGuard.logger.warn(`Hashes match for ${asset.id}, byte mismatch is an issue in the distro index.`)
|
2018-07-22 08:40:15 -07:00
|
|
|
} else {
|
2022-12-24 19:17:30 -08:00
|
|
|
AssetGuard.logger.error(`Hashes do not match, ${asset.id} may be corrupted.`)
|
2018-07-22 08:40:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
cb()
|
|
|
|
})
|
|
|
|
req.pipe(writeStream)
|
|
|
|
req.resume()
|
2018-07-22 08:40:15 -07:00
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
} else {
|
2018-07-22 08:40:15 -07:00
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
req.abort()
|
2022-12-24 19:17:30 -08:00
|
|
|
AssetGuard.logger.error(`Failed to download ${asset.id}(${typeof asset.from === 'object' ? asset.from.url : asset.from}). Response code ${resp.statusCode}`)
|
2018-03-30 05:31:54 -07:00
|
|
|
self.progress += asset.size*1
|
2018-07-22 08:40:15 -07:00
|
|
|
self.emit('progress', 'download', self.progress, self.totaldlsize)
|
2018-03-30 05:31:54 -07:00
|
|
|
cb()
|
2018-07-22 08:40:15 -07:00
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
}
|
2018-07-22 08:40:15 -07:00
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
})
|
2018-07-22 08:40:15 -07:00
|
|
|
|
2018-05-06 18:45:20 -07:00
|
|
|
req.on('error', (err) => {
|
2018-07-22 08:40:15 -07:00
|
|
|
self.emit('error', 'download', err)
|
2018-05-06 18:45:20 -07:00
|
|
|
})
|
2018-07-22 08:40:15 -07:00
|
|
|
|
2018-05-07 15:15:59 -07:00
|
|
|
req.on('data', (chunk) => {
|
2018-03-30 05:31:54 -07:00
|
|
|
self.progress += chunk.length
|
2018-07-22 08:40:15 -07:00
|
|
|
self.emit('progress', 'download', self.progress, self.totaldlsize)
|
2018-03-30 05:31:54 -07:00
|
|
|
})
|
2018-07-22 08:40:15 -07:00
|
|
|
|
2018-05-07 15:15:59 -07:00
|
|
|
}, (err) => {
|
2018-07-22 08:40:15 -07:00
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
if(err){
|
2022-12-24 19:17:30 -08:00
|
|
|
AssetGuard.logger.warn('An item in ' + identifier + ' failed to process')
|
2018-03-30 05:31:54 -07:00
|
|
|
} else {
|
2022-12-24 19:17:30 -08:00
|
|
|
AssetGuard.logger.info('All ' + identifier + ' have been processed successfully')
|
2018-03-30 05:31:54 -07:00
|
|
|
}
|
2018-07-22 08:40:15 -07:00
|
|
|
|
|
|
|
//self.totaldlsize -= dlTracker.dlsize
|
|
|
|
//self.progress -= dlTracker.dlsize
|
2018-03-30 05:31:54 -07:00
|
|
|
self[identifier] = new DLTracker([], 0)
|
2018-07-22 08:40:15 -07:00
|
|
|
|
|
|
|
if(self.progress >= self.totaldlsize) {
|
2018-04-28 20:34:23 -07:00
|
|
|
if(self.extractQueue.length > 0){
|
2018-07-22 08:40:15 -07:00
|
|
|
self.emit('progress', 'extract', 1, 1)
|
|
|
|
//self.emit('extracting')
|
2018-04-28 20:34:23 -07:00
|
|
|
AssetGuard._extractPackXZ(self.extractQueue, self.javaexec).then(() => {
|
|
|
|
self.extractQueue = []
|
2018-07-22 08:40:15 -07:00
|
|
|
self.emit('complete', 'download')
|
2018-04-28 20:34:23 -07:00
|
|
|
})
|
|
|
|
} else {
|
2018-07-22 08:40:15 -07:00
|
|
|
self.emit('complete', 'download')
|
2018-04-28 20:34:23 -07:00
|
|
|
}
|
2018-03-30 05:31:54 -07:00
|
|
|
}
|
2018-07-22 08:40:15 -07:00
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
})
|
2018-07-22 08:40:15 -07:00
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
return true
|
2018-07-22 08:40:15 -07:00
|
|
|
|
|
|
|
} else {
|
|
|
|
return false
|
2018-03-30 05:31:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 00:00:06 -08:00
|
|
|
/**
|
|
|
|
* This function will initiate the download processed for the specified identifiers. If no argument is
|
|
|
|
* given, all identifiers will be initiated. Note that in order for files to be processed you need to run
|
|
|
|
* the processing function corresponding to that identifier. If you run this function without processing
|
|
|
|
* the files, it is likely nothing will be enqueued in the object and processing will complete
|
2018-07-22 08:40:15 -07:00
|
|
|
* immediately. Once all downloads are complete, this function will fire the 'complete' event on the
|
2017-11-30 00:00:06 -08:00
|
|
|
* global object instance.
|
|
|
|
*
|
2018-03-28 13:42:10 -07:00
|
|
|
* @param {Array.<{id: string, limit: number}>} identifiers Optional. The identifiers to process and corresponding parallel async task limit.
|
2017-08-26 11:43:24 -07:00
|
|
|
*/
|
2017-11-30 00:00:06 -08:00
|
|
|
processDlQueues(identifiers = [{id:'assets', limit:20}, {id:'libraries', limit:5}, {id:'files', limit:5}, {id:'forge', limit:5}]){
|
2018-07-22 08:40:15 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let shouldFire = true
|
2017-05-22 22:45:20 -07:00
|
|
|
|
2018-07-22 08:40:15 -07:00
|
|
|
// Assign dltracking variables.
|
|
|
|
this.totaldlsize = 0
|
|
|
|
this.progress = 0
|
2017-05-16 23:26:46 -07:00
|
|
|
|
2018-07-22 08:40:15 -07:00
|
|
|
for(let iden of identifiers){
|
|
|
|
this.totaldlsize += this[iden.id].dlsize
|
|
|
|
}
|
2017-05-16 23:26:46 -07:00
|
|
|
|
2018-07-22 08:40:15 -07:00
|
|
|
this.once('complete', (data) => {
|
|
|
|
resolve()
|
|
|
|
})
|
2017-11-27 01:31:54 -08:00
|
|
|
|
2018-07-22 08:40:15 -07:00
|
|
|
for(let iden of identifiers){
|
|
|
|
let r = this.startAsyncProcess(iden.id, iden.limit)
|
|
|
|
if(r) shouldFire = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if(shouldFire){
|
|
|
|
this.emit('complete', 'download')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-30 05:31:54 -07:00
|
|
|
// #endregion
|
|
|
|
|
2017-05-16 23:26:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2017-11-30 00:00:06 -08:00
|
|
|
AssetGuard,
|
2023-02-25 00:09:22 -08:00
|
|
|
JavaGuard
|
2017-05-16 23:26:46 -07:00
|
|
|
}
|