ソースを参照

Native files are now stored in the OS temp directory.

Temp folder is deleted when minecraft is closed. If the Node.js process ends abruptly, the folder will not delete. As a dirty way to counter this, the directory we extract native files to is cleaned in the preloader. Maybe we'll come up with a more elegant solution in the future.
Daniel Scalzi 7 年 前
コミット
be39d60705

+ 1 - 1
app/assets/css/launcher.css

@@ -1350,7 +1350,7 @@ p {
 
 /*******************************************************************************
  *                                                                             *
- * Overlay View (app.ejs)                                                      *
+ * Overlay View (overlay.ejs)                                                  *
  *                                                                             *
  ******************************************************************************/
 

+ 1 - 1
app/assets/js/actionbinder.js

@@ -234,7 +234,7 @@ function asyncSystemScan(launchAfter = true){
     
     sysAEx.on('message', (m) => {
         if(m.content === 'validateJava'){
-            m.result = null
+
             if(m.result == null){
                 // If the result is null, no valid Java installation was found.
                 // Show this information to the user.

+ 10 - 0
app/assets/js/configmanager.js

@@ -100,6 +100,16 @@ exports.isFirstLaunch = function(){
     return firstLaunch
 }
 
+/**
+ * Returns the name of the folder in the OS temp directory which we
+ * will use to extract and store native dependencies for game launch.
+ * 
+ * @returns {string} The name of the folder.
+ */
+exports.getTempNativeFolder = function(){
+    return 'WCNatives'
+}
+
 // System Settings (Unconfigurable on UI)
 
 /**

+ 12 - 1
app/assets/js/preloader.js

@@ -1,6 +1,8 @@
 const {AssetGuard} = require('./assetguard.js')
 const ConfigManager = require('./configmanager.js')
+const os = require('os')
 const path = require('path')
+const rimraf = require('rimraf')
 
 console.log('Preloading')
 
@@ -15,4 +17,13 @@ if(ConfigManager.getSelectedServer() == null){
     console.log('Determining default selected server..')
     ConfigManager.setSelectedServer(AssetGuard.resolveSelectedServer(ConfigManager.getGameDirectory()))
     ConfigManager.save()
-}
+}
+
+// Clean up temp dir.
+rimraf(path.join(os.tmpdir(), ConfigManager.getTempNativeFolder()), (err) => {
+    if(err){
+        console.warn('Error while cleaning temp dir', err)
+    } else {
+        console.log('Cleaned temp dir.')
+    }
+})

+ 24 - 13
app/assets/js/processbuilder.js

@@ -6,9 +6,12 @@ const AdmZip = require('adm-zip')
 const {AssetGuard, Library} = require('./assetguard.js')
 const child_process = require('child_process')
 const ConfigManager = require('./configmanager.js')
+const crypto = require('crypto')
 const fs = require('fs')
 const mkpath = require('mkdirp')
+const os = require('os')
 const path = require('path')
+const rimraf = require('rimraf')
 const {URL} = require('url')
 
 class ProcessBuilder {
@@ -32,10 +35,11 @@ class ProcessBuilder {
      * Convienence method to run the functions typically used to build a process.
      */
     build(){
+        const tempNativePath = path.join(os.tmpdir(), ConfigManager.getTempNativeFolder(), crypto.pseudoRandomBytes(16).toString('hex'))
         process.throwDeprecation = true
         const mods = this.resolveDefaultMods()
         this.constructFMLModList(mods, true)
-        const args = this.constructJVMArguments(mods)
+        const args = this.constructJVMArguments(mods, tempNativePath)
 
         console.log(args)
 
@@ -51,6 +55,13 @@ class ProcessBuilder {
         })
         child.on('close', (code, signal) => {
             console.log('Exited with code', code)
+            rimraf(tempNativePath, (err) => {
+                if(err){
+                    console.warn('Error while deleting temp dir', err)
+                } else {
+                    console.log('Temp dir deleted successfully.')
+                }
+            })
         })
 
         return child
@@ -93,15 +104,16 @@ class ProcessBuilder {
      * Construct the argument array that will be passed to the JVM process.
      * 
      * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
+     * @param {string} tempNativePath The path to store the native libraries.
      * @returns {Array.<string>} An array containing the full JVM arguments for this process.
      */
-    constructJVMArguments(mods){
-        
+    constructJVMArguments(mods, tempNativePath){
+
         let args = ['-Xmx' + ConfigManager.getMaxRAM(),
         '-Xms' + ConfigManager.getMinRAM(),,
-        '-Djava.library.path=' + path.join(this.dir, 'natives'),
+        '-Djava.library.path=' + tempNativePath,
         '-cp',
-        this.classpathArg(mods).join(';'),
+        this.classpathArg(mods, tempNativePath).join(';'),
         this.forgeData.mainClass]
 
         // For some reason this will add an undefined value unless
@@ -196,9 +208,10 @@ class ProcessBuilder {
      * this method requires all enabled mods as an input
      * 
      * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
+     * @param {string} tempNativePath The path to store the native libraries.
      * @returns {Array.<string>} An array containing the paths of each library required by this process.
      */
-    classpathArg(mods){
+    classpathArg(mods, tempNativePath){
         let cpArgs = []
 
         // Add the version.jar to the classpath.
@@ -206,7 +219,7 @@ class ProcessBuilder {
         cpArgs.push(path.join(this.dir, 'versions', version, version + '.jar'))
 
         // Resolve the Mojang declared libraries.
-        const mojangLibs = this._resolveMojangLibraries()
+        const mojangLibs = this._resolveMojangLibraries(tempNativePath)
         cpArgs = cpArgs.concat(mojangLibs)
 
         // Resolve the server declared libraries.
@@ -222,13 +235,14 @@ class ProcessBuilder {
      * 
      * TODO - clean up function
      * 
+     * @param {string} tempNativePath The path to store the native libraries.
      * @returns {Array.<string>} An array containing the paths of each library mojang declares.
      */
-    _resolveMojangLibraries(){
+    _resolveMojangLibraries(tempNativePath){
         const libs = []
 
         const libArr = this.versionData.libraries
-        const nativePath = path.join(this.dir, 'natives')
+        mkpath.sync(tempNativePath)
         for(let i=0; i<libArr.length; i++){
             const lib = libArr[i]
             if(Library.validateRules(lib.rules)){
@@ -269,8 +283,7 @@ class ProcessBuilder {
 
                         // Extract the file.
                         if(!shouldExclude){
-                            mkpath.sync(path.join(nativePath, fileName, '..'))
-                            fs.writeFile(path.join(nativePath, fileName), zipEntries[i].getData(), (err) => {
+                            fs.writeFile(path.join(tempNativePath, fileName), zipEntries[i].getData(), (err) => {
                                 if(err){
                                     console.error('Error while extracting native library:', err)
                                 }
@@ -278,8 +291,6 @@ class ProcessBuilder {
                         }
     
                     }
-    
-                    libs.push(to)
                 }
             }
         }

+ 4 - 13
package-lock.json

@@ -222,8 +222,7 @@
     "balanced-match": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
-      "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
-      "dev": true
+      "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c="
     },
     "base64-js": {
       "version": "1.2.0",
@@ -361,7 +360,6 @@
       "version": "1.1.11",
       "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
       "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
-      "dev": true,
       "requires": {
         "balanced-match": "1.0.0",
         "concat-map": "0.0.1"
@@ -580,8 +578,7 @@
     "concat-map": {
       "version": "0.0.1",
       "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
-      "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
-      "dev": true
+      "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s="
     },
     "concat-stream": {
       "version": "1.6.0",
@@ -1409,8 +1406,7 @@
     "fs.realpath": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
-      "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=",
-      "dev": true
+      "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8="
     },
     "get-caller-file": {
       "version": "1.0.2",
@@ -1443,7 +1439,6 @@
       "version": "7.1.2",
       "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz",
       "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==",
-      "dev": true,
       "requires": {
         "fs.realpath": "1.0.0",
         "inflight": "1.0.6",
@@ -1581,7 +1576,6 @@
       "version": "1.0.6",
       "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
       "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
-      "dev": true,
       "requires": {
         "once": "1.4.0",
         "wrappy": "1.0.2"
@@ -1974,7 +1968,6 @@
       "version": "3.0.4",
       "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
       "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
-      "dev": true,
       "requires": {
         "brace-expansion": "1.1.11"
       }
@@ -2170,8 +2163,7 @@
     "path-is-absolute": {
       "version": "1.0.1",
       "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
-      "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=",
-      "dev": true
+      "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18="
     },
     "path-is-inside": {
       "version": "1.0.2",
@@ -2475,7 +2467,6 @@
       "version": "2.6.2",
       "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.6.2.tgz",
       "integrity": "sha512-lreewLK/BlghmxtfH36YYVg1i8IAce4TI7oao75I1g245+6BctqTVQiBP3YUJ9C6DQOXJmkYR9X9fCLtCOJc5w==",
-      "dev": true,
       "requires": {
         "glob": "7.1.2"
       }

+ 1 - 0
package.json

@@ -36,6 +36,7 @@
     "jquery": "^3.3.1",
     "mkdirp": "^0.5.1",
     "request-promise-native": "^1.0.5",
+    "rimraf": "^2.6.2",
     "tar-fs": "^1.16.0",
     "uuid": "^3.2.1",
     "winreg": "^1.2.4"