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.
This commit is contained in:
Daniel Scalzi
2018-04-15 00:00:08 -04:00
parent 5475ac0c69
commit be39d60705
7 changed files with 53 additions and 29 deletions

View File

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

View File

@@ -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.

View File

@@ -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)
/**

View File

@@ -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.')
}
})

View File

@@ -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)
}
}
}