Game now saves to OS-specific data directory. Fixed issue where logs folder did not save to correct location. Fixed issue with authentication code. Continuing work on Java validation code.

This commit is contained in:
Daniel Scalzi 2018-03-31 13:05:05 -04:00
parent f8131d9322
commit 13cc555afd
No known key found for this signature in database
GPG Key ID: 5CA2F145B63535F9
5 changed files with 115 additions and 13 deletions

View File

@ -33,6 +33,13 @@ const path = require('path')
const Registry = require('winreg') const Registry = require('winreg')
const request = require('request') const request = require('request')
// Constants
const PLATFORM_MAP = {
win32: '-windows-x64.tar.gz',
darwin: '-macosx-x64.tar.gz',
linux: '-linux-x64.tar.gz'
}
// Classes // Classes
/** Class representing a base asset. */ /** Class representing a base asset. */
@ -514,6 +521,42 @@ class AssetGuard extends EventEmitter {
// Static Java Utility // Static Java Utility
// #region // #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.<OracleJREData>} 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 * 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,
@ -737,7 +780,7 @@ class AssetGuard extends EventEmitter {
return null return null
} }
static async validate(){ static async validateJava(){
return await AssetGuard['_' + process.platform + 'JavaValidate']() 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 // Asset (Category=''') Validation Functions
// #region // #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
// #endregion // #endregion
@ -1182,7 +1259,8 @@ class AssetGuard extends EventEmitter {
req.resume() req.resume()
} else { } else {
req.abort() 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.progress += asset.size*1
self.emit('totaldlprogress', {acc: self.progress, total: self.totaldlsize}) self.emit('totaldlprogress', {acc: self.progress, total: self.totaldlsize})
cb() cb()

View File

@ -4,12 +4,12 @@ const Mojang = require('./mojang.js')
exports.addAccount = async function(username, password){ exports.addAccount = async function(username, password){
try{ try{
const session = await Mojang.authenticate(username, password, ConfigManager.getClientToken) 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){ } catch (err){
return Promise.reject(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(){ exports.validateSelected = async function(){

View File

@ -4,6 +4,9 @@ const os = require('os')
const path = require('path') const path = require('path')
const uuidV4 = require('uuid/v4') 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(){ function resolveMaxRAM(){
const mem = os.totalmem() const mem = os.totalmem()
return mem >= 8000000000 ? '4G' : (mem >= 6000000000 ? '3G' : '2G') return mem >= 8000000000 ? '4G' : (mem >= 6000000000 ? '3G' : '2G')
@ -29,15 +32,13 @@ const DEFAULT_CONFIG = {
], ],
}, },
game: { game: {
directory: path.join(__dirname, '..', '..', '..', 'target', 'test', 'mcfiles'), directory: path.join(dataPath, 'game'),
resWidth: 1280, resWidth: 1280,
resHeight: 720, resHeight: 720,
fullscreen: false, fullscreen: false,
autoConnect: true autoConnect: true
}, },
launcher: { launcher: {}
}
}, },
clientToken: uuidV4().replace(/-/g, ''), clientToken: uuidV4().replace(/-/g, ''),
selectedServer: null, // Resolved selectedServer: null, // Resolved
@ -53,7 +54,7 @@ let config = null;
* Save the current configuration to a file. * Save the current configuration to a file.
*/ */
exports.save = function(){ 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') fs.writeFileSync(filePath, JSON.stringify(config, null, 4), 'UTF-8')
} }
@ -65,8 +66,8 @@ exports.save = function(){
*/ */
exports.load = function(){ exports.load = function(){
// Determine the effective configuration. // Determine the effective configuration.
const EFFECTIVE_CONFIG = config == null ? DEFAULT_CONFIG : config //const EFFECTIVE_CONFIG = config == null ? DEFAULT_CONFIG : config
const filePath = path.join(EFFECTIVE_CONFIG.settings.game.directory, 'config.json') const filePath = path.join(dataPath, 'config.json')
if(!fs.existsSync(filePath)){ if(!fs.existsSync(filePath)){
// Create all parent directories. // 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) // System Settings (Unconfigurable on UI)
/** /**

View File

@ -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){ async _downloadOracleJRE(acceptLicense, dir){
if(!acceptLicense){ if(!acceptLicense){

View File

@ -42,7 +42,9 @@ class ProcessBuilder {
console.log(args) 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) => { child.stdout.on('data', (data) => {
console.log('Minecraft:', data.toString('utf8')) console.log('Minecraft:', data.toString('utf8'))