Эх сурвалжийг харах

First attempt at Java validations for darwin (osx).

Daniel Scalzi 7 жил өмнө
parent
commit
4eb9d267eb
1 өөрчлөгдсөн 97 нэмэгдсэн , 19 устгасан
  1. 97 19
      app/assets/js/assetguard.js

+ 97 - 19
app/assets/js/assetguard.js

@@ -564,18 +564,36 @@ class AssetGuard extends EventEmitter {
      * installation. Supported OS's are win32, darwin, linux.
      * 
      * @param {string} rootDir The root directory of the Java installation.
+     * @returns {string} The path to the Java executable.
      */
     static javaExecFromRoot(rootDir){
         if(process.platform === 'win32'){
             return path.join(rootDir, 'bin', 'javaw.exe')
         } else if(process.platform === 'darwin'){
-            return path.join(rootDir, 'Contents', 'Home', 'bin', 'java')
+            return path.join(rootDir, 'bin', 'java')
         } else if(process.platform === 'linux'){
             return path.join(rootDir, 'bin', 'java')
         }
         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.
      * 
@@ -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
      * 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) => {
             if(fs.existsSync(binaryExecPath)){
                 child_process.exec('"' + binaryExecPath + '" -XshowSettings:properties', (err, stdout, stderr) => {
-
                     try {
                         // Output is stored in 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() + ' for ' + binaryExecPath)
-                                resolve(parseInt(arch) >= 64)
-                            }
-                        }
-
-                        // sun.arch.data.model not found?
-                        // Disregard this test.
-                        resolve(true)
-
+                        resolve(this._validateJVMProperties(stderr))
                     } catch (err){
-
                         // Output format might have changed, validation cannot be completed.
-                        // Disregard this test in that case.
-                        resolve(true)
+                        resolve(false)
                     }
                 })
             } 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
      * 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.
      */
     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
     }