diff --git a/app/assets/js/assetguard.js b/app/assets/js/assetguard.js index 381ea84..309498e 100644 --- a/app/assets/js/assetguard.js +++ b/app/assets/js/assetguard.js @@ -33,6 +33,13 @@ const path = require('path') const Registry = require('winreg') const request = require('request') +// Constants +const PLATFORM_MAP = { + win32: '-windows-x64.tar.gz', + darwin: '-macosx-x64.tar.gz', + linux: '-linux-x64.tar.gz' +} + // Classes /** Class representing a base asset. */ @@ -514,6 +521,42 @@ class AssetGuard extends EventEmitter { // Static Java Utility // #region + /** + * @typedef OracleJREData + * @property {string} uri The base uri of the JRE. + * @property {{major: string, update: string, build: string}} version Object containing version information. + */ + + /** + * Resolves the latest version of Oracle's JRE and parses its download link. + * + * @returns {Promise.} Promise which resolved to an object containing the JRE download data. + */ + static _latestJREOracle(){ + + const url = 'http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html' + const regex = /http:\/\/.+?(?=\/java)\/java\/jdk\/([0-9]+u[0-9]+)-(b[0-9]+)\/([a-f0-9]{32})?\/jre-\1/ + + return new Promise((resolve, reject) => { + request(url, (err, resp, body) => { + if(!err){ + const arr = body.match(regex) + const verSplit = arr[1].split('u') + resolve({ + uri: arr[0], + version: { + major: verSplit[0], + update: verSplit[1], + build: arr[2] + } + }) + } else { + resolve(null) + } + }) + }) + } + /** * 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, @@ -737,7 +780,7 @@ class AssetGuard extends EventEmitter { return null } - static async validate(){ + static async validateJava(){ return await AssetGuard['_' + process.platform + 'JavaValidate']() } @@ -778,6 +821,10 @@ class AssetGuard extends EventEmitter { }) } + loadMojangLauncherData(){ + //https://launchermeta.mojang.com/mc/launcher.json + } + // Asset (Category=''') Validation Functions // #region @@ -1141,6 +1188,36 @@ class AssetGuard extends EventEmitter { */ } + // #endregion + + // Java (Category=''') Validation (download) Functions + // #region + + async _enqueueOracleJRE(dir){ + const verData = await AssetGuard._latestJREOracle() + + const combined = verData.uri + PLATFORM_MAP[process.platform] + const name = combined.substring(combined.lastIndexOf('/')+1) + const fDir = path.join(dir, name) + + const opts = { + url: combined, + headers: { + 'Cookie': 'oraclelicense=accept-securebackup-cookie' + } + } + + if(verData != null){ + const jre = new Asset(name, null, 0, opts, fDir) + this.java = new DLTracker([jre], jre.size) + return true + } else { + return false + } + } + + + // #endregion // #endregion @@ -1182,7 +1259,8 @@ class AssetGuard extends EventEmitter { req.resume() } else { req.abort() - console.log('Failed to download ' + asset.from + '. Response code', resp.statusCode) + const realFrom = typeof asset.from === 'object' ? asset.from.url : asset.from + console.log('Failed to download ' + realFrom + '. Response code', resp.statusCode) self.progress += asset.size*1 self.emit('totaldlprogress', {acc: self.progress, total: self.totaldlsize}) cb() diff --git a/app/assets/js/authmanager.js b/app/assets/js/authmanager.js index 8d1bbf8..da051d0 100644 --- a/app/assets/js/authmanager.js +++ b/app/assets/js/authmanager.js @@ -4,12 +4,12 @@ const Mojang = require('./mojang.js') exports.addAccount = async function(username, password){ try{ const session = await Mojang.authenticate(username, password, ConfigManager.getClientToken) + const ret = ConfigManager.addAuthAccount(session.selectedProfile.id, session.accessToken, username, session.selectedProfile.name) + ConfigManager.save() + return ret } catch (err){ return Promise.reject(err) } - const ret = ConfigManager.addAuthAccount(session.selectedProfile.id, session.accessToken, username, session.selectedProfile.name) - ConfigManager.save() - return ret } exports.validateSelected = async function(){ diff --git a/app/assets/js/configmanager.js b/app/assets/js/configmanager.js index 44e0d52..b789afa 100644 --- a/app/assets/js/configmanager.js +++ b/app/assets/js/configmanager.js @@ -4,6 +4,9 @@ const os = require('os') const path = require('path') const uuidV4 = require('uuid/v4') +const sysRoot = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Application Support' : '/var/local') +const dataPath = path.join(sysRoot, '.westeroscraft') + function resolveMaxRAM(){ const mem = os.totalmem() return mem >= 8000000000 ? '4G' : (mem >= 6000000000 ? '3G' : '2G') @@ -29,15 +32,13 @@ const DEFAULT_CONFIG = { ], }, game: { - directory: path.join(__dirname, '..', '..', '..', 'target', 'test', 'mcfiles'), + directory: path.join(dataPath, 'game'), resWidth: 1280, resHeight: 720, fullscreen: false, autoConnect: true }, - launcher: { - - } + launcher: {} }, clientToken: uuidV4().replace(/-/g, ''), selectedServer: null, // Resolved @@ -53,7 +54,7 @@ let config = null; * Save the current configuration to a file. */ exports.save = function(){ - const filePath = path.join(config.settings.game.directory, 'config.json') + const filePath = path.join(dataPath, 'config.json') fs.writeFileSync(filePath, JSON.stringify(config, null, 4), 'UTF-8') } @@ -65,8 +66,8 @@ exports.save = function(){ */ exports.load = function(){ // Determine the effective configuration. - const EFFECTIVE_CONFIG = config == null ? DEFAULT_CONFIG : config - const filePath = path.join(EFFECTIVE_CONFIG.settings.game.directory, 'config.json') + //const EFFECTIVE_CONFIG = config == null ? DEFAULT_CONFIG : config + const filePath = path.join(dataPath, 'config.json') if(!fs.existsSync(filePath)){ // Create all parent directories. @@ -78,6 +79,15 @@ exports.load = function(){ } } +/** + * Retrieve the absolute path of the launcher directory. + * + * @returns {string} The absolute path of the launcher directory. + */ +exports.getLauncherDirectory = function(){ + return dataPath +} + // System Settings (Unconfigurable on UI) /** diff --git a/app/assets/js/javaguard.js b/app/assets/js/javaguard.js index 89c08bf..aeb39a3 100644 --- a/app/assets/js/javaguard.js +++ b/app/assets/js/javaguard.js @@ -267,6 +267,18 @@ class JavaGuard extends EventEmitter { }) } + _headOracleJREDlSize(url){ + return new Promise((resolve, reject) => { + request.head(url, (err, resp, body) => { + if(err){ + reject(err) + } else { + resolve(resp.headers['content-length']) + } + }) + }) + } + async _downloadOracleJRE(acceptLicense, dir){ if(!acceptLicense){ diff --git a/app/assets/js/processbuilder.js b/app/assets/js/processbuilder.js index 1b17984..b4d7e73 100644 --- a/app/assets/js/processbuilder.js +++ b/app/assets/js/processbuilder.js @@ -42,7 +42,9 @@ class ProcessBuilder { console.log(args) - const child = child_process.spawn(ConfigManager.getJavaExecutable(), args) + const child = child_process.spawn(ConfigManager.getJavaExecutable(), args, { + cwd: ConfigManager.getGameDirectory() + }) child.stdout.on('data', (data) => { console.log('Minecraft:', data.toString('utf8'))