2017-05-17 06:26:46 +00:00
|
|
|
/**
|
|
|
|
* AssetGuard
|
|
|
|
*
|
|
|
|
* This module aims to provide a comprehensive and stable method for processing
|
2017-11-30 08:00:06 +00:00
|
|
|
* and downloading game assets for the WesterosCraft server. Download meta is
|
|
|
|
* for several identifiers (categories) is stored inside of an AssetGuard object.
|
|
|
|
* This meta data is initially empty until one of the module's processing functions
|
|
|
|
* are called. That function will process the corresponding asset index and validate
|
|
|
|
* any exisitng local files. If a file is missing or fails validation, it will be
|
|
|
|
* placed into a download queue (array). This queue is wrapped in a download tracker object
|
2017-05-17 06:26:46 +00:00
|
|
|
* so that essential information can be cached. The download tracker object is then
|
2017-11-30 08:00:06 +00:00
|
|
|
* assigned as the value of the identifier in the AssetGuard object. These download
|
2017-05-17 06:26:46 +00:00
|
|
|
* trackers will remain idle until an async process is started to process them.
|
|
|
|
*
|
2017-11-30 08:00:06 +00:00
|
|
|
* Once the async process is started, any enqueued assets will be downloaded. The AssetGuard
|
2017-05-17 06:26:46 +00:00
|
|
|
* object will emit events throughout the download whose name correspond to the identifier
|
|
|
|
* being processed. For example, if the 'assets' identifier was being processed, whenever
|
|
|
|
* the download stream recieves data, the event 'assetsdlprogress' will be emitted off of
|
2017-11-30 08:00:06 +00:00
|
|
|
* the AssetGuard instance. This can be listened to by external modules allowing for
|
2017-05-17 06:26:46 +00:00
|
|
|
* categorical tracking of the downloading process.
|
|
|
|
*
|
|
|
|
* @module assetguard
|
|
|
|
*/
|
|
|
|
// Requirements
|
|
|
|
const AdmZip = require('adm-zip')
|
2017-11-30 06:40:56 +00:00
|
|
|
const async = require('async')
|
2017-05-18 05:49:28 +00:00
|
|
|
const child_process = require('child_process')
|
2017-11-30 06:40:56 +00:00
|
|
|
const crypto = require('crypto')
|
2017-05-18 05:49:28 +00:00
|
|
|
const EventEmitter = require('events')
|
2017-11-30 06:40:56 +00:00
|
|
|
const fs = require('fs')
|
2018-04-28 22:07:39 +00:00
|
|
|
const isDev = require('electron-is-dev')
|
2017-11-30 06:40:56 +00:00
|
|
|
const mkpath = require('mkdirp');
|
|
|
|
const path = require('path')
|
2018-03-30 12:31:54 +00:00
|
|
|
const Registry = require('winreg')
|
2017-11-30 06:40:56 +00:00
|
|
|
const request = require('request')
|
2018-04-07 22:06:49 +00:00
|
|
|
const tar = require('tar-fs')
|
|
|
|
const zlib = require('zlib')
|
2017-05-17 06:26:46 +00:00
|
|
|
|
2018-03-31 17:05:05 +00:00
|
|
|
// Constants
|
|
|
|
const PLATFORM_MAP = {
|
|
|
|
win32: '-windows-x64.tar.gz',
|
|
|
|
darwin: '-macosx-x64.tar.gz',
|
|
|
|
linux: '-linux-x64.tar.gz'
|
|
|
|
}
|
|
|
|
|
2017-05-17 06:26:46 +00:00
|
|
|
// Classes
|
|
|
|
|
|
|
|
/** Class representing a base asset. */
|
2017-11-30 06:40:56 +00:00
|
|
|
class Asset {
|
2017-05-17 06:26:46 +00:00
|
|
|
/**
|
|
|
|
* Create an asset.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {any} id The id of the asset.
|
|
|
|
* @param {string} hash The hash value of the asset.
|
|
|
|
* @param {number} size The size in bytes of the asset.
|
|
|
|
* @param {string} from The url where the asset can be found.
|
|
|
|
* @param {string} to The absolute local file path of the asset.
|
2017-05-17 06:26:46 +00:00
|
|
|
*/
|
|
|
|
constructor(id, hash, size, from, to){
|
|
|
|
this.id = id
|
|
|
|
this.hash = hash
|
|
|
|
this.size = size
|
|
|
|
this.from = from
|
|
|
|
this.to = to
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Class representing a mojang library. */
|
2017-11-30 06:40:56 +00:00
|
|
|
class Library extends Asset {
|
2017-05-17 06:26:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the process.platform OS names to match mojang's OS names.
|
|
|
|
*/
|
|
|
|
static mojangFriendlyOS(){
|
|
|
|
const opSys = process.platform
|
|
|
|
if (opSys === 'darwin') {
|
|
|
|
return 'osx';
|
|
|
|
} else if (opSys === 'win32'){
|
|
|
|
return 'windows';
|
|
|
|
} else if (opSys === 'linux'){
|
|
|
|
return 'linux';
|
|
|
|
} else {
|
|
|
|
return 'unknown_os';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether or not a library is valid for download on a particular OS, following
|
|
|
|
* the rule format specified in the mojang version data index. If the allow property has
|
|
|
|
* an OS specified, then the library can ONLY be downloaded on that OS. If the disallow
|
|
|
|
* property has instead specified an OS, the library can be downloaded on any OS EXCLUDING
|
|
|
|
* the one specified.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {Object} rules The Library's download rules.
|
|
|
|
* @returns {boolean} True if the Library follows the specified rules, otherwise false.
|
2017-05-17 06:26:46 +00:00
|
|
|
*/
|
|
|
|
static validateRules(rules){
|
|
|
|
if(rules == null) return true
|
|
|
|
|
|
|
|
let result = true
|
2018-05-07 22:15:59 +00:00
|
|
|
rules.forEach((rule) => {
|
2017-05-17 06:26:46 +00:00
|
|
|
const action = rule['action']
|
|
|
|
const osProp = rule['os']
|
|
|
|
if(action != null){
|
|
|
|
if(osProp != null){
|
|
|
|
const osName = osProp['name']
|
|
|
|
const osMoj = Library.mojangFriendlyOS()
|
|
|
|
if(action === 'allow'){
|
|
|
|
result = osName === osMoj
|
|
|
|
return
|
|
|
|
} else if(action === 'disallow'){
|
|
|
|
result = osName !== osMoj
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 22:06:48 +00:00
|
|
|
class DistroModule extends Asset {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a DistroModule. This is for processing,
|
|
|
|
* not equivalent to the module objects in the
|
|
|
|
* distro index.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {any} id The id of the asset.
|
|
|
|
* @param {string} hash The hash value of the asset.
|
|
|
|
* @param {number} size The size in bytes of the asset.
|
|
|
|
* @param {string} from The url where the asset can be found.
|
|
|
|
* @param {string} to The absolute local file path of the asset.
|
|
|
|
* @param {string} type The the module type.
|
2017-05-21 22:06:48 +00:00
|
|
|
*/
|
|
|
|
constructor(id, hash, size, from, to, type){
|
|
|
|
super(id, hash, size, from, to)
|
|
|
|
this.type = type
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-17 06:26:46 +00:00
|
|
|
/**
|
|
|
|
* Class representing a download tracker. This is used to store meta data
|
|
|
|
* about a download queue, including the queue itself.
|
|
|
|
*/
|
|
|
|
class DLTracker {
|
2017-11-30 08:00:06 +00:00
|
|
|
|
2017-05-17 06:26:46 +00:00
|
|
|
/**
|
|
|
|
* Create a DLTracker
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {Array.<Asset>} dlqueue An array containing assets queued for download.
|
|
|
|
* @param {number} dlsize The combined size of each asset in the download queue array.
|
|
|
|
* @param {function(Asset)} callback Optional callback which is called when an asset finishes downloading.
|
2017-05-17 06:26:46 +00:00
|
|
|
*/
|
2017-05-21 08:56:39 +00:00
|
|
|
constructor(dlqueue, dlsize, callback = null){
|
2017-05-17 06:26:46 +00:00
|
|
|
this.dlqueue = dlqueue
|
|
|
|
this.dlsize = dlsize
|
2017-05-21 08:56:39 +00:00
|
|
|
this.callback = callback
|
2017-05-17 06:26:46 +00:00
|
|
|
}
|
2017-11-30 08:00:06 +00:00
|
|
|
|
2017-05-17 06:26:46 +00:00
|
|
|
}
|
|
|
|
|
2017-12-02 07:59:25 +00:00
|
|
|
let distributionData = null
|
2018-05-09 00:10:46 +00:00
|
|
|
let launchWithLocal = false
|
2017-12-02 07:59:25 +00:00
|
|
|
|
2017-05-17 06:26:46 +00:00
|
|
|
/**
|
|
|
|
* Central object class used for control flow. This object stores data about
|
|
|
|
* categories of downloads. Each category is assigned an identifier with a
|
|
|
|
* DLTracker object as its value. Combined information is also stored, such as
|
|
|
|
* the total size of all the queued files in each category. This event is used
|
|
|
|
* to emit events so that external modules can listen into processing done in
|
|
|
|
* this module.
|
|
|
|
*/
|
2017-11-30 08:00:06 +00:00
|
|
|
class AssetGuard extends EventEmitter {
|
|
|
|
|
2017-05-17 06:26:46 +00:00
|
|
|
/**
|
2017-12-03 03:41:47 +00:00
|
|
|
* Create an instance of AssetGuard.
|
|
|
|
* On creation the object's properties are never-null default
|
2017-05-17 06:26:46 +00:00
|
|
|
* values. Each identifier is resolved to an empty DLTracker.
|
2017-12-03 03:41:47 +00:00
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {string} basePath The base path for asset validation (game root).
|
2018-05-07 05:34:57 +00:00
|
|
|
* @param {string} launcherPath The root launcher directory.
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {string} javaexec The path to a java executable which will be used
|
2017-12-03 03:41:47 +00:00
|
|
|
* to finalize installation.
|
2017-05-17 06:26:46 +00:00
|
|
|
*/
|
2018-05-07 05:34:57 +00:00
|
|
|
constructor(basePath, launcherPath, javaexec){
|
2017-05-17 06:26:46 +00:00
|
|
|
super()
|
2018-03-30 12:31:54 +00:00
|
|
|
this.totaldlsize = 0
|
|
|
|
this.progress = 0
|
2017-05-17 06:26:46 +00:00
|
|
|
this.assets = new DLTracker([], 0)
|
|
|
|
this.libraries = new DLTracker([], 0)
|
|
|
|
this.files = new DLTracker([], 0)
|
2017-05-17 09:03:09 +00:00
|
|
|
this.forge = new DLTracker([], 0)
|
2018-03-30 12:31:54 +00:00
|
|
|
this.java = new DLTracker([], 0)
|
2018-04-29 00:52:13 +00:00
|
|
|
this.extractQueue = []
|
2017-12-03 03:41:47 +00:00
|
|
|
this.basePath = basePath
|
2018-05-07 05:34:57 +00:00
|
|
|
this.launcherPath = launcherPath
|
2017-12-03 03:41:47 +00:00
|
|
|
this.javaexec = javaexec
|
2017-05-17 06:26:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
// Static Utility Functions
|
2018-03-30 12:31:54 +00:00
|
|
|
// #region
|
|
|
|
|
|
|
|
// Static General Resolve Functions
|
|
|
|
// #region
|
2017-05-17 06:26:46 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
/**
|
|
|
|
* Resolve an artifact id into a path. For example, on windows
|
|
|
|
* 'net.minecraftforge:forge:1.11.2-13.20.0.2282', '.jar' becomes
|
|
|
|
* net\minecraftforge\forge\1.11.2-13.20.0.2282\forge-1.11.2-13.20.0.2282.jar
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {string} artifactid The artifact id string.
|
|
|
|
* @param {string} extension The extension of the file at the resolved path.
|
|
|
|
* @returns {string} The resolved relative path from the artifact id.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
|
|
|
static _resolvePath(artifactid, extension){
|
|
|
|
let ps = artifactid.split(':')
|
|
|
|
let cs = ps[0].split('.')
|
2017-05-18 17:03:25 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
cs.push(ps[1])
|
|
|
|
cs.push(ps[2])
|
|
|
|
cs.push(ps[1].concat('-').concat(ps[2]).concat(extension))
|
2017-05-18 17:03:25 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
return path.join.apply(path, cs)
|
|
|
|
}
|
2017-05-18 17:03:25 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
/**
|
|
|
|
* Resolve an artifact id into a URL. For example,
|
|
|
|
* 'net.minecraftforge:forge:1.11.2-13.20.0.2282', '.jar' becomes
|
|
|
|
* net/minecraftforge/forge/1.11.2-13.20.0.2282/forge-1.11.2-13.20.0.2282.jar
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {string} artifactid The artifact id string.
|
|
|
|
* @param {string} extension The extension of the file at the resolved url.
|
|
|
|
* @returns {string} The resolved relative URL from the artifact id.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
|
|
|
static _resolveURL(artifactid, extension){
|
|
|
|
let ps = artifactid.split(':')
|
|
|
|
let cs = ps[0].split('.')
|
2017-05-23 05:45:20 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
cs.push(ps[1])
|
|
|
|
cs.push(ps[2])
|
|
|
|
cs.push(ps[1].concat('-').concat(ps[2]).concat(extension))
|
2017-05-23 05:45:20 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
return cs.join('/')
|
|
|
|
}
|
2017-05-23 05:45:20 +00:00
|
|
|
|
2018-03-30 12:31:54 +00:00
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// Static Hash Validation Functions
|
|
|
|
// #region
|
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
/**
|
|
|
|
* Calculates the hash for a file using the specified algorithm.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {Buffer} buf The buffer containing file data.
|
|
|
|
* @param {string} algo The hash algorithm.
|
|
|
|
* @returns {string} The calculated hash in hex.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
|
|
|
static _calculateHash(buf, algo){
|
|
|
|
return crypto.createHash(algo).update(buf).digest('hex')
|
|
|
|
}
|
2017-05-17 06:26:46 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
/**
|
|
|
|
* Used to parse a checksums file. This is specifically designed for
|
|
|
|
* the checksums.sha1 files found inside the forge scala dependencies.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {string} content The string content of the checksums file.
|
|
|
|
* @returns {Object} An object with keys being the file names, and values being the hashes.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
|
|
|
static _parseChecksumsFile(content){
|
|
|
|
let finalContent = {}
|
|
|
|
let lines = content.split('\n')
|
|
|
|
for(let i=0; i<lines.length; i++){
|
|
|
|
let bits = lines[i].split(' ')
|
|
|
|
if(bits[1] == null) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
finalContent[bits[1]] = bits[0]
|
2017-05-17 06:26:46 +00:00
|
|
|
}
|
2017-11-30 08:00:06 +00:00
|
|
|
return finalContent
|
2017-05-17 06:26:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
/**
|
|
|
|
* Validate that a file exists and matches a given hash value.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {string} filePath The path of the file to validate.
|
|
|
|
* @param {string} algo The hash algorithm to check against.
|
|
|
|
* @param {string} hash The existing hash to check against.
|
|
|
|
* @returns {boolean} True if the file exists and calculated hash matches the given hash, otherwise false.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
|
|
|
static _validateLocal(filePath, algo, hash){
|
|
|
|
if(fs.existsSync(filePath)){
|
|
|
|
//No hash provided, have to assume it's good.
|
|
|
|
if(hash == null){
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
let fileName = path.basename(filePath)
|
|
|
|
let buf = fs.readFileSync(filePath)
|
|
|
|
let calcdhash = AssetGuard._calculateHash(buf, algo)
|
|
|
|
return calcdhash === hash
|
2017-11-19 05:41:36 +00:00
|
|
|
}
|
2017-11-30 08:00:06 +00:00
|
|
|
return false;
|
2017-05-17 06:26:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 12:31:54 +00:00
|
|
|
/**
|
|
|
|
* Validates a file in the style used by forge's version index.
|
|
|
|
*
|
|
|
|
* @param {string} filePath The path of the file to validate.
|
|
|
|
* @param {Array.<string>} checksums The checksums listed in the forge version index.
|
|
|
|
* @returns {boolean} True if the file exists and the hashes match, otherwise false.
|
|
|
|
*/
|
|
|
|
static _validateForgeChecksum(filePath, checksums){
|
|
|
|
if(fs.existsSync(filePath)){
|
|
|
|
if(checksums == null || checksums.length === 0){
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
let buf = fs.readFileSync(filePath)
|
|
|
|
let calcdhash = AssetGuard._calculateHash(buf, 'sha1')
|
|
|
|
let valid = checksums.includes(calcdhash)
|
|
|
|
if(!valid && filePath.endsWith('.jar')){
|
|
|
|
valid = AssetGuard._validateForgeJar(filePath, checksums)
|
|
|
|
}
|
|
|
|
return valid
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates a forge jar file dependency who declares a checksums.sha1 file.
|
|
|
|
* This can be an expensive task as it usually requires that we calculate thousands
|
|
|
|
* of hashes.
|
|
|
|
*
|
|
|
|
* @param {Buffer} buf The buffer of the jar file.
|
|
|
|
* @param {Array.<string>} checksums The checksums listed in the forge version index.
|
|
|
|
* @returns {boolean} True if all hashes declared in the checksums.sha1 file match the actual hashes.
|
|
|
|
*/
|
|
|
|
static _validateForgeJar(buf, checksums){
|
|
|
|
// Double pass method was the quickest I found. I tried a version where we store data
|
|
|
|
// to only require a single pass, plus some quick cleanup but that seemed to take slightly more time.
|
|
|
|
|
|
|
|
const hashes = {}
|
|
|
|
let expected = {}
|
|
|
|
|
|
|
|
const zip = new AdmZip(buf)
|
|
|
|
const zipEntries = zip.getEntries()
|
|
|
|
|
|
|
|
//First pass
|
|
|
|
for(let i=0; i<zipEntries.length; i++){
|
|
|
|
let entry = zipEntries[i]
|
|
|
|
if(entry.entryName === 'checksums.sha1'){
|
|
|
|
expected = AssetGuard._parseChecksumsFile(zip.readAsText(entry))
|
|
|
|
}
|
|
|
|
hashes[entry.entryName] = AssetGuard._calculateHash(entry.getData(), 'sha1')
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!checksums.includes(hashes['checksums.sha1'])){
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check against expected
|
|
|
|
const expectedEntries = Object.keys(expected)
|
|
|
|
for(let i=0; i<expectedEntries.length; i++){
|
|
|
|
if(expected[expectedEntries[i]] !== hashes[expectedEntries[i]]){
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// Static Distribution Index Functions
|
|
|
|
// #region
|
|
|
|
|
2017-12-02 07:59:25 +00:00
|
|
|
/**
|
2018-05-08 10:34:16 +00:00
|
|
|
* Retrieve a new copy of the distribution index from our servers.
|
2017-12-02 07:59:25 +00:00
|
|
|
*
|
2018-05-07 05:34:57 +00:00
|
|
|
* @param {string} launcherPath The root launcher directory.
|
2018-03-28 20:42:10 +00:00
|
|
|
* @returns {Promise.<Object>} A promise which resolves to the distribution data object.
|
2017-12-02 07:59:25 +00:00
|
|
|
*/
|
2018-05-09 00:10:46 +00:00
|
|
|
static refreshDistributionDataRemote(launcherPath){
|
2018-05-07 22:15:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-05-08 10:34:16 +00:00
|
|
|
const distroURL = 'http://mc.westeroscraft.com/WesterosCraftLauncher/westeroscraft.json'
|
2018-05-22 12:41:22 +00:00
|
|
|
const opts = {
|
|
|
|
url: distroURL,
|
|
|
|
timeout: 2500
|
|
|
|
}
|
2018-05-08 10:34:16 +00:00
|
|
|
const distroDest = path.join(launcherPath, 'westeroscraft.json')
|
2018-05-22 12:41:22 +00:00
|
|
|
request(opts, (error, resp, body) => {
|
2018-05-08 10:34:16 +00:00
|
|
|
if(!error){
|
2018-05-07 05:34:57 +00:00
|
|
|
distributionData = JSON.parse(body)
|
2017-12-02 07:59:25 +00:00
|
|
|
|
2018-05-07 05:34:57 +00:00
|
|
|
fs.writeFile(distroDest, body, 'utf-8', (err) => {
|
|
|
|
if(!err){
|
|
|
|
resolve(distributionData)
|
|
|
|
} else {
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
})
|
2018-05-08 10:34:16 +00:00
|
|
|
} else {
|
|
|
|
reject(error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a local copy of the distribution index asynchronously.
|
|
|
|
*
|
|
|
|
* @param {string} launcherPath The root launcher directory.
|
|
|
|
* @returns {Promise.<Object>} A promise which resolves to the distribution data object.
|
|
|
|
*/
|
2018-05-09 00:10:46 +00:00
|
|
|
static refreshDistributionDataLocal(launcherPath){
|
2018-05-08 10:34:16 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-05-09 00:10:46 +00:00
|
|
|
fs.readFile(path.join(launcherPath, 'westeroscraft.json'), 'utf-8', (err, data) => {
|
|
|
|
if(!err){
|
|
|
|
distributionData = JSON.parse(data)
|
|
|
|
resolve(distributionData)
|
|
|
|
} else {
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
})
|
2017-12-02 07:59:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-08 10:34:16 +00:00
|
|
|
* Retrieve a local copy of the distribution index synchronously.
|
2017-12-02 07:59:25 +00:00
|
|
|
*
|
2018-05-07 05:34:57 +00:00
|
|
|
* @param {string} launcherPath The root launcher directory.
|
2018-03-28 20:42:10 +00:00
|
|
|
* @returns {Object} The distribution data object.
|
2017-12-02 07:59:25 +00:00
|
|
|
*/
|
2018-05-09 00:10:46 +00:00
|
|
|
static refreshDistributionDataLocalSync(launcherPath){
|
|
|
|
distributionData = JSON.parse(fs.readFileSync(path.join(launcherPath, 'westeroscraft.json'), 'utf-8'))
|
|
|
|
return distributionData
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a cached copy of the distribution index.
|
|
|
|
*/
|
|
|
|
static getDistributionData(){
|
2017-12-02 07:59:25 +00:00
|
|
|
return distributionData
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve the default selected server from the distribution index.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @returns {Object} An object resolving to the default selected server.
|
2017-12-02 07:59:25 +00:00
|
|
|
*/
|
2018-05-09 00:10:46 +00:00
|
|
|
static resolveSelectedServer(){
|
|
|
|
const distro = AssetGuard.getDistributionData()
|
2017-12-02 07:59:25 +00:00
|
|
|
const servers = distro.servers
|
|
|
|
for(let i=0; i<servers.length; i++){
|
|
|
|
if(servers[i].default_selected){
|
2018-04-16 02:35:14 +00:00
|
|
|
return servers[i]
|
2017-12-02 07:59:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If no server declares default_selected, default to the first one declared.
|
2018-04-16 02:35:14 +00:00
|
|
|
return (servers.length > 0) ? servers[0] : null
|
2017-12-02 07:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a server from the distro index which maches the provided ID.
|
|
|
|
* Returns null if the ID could not be found or the distro index has
|
|
|
|
* not yet been loaded.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {string} serverID The id of the server to retrieve.
|
|
|
|
* @returns {Object} The server object whose id matches the parameter.
|
2017-12-02 07:59:25 +00:00
|
|
|
*/
|
2018-05-09 00:10:46 +00:00
|
|
|
static getServerById(serverID){
|
|
|
|
const distro = AssetGuard.getDistributionData()
|
|
|
|
const servers = distro.servers
|
2017-12-02 07:59:25 +00:00
|
|
|
let serv = null
|
|
|
|
for(let i=0; i<servers.length; i++){
|
|
|
|
if(servers[i].id === serverID){
|
|
|
|
serv = servers[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return serv
|
|
|
|
}
|
|
|
|
|
2018-05-09 00:10:46 +00:00
|
|
|
/**
|
|
|
|
* Set whether or not we should launch with a local copy of the distribution
|
|
|
|
* index. This is useful for testing experimental changes to the distribution index.
|
|
|
|
*
|
|
|
|
* @param {boolean} value True if we should launch with a local copy. Otherwise false.
|
|
|
|
*/
|
|
|
|
static launchWithLocal(value, silent = false){
|
|
|
|
if(!silent){
|
|
|
|
if(value){
|
|
|
|
console.log('%c[AssetGuard]', 'color: #a02d2a; font-weight: bold', 'Will now launch using a local copy of the distribution index.')
|
|
|
|
console.log('%c[AssetGuard]', 'color: #a02d2a; font-weight: bold', 'Unless you are a developer, revert this change immediately.')
|
|
|
|
} else {
|
|
|
|
console.log('%c[AssetGuard]', 'color: #a02d2a; font-weight: bold', 'Will now retrieve a fresh copy of the distribution index on launch.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
launchWithLocal = value
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if AssetGuard is configured to launch with a local copy
|
|
|
|
* of the distribution index.
|
|
|
|
*
|
|
|
|
* @returns {boolean} True if launching with local, otherwise false.
|
|
|
|
*/
|
|
|
|
static isLocalLaunch(){
|
|
|
|
return launchWithLocal
|
|
|
|
}
|
|
|
|
|
2018-03-30 12:31:54 +00:00
|
|
|
// #endregion
|
2017-11-30 08:00:06 +00:00
|
|
|
|
2018-03-30 12:31:54 +00:00
|
|
|
// Miscellaneous Static Functions
|
|
|
|
// #region
|
2017-05-17 06:26:46 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
/**
|
|
|
|
* Extracts and unpacks a file from .pack.xz format.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {Array.<string>} filePaths The paths of the files to be extracted and unpacked.
|
|
|
|
* @returns {Promise.<void>} An empty promise to indicate the extraction has completed.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
2017-12-03 03:41:47 +00:00
|
|
|
static _extractPackXZ(filePaths, javaExecutable){
|
2018-05-07 22:15:59 +00:00
|
|
|
console.log('[PackXZExtract] Starting')
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-28 22:07:39 +00:00
|
|
|
let libPath
|
|
|
|
if(isDev){
|
|
|
|
libPath = path.join(process.cwd(), 'libraries', 'java', 'PackXZExtract.jar')
|
|
|
|
} else {
|
|
|
|
if(process.platform === 'darwin'){
|
|
|
|
libPath = path.join(process.cwd(),'Contents', 'Resources', 'libraries', 'java', 'PackXZExtract.jar')
|
|
|
|
} else {
|
|
|
|
libPath = path.join(process.cwd(), 'resources', 'libraries', 'java', 'PackXZExtract.jar')
|
|
|
|
}
|
|
|
|
}
|
2018-05-07 22:15:59 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
const filePath = filePaths.join(',')
|
2017-12-03 03:41:47 +00:00
|
|
|
const child = child_process.spawn(javaExecutable, ['-jar', libPath, '-packxz', filePath])
|
2017-11-30 08:00:06 +00:00
|
|
|
child.stdout.on('data', (data) => {
|
2018-05-07 22:15:59 +00:00
|
|
|
console.log('[PackXZExtract]', data.toString('utf8'))
|
2017-11-30 08:00:06 +00:00
|
|
|
})
|
|
|
|
child.stderr.on('data', (data) => {
|
2018-05-07 22:15:59 +00:00
|
|
|
console.log('[PackXZExtract]', data.toString('utf8'))
|
2017-11-30 08:00:06 +00:00
|
|
|
})
|
|
|
|
child.on('close', (code, signal) => {
|
2018-05-07 22:15:59 +00:00
|
|
|
console.log('[PackXZExtract]', 'Exited with code', code)
|
2018-05-07 05:34:57 +00:00
|
|
|
resolve()
|
2017-11-30 08:00:06 +00:00
|
|
|
})
|
2017-05-18 17:03:25 +00:00
|
|
|
})
|
2017-11-30 08:00:06 +00:00
|
|
|
}
|
2017-05-18 05:49:28 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
/**
|
|
|
|
* Function which finalizes the forge installation process. This creates a 'version'
|
|
|
|
* instance for forge and saves its version.json file into that instance. If that
|
|
|
|
* instance already exists, the contents of the version.json file are read and returned
|
|
|
|
* in a promise.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {Asset} asset The Asset object representing Forge.
|
|
|
|
* @param {string} basePath Base path for asset validation (game root).
|
|
|
|
* @returns {Promise.<Object>} A promise which resolves to the contents of forge's version.json.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
|
|
|
static _finalizeForgeAsset(asset, basePath){
|
2018-05-07 22:15:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-11-30 08:00:06 +00:00
|
|
|
fs.readFile(asset.to, (err, data) => {
|
|
|
|
const zip = new AdmZip(data)
|
|
|
|
const zipEntries = zip.getEntries()
|
|
|
|
|
|
|
|
for(let i=0; i<zipEntries.length; i++){
|
|
|
|
if(zipEntries[i].entryName === 'version.json'){
|
|
|
|
const forgeVersion = JSON.parse(zip.readAsText(zipEntries[i]))
|
|
|
|
const versionPath = path.join(basePath, 'versions', forgeVersion.id)
|
|
|
|
const versionFile = path.join(versionPath, forgeVersion.id + '.json')
|
|
|
|
if(!fs.existsSync(versionFile)){
|
|
|
|
mkpath.sync(versionPath)
|
|
|
|
fs.writeFileSync(path.join(versionPath, forgeVersion.id + '.json'), zipEntries[i].getData())
|
2018-05-07 05:34:57 +00:00
|
|
|
resolve(forgeVersion)
|
2017-11-30 08:00:06 +00:00
|
|
|
} else {
|
|
|
|
//Read the saved file to allow for user modifications.
|
2018-05-07 05:34:57 +00:00
|
|
|
resolve(JSON.parse(fs.readFileSync(versionFile, 'utf-8')))
|
2017-11-30 08:00:06 +00:00
|
|
|
}
|
|
|
|
return
|
2017-05-21 22:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-30 08:00:06 +00:00
|
|
|
//We didn't find forge's version.json.
|
|
|
|
reject('Unable to finalize Forge processing, version.json not found! Has forge changed their format?')
|
|
|
|
})
|
2017-05-21 22:06:48 +00:00
|
|
|
})
|
2017-11-30 08:00:06 +00:00
|
|
|
}
|
2017-05-21 22:06:48 +00:00
|
|
|
|
2018-03-30 12:31:54 +00:00
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// Static Java Utility
|
|
|
|
// #region
|
|
|
|
|
2018-03-31 17:05:05 +00:00
|
|
|
/**
|
|
|
|
* @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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-07 22:06:49 +00:00
|
|
|
/**
|
|
|
|
* Returns the path of the OS-specific executable for the given Java
|
|
|
|
* installation. Supported OS's are win32, darwin, linux.
|
|
|
|
*
|
|
|
|
* @param {string} rootDir The root directory of the Java installation.
|
2018-04-12 20:12:45 +00:00
|
|
|
* @returns {string} The path to the Java executable.
|
2018-04-07 22:06:49 +00:00
|
|
|
*/
|
|
|
|
static javaExecFromRoot(rootDir){
|
|
|
|
if(process.platform === 'win32'){
|
|
|
|
return path.join(rootDir, 'bin', 'javaw.exe')
|
|
|
|
} else if(process.platform === 'darwin'){
|
2018-04-13 02:13:26 +00:00
|
|
|
return path.join(rootDir, 'Contents', 'Home', 'bin', 'java')
|
2018-04-07 22:06:49 +00:00
|
|
|
} else if(process.platform === 'linux'){
|
|
|
|
return path.join(rootDir, 'bin', 'java')
|
|
|
|
}
|
|
|
|
return rootDir
|
|
|
|
}
|
|
|
|
|
2018-04-12 20:12:45 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
}
|
|
|
|
|
2018-04-01 00:45:24 +00:00
|
|
|
/**
|
|
|
|
* Load Mojang's launcher.json file.
|
|
|
|
*
|
|
|
|
* @returns {Promise.<Object>} Promise which resolves to Mojang's launcher.json object.
|
|
|
|
*/
|
|
|
|
static loadMojangLauncherData(){
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request.get('https://launchermeta.mojang.com/mc/launcher.json', (err, resp, body) => {
|
|
|
|
if(err){
|
|
|
|
resolve(null)
|
|
|
|
} else {
|
|
|
|
resolve(JSON.parse(body))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-12 20:12:45 +00:00
|
|
|
/**
|
2018-04-13 01:38:27 +00:00
|
|
|
* Parses a **full** Java Runtime version string and resolves
|
|
|
|
* the version information. Uses Java 8 formatting.
|
|
|
|
*
|
|
|
|
* @param {string} verString Full version string to parse.
|
|
|
|
* @returns Object containing the version information.
|
|
|
|
*/
|
|
|
|
static parseJavaRuntimeVersion(verString){
|
|
|
|
// 1.{major}.0_{update}-b{build}
|
|
|
|
// ex. 1.8.0_152-b16
|
|
|
|
const ret = {}
|
|
|
|
let pts = verString.split('-')
|
|
|
|
ret.build = parseInt(pts[1].substring(1))
|
2018-04-13 02:40:48 +00:00
|
|
|
pts = pts[0].split('_')
|
2018-04-13 01:38:27 +00:00
|
|
|
ret.update = parseInt(pts[1])
|
2018-04-13 02:40:48 +00:00
|
|
|
ret.major = parseInt(pts[0].split('.')[1])
|
2018-04-13 01:38:27 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates the output of a JVM's properties. Currently validates that a JRE is x64
|
|
|
|
* and that the major = 8, update > 52.
|
2018-04-12 20:12:45 +00:00
|
|
|
*
|
|
|
|
* @param {string} stderr The output to validate.
|
|
|
|
*
|
2018-05-15 05:05:10 +00:00
|
|
|
* @returns {Promise.<Object>} A promise which resolves to a meta object about the JVM.
|
|
|
|
* The validity is stored inside the `valid` property.
|
2018-04-12 20:12:45 +00:00
|
|
|
*/
|
|
|
|
static _validateJVMProperties(stderr){
|
|
|
|
const res = stderr
|
|
|
|
const props = res.split('\n')
|
2018-04-13 01:38:27 +00:00
|
|
|
|
|
|
|
const goal = 2
|
|
|
|
let checksum = 0
|
|
|
|
|
2018-05-15 05:05:10 +00:00
|
|
|
const meta = {}
|
|
|
|
|
2018-04-12 20:12:45 +00:00
|
|
|
for(let i=0; i<props.length; i++){
|
|
|
|
if(props[i].indexOf('sun.arch.data.model') > -1){
|
|
|
|
let arch = props[i].split('=')[1].trim()
|
2018-05-15 05:05:10 +00:00
|
|
|
arch = parseInt(arch)
|
2018-04-12 20:12:45 +00:00
|
|
|
console.log(props[i].trim())
|
2018-05-15 05:05:10 +00:00
|
|
|
if(arch === 64){
|
|
|
|
meta.arch = arch
|
2018-04-13 01:38:27 +00:00
|
|
|
++checksum
|
|
|
|
if(checksum === goal){
|
2018-05-15 05:05:10 +00:00
|
|
|
break
|
2018-04-13 01:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if(props[i].indexOf('java.runtime.version') > -1){
|
|
|
|
let verString = props[i].split('=')[1].trim()
|
|
|
|
console.log(props[i].trim())
|
|
|
|
const verOb = AssetGuard.parseJavaRuntimeVersion(verString)
|
|
|
|
if(verOb.major === 8 && verOb.update > 52){
|
2018-05-15 05:05:10 +00:00
|
|
|
meta.version = verOb
|
2018-04-13 01:38:27 +00:00
|
|
|
++checksum
|
|
|
|
if(checksum === goal){
|
2018-05-15 05:05:10 +00:00
|
|
|
break
|
2018-04-13 01:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-12 20:12:45 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-15 05:05:10 +00:00
|
|
|
|
|
|
|
meta.valid = checksum === goal
|
2018-04-13 01:38:27 +00:00
|
|
|
|
2018-05-15 05:05:10 +00:00
|
|
|
return meta
|
2018-04-12 20:12:45 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
/**
|
2018-03-30 12:31:54 +00:00
|
|
|
* 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,
|
|
|
|
* sun.arch.data.model = ARCH, in which ARCH is either 32 or 64. This option is supported
|
|
|
|
* in Java 8 and 9. Since this is a non-standard option. This will resolve to true if
|
|
|
|
* the function's code throws errors. That would indicate that the option is changed or
|
|
|
|
* removed.
|
2017-11-30 08:00:06 +00:00
|
|
|
*
|
2018-04-07 22:06:49 +00:00
|
|
|
* @param {string} binaryExecPath Path to the java executable we wish to validate.
|
2018-03-30 12:31:54 +00:00
|
|
|
*
|
2018-05-15 05:05:10 +00:00
|
|
|
* @returns {Promise.<Object>} A promise which resolves to a meta object about the JVM.
|
|
|
|
* The validity is stored inside the `valid` property.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
2018-04-07 22:06:49 +00:00
|
|
|
static _validateJavaBinary(binaryExecPath){
|
2018-03-30 12:31:54 +00:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-04-07 22:06:49 +00:00
|
|
|
if(fs.existsSync(binaryExecPath)){
|
|
|
|
child_process.exec('"' + binaryExecPath + '" -XshowSettings:properties', (err, stdout, stderr) => {
|
2018-03-30 12:31:54 +00:00
|
|
|
try {
|
|
|
|
// Output is stored in stderr?
|
2018-04-12 20:12:45 +00:00
|
|
|
resolve(this._validateJVMProperties(stderr))
|
2018-03-30 12:31:54 +00:00
|
|
|
} catch (err){
|
|
|
|
// Output format might have changed, validation cannot be completed.
|
2018-05-15 05:05:10 +00:00
|
|
|
resolve({valid: false})
|
2018-03-30 12:31:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
2018-05-15 05:05:10 +00:00
|
|
|
resolve({valid: false})
|
2018-03-30 12:31:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @returns {string} The path defined by JAVA_HOME, if it exists. Otherwise null.
|
|
|
|
*/
|
|
|
|
static _scanJavaHome(){
|
|
|
|
const jHome = process.env.JAVA_HOME
|
|
|
|
try {
|
|
|
|
let res = fs.existsSync(jHome)
|
|
|
|
return res ? jHome : null
|
|
|
|
} catch (err) {
|
|
|
|
// Malformed JAVA_HOME property.
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scans the registry for 64-bit Java entries. The paths of each entry are added to
|
|
|
|
* a set and returned. Currently, only Java 8 (1.8) is supported.
|
|
|
|
*
|
|
|
|
* @returns {Promise.<Set.<string>>} A promise which resolves to a set of 64-bit Java root
|
|
|
|
* paths found in the registry.
|
|
|
|
*/
|
|
|
|
static _scanRegistry(){
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Keys for Java v9.0.0 and later:
|
|
|
|
// 'SOFTWARE\\JavaSoft\\JRE'
|
|
|
|
// 'SOFTWARE\\JavaSoft\\JDK'
|
|
|
|
// Forge does not yet support Java 9, therefore we do not.
|
|
|
|
|
|
|
|
// Keys for Java 1.8 and prior:
|
|
|
|
const regKeys = [
|
|
|
|
'\\SOFTWARE\\JavaSoft\\Java Runtime Environment',
|
|
|
|
'\\SOFTWARE\\JavaSoft\\Java Development Kit'
|
|
|
|
]
|
|
|
|
|
2018-05-10 08:36:52 +00:00
|
|
|
let keysDone = 0
|
|
|
|
|
2018-03-30 12:31:54 +00:00
|
|
|
const candidates = new Set()
|
|
|
|
|
|
|
|
for(let i=0; i<regKeys.length; i++){
|
|
|
|
const key = new Registry({
|
|
|
|
hive: Registry.HKLM,
|
|
|
|
key: regKeys[i],
|
|
|
|
arch: 'x64'
|
|
|
|
})
|
|
|
|
key.keyExists((err, exists) => {
|
|
|
|
if(exists) {
|
|
|
|
key.keys((err, javaVers) => {
|
|
|
|
if(err){
|
2018-05-10 08:36:52 +00:00
|
|
|
keysDone++
|
2018-03-30 12:31:54 +00:00
|
|
|
console.error(err)
|
2018-05-10 08:36:52 +00:00
|
|
|
|
|
|
|
// REG KEY DONE
|
|
|
|
// DUE TO ERROR
|
|
|
|
if(keysDone === regKeys.length){
|
2018-03-30 12:31:54 +00:00
|
|
|
resolve(candidates)
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-10 08:36:52 +00:00
|
|
|
if(javaVers.length === 0){
|
|
|
|
// REG KEY DONE
|
|
|
|
// NO SUBKEYS
|
|
|
|
keysDone++
|
|
|
|
if(keysDone === regKeys.length){
|
|
|
|
resolve(candidates)
|
|
|
|
}
|
2018-03-30 12:31:54 +00:00
|
|
|
} else {
|
2018-05-10 08:36:52 +00:00
|
|
|
|
|
|
|
let numDone = 0
|
|
|
|
|
2018-03-30 12:31:54 +00:00
|
|
|
for(let j=0; j<javaVers.length; j++){
|
|
|
|
const javaVer = javaVers[j]
|
|
|
|
const vKey = javaVer.key.substring(javaVer.key.lastIndexOf('\\')+1)
|
|
|
|
// Only Java 8 is supported currently.
|
|
|
|
if(parseFloat(vKey) === 1.8){
|
|
|
|
javaVer.get('JavaHome', (err, res) => {
|
|
|
|
const jHome = res.value
|
|
|
|
if(jHome.indexOf('(x86)') === -1){
|
|
|
|
candidates.add(jHome)
|
|
|
|
}
|
2018-05-10 08:36:52 +00:00
|
|
|
|
|
|
|
// SUBKEY DONE
|
|
|
|
|
|
|
|
numDone++
|
|
|
|
if(numDone === javaVers.length){
|
|
|
|
keysDone++
|
|
|
|
if(keysDone === regKeys.length){
|
|
|
|
resolve(candidates)
|
|
|
|
}
|
2018-03-30 12:31:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
2018-05-10 08:36:52 +00:00
|
|
|
|
|
|
|
// SUBKEY DONE
|
|
|
|
// NOT JAVA 8
|
|
|
|
|
|
|
|
numDone++
|
|
|
|
if(numDone === javaVers.length){
|
|
|
|
keysDone++
|
|
|
|
if(keysDone === regKeys.length){
|
|
|
|
resolve(candidates)
|
|
|
|
}
|
2018-03-30 12:31:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-30 08:00:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
2018-05-10 08:36:52 +00:00
|
|
|
|
|
|
|
// REG KEY DONE
|
|
|
|
// DUE TO NON-EXISTANCE
|
|
|
|
|
|
|
|
keysDone++
|
|
|
|
if(keysDone === regKeys.length){
|
2018-03-30 12:31:54 +00:00
|
|
|
resolve(candidates)
|
|
|
|
}
|
2017-11-30 08:00:06 +00:00
|
|
|
}
|
|
|
|
})
|
2018-03-30 12:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-15 05:05:10 +00:00
|
|
|
/**
|
|
|
|
* 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'
|
|
|
|
const res = fs.existsSync(AssetGuard.javaExecFromRoot(pth))
|
|
|
|
return res ? pth : null
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scan a directory for root JVM folders.
|
|
|
|
*
|
|
|
|
* @param {string} scanDir The directory to scan.
|
|
|
|
* @returns {Promise.<Set.<string>>} A promise which resolves to a set of the discovered
|
|
|
|
* root JVM folders.
|
|
|
|
*/
|
|
|
|
static _scanFileSystem(scanDir){
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
fs.exists(scanDir, (e) => {
|
|
|
|
|
|
|
|
let res = new Set()
|
|
|
|
|
|
|
|
if(e){
|
|
|
|
fs.readdir(scanDir, (err, files) => {
|
|
|
|
if(err){
|
|
|
|
resolve(res)
|
|
|
|
console.log(err)
|
|
|
|
} else {
|
|
|
|
let pathsDone = 0
|
|
|
|
|
|
|
|
for(let i=0; i<files.length; i++){
|
|
|
|
|
|
|
|
const combinedPath = path.join(scanDir, files[i])
|
|
|
|
const execPath = AssetGuard.javaExecFromRoot(combinedPath)
|
|
|
|
|
|
|
|
fs.exists(execPath, (v) => {
|
|
|
|
|
|
|
|
if(v){
|
|
|
|
res.add(combinedPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
++pathsDone
|
|
|
|
|
|
|
|
if(pathsDone === files.length){
|
|
|
|
resolve(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if(pathsDone === files.length){
|
|
|
|
resolve(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
resolve(res)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Set.<string>} rootSet A set of JVM root strings to validate.
|
|
|
|
* @returns {Promise.<Object[]>} A promise which resolves to an array of meta objects
|
|
|
|
* for each valid JVM root directory.
|
|
|
|
*/
|
|
|
|
static async _validateJavaRootSet(rootSet){
|
|
|
|
|
|
|
|
const rootArr = Array.from(rootSet)
|
|
|
|
const validArr = []
|
|
|
|
|
|
|
|
for(let i=0; i<rootArr.length; i++){
|
|
|
|
|
|
|
|
const execPath = AssetGuard.javaExecFromRoot(rootArr[i])
|
|
|
|
const metaOb = await AssetGuard._validateJavaBinary(execPath)
|
|
|
|
|
|
|
|
if(metaOb.valid){
|
|
|
|
metaOb.execPath = execPath
|
|
|
|
validArr.push(metaOb)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return validArr
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort an array of JVM meta objects. Best candidates are placed before all others.
|
|
|
|
* Sorts based on version and gives priority to JREs over JDKs if versions match.
|
|
|
|
*
|
|
|
|
* @param {Object[]} validArr An array of JVM meta objects.
|
|
|
|
* @returns {Object[]} A sorted array of JVM meta objects.
|
|
|
|
*/
|
|
|
|
static _sortValidJavaArray(validArr){
|
|
|
|
const retArr = validArr.sort((a, b) => {
|
|
|
|
// Note that Java 9+ uses semver and that will need to be accounted for in
|
|
|
|
// the future.
|
|
|
|
|
|
|
|
if(a.version.major === b.version.major){
|
|
|
|
|
|
|
|
if(a.version.update === b.version.update){
|
|
|
|
|
|
|
|
if(a.version.build === b.version.build){
|
|
|
|
|
|
|
|
// Same version, give priority to JRE.
|
|
|
|
|
|
|
|
if(a.execPath.toLowerCase().indexOf('jdk') > -1){
|
2018-05-15 06:30:26 +00:00
|
|
|
return b.execPath.toLowerCase().indexOf('jdk') > -1 ? 0 : 1
|
2018-05-15 05:05:10 +00:00
|
|
|
} else {
|
2018-05-15 06:30:26 +00:00
|
|
|
return -1
|
2018-05-15 05:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
2018-05-15 06:30:26 +00:00
|
|
|
return a.version.build > b.version.build ? -1 : 1
|
2018-05-15 05:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2018-05-15 06:30:26 +00:00
|
|
|
return a.version.update > b.version.update ? -1 : 1
|
2018-05-15 05:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2018-05-15 06:30:26 +00:00
|
|
|
return a.version.major > b.version.major ? -1 : 1
|
2018-05-15 05:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
return retArr
|
|
|
|
}
|
|
|
|
|
2018-03-30 12:31:54 +00:00
|
|
|
/**
|
|
|
|
* Attempts to find a valid x64 installation of Java on Windows machines.
|
|
|
|
* Possible paths will be pulled from the registry and the JAVA_HOME environment
|
|
|
|
* variable. The paths will be sorted with higher versions preceeding lower, and
|
|
|
|
* JREs preceeding JDKs. The binaries at the sorted paths will then be validated.
|
|
|
|
* The first validated is returned.
|
|
|
|
*
|
|
|
|
* Higher versions > Lower versions
|
|
|
|
* If versions are equal, JRE > JDK.
|
|
|
|
*
|
2018-04-02 22:40:32 +00:00
|
|
|
* @param {string} dataDir The base launcher directory.
|
2018-04-07 22:06:49 +00:00
|
|
|
* @returns {Promise.<string>} A Promise which resolves to the executable path of a valid
|
2018-04-01 00:45:24 +00:00
|
|
|
* x64 Java installation. If none are found, null is returned.
|
2018-03-30 12:31:54 +00:00
|
|
|
*/
|
2018-04-02 22:40:32 +00:00
|
|
|
static async _win32JavaValidate(dataDir){
|
2018-03-30 12:31:54 +00:00
|
|
|
|
|
|
|
// Get possible paths from the registry.
|
2018-05-15 05:05:10 +00:00
|
|
|
let pathSet1 = await AssetGuard._scanRegistry()
|
|
|
|
if(pathSet1.length === 0){
|
|
|
|
// Do a manual file system scan of program files.
|
|
|
|
pathSet1 = AssetGuard._scanFileSystem('C:\\Program Files\\Java')
|
|
|
|
}
|
2018-03-30 12:31:54 +00:00
|
|
|
|
2018-04-02 22:40:32 +00:00
|
|
|
// Get possible paths from the data directory.
|
2018-05-15 05:05:10 +00:00
|
|
|
const pathSet2 = await AssetGuard._scanFileSystem(path.join(dataDir, 'runtime', 'x64'))
|
2018-04-02 22:40:32 +00:00
|
|
|
|
2018-05-15 05:05:10 +00:00
|
|
|
// Merge the results.
|
|
|
|
const uberSet = new Set([...pathSet1, ...pathSet2])
|
|
|
|
|
|
|
|
// Validate JAVA_HOME.
|
2018-03-30 12:31:54 +00:00
|
|
|
const jHome = AssetGuard._scanJavaHome()
|
|
|
|
if(jHome != null && jHome.indexOf('(x86)') === -1){
|
2018-05-15 05:05:10 +00:00
|
|
|
uberSet.add(jHome)
|
2018-03-30 12:31:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 05:05:10 +00:00
|
|
|
let pathArr = await AssetGuard._validateJavaRootSet(uberSet)
|
|
|
|
pathArr = AssetGuard._sortValidJavaArray(pathArr)
|
2018-03-30 12:31:54 +00:00
|
|
|
|
2018-05-15 05:05:10 +00:00
|
|
|
if(pathArr.length > 0){
|
|
|
|
return pathArr[0].execPath
|
|
|
|
} else {
|
|
|
|
return null
|
2017-11-30 08:00:06 +00:00
|
|
|
}
|
2018-03-30 12:31:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-12 20:12:45 +00:00
|
|
|
/**
|
2018-05-15 05:05:10 +00:00
|
|
|
* Attempts to find a valid x64 installation of Java on MacOS.
|
|
|
|
* The system JVM directory is scanned for possible installations.
|
|
|
|
* The JAVA_HOME enviroment variable and internet plugins directory
|
|
|
|
* are also scanned and validated.
|
2018-04-12 20:12:45 +00:00
|
|
|
*
|
2018-05-15 05:05:10 +00:00
|
|
|
* Higher versions > Lower versions
|
|
|
|
* If versions are equal, JRE > JDK.
|
|
|
|
*
|
|
|
|
* @param {string} dataDir The base launcher directory.
|
|
|
|
* @returns {Promise.<string>} A Promise which resolves to the executable path of a valid
|
|
|
|
* x64 Java installation. If none are found, null is returned.
|
2018-03-30 12:31:54 +00:00
|
|
|
*/
|
2018-04-02 22:40:32 +00:00
|
|
|
static async _darwinJavaValidate(dataDir){
|
2018-04-12 20:12:45 +00:00
|
|
|
|
2018-05-15 05:05:10 +00:00
|
|
|
const pathSet1 = await AssetGuard._scanFileSystem('/Library/Java/JavaVirtualMachines')
|
|
|
|
const pathSet2 = await AssetGuard._scanFileSystem(path.join(dataDir, 'runtime', 'x64'))
|
|
|
|
|
|
|
|
const uberSet = new Set([...pathSet1, ...pathSet2])
|
2018-04-12 20:12:45 +00:00
|
|
|
|
2018-04-13 01:38:27 +00:00
|
|
|
// Check Internet Plugins folder.
|
2018-04-12 20:12:45 +00:00
|
|
|
const iPPath = AssetGuard._scanInternetPlugins()
|
|
|
|
if(iPPath != null){
|
2018-05-15 05:05:10 +00:00
|
|
|
uberSet.add(iPPath)
|
2018-04-12 20:12:45 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 01:38:27 +00:00
|
|
|
// Check the JAVA_HOME environment variable.
|
2018-04-12 20:12:45 +00:00
|
|
|
const jHome = AssetGuard._scanJavaHome()
|
|
|
|
if(jHome != null){
|
2018-04-13 02:13:26 +00:00
|
|
|
// Ensure we are at the absolute root.
|
|
|
|
if(jHome.contains('/Contents/Home')){
|
|
|
|
jHome = jHome.substring(0, jHome.indexOf('/Contents/Home'))
|
|
|
|
}
|
2018-05-15 05:05:10 +00:00
|
|
|
uberSet.add(jHome)
|
2018-04-12 20:12:45 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 05:05:10 +00:00
|
|
|
let pathArr = await AssetGuard._validateJavaRootSet(uberSet)
|
|
|
|
pathArr = AssetGuard._sortValidJavaArray(pathArr)
|
2018-04-13 01:38:27 +00:00
|
|
|
|
2018-05-15 05:05:10 +00:00
|
|
|
if(pathArr.length > 0){
|
|
|
|
return pathArr[0].execPath
|
|
|
|
} else {
|
|
|
|
return null
|
2018-04-12 20:12:45 +00:00
|
|
|
}
|
2017-11-30 08:00:06 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 12:31:54 +00:00
|
|
|
/**
|
2018-05-15 05:05:10 +00:00
|
|
|
* Attempts to find a valid x64 installation of Java on Linux.
|
|
|
|
* The system JVM directory is scanned for possible installations.
|
|
|
|
* The JAVA_HOME enviroment variable is also scanned and validated.
|
|
|
|
*
|
|
|
|
* Higher versions > Lower versions
|
|
|
|
* If versions are equal, JRE > JDK.
|
|
|
|
*
|
|
|
|
* @param {string} dataDir The base launcher directory.
|
|
|
|
* @returns {Promise.<string>} A Promise which resolves to the executable path of a valid
|
|
|
|
* x64 Java installation. If none are found, null is returned.
|
2018-03-30 12:31:54 +00:00
|
|
|
*/
|
2018-04-02 22:40:32 +00:00
|
|
|
static async _linuxJavaValidate(dataDir){
|
2018-05-15 05:05:10 +00:00
|
|
|
|
|
|
|
const pathSet1 = await AssetGuard._scanFileSystem('/usr/lib/jvm')
|
|
|
|
const pathSet2 = await AssetGuard._scanFileSystem(path.join(dataDir, 'runtime', 'x64'))
|
|
|
|
|
|
|
|
const uberSet = new Set([...pathSet1, ...pathSet2])
|
|
|
|
|
|
|
|
// Validate JAVA_HOME
|
|
|
|
const jHome = AssetGuard._scanJavaHome()
|
|
|
|
if(jHome != null){
|
|
|
|
uberSet.add(jHome)
|
|
|
|
}
|
|
|
|
|
|
|
|
let pathArr = await AssetGuard._validateJavaRootSet(uberSet)
|
|
|
|
pathArr = AssetGuard._sortValidJavaArray(pathArr)
|
|
|
|
|
|
|
|
if(pathArr.length > 0){
|
|
|
|
return pathArr[0].execPath
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
2018-03-30 12:31:54 +00:00
|
|
|
}
|
|
|
|
|
2018-04-01 00:45:24 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the path of a valid x64 Java installation.
|
|
|
|
*
|
2018-04-02 22:40:32 +00:00
|
|
|
* @param {string} dataDir The base launcher directory.
|
2018-04-01 00:45:24 +00:00
|
|
|
* @returns {string} A path to a valid x64 Java installation, null if none found.
|
|
|
|
*/
|
2018-04-02 22:40:32 +00:00
|
|
|
static async validateJava(dataDir){
|
|
|
|
return await AssetGuard['_' + process.platform + 'JavaValidate'](dataDir)
|
2018-03-30 12:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
// Validation Functions
|
2018-03-30 12:31:54 +00:00
|
|
|
// #region
|
2017-11-30 08:00:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the version data for a given minecraft version.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {string} version The game version for which to load the index data.
|
|
|
|
* @param {boolean} force Optional. If true, the version index will be downloaded even if it exists locally. Defaults to false.
|
|
|
|
* @returns {Promise.<Object>} Promise which resolves to the version data object.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
2017-12-03 03:41:47 +00:00
|
|
|
loadVersionData(version, force = false){
|
|
|
|
const self = this
|
2018-04-01 00:45:24 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-11-30 08:00:06 +00:00
|
|
|
const name = version + '.json'
|
|
|
|
const url = 'https://s3.amazonaws.com/Minecraft.Download/versions/' + version + '/' + name
|
2017-12-03 03:41:47 +00:00
|
|
|
const versionPath = path.join(self.basePath, 'versions', version)
|
2017-11-30 08:00:06 +00:00
|
|
|
const versionFile = path.join(versionPath, name)
|
|
|
|
if(!fs.existsSync(versionFile) || force){
|
|
|
|
//This download will never be tracked as it's essential and trivial.
|
2018-04-01 00:45:24 +00:00
|
|
|
console.log('Preparing download of ' + version + ' assets.')
|
|
|
|
mkpath.sync(versionPath)
|
|
|
|
const stream = request(url).pipe(fs.createWriteStream(versionFile))
|
|
|
|
stream.on('finish', () => {
|
|
|
|
resolve(JSON.parse(fs.readFileSync(versionFile)))
|
2017-11-30 08:00:06 +00:00
|
|
|
})
|
2017-05-17 06:26:46 +00:00
|
|
|
} else {
|
2018-04-01 00:45:24 +00:00
|
|
|
resolve(JSON.parse(fs.readFileSync(versionFile)))
|
2017-05-17 06:26:46 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-30 12:31:54 +00:00
|
|
|
|
|
|
|
// Asset (Category=''') Validation Functions
|
|
|
|
// #region
|
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
/**
|
|
|
|
* Public asset validation function. This function will handle the validation of assets.
|
|
|
|
* It will parse the asset index specified in the version data, analyzing each
|
|
|
|
* asset entry. In this analysis it will check to see if the local file exists and is valid.
|
|
|
|
* If not, it will be added to the download queue for the 'assets' identifier.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {Object} versionData The version data for the assets.
|
|
|
|
* @param {boolean} force Optional. If true, the asset index will be downloaded even if it exists locally. Defaults to false.
|
|
|
|
* @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
2017-12-03 03:41:47 +00:00
|
|
|
validateAssets(versionData, force = false){
|
2017-11-30 08:00:06 +00:00
|
|
|
const self = this
|
2018-05-07 22:15:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-03 03:41:47 +00:00
|
|
|
self._assetChainIndexData(versionData, force).then(() => {
|
2018-05-07 22:15:59 +00:00
|
|
|
resolve()
|
2017-05-17 06:26:46 +00:00
|
|
|
})
|
|
|
|
})
|
2017-11-30 08:00:06 +00:00
|
|
|
}
|
2017-05-17 06:26:46 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
//Chain the asset tasks to provide full async. The below functions are private.
|
|
|
|
/**
|
|
|
|
* Private function used to chain the asset validation process. This function retrieves
|
|
|
|
* the index data.
|
|
|
|
* @param {Object} versionData
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {boolean} force
|
|
|
|
* @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
2017-12-03 03:41:47 +00:00
|
|
|
_assetChainIndexData(versionData, force = false){
|
2017-11-30 08:00:06 +00:00
|
|
|
const self = this
|
2018-05-07 22:15:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-11-30 08:00:06 +00:00
|
|
|
//Asset index constants.
|
|
|
|
const assetIndex = versionData.assetIndex
|
|
|
|
const name = assetIndex.id + '.json'
|
2017-12-03 03:41:47 +00:00
|
|
|
const indexPath = path.join(self.basePath, 'assets', 'indexes')
|
2017-11-30 08:00:06 +00:00
|
|
|
const assetIndexLoc = path.join(indexPath, name)
|
|
|
|
|
|
|
|
let data = null
|
|
|
|
if(!fs.existsSync(assetIndexLoc) || force){
|
|
|
|
console.log('Downloading ' + versionData.id + ' asset index.')
|
|
|
|
mkpath.sync(indexPath)
|
|
|
|
const stream = request(assetIndex.url).pipe(fs.createWriteStream(assetIndexLoc))
|
2018-05-07 22:15:59 +00:00
|
|
|
stream.on('finish', () => {
|
2017-11-30 08:00:06 +00:00
|
|
|
data = JSON.parse(fs.readFileSync(assetIndexLoc, 'utf-8'))
|
2017-12-03 03:41:47 +00:00
|
|
|
self._assetChainValidateAssets(versionData, data).then(() => {
|
2018-05-07 22:15:59 +00:00
|
|
|
resolve()
|
2017-11-30 08:00:06 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
2017-05-17 06:26:46 +00:00
|
|
|
data = JSON.parse(fs.readFileSync(assetIndexLoc, 'utf-8'))
|
2017-12-03 03:41:47 +00:00
|
|
|
self._assetChainValidateAssets(versionData, data).then(() => {
|
2018-05-07 22:15:59 +00:00
|
|
|
resolve()
|
2017-05-17 06:26:46 +00:00
|
|
|
})
|
2017-11-30 08:00:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private function used to chain the asset validation process. This function processes
|
|
|
|
* the assets and enqueues missing or invalid files.
|
|
|
|
* @param {Object} versionData
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {boolean} force
|
|
|
|
* @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
2017-12-03 03:41:47 +00:00
|
|
|
_assetChainValidateAssets(versionData, indexData){
|
2017-11-30 08:00:06 +00:00
|
|
|
const self = this
|
2018-05-07 22:15:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-11-30 08:00:06 +00:00
|
|
|
|
|
|
|
//Asset constants
|
|
|
|
const resourceURL = 'http://resources.download.minecraft.net/'
|
2017-12-03 03:41:47 +00:00
|
|
|
const localPath = path.join(self.basePath, 'assets')
|
2017-11-30 08:00:06 +00:00
|
|
|
const indexPath = path.join(localPath, 'indexes')
|
|
|
|
const objectPath = path.join(localPath, 'objects')
|
|
|
|
|
|
|
|
const assetDlQueue = []
|
2018-04-07 22:06:49 +00:00
|
|
|
let dlSize = 0
|
|
|
|
let acc = 0
|
|
|
|
const total = Object.keys(indexData.objects).length
|
2017-11-30 08:00:06 +00:00
|
|
|
//const objKeys = Object.keys(data.objects)
|
2018-05-07 22:15:59 +00:00
|
|
|
async.forEachOfLimit(indexData.objects, 10, (value, key, cb) => {
|
2018-04-07 22:06:49 +00:00
|
|
|
acc++
|
|
|
|
self.emit('assetVal', {acc, total})
|
2017-11-30 08:00:06 +00:00
|
|
|
const hash = value.hash
|
|
|
|
const assetName = path.join(hash.substring(0, 2), hash)
|
|
|
|
const urlName = hash.substring(0, 2) + "/" + hash
|
|
|
|
const ast = new Asset(key, hash, String(value.size), resourceURL + urlName, path.join(objectPath, assetName))
|
|
|
|
if(!AssetGuard._validateLocal(ast.to, 'sha1', ast.hash)){
|
|
|
|
dlSize += (ast.size*1)
|
|
|
|
assetDlQueue.push(ast)
|
|
|
|
}
|
|
|
|
cb()
|
2018-05-07 22:15:59 +00:00
|
|
|
}, (err) => {
|
2017-11-30 08:00:06 +00:00
|
|
|
self.assets = new DLTracker(assetDlQueue, dlSize)
|
2018-05-07 22:15:59 +00:00
|
|
|
resolve()
|
2017-05-17 06:26:46 +00:00
|
|
|
})
|
2017-11-30 08:00:06 +00:00
|
|
|
})
|
|
|
|
}
|
2018-03-30 12:31:54 +00:00
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// Library (Category=''') Validation Functions
|
|
|
|
// #region
|
2017-11-30 08:00:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Public library validation function. This function will handle the validation of libraries.
|
|
|
|
* It will parse the version data, analyzing each library entry. In this analysis, it will
|
|
|
|
* check to see if the local file exists and is valid. If not, it will be added to the download
|
|
|
|
* queue for the 'libraries' identifier.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {Object} versionData The version data for the assets.
|
|
|
|
* @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
2017-12-03 03:41:47 +00:00
|
|
|
validateLibraries(versionData){
|
2017-11-30 08:00:06 +00:00
|
|
|
const self = this
|
2018-05-07 22:15:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-11-30 08:00:06 +00:00
|
|
|
|
|
|
|
const libArr = versionData.libraries
|
2017-12-03 03:41:47 +00:00
|
|
|
const libPath = path.join(self.basePath, 'libraries')
|
2017-11-30 08:00:06 +00:00
|
|
|
|
|
|
|
const libDlQueue = []
|
|
|
|
let dlSize = 0
|
|
|
|
|
|
|
|
//Check validity of each library. If the hashs don't match, download the library.
|
2018-05-07 22:15:59 +00:00
|
|
|
async.eachLimit(libArr, 5, (lib, cb) => {
|
2017-11-30 08:00:06 +00:00
|
|
|
if(Library.validateRules(lib.rules)){
|
|
|
|
let artifact = (lib.natives == null) ? lib.downloads.artifact : lib.downloads.classifiers[lib.natives[Library.mojangFriendlyOS()]]
|
|
|
|
const libItm = new Library(lib.name, artifact.sha1, artifact.size, artifact.url, path.join(libPath, artifact.path))
|
|
|
|
if(!AssetGuard._validateLocal(libItm.to, 'sha1', libItm.hash)){
|
|
|
|
dlSize += (libItm.size*1)
|
|
|
|
libDlQueue.push(libItm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cb()
|
2018-05-07 22:15:59 +00:00
|
|
|
}, (err) => {
|
2017-11-30 08:00:06 +00:00
|
|
|
self.libraries = new DLTracker(libDlQueue, dlSize)
|
2018-05-07 22:15:59 +00:00
|
|
|
resolve()
|
2017-05-17 06:26:46 +00:00
|
|
|
})
|
2017-11-30 08:00:06 +00:00
|
|
|
})
|
|
|
|
}
|
2017-05-17 06:26:46 +00:00
|
|
|
|
2018-03-30 12:31:54 +00:00
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// Miscellaneous (Category=files) Validation Functions
|
|
|
|
// #region
|
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
/**
|
|
|
|
* Public miscellaneous mojang file validation function. These files will be enqueued under
|
|
|
|
* the 'files' identifier.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {Object} versionData The version data for the assets.
|
|
|
|
* @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
2017-12-03 03:41:47 +00:00
|
|
|
validateMiscellaneous(versionData){
|
2017-11-30 08:00:06 +00:00
|
|
|
const self = this
|
2018-05-07 22:15:59 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2017-12-03 03:41:47 +00:00
|
|
|
await self.validateClient(versionData)
|
|
|
|
await self.validateLogConfig(versionData)
|
2018-05-07 22:15:59 +00:00
|
|
|
resolve()
|
2017-05-17 06:26:46 +00:00
|
|
|
})
|
2017-11-30 08:00:06 +00:00
|
|
|
}
|
2017-05-17 06:26:46 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
/**
|
|
|
|
* Validate client file - artifact renamed from client.jar to '{version}'.jar.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {Object} versionData The version data for the assets.
|
|
|
|
* @param {boolean} force Optional. If true, the asset index will be downloaded even if it exists locally. Defaults to false.
|
|
|
|
* @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
2017-12-03 03:41:47 +00:00
|
|
|
validateClient(versionData, force = false){
|
2017-11-30 08:00:06 +00:00
|
|
|
const self = this
|
2018-05-07 22:15:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-11-30 08:00:06 +00:00
|
|
|
const clientData = versionData.downloads.client
|
|
|
|
const version = versionData.id
|
2017-12-03 03:41:47 +00:00
|
|
|
const targetPath = path.join(self.basePath, 'versions', version)
|
2017-11-30 08:00:06 +00:00
|
|
|
const targetFile = version + '.jar'
|
|
|
|
|
|
|
|
let client = new Asset(version + ' client', clientData.sha1, clientData.size, clientData.url, path.join(targetPath, targetFile))
|
|
|
|
|
|
|
|
if(!AssetGuard._validateLocal(client.to, 'sha1', client.hash) || force){
|
|
|
|
self.files.dlqueue.push(client)
|
|
|
|
self.files.dlsize += client.size*1
|
2018-05-07 22:15:59 +00:00
|
|
|
resolve()
|
2017-11-30 08:00:06 +00:00
|
|
|
} else {
|
2018-05-07 22:15:59 +00:00
|
|
|
resolve()
|
2017-05-17 06:26:46 +00:00
|
|
|
}
|
|
|
|
})
|
2017-11-30 08:00:06 +00:00
|
|
|
}
|
2017-05-17 06:26:46 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
/**
|
|
|
|
* Validate log config.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {Object} versionData The version data for the assets.
|
|
|
|
* @param {boolean} force Optional. If true, the asset index will be downloaded even if it exists locally. Defaults to false.
|
|
|
|
* @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
2017-12-03 03:41:47 +00:00
|
|
|
validateLogConfig(versionData){
|
2017-11-30 08:00:06 +00:00
|
|
|
const self = this
|
2018-05-07 22:15:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-11-30 08:00:06 +00:00
|
|
|
const client = versionData.logging.client
|
|
|
|
const file = client.file
|
2017-12-03 03:41:47 +00:00
|
|
|
const targetPath = path.join(self.basePath, 'assets', 'log_configs')
|
2017-11-30 08:00:06 +00:00
|
|
|
|
|
|
|
let logConfig = new Asset(file.id, file.sha1, file.size, file.url, path.join(targetPath, file.id))
|
|
|
|
|
|
|
|
if(!AssetGuard._validateLocal(logConfig.to, 'sha1', logConfig.hash)){
|
|
|
|
self.files.dlqueue.push(logConfig)
|
|
|
|
self.files.dlsize += logConfig.size*1
|
2018-05-07 22:15:59 +00:00
|
|
|
resolve()
|
2017-11-30 08:00:06 +00:00
|
|
|
} else {
|
2018-05-07 22:15:59 +00:00
|
|
|
resolve()
|
2017-11-30 08:00:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-05-17 06:26:46 +00:00
|
|
|
|
2018-03-30 12:31:54 +00:00
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// Distribution (Category=forge) Validation Functions
|
|
|
|
// #region
|
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
/**
|
|
|
|
* Validate the distribution.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {string} serverpackid The id of the server to validate.
|
|
|
|
* @returns {Promise.<Object>} A promise which resolves to the server distribution object.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
2017-12-03 03:41:47 +00:00
|
|
|
validateDistribution(serverpackid){
|
2017-11-30 08:00:06 +00:00
|
|
|
const self = this
|
2018-05-07 22:15:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-05-09 00:10:46 +00:00
|
|
|
AssetGuard.refreshDistributionDataLocal(self.launcherPath).then((v) => {
|
|
|
|
const serv = AssetGuard.getServerById(serverpackid)
|
2017-05-17 06:26:46 +00:00
|
|
|
|
2018-01-19 04:45:50 +00:00
|
|
|
if(serv == null) {
|
|
|
|
console.error('Invalid server pack id:', serverpackid)
|
|
|
|
}
|
|
|
|
|
2017-12-03 03:41:47 +00:00
|
|
|
self.forge = self._parseDistroModules(serv.modules, serv.mc_version)
|
2018-04-29 00:52:13 +00:00
|
|
|
// Correct our workaround here.
|
2017-11-30 08:00:06 +00:00
|
|
|
let decompressqueue = self.forge.callback
|
2018-04-29 00:52:13 +00:00
|
|
|
self.extractQueue = decompressqueue
|
2018-05-07 22:15:59 +00:00
|
|
|
self.forge.callback = (asset, self) => {
|
2017-11-30 08:00:06 +00:00
|
|
|
if(asset.type === 'forge-hosted' || asset.type === 'forge'){
|
2017-12-03 03:41:47 +00:00
|
|
|
AssetGuard._finalizeForgeAsset(asset, self.basePath)
|
2017-11-30 08:00:06 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-07 22:15:59 +00:00
|
|
|
resolve(serv)
|
2017-11-30 08:00:06 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2017-05-17 06:26:46 +00:00
|
|
|
|
2017-12-03 03:41:47 +00:00
|
|
|
_parseDistroModules(modules, version){
|
2017-11-30 08:00:06 +00:00
|
|
|
let alist = []
|
|
|
|
let asize = 0;
|
|
|
|
let decompressqueue = []
|
|
|
|
for(let i=0; i<modules.length; i++){
|
|
|
|
let ob = modules[i]
|
|
|
|
let obType = ob.type
|
|
|
|
let obArtifact = ob.artifact
|
|
|
|
let obPath = obArtifact.path == null ? AssetGuard._resolvePath(ob.id, obArtifact.extension) : obArtifact.path
|
|
|
|
switch(obType){
|
|
|
|
case 'forge-hosted':
|
|
|
|
case 'forge':
|
|
|
|
case 'library':
|
2017-12-03 03:41:47 +00:00
|
|
|
obPath = path.join(this.basePath, 'libraries', obPath)
|
2017-11-30 08:00:06 +00:00
|
|
|
break
|
|
|
|
case 'forgemod':
|
2017-12-03 03:41:47 +00:00
|
|
|
//obPath = path.join(this.basePath, 'mods', obPath)
|
|
|
|
obPath = path.join(this.basePath, 'modstore', obPath)
|
2017-11-30 08:00:06 +00:00
|
|
|
break
|
|
|
|
case 'litemod':
|
2017-12-03 03:41:47 +00:00
|
|
|
//obPath = path.join(this.basePath, 'mods', version, obPath)
|
|
|
|
obPath = path.join(this.basePath, 'modstore', obPath)
|
2017-11-30 08:00:06 +00:00
|
|
|
break
|
|
|
|
case 'file':
|
|
|
|
default:
|
2017-12-03 03:41:47 +00:00
|
|
|
obPath = path.join(this.basePath, obPath)
|
2017-11-30 08:00:06 +00:00
|
|
|
}
|
|
|
|
let artifact = new DistroModule(ob.id, obArtifact.MD5, obArtifact.size, obArtifact.url, obPath, obType)
|
|
|
|
const validationPath = obPath.toLowerCase().endsWith('.pack.xz') ? obPath.substring(0, obPath.toLowerCase().lastIndexOf('.pack.xz')) : obPath
|
|
|
|
if(!AssetGuard._validateLocal(validationPath, 'MD5', artifact.hash)){
|
|
|
|
asize += artifact.size*1
|
|
|
|
alist.push(artifact)
|
|
|
|
if(validationPath !== obPath) decompressqueue.push(obPath)
|
|
|
|
}
|
|
|
|
//Recursively process the submodules then combine the results.
|
|
|
|
if(ob.sub_modules != null){
|
2017-12-03 03:41:47 +00:00
|
|
|
let dltrack = this._parseDistroModules(ob.sub_modules, version)
|
2017-11-30 08:00:06 +00:00
|
|
|
asize += dltrack.dlsize*1
|
|
|
|
alist = alist.concat(dltrack.dlqueue)
|
|
|
|
decompressqueue = decompressqueue.concat(dltrack.callback)
|
|
|
|
}
|
2017-05-17 06:26:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
//Since we have no callback at this point, we use this value to store the decompressqueue.
|
|
|
|
return new DLTracker(alist, asize, decompressqueue)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads Forge's version.json data into memory for the specified server id.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {string} serverpack The id of the server to load Forge data for.
|
|
|
|
* @returns {Promise.<Object>} A promise which resolves to Forge's version.json data.
|
2017-11-30 08:00:06 +00:00
|
|
|
*/
|
2017-12-03 03:41:47 +00:00
|
|
|
loadForgeData(serverpack){
|
2017-11-30 08:00:06 +00:00
|
|
|
const self = this
|
2018-05-07 22:15:59 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2018-05-09 00:10:46 +00:00
|
|
|
let distro = AssetGuard.getDistributionData()
|
2017-11-30 08:00:06 +00:00
|
|
|
|
|
|
|
const servers = distro.servers
|
2017-05-21 05:06:01 +00:00
|
|
|
let serv = null
|
|
|
|
for(let i=0; i<servers.length; i++){
|
2017-11-30 08:00:06 +00:00
|
|
|
if(servers[i].id === serverpack){
|
2017-05-21 05:06:01 +00:00
|
|
|
serv = servers[i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-05-17 09:03:09 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
const modules = serv.modules
|
|
|
|
for(let i=0; i<modules.length; i++){
|
|
|
|
const ob = modules[i]
|
|
|
|
if(ob.type === 'forge-hosted' || ob.type === 'forge'){
|
|
|
|
let obArtifact = ob.artifact
|
2017-12-03 03:41:47 +00:00
|
|
|
let obPath = obArtifact.path == null ? path.join(self.basePath, 'libraries', AssetGuard._resolvePath(ob.id, obArtifact.extension)) : obArtifact.path
|
2017-11-30 08:00:06 +00:00
|
|
|
let asset = new DistroModule(ob.id, obArtifact.MD5, obArtifact.size, obArtifact.url, obPath, ob.type)
|
2017-12-03 03:41:47 +00:00
|
|
|
let forgeData = await AssetGuard._finalizeForgeAsset(asset, self.basePath)
|
2018-05-07 22:15:59 +00:00
|
|
|
resolve(forgeData)
|
2017-11-30 08:00:06 +00:00
|
|
|
return
|
2017-05-21 22:06:48 +00:00
|
|
|
}
|
2017-05-21 08:56:39 +00:00
|
|
|
}
|
2017-11-30 08:00:06 +00:00
|
|
|
reject('No forge module found!')
|
2017-05-21 05:06:01 +00:00
|
|
|
})
|
|
|
|
}
|
2017-08-26 18:43:24 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
_parseForgeLibraries(){
|
|
|
|
/* TODO
|
|
|
|
* Forge asset validations are already implemented. When there's nothing much
|
|
|
|
* to work on, implement forge downloads using forge's version.json. This is to
|
|
|
|
* have the code on standby if we ever need it (since it's half implemented already).
|
|
|
|
*/
|
|
|
|
}
|
2017-05-21 22:06:48 +00:00
|
|
|
|
2018-03-31 17:05:05 +00:00
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// Java (Category=''') Validation (download) Functions
|
|
|
|
// #region
|
|
|
|
|
2018-04-07 22:06:49 +00:00
|
|
|
_enqueueOracleJRE(dataDir){
|
2018-04-01 00:45:24 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
AssetGuard._latestJREOracle().then(verData => {
|
|
|
|
if(verData != null){
|
2018-03-31 17:05:05 +00:00
|
|
|
|
2018-04-01 00:45:24 +00:00
|
|
|
const combined = verData.uri + PLATFORM_MAP[process.platform]
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
url: combined,
|
|
|
|
headers: {
|
|
|
|
'Cookie': 'oraclelicense=accept-securebackup-cookie'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request.head(opts, (err, resp, body) => {
|
|
|
|
if(err){
|
|
|
|
resolve(false)
|
|
|
|
} else {
|
2018-04-07 22:06:49 +00:00
|
|
|
dataDir = path.join(dataDir, 'runtime', 'x64')
|
2018-04-01 00:45:24 +00:00
|
|
|
const name = combined.substring(combined.lastIndexOf('/')+1)
|
2018-04-07 22:06:49 +00:00
|
|
|
const fDir = path.join(dataDir, name)
|
2018-04-01 00:45:24 +00:00
|
|
|
const jre = new Asset(name, null, resp.headers['content-length'], opts, fDir)
|
2018-04-07 22:06:49 +00:00
|
|
|
this.java = new DLTracker([jre], jre.size, (a, self) => {
|
|
|
|
let h = null
|
|
|
|
fs.createReadStream(a.to)
|
|
|
|
.on('error', err => console.log(err))
|
|
|
|
.pipe(zlib.createGunzip())
|
|
|
|
.on('error', err => console.log(err))
|
|
|
|
.pipe(tar.extract(dataDir, {
|
|
|
|
map: (header) => {
|
|
|
|
if(h == null){
|
|
|
|
h = header.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
.on('error', err => console.log(err))
|
|
|
|
.on('finish', () => {
|
2018-04-01 00:45:24 +00:00
|
|
|
fs.unlink(a.to, err => {
|
|
|
|
if(err){
|
|
|
|
console.log(err)
|
|
|
|
}
|
2018-04-07 22:06:49 +00:00
|
|
|
if(h.indexOf('/') > -1){
|
|
|
|
h = h.substring(0, h.indexOf('/'))
|
|
|
|
}
|
|
|
|
const pos = path.join(dataDir, h)
|
|
|
|
self.emit('jExtracted', AssetGuard.javaExecFromRoot(pos))
|
2018-04-01 00:45:24 +00:00
|
|
|
})
|
2018-04-07 22:06:49 +00:00
|
|
|
})
|
|
|
|
|
2018-04-01 00:45:24 +00:00
|
|
|
})
|
|
|
|
resolve(true)
|
|
|
|
}
|
|
|
|
})
|
2018-03-31 17:05:05 +00:00
|
|
|
|
2018-04-01 00:45:24 +00:00
|
|
|
} else {
|
|
|
|
resolve(false)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2018-03-31 17:05:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-01 00:45:24 +00:00
|
|
|
/*_enqueueMojangJRE(dir){
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Mojang does not host the JRE for linux.
|
|
|
|
if(process.platform === 'linux'){
|
|
|
|
resolve(false)
|
|
|
|
}
|
|
|
|
AssetGuard.loadMojangLauncherData().then(data => {
|
|
|
|
if(data != null) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
const mJRE = data[Library.mojangFriendlyOS()]['64'].jre
|
|
|
|
const url = mJRE.url
|
|
|
|
|
|
|
|
request.head(url, (err, resp, body) => {
|
|
|
|
if(err){
|
|
|
|
resolve(false)
|
|
|
|
} else {
|
|
|
|
const name = url.substring(url.lastIndexOf('/')+1)
|
|
|
|
const fDir = path.join(dir, name)
|
|
|
|
const jre = new Asset('jre' + mJRE.version, mJRE.sha1, resp.headers['content-length'], url, fDir)
|
|
|
|
this.java = new DLTracker([jre], jre.size, a => {
|
|
|
|
fs.readFile(a.to, (err, data) => {
|
|
|
|
// Data buffer needs to be decompressed from lzma,
|
|
|
|
// not really possible using node.js
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} catch (err){
|
|
|
|
resolve(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}*/
|
2018-03-31 17:05:05 +00:00
|
|
|
|
|
|
|
|
2018-03-30 12:31:54 +00:00
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// Control Flow Functions
|
|
|
|
// #region
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiate an async download process for an AssetGuard DLTracker.
|
|
|
|
*
|
|
|
|
* @param {string} identifier The identifier of the AssetGuard DLTracker.
|
|
|
|
* @param {number} limit Optional. The number of async processes to run in parallel.
|
|
|
|
* @returns {boolean} True if the process began, otherwise false.
|
|
|
|
*/
|
|
|
|
startAsyncProcess(identifier, limit = 5){
|
|
|
|
const self = this
|
|
|
|
let acc = 0
|
|
|
|
const concurrentDlTracker = this[identifier]
|
|
|
|
const concurrentDlQueue = concurrentDlTracker.dlqueue.slice(0)
|
|
|
|
if(concurrentDlQueue.length === 0){
|
|
|
|
return false
|
|
|
|
} else {
|
2018-05-10 04:22:00 +00:00
|
|
|
console.log('DLQueue', concurrentDlQueue)
|
2018-05-07 22:15:59 +00:00
|
|
|
async.eachLimit(concurrentDlQueue, limit, (asset, cb) => {
|
2018-03-30 12:31:54 +00:00
|
|
|
let count = 0;
|
|
|
|
mkpath.sync(path.join(asset.to, ".."))
|
|
|
|
let req = request(asset.from)
|
|
|
|
req.pause()
|
|
|
|
req.on('response', (resp) => {
|
|
|
|
if(resp.statusCode === 200){
|
|
|
|
let writeStream = fs.createWriteStream(asset.to)
|
|
|
|
writeStream.on('close', () => {
|
|
|
|
//console.log('DLResults ' + asset.size + ' ' + count + ' ', asset.size === count)
|
|
|
|
if(concurrentDlTracker.callback != null){
|
2018-04-07 22:06:49 +00:00
|
|
|
concurrentDlTracker.callback.apply(concurrentDlTracker, [asset, self])
|
2018-03-30 12:31:54 +00:00
|
|
|
}
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
req.pipe(writeStream)
|
|
|
|
req.resume()
|
|
|
|
} else {
|
|
|
|
req.abort()
|
2018-03-31 17:05:05 +00:00
|
|
|
const realFrom = typeof asset.from === 'object' ? asset.from.url : asset.from
|
|
|
|
console.log('Failed to download ' + realFrom + '. Response code', resp.statusCode)
|
2018-03-30 12:31:54 +00:00
|
|
|
self.progress += asset.size*1
|
|
|
|
self.emit('totaldlprogress', {acc: self.progress, total: self.totaldlsize})
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
})
|
2018-05-07 01:45:20 +00:00
|
|
|
req.on('error', (err) => {
|
|
|
|
self.emit('dlerror', err)
|
|
|
|
})
|
2018-05-07 22:15:59 +00:00
|
|
|
req.on('data', (chunk) => {
|
2018-03-30 12:31:54 +00:00
|
|
|
count += chunk.length
|
|
|
|
self.progress += chunk.length
|
|
|
|
acc += chunk.length
|
|
|
|
self.emit(identifier + 'dlprogress', acc)
|
|
|
|
self.emit('totaldlprogress', {acc: self.progress, total: self.totaldlsize})
|
|
|
|
})
|
2018-05-07 22:15:59 +00:00
|
|
|
}, (err) => {
|
2018-03-30 12:31:54 +00:00
|
|
|
if(err){
|
|
|
|
self.emit(identifier + 'dlerror')
|
|
|
|
console.log('An item in ' + identifier + ' failed to process');
|
|
|
|
} else {
|
|
|
|
self.emit(identifier + 'dlcomplete')
|
|
|
|
console.log('All ' + identifier + ' have been processed successfully')
|
|
|
|
}
|
|
|
|
self.totaldlsize -= self[identifier].dlsize
|
|
|
|
self.progress -= self[identifier].dlsize
|
|
|
|
self[identifier] = new DLTracker([], 0)
|
|
|
|
if(self.totaldlsize === 0) {
|
2018-04-29 03:34:23 +00:00
|
|
|
if(self.extractQueue.length > 0){
|
|
|
|
self.emit('extracting')
|
|
|
|
AssetGuard._extractPackXZ(self.extractQueue, self.javaexec).then(() => {
|
|
|
|
self.extractQueue = []
|
|
|
|
self.emit('dlcomplete')
|
|
|
|
})
|
|
|
|
} else {
|
2018-04-29 00:52:13 +00:00
|
|
|
self.emit('dlcomplete')
|
2018-04-29 03:34:23 +00:00
|
|
|
}
|
2018-03-30 12:31:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
/**
|
|
|
|
* This function will initiate the download processed for the specified identifiers. If no argument is
|
|
|
|
* given, all identifiers will be initiated. Note that in order for files to be processed you need to run
|
|
|
|
* the processing function corresponding to that identifier. If you run this function without processing
|
|
|
|
* the files, it is likely nothing will be enqueued in the object and processing will complete
|
|
|
|
* immediately. Once all downloads are complete, this function will fire the 'dlcomplete' event on the
|
|
|
|
* global object instance.
|
|
|
|
*
|
2018-03-28 20:42:10 +00:00
|
|
|
* @param {Array.<{id: string, limit: number}>} identifiers Optional. The identifiers to process and corresponding parallel async task limit.
|
2017-08-26 18:43:24 +00:00
|
|
|
*/
|
2017-11-30 08:00:06 +00:00
|
|
|
processDlQueues(identifiers = [{id:'assets', limit:20}, {id:'libraries', limit:5}, {id:'files', limit:5}, {id:'forge', limit:5}]){
|
|
|
|
this.progress = 0;
|
2017-05-23 05:45:20 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
let shouldFire = true
|
2017-05-17 06:26:46 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
// Assign dltracking variables.
|
|
|
|
this.totaldlsize = 0
|
|
|
|
this.progress = 0
|
|
|
|
for(let i=0; i<identifiers.length; i++){
|
|
|
|
this.totaldlsize += this[identifiers[i].id].dlsize
|
|
|
|
}
|
2017-05-17 06:26:46 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
for(let i=0; i<identifiers.length; i++){
|
|
|
|
let iden = identifiers[i]
|
|
|
|
let r = this.startAsyncProcess(iden.id, iden.limit)
|
|
|
|
if(r) shouldFire = false
|
|
|
|
}
|
2017-11-27 09:31:54 +00:00
|
|
|
|
2017-11-30 08:00:06 +00:00
|
|
|
if(shouldFire){
|
|
|
|
this.emit('dlcomplete')
|
|
|
|
}
|
2017-05-17 06:26:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 12:31:54 +00:00
|
|
|
// #endregion
|
|
|
|
|
2017-05-17 06:26:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2017-11-30 08:00:06 +00:00
|
|
|
AssetGuard,
|
2017-05-17 06:26:46 +00:00
|
|
|
Asset,
|
2017-11-30 08:00:06 +00:00
|
|
|
Library
|
2017-05-17 06:26:46 +00:00
|
|
|
}
|