First attempt at Java validations for darwin (osx).

pull/1/head
Daniel Scalzi 2018-04-12 16:12:45 -04:00
parent 40a02726ad
commit 4eb9d267eb
No known key found for this signature in database
GPG Key ID: 5CA2F145B63535F9
1 changed files with 97 additions and 19 deletions

View File

@ -564,18 +564,36 @@ class AssetGuard extends EventEmitter {
* installation. Supported OS's are win32, darwin, linux. * installation. Supported OS's are win32, darwin, linux.
* *
* @param {string} rootDir The root directory of the Java installation. * @param {string} rootDir The root directory of the Java installation.
* @returns {string} The path to the Java executable.
*/ */
static javaExecFromRoot(rootDir){ static javaExecFromRoot(rootDir){
if(process.platform === 'win32'){ if(process.platform === 'win32'){
return path.join(rootDir, 'bin', 'javaw.exe') return path.join(rootDir, 'bin', 'javaw.exe')
} else if(process.platform === 'darwin'){ } else if(process.platform === 'darwin'){
return path.join(rootDir, 'Contents', 'Home', 'bin', 'java') return path.join(rootDir, 'bin', 'java')
} else if(process.platform === 'linux'){ } else if(process.platform === 'linux'){
return path.join(rootDir, 'bin', 'java') return path.join(rootDir, 'bin', 'java')
} }
return rootDir return rootDir
} }
/**
* 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){
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
}
/** /**
* Load Mojang's launcher.json file. * Load Mojang's launcher.json file.
* *
@ -593,6 +611,28 @@ class AssetGuard extends EventEmitter {
}) })
} }
/**
* Validates the output of a JVM's properties. Currently validates that a JRE is x64.
*
* @param {string} stderr The output to validate.
*
* @returns {Promise.<boolean>} A promise which resolves to true if the properties are valid.
* Otherwise false.
*/
static _validateJVMProperties(stderr){
const res = stderr
const props = res.split('\n')
for(let i=0; i<props.length; i++){
if(props[i].indexOf('sun.arch.data.model') > -1){
let arch = props[i].split('=')[1].trim()
console.log(props[i].trim())
return parseInt(arch) >= 64
}
}
// sun.arch.data.model not found?
return false
}
/** /**
* Validates that a Java binary is at least 64 bit. This makes use of the non-standard * 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, * command line option -XshowSettings:properties. The output of this contains a property,
@ -611,28 +651,12 @@ class AssetGuard extends EventEmitter {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(fs.existsSync(binaryExecPath)){ if(fs.existsSync(binaryExecPath)){
child_process.exec('"' + binaryExecPath + '" -XshowSettings:properties', (err, stdout, stderr) => { child_process.exec('"' + binaryExecPath + '" -XshowSettings:properties', (err, stdout, stderr) => {
try { try {
// Output is stored in stderr? // Output is stored in stderr?
const res = stderr resolve(this._validateJVMProperties(stderr))
const props = res.split('\n')
for(let i=0; i<props.length; i++){
if(props[i].indexOf('sun.arch.data.model') > -1){
let arch = props[i].split('=')[1].trim()
console.log(props[i].trim() + ' for ' + binaryExecPath)
resolve(parseInt(arch) >= 64)
}
}
// sun.arch.data.model not found?
// Disregard this test.
resolve(true)
} catch (err){ } catch (err){
// Output format might have changed, validation cannot be completed. // Output format might have changed, validation cannot be completed.
// Disregard this test in that case. resolve(false)
resolve(true)
} }
}) })
} else { } else {
@ -642,6 +666,26 @@ class AssetGuard extends EventEmitter {
} }
/*static _validateJavaBinaryDarwin(binaryPath){
return new Promise((resolve, reject) => {
if(fs.existsSync(binaryExecPath)){
child_process.exec('export JAVA_HOME="' + binaryPath + '"; java -XshowSettings:properties', (err, stdout, stderr) => {
try {
// Output is stored in stderr?
resolve(this._validateJVMProperties(stderr))
} catch (err){
// Output format might have changed, validation cannot be completed.
resolve(false)
}
})
} else {
resolve(false)
}
})
}*/
/** /**
* Checks for the presence of the environment variable JAVA_HOME. If it exits, we will check * 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. * to see if the value points to a path which exists. If the path exits, the path is returned.
@ -839,10 +883,44 @@ class AssetGuard extends EventEmitter {
} }
/**
* 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/Contents/Home'
const res = fs.existsSync(pth)
return res ? pthRoot : null
}
/** /**
* WIP -> get a valid x64 Java path on macOS. * WIP -> get a valid x64 Java path on macOS.
*/ */
static async _darwinJavaValidate(dataDir){ static async _darwinJavaValidate(dataDir){
const pathSet = new Set()
const iPPath = AssetGuard._scanInternetPlugins()
if(iPPath != null){
pathSet.add(iPPath)
}
const jHome = AssetGuard._scanJavaHome()
if(jHome != null){
pathSet.add(jHome)
}
let pathArr = Array.from(pathSet)
for(let i=0; i<pathArr.length; i++) {
const execPath = AssetGuard.javaExecFromRoot(pathArr[i])
let res = await AssetGuard._validateJavaBinary(execPath)
if(res){
return execPath
}
}
return null return null
} }