Use OpenJDK downloads instead of Oracle (no longer possible).

Makes use of https://adoptopenjdk.net/, star them on GitHub, great product.
This commit is contained in:
Daniel Scalzi 2019-06-02 18:11:39 -04:00
parent 555cb0125c
commit d94365f535
No known key found for this signature in database
GPG Key ID: D18EA3FB4B142A57
3 changed files with 244 additions and 121 deletions

View File

@ -14,18 +14,22 @@ console.log('AssetExec Started')
// Temporary for debug purposes. // Temporary for debug purposes.
process.on('unhandledRejection', r => console.log(r)) process.on('unhandledRejection', r => console.log(r))
tracker.on('validate', (data) => { function assignListeners(){
process.send({context: 'validate', data}) tracker.on('validate', (data) => {
}) process.send({context: 'validate', data})
tracker.on('progress', (data, acc, total) => { })
process.send({context: 'progress', data, value: acc, total, percent: parseInt((acc/total)*100)}) tracker.on('progress', (data, acc, total) => {
}) process.send({context: 'progress', data, value: acc, total, percent: parseInt((acc/total)*100)})
tracker.on('complete', (data, ...args) => { })
process.send({context: 'complete', data, args}) tracker.on('complete', (data, ...args) => {
}) process.send({context: 'complete', data, args})
tracker.on('error', (data, error) => { })
process.send({context: 'error', data, error}) tracker.on('error', (data, error) => {
}) process.send({context: 'error', data, error})
})
}
assignListeners()
process.on('message', (msg) => { process.on('message', (msg) => {
if(msg.task === 'execute'){ if(msg.task === 'execute'){
@ -44,6 +48,16 @@ process.on('message', (msg) => {
} else { } else {
process.send({result: res, context: func}) process.send({result: res, context: func})
} }
} else {
process.send({context: 'error', data: null, error: `Function ${func} not found on ${process.argv[2]}`})
}
} else if(msg.task === 'changeContext'){
target = require('./assetguard')[msg.class]
if(target == null){
process.send({context: 'error', data: null, error: `Invalid class ${msg.class}`})
} else {
tracker = new target(...(msg.args))
assignListeners()
} }
} }
}) })

View File

@ -16,11 +16,11 @@ const DistroManager = require('./distromanager')
const isDev = require('./isdev') const isDev = require('./isdev')
// Constants // Constants
const PLATFORM_MAP = { // const PLATFORM_MAP = {
win32: '-windows-x64.tar.gz', // win32: '-windows-x64.tar.gz',
darwin: '-macosx-x64.tar.gz', // darwin: '-macosx-x64.tar.gz',
linux: '-linux-x64.tar.gz' // linux: '-linux-x64.tar.gz'
} // }
// Classes // Classes
@ -176,34 +176,69 @@ class JavaGuard extends EventEmitter {
this.mcVersion = mcVersion this.mcVersion = mcVersion
} }
/** // /**
* @typedef OracleJREData // * @typedef OracleJREData
* @property {string} uri The base uri of the JRE. // * @property {string} uri The base uri of the JRE.
* @property {{major: string, update: string, build: string}} version Object containing version information. // * @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. // * 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. // * @returns {Promise.<OracleJREData>} Promise which resolved to an object containing the JRE download data.
*/ // */
static _latestJREOracle(){ // static _latestJREOracle(){
const url = 'https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html' // const url = 'https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html'
const regex = /https:\/\/.+?(?=\/java)\/java\/jdk\/([0-9]+u[0-9]+)-(b[0-9]+)\/([a-f0-9]{32})?\/jre-\1/ // const regex = /https:\/\/.+?(?=\/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)
// }
// })
// })
// }
/**
* @typedef OpenJDKData
* @property {string} uri The base uri of the JRE.
* @property {number} size The size of the download.
* @property {string} name The name of the artifact.
*/
/**
* Fetch the last open JDK binary. Uses https://api.adoptopenjdk.net/
*
* @param {string} major The major version of Java to fetch.
*
* @returns {Promise.<OpenJDKData>} Promise which resolved to an object containing the JRE download data.
*/
static _latestOpenJDK(major = '8'){
const sanitizedOS = process.platform === 'win32' ? 'windows' : (process.platform === 'darwin' ? 'mac' : process.platform)
const url = `https://api.adoptopenjdk.net/v2/latestAssets/nightly/openjdk${major}?os=${sanitizedOS}&arch=x64&heap_size=normal&openjdk_impl=hotspot&type=jre`
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
request(url, (err, resp, body) => { request({url, json: true}, (err, resp, body) => {
if(!err){ if(!err && body.length > 0){
const arr = body.match(regex)
const verSplit = arr[1].split('u')
resolve({ resolve({
uri: arr[0], uri: body[0].binary_link,
version: { size: body[0].binary_size,
major: verSplit[0], name: body[0].binary_name
update: verSplit[1],
build: arr[2]
}
}) })
} else { } else {
resolve(null) resolve(null)
@ -401,6 +436,11 @@ class JavaGuard extends EventEmitter {
if(!JavaGuard.isJavaExecPath(binaryExecPath)){ if(!JavaGuard.isJavaExecPath(binaryExecPath)){
resolve({valid: false}) resolve({valid: false})
} else if(fs.existsSync(binaryExecPath)){ } else if(fs.existsSync(binaryExecPath)){
// Workaround (javaw.exe no longer outputs this information.)
console.log(typeof binaryExecPath)
if(binaryExecPath.indexOf('javaw.exe') > -1) {
binaryExecPath.replace('javaw.exe', 'java.exe')
}
child_process.exec('"' + binaryExecPath + '" -XshowSettings:properties', (err, stdout, stderr) => { child_process.exec('"' + binaryExecPath + '" -XshowSettings:properties', (err, stdout, stderr) => {
try { try {
// Output is stored in stderr? // Output is stored in stderr?
@ -1447,59 +1487,63 @@ class AssetGuard extends EventEmitter {
// Java (Category=''') Validation (download) Functions // Java (Category=''') Validation (download) Functions
// #region // #region
_enqueueOracleJRE(dataDir){ _enqueueOpenJDK(dataDir){
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
AssetGuard._latestJREOracle().then(verData => { JavaGuard._latestOpenJDK('8').then(verData => {
if(verData != null){ if(verData != null){
const combined = verData.uri + PLATFORM_MAP[process.platform] dataDir = path.join(dataDir, 'runtime', 'x64')
const fDir = path.join(dataDir, verData.name)
const opts = { const jre = new Asset(verData.name, null, verData.size, verData.uri, fDir)
url: combined, this.java = new DLTracker([jre], jre.size, (a, self) => {
headers: { if(verData.name.endsWith('zip')){
'Cookie': 'oraclelicense=accept-securebackup-cookie'
} const zip = new AdmZip(a.to)
} const pos = path.join(dataDir, zip.getEntries()[0].entryName)
zip.extractAllToAsync(dataDir, true, (err) => {
request.head(opts, (err, resp, body) => { if(err){
if(err){ console.log(err)
resolve(false) self.emit('complete', 'java', JavaGuard.javaExecFromRoot(pos))
} else { } else {
dataDir = path.join(dataDir, 'runtime', 'x64') fs.unlink(a.to, err => {
const name = combined.substring(combined.lastIndexOf('/')+1) if(err){
const fDir = path.join(dataDir, name) console.log(err)
const jre = new Asset(name, null, parseInt(resp.headers['content-length']), opts, fDir)
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
}
} }
})) self.emit('complete', 'java', JavaGuard.javaExecFromRoot(pos))
.on('error', err => console.log(err))
.on('finish', () => {
fs.unlink(a.to, err => {
if(err){
console.log(err)
}
if(h.indexOf('/') > -1){
h = h.substring(0, h.indexOf('/'))
}
const pos = path.join(dataDir, h)
self.emit('complete', 'java', AssetGuard.javaExecFromRoot(pos))
})
}) })
}
}) })
resolve(true)
} else {
// Tar.gz
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', () => {
fs.unlink(a.to, err => {
if(err){
console.log(err)
}
if(h.indexOf('/') > -1){
h = h.substring(0, h.indexOf('/'))
}
const pos = path.join(dataDir, h)
self.emit('complete', 'java', JavaGuard.javaExecFromRoot(pos))
})
})
} }
}) })
resolve(true)
} else { } else {
resolve(false) resolve(false)
@ -1509,42 +1553,104 @@ class AssetGuard extends EventEmitter {
} }
/*_enqueueMojangJRE(dir){ // _enqueueOracleJRE(dataDir){
return new Promise((resolve, reject) => { // return new Promise((resolve, reject) => {
// Mojang does not host the JRE for linux. // JavaGuard._latestJREOracle().then(verData => {
if(process.platform === 'linux'){ // if(verData != null){
resolve(false)
}
AssetGuard.loadMojangLauncherData().then(data => {
if(data != null) {
try { // const combined = verData.uri + PLATFORM_MAP[process.platform]
const mJRE = data[Library.mojangFriendlyOS()]['64'].jre
const url = mJRE.url // const opts = {
// url: combined,
// headers: {
// 'Cookie': 'oraclelicense=accept-securebackup-cookie'
// }
// }
// request.head(opts, (err, resp, body) => {
// if(err){
// resolve(false)
// } else {
// dataDir = path.join(dataDir, 'runtime', 'x64')
// const name = combined.substring(combined.lastIndexOf('/')+1)
// const fDir = path.join(dataDir, name)
// const jre = new Asset(name, null, parseInt(resp.headers['content-length']), opts, fDir)
// 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', () => {
// fs.unlink(a.to, err => {
// if(err){
// console.log(err)
// }
// if(h.indexOf('/') > -1){
// h = h.substring(0, h.indexOf('/'))
// }
// const pos = path.join(dataDir, h)
// self.emit('complete', 'java', JavaGuard.javaExecFromRoot(pos))
// })
// })
// })
// resolve(true)
// }
// })
request.head(url, (err, resp, body) => { // } else {
if(err){ // resolve(false)
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)
}
} // }
})
}) // _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)
// }
// }
// })
// })
// }
// #endregion // #endregion

View File

@ -329,7 +329,8 @@ function asyncSystemScan(mcVersion, launchAfter = true){
) )
setOverlayHandler(() => { setOverlayHandler(() => {
setLaunchDetails('Preparing Java Download..') setLaunchDetails('Preparing Java Download..')
sysAEx.send({task: 'execute', function: '_enqueueOracleJRE', argsArr: [ConfigManager.getDataDirectory()]}) sysAEx.send({task: 'changeContext', class: 'AssetGuard', args: [ConfigManager.getCommonDirectory(),ConfigManager.getJavaExecutable()]})
sysAEx.send({task: 'execute', function: '_enqueueOpenJDK', argsArr: [ConfigManager.getDataDirectory()]})
toggleOverlay(false) toggleOverlay(false)
}) })
setDismissHandler(() => { setDismissHandler(() => {
@ -369,7 +370,7 @@ function asyncSystemScan(mcVersion, launchAfter = true){
} }
sysAEx.disconnect() sysAEx.disconnect()
} }
} else if(m.context === '_enqueueOracleJRE'){ } else if(m.context === '_enqueueOpenJDK'){
if(m.result === true){ if(m.result === true){
@ -399,7 +400,7 @@ function asyncSystemScan(mcVersion, launchAfter = true){
switch(m.data){ switch(m.data){
case 'download': case 'download':
// Downloading.. // Downloading..
setDownloadPercentage(m.value, m.total, m.percent) setDownloadPercentage(m.value, m.total, m.percent)
break break
} }
@ -448,6 +449,8 @@ function asyncSystemScan(mcVersion, launchAfter = true){
break break
} }
} else if(m.context === 'error'){
console.log(m.error)
} }
}) })