assetguard.js 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  1. // Requirements
  2. const AdmZip = require('adm-zip')
  3. const async = require('async')
  4. const child_process = require('child_process')
  5. const crypto = require('crypto')
  6. const EventEmitter = require('events')
  7. const fs = require('fs-extra')
  8. const path = require('path')
  9. const Registry = require('winreg')
  10. const request = require('request')
  11. const tar = require('tar-fs')
  12. const zlib = require('zlib')
  13. const ConfigManager = require('./configmanager')
  14. const DistroManager = require('./distromanager')
  15. const isDev = require('./isdev')
  16. // Constants
  17. // const PLATFORM_MAP = {
  18. // win32: '-windows-x64.tar.gz',
  19. // darwin: '-macosx-x64.tar.gz',
  20. // linux: '-linux-x64.tar.gz'
  21. // }
  22. // Classes
  23. /** Class representing a base asset. */
  24. class Asset {
  25. /**
  26. * Create an asset.
  27. *
  28. * @param {any} id The id of the asset.
  29. * @param {string} hash The hash value of the asset.
  30. * @param {number} size The size in bytes of the asset.
  31. * @param {string} from The url where the asset can be found.
  32. * @param {string} to The absolute local file path of the asset.
  33. */
  34. constructor(id, hash, size, from, to){
  35. this.id = id
  36. this.hash = hash
  37. this.size = size
  38. this.from = from
  39. this.to = to
  40. }
  41. }
  42. /** Class representing a mojang library. */
  43. class Library extends Asset {
  44. /**
  45. * Converts the process.platform OS names to match mojang's OS names.
  46. */
  47. static mojangFriendlyOS(){
  48. const opSys = process.platform
  49. if (opSys === 'darwin') {
  50. return 'osx'
  51. } else if (opSys === 'win32'){
  52. return 'windows'
  53. } else if (opSys === 'linux'){
  54. return 'linux'
  55. } else {
  56. return 'unknown_os'
  57. }
  58. }
  59. /**
  60. * Checks whether or not a library is valid for download on a particular OS, following
  61. * the rule format specified in the mojang version data index. If the allow property has
  62. * an OS specified, then the library can ONLY be downloaded on that OS. If the disallow
  63. * property has instead specified an OS, the library can be downloaded on any OS EXCLUDING
  64. * the one specified.
  65. *
  66. * If the rules are undefined, the natives property will be checked for a matching entry
  67. * for the current OS.
  68. *
  69. * @param {Array.<Object>} rules The Library's download rules.
  70. * @param {Object} natives The Library's natives object.
  71. * @returns {boolean} True if the Library follows the specified rules, otherwise false.
  72. */
  73. static validateRules(rules, natives){
  74. if(rules == null) {
  75. if(natives == null) {
  76. return true
  77. } else {
  78. return natives[Library.mojangFriendlyOS()] != null
  79. }
  80. }
  81. for(let rule of rules){
  82. const action = rule.action
  83. const osProp = rule.os
  84. if(action != null && osProp != null){
  85. const osName = osProp.name
  86. const osMoj = Library.mojangFriendlyOS()
  87. if(action === 'allow'){
  88. return osName === osMoj
  89. } else if(action === 'disallow'){
  90. return osName !== osMoj
  91. }
  92. }
  93. }
  94. return true
  95. }
  96. }
  97. class DistroModule extends Asset {
  98. /**
  99. * Create a DistroModule. This is for processing,
  100. * not equivalent to the module objects in the
  101. * distro index.
  102. *
  103. * @param {any} id The id of the asset.
  104. * @param {string} hash The hash value of the asset.
  105. * @param {number} size The size in bytes of the asset.
  106. * @param {string} from The url where the asset can be found.
  107. * @param {string} to The absolute local file path of the asset.
  108. * @param {string} type The the module type.
  109. */
  110. constructor(id, hash, size, from, to, type){
  111. super(id, hash, size, from, to)
  112. this.type = type
  113. }
  114. }
  115. /**
  116. * Class representing a download tracker. This is used to store meta data
  117. * about a download queue, including the queue itself.
  118. */
  119. class DLTracker {
  120. /**
  121. * Create a DLTracker
  122. *
  123. * @param {Array.<Asset>} dlqueue An array containing assets queued for download.
  124. * @param {number} dlsize The combined size of each asset in the download queue array.
  125. * @param {function(Asset)} callback Optional callback which is called when an asset finishes downloading.
  126. */
  127. constructor(dlqueue, dlsize, callback = null){
  128. this.dlqueue = dlqueue
  129. this.dlsize = dlsize
  130. this.callback = callback
  131. }
  132. }
  133. class Util {
  134. /**
  135. * Returns true if the actual version is greater than
  136. * or equal to the desired version.
  137. *
  138. * @param {string} desired The desired version.
  139. * @param {string} actual The actual version.
  140. */
  141. static mcVersionAtLeast(desired, actual){
  142. const des = desired.split('.')
  143. const act = actual.split('.')
  144. for(let i=0; i<des.length; i++){
  145. if(!(parseInt(act[i]) >= parseInt(des[i]))){
  146. return false
  147. }
  148. }
  149. return true
  150. }
  151. }
  152. class JavaGuard extends EventEmitter {
  153. constructor(mcVersion){
  154. super()
  155. this.mcVersion = mcVersion
  156. }
  157. // /**
  158. // * @typedef OracleJREData
  159. // * @property {string} uri The base uri of the JRE.
  160. // * @property {{major: string, update: string, build: string}} version Object containing version information.
  161. // */
  162. // /**
  163. // * Resolves the latest version of Oracle's JRE and parses its download link.
  164. // *
  165. // * @returns {Promise.<OracleJREData>} Promise which resolved to an object containing the JRE download data.
  166. // */
  167. // static _latestJREOracle(){
  168. // const url = 'https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html'
  169. // const regex = /https:\/\/.+?(?=\/java)\/java\/jdk\/([0-9]+u[0-9]+)-(b[0-9]+)\/([a-f0-9]{32})?\/jre-\1/
  170. // return new Promise((resolve, reject) => {
  171. // request(url, (err, resp, body) => {
  172. // if(!err){
  173. // const arr = body.match(regex)
  174. // const verSplit = arr[1].split('u')
  175. // resolve({
  176. // uri: arr[0],
  177. // version: {
  178. // major: verSplit[0],
  179. // update: verSplit[1],
  180. // build: arr[2]
  181. // }
  182. // })
  183. // } else {
  184. // resolve(null)
  185. // }
  186. // })
  187. // })
  188. // }
  189. /**
  190. * @typedef OpenJDKData
  191. * @property {string} uri The base uri of the JRE.
  192. * @property {number} size The size of the download.
  193. * @property {string} name The name of the artifact.
  194. */
  195. /**
  196. * Fetch the last open JDK binary. Uses https://api.adoptopenjdk.net/
  197. *
  198. * @param {string} major The major version of Java to fetch.
  199. *
  200. * @returns {Promise.<OpenJDKData>} Promise which resolved to an object containing the JRE download data.
  201. */
  202. static _latestOpenJDK(major = '8'){
  203. const sanitizedOS = process.platform === 'win32' ? 'windows' : (process.platform === 'darwin' ? 'mac' : process.platform)
  204. const url = `https://api.adoptopenjdk.net/v2/latestAssets/nightly/openjdk${major}?os=${sanitizedOS}&arch=x64&heap_size=normal&openjdk_impl=hotspot&type=jre`
  205. return new Promise((resolve, reject) => {
  206. request({url, json: true}, (err, resp, body) => {
  207. if(!err && body.length > 0){
  208. resolve({
  209. uri: body[0].binary_link,
  210. size: body[0].binary_size,
  211. name: body[0].binary_name
  212. })
  213. } else {
  214. resolve(null)
  215. }
  216. })
  217. })
  218. }
  219. /**
  220. * Returns the path of the OS-specific executable for the given Java
  221. * installation. Supported OS's are win32, darwin, linux.
  222. *
  223. * @param {string} rootDir The root directory of the Java installation.
  224. * @returns {string} The path to the Java executable.
  225. */
  226. static javaExecFromRoot(rootDir){
  227. if(process.platform === 'win32'){
  228. return path.join(rootDir, 'bin', 'javaw.exe')
  229. } else if(process.platform === 'darwin'){
  230. return path.join(rootDir, 'Contents', 'Home', 'bin', 'java')
  231. } else if(process.platform === 'linux'){
  232. return path.join(rootDir, 'bin', 'java')
  233. }
  234. return rootDir
  235. }
  236. /**
  237. * Check to see if the given path points to a Java executable.
  238. *
  239. * @param {string} pth The path to check against.
  240. * @returns {boolean} True if the path points to a Java executable, otherwise false.
  241. */
  242. static isJavaExecPath(pth){
  243. if(process.platform === 'win32'){
  244. return pth.endsWith(path.join('bin', 'javaw.exe'))
  245. } else if(process.platform === 'darwin'){
  246. return pth.endsWith(path.join('bin', 'java'))
  247. } else if(process.platform === 'linux'){
  248. return pth.endsWith(path.join('bin', 'java'))
  249. }
  250. return false
  251. }
  252. /**
  253. * Load Mojang's launcher.json file.
  254. *
  255. * @returns {Promise.<Object>} Promise which resolves to Mojang's launcher.json object.
  256. */
  257. static loadMojangLauncherData(){
  258. return new Promise((resolve, reject) => {
  259. request.get('https://launchermeta.mojang.com/mc/launcher.json', (err, resp, body) => {
  260. if(err){
  261. resolve(null)
  262. } else {
  263. resolve(JSON.parse(body))
  264. }
  265. })
  266. })
  267. }
  268. /**
  269. * Parses a **full** Java Runtime version string and resolves
  270. * the version information. Dynamically detects the formatting
  271. * to use.
  272. *
  273. * @param {string} verString Full version string to parse.
  274. * @returns Object containing the version information.
  275. */
  276. static parseJavaRuntimeVersion(verString){
  277. const major = verString.split('.')[0]
  278. if(major == 1){
  279. return JavaGuard._parseJavaRuntimeVersion_8(verString)
  280. } else {
  281. return JavaGuard._parseJavaRuntimeVersion_9(verString)
  282. }
  283. }
  284. /**
  285. * Parses a **full** Java Runtime version string and resolves
  286. * the version information. Uses Java 8 formatting.
  287. *
  288. * @param {string} verString Full version string to parse.
  289. * @returns Object containing the version information.
  290. */
  291. static _parseJavaRuntimeVersion_8(verString){
  292. // 1.{major}.0_{update}-b{build}
  293. // ex. 1.8.0_152-b16
  294. const ret = {}
  295. let pts = verString.split('-')
  296. ret.build = parseInt(pts[1].substring(1))
  297. pts = pts[0].split('_')
  298. ret.update = parseInt(pts[1])
  299. ret.major = parseInt(pts[0].split('.')[1])
  300. return ret
  301. }
  302. /**
  303. * Parses a **full** Java Runtime version string and resolves
  304. * the version information. Uses Java 9+ formatting.
  305. *
  306. * @param {string} verString Full version string to parse.
  307. * @returns Object containing the version information.
  308. */
  309. static _parseJavaRuntimeVersion_9(verString){
  310. // {major}.{minor}.{revision}+{build}
  311. // ex. 10.0.2+13
  312. const ret = {}
  313. let pts = verString.split('+')
  314. ret.build = parseInt(pts[1])
  315. pts = pts[0].split('.')
  316. ret.major = parseInt(pts[0])
  317. ret.minor = parseInt(pts[1])
  318. ret.revision = parseInt(pts[2])
  319. return ret
  320. }
  321. /**
  322. * Validates the output of a JVM's properties. Currently validates that a JRE is x64
  323. * and that the major = 8, update > 52.
  324. *
  325. * @param {string} stderr The output to validate.
  326. *
  327. * @returns {Promise.<Object>} A promise which resolves to a meta object about the JVM.
  328. * The validity is stored inside the `valid` property.
  329. */
  330. _validateJVMProperties(stderr){
  331. const res = stderr
  332. const props = res.split('\n')
  333. const goal = 2
  334. let checksum = 0
  335. const meta = {}
  336. for(let i=0; i<props.length; i++){
  337. if(props[i].indexOf('sun.arch.data.model') > -1){
  338. let arch = props[i].split('=')[1].trim()
  339. arch = parseInt(arch)
  340. console.log(props[i].trim())
  341. if(arch === 64){
  342. meta.arch = arch
  343. ++checksum
  344. if(checksum === goal){
  345. break
  346. }
  347. }
  348. } else if(props[i].indexOf('java.runtime.version') > -1){
  349. let verString = props[i].split('=')[1].trim()
  350. console.log(props[i].trim())
  351. const verOb = JavaGuard.parseJavaRuntimeVersion(verString)
  352. if(verOb.major < 9){
  353. // Java 8
  354. if(verOb.major === 8 && verOb.update > 52){
  355. meta.version = verOb
  356. ++checksum
  357. if(checksum === goal){
  358. break
  359. }
  360. }
  361. } else {
  362. // Java 9+
  363. if(Util.mcVersionAtLeast('1.13', this.mcVersion)){
  364. console.log('Java 9+ not yet tested.')
  365. /* meta.version = verOb
  366. ++checksum
  367. if(checksum === goal){
  368. break
  369. } */
  370. }
  371. }
  372. }
  373. }
  374. meta.valid = checksum === goal
  375. return meta
  376. }
  377. /**
  378. * Validates that a Java binary is at least 64 bit. This makes use of the non-standard
  379. * command line option -XshowSettings:properties. The output of this contains a property,
  380. * sun.arch.data.model = ARCH, in which ARCH is either 32 or 64. This option is supported
  381. * in Java 8 and 9. Since this is a non-standard option. This will resolve to true if
  382. * the function's code throws errors. That would indicate that the option is changed or
  383. * removed.
  384. *
  385. * @param {string} binaryExecPath Path to the java executable we wish to validate.
  386. *
  387. * @returns {Promise.<Object>} A promise which resolves to a meta object about the JVM.
  388. * The validity is stored inside the `valid` property.
  389. */
  390. _validateJavaBinary(binaryExecPath){
  391. return new Promise((resolve, reject) => {
  392. if(!JavaGuard.isJavaExecPath(binaryExecPath)){
  393. resolve({valid: false})
  394. } else if(fs.existsSync(binaryExecPath)){
  395. // Workaround (javaw.exe no longer outputs this information.)
  396. console.log(typeof binaryExecPath)
  397. if(binaryExecPath.indexOf('javaw.exe') > -1) {
  398. binaryExecPath.replace('javaw.exe', 'java.exe')
  399. }
  400. child_process.exec('"' + binaryExecPath + '" -XshowSettings:properties', (err, stdout, stderr) => {
  401. try {
  402. // Output is stored in stderr?
  403. resolve(this._validateJVMProperties(stderr))
  404. } catch (err){
  405. // Output format might have changed, validation cannot be completed.
  406. resolve({valid: false})
  407. }
  408. })
  409. } else {
  410. resolve({valid: false})
  411. }
  412. })
  413. }
  414. /**
  415. * Checks for the presence of the environment variable JAVA_HOME. If it exits, we will check
  416. * to see if the value points to a path which exists. If the path exits, the path is returned.
  417. *
  418. * @returns {string} The path defined by JAVA_HOME, if it exists. Otherwise null.
  419. */
  420. static _scanJavaHome(){
  421. const jHome = process.env.JAVA_HOME
  422. try {
  423. let res = fs.existsSync(jHome)
  424. return res ? jHome : null
  425. } catch (err) {
  426. // Malformed JAVA_HOME property.
  427. return null
  428. }
  429. }
  430. /**
  431. * Scans the registry for 64-bit Java entries. The paths of each entry are added to
  432. * a set and returned. Currently, only Java 8 (1.8) is supported.
  433. *
  434. * @returns {Promise.<Set.<string>>} A promise which resolves to a set of 64-bit Java root
  435. * paths found in the registry.
  436. */
  437. static _scanRegistry(){
  438. return new Promise((resolve, reject) => {
  439. // Keys for Java v9.0.0 and later:
  440. // 'SOFTWARE\\JavaSoft\\JRE'
  441. // 'SOFTWARE\\JavaSoft\\JDK'
  442. // Forge does not yet support Java 9, therefore we do not.
  443. // Keys for Java 1.8 and prior:
  444. const regKeys = [
  445. '\\SOFTWARE\\JavaSoft\\Java Runtime Environment',
  446. '\\SOFTWARE\\JavaSoft\\Java Development Kit'
  447. ]
  448. let keysDone = 0
  449. const candidates = new Set()
  450. for(let i=0; i<regKeys.length; i++){
  451. const key = new Registry({
  452. hive: Registry.HKLM,
  453. key: regKeys[i],
  454. arch: 'x64'
  455. })
  456. key.keyExists((err, exists) => {
  457. if(exists) {
  458. key.keys((err, javaVers) => {
  459. if(err){
  460. keysDone++
  461. console.error(err)
  462. // REG KEY DONE
  463. // DUE TO ERROR
  464. if(keysDone === regKeys.length){
  465. resolve(candidates)
  466. }
  467. } else {
  468. if(javaVers.length === 0){
  469. // REG KEY DONE
  470. // NO SUBKEYS
  471. keysDone++
  472. if(keysDone === regKeys.length){
  473. resolve(candidates)
  474. }
  475. } else {
  476. let numDone = 0
  477. for(let j=0; j<javaVers.length; j++){
  478. const javaVer = javaVers[j]
  479. const vKey = javaVer.key.substring(javaVer.key.lastIndexOf('\\')+1)
  480. // Only Java 8 is supported currently.
  481. if(parseFloat(vKey) === 1.8){
  482. javaVer.get('JavaHome', (err, res) => {
  483. const jHome = res.value
  484. if(jHome.indexOf('(x86)') === -1){
  485. candidates.add(jHome)
  486. }
  487. // SUBKEY DONE
  488. numDone++
  489. if(numDone === javaVers.length){
  490. keysDone++
  491. if(keysDone === regKeys.length){
  492. resolve(candidates)
  493. }
  494. }
  495. })
  496. } else {
  497. // SUBKEY DONE
  498. // NOT JAVA 8
  499. numDone++
  500. if(numDone === javaVers.length){
  501. keysDone++
  502. if(keysDone === regKeys.length){
  503. resolve(candidates)
  504. }
  505. }
  506. }
  507. }
  508. }
  509. }
  510. })
  511. } else {
  512. // REG KEY DONE
  513. // DUE TO NON-EXISTANCE
  514. keysDone++
  515. if(keysDone === regKeys.length){
  516. resolve(candidates)
  517. }
  518. }
  519. })
  520. }
  521. })
  522. }
  523. /**
  524. * See if JRE exists in the Internet Plug-Ins folder.
  525. *
  526. * @returns {string} The path of the JRE if found, otherwise null.
  527. */
  528. static _scanInternetPlugins(){
  529. // /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java
  530. const pth = '/Library/Internet Plug-Ins/JavaAppletPlugin.plugin'
  531. const res = fs.existsSync(JavaGuard.javaExecFromRoot(pth))
  532. return res ? pth : null
  533. }
  534. /**
  535. * Scan a directory for root JVM folders.
  536. *
  537. * @param {string} scanDir The directory to scan.
  538. * @returns {Promise.<Set.<string>>} A promise which resolves to a set of the discovered
  539. * root JVM folders.
  540. */
  541. static _scanFileSystem(scanDir){
  542. return new Promise((resolve, reject) => {
  543. fs.exists(scanDir, (e) => {
  544. let res = new Set()
  545. if(e){
  546. fs.readdir(scanDir, (err, files) => {
  547. if(err){
  548. resolve(res)
  549. console.log(err)
  550. } else {
  551. let pathsDone = 0
  552. for(let i=0; i<files.length; i++){
  553. const combinedPath = path.join(scanDir, files[i])
  554. const execPath = JavaGuard.javaExecFromRoot(combinedPath)
  555. fs.exists(execPath, (v) => {
  556. if(v){
  557. res.add(combinedPath)
  558. }
  559. ++pathsDone
  560. if(pathsDone === files.length){
  561. resolve(res)
  562. }
  563. })
  564. }
  565. if(pathsDone === files.length){
  566. resolve(res)
  567. }
  568. }
  569. })
  570. } else {
  571. resolve(res)
  572. }
  573. })
  574. })
  575. }
  576. /**
  577. *
  578. * @param {Set.<string>} rootSet A set of JVM root strings to validate.
  579. * @returns {Promise.<Object[]>} A promise which resolves to an array of meta objects
  580. * for each valid JVM root directory.
  581. */
  582. async _validateJavaRootSet(rootSet){
  583. const rootArr = Array.from(rootSet)
  584. const validArr = []
  585. for(let i=0; i<rootArr.length; i++){
  586. const execPath = JavaGuard.javaExecFromRoot(rootArr[i])
  587. const metaOb = await this._validateJavaBinary(execPath)
  588. if(metaOb.valid){
  589. metaOb.execPath = execPath
  590. validArr.push(metaOb)
  591. }
  592. }
  593. return validArr
  594. }
  595. /**
  596. * Sort an array of JVM meta objects. Best candidates are placed before all others.
  597. * Sorts based on version and gives priority to JREs over JDKs if versions match.
  598. *
  599. * @param {Object[]} validArr An array of JVM meta objects.
  600. * @returns {Object[]} A sorted array of JVM meta objects.
  601. */
  602. static _sortValidJavaArray(validArr){
  603. const retArr = validArr.sort((a, b) => {
  604. if(a.version.major === b.version.major){
  605. if(a.version.major < 9){
  606. // Java 8
  607. if(a.version.update === b.version.update){
  608. if(a.version.build === b.version.build){
  609. // Same version, give priority to JRE.
  610. if(a.execPath.toLowerCase().indexOf('jdk') > -1){
  611. return b.execPath.toLowerCase().indexOf('jdk') > -1 ? 0 : 1
  612. } else {
  613. return -1
  614. }
  615. } else {
  616. return a.version.build > b.version.build ? -1 : 1
  617. }
  618. } else {
  619. return a.version.update > b.version.update ? -1 : 1
  620. }
  621. } else {
  622. // Java 9+
  623. if(a.version.minor === b.version.minor){
  624. if(a.version.revision === b.version.revision){
  625. // Same version, give priority to JRE.
  626. if(a.execPath.toLowerCase().indexOf('jdk') > -1){
  627. return b.execPath.toLowerCase().indexOf('jdk') > -1 ? 0 : 1
  628. } else {
  629. return -1
  630. }
  631. } else {
  632. return a.version.revision > b.version.revision ? -1 : 1
  633. }
  634. } else {
  635. return a.version.minor > b.version.minor ? -1 : 1
  636. }
  637. }
  638. } else {
  639. return a.version.major > b.version.major ? -1 : 1
  640. }
  641. })
  642. return retArr
  643. }
  644. /**
  645. * Attempts to find a valid x64 installation of Java on Windows machines.
  646. * Possible paths will be pulled from the registry and the JAVA_HOME environment
  647. * variable. The paths will be sorted with higher versions preceeding lower, and
  648. * JREs preceeding JDKs. The binaries at the sorted paths will then be validated.
  649. * The first validated is returned.
  650. *
  651. * Higher versions > Lower versions
  652. * If versions are equal, JRE > JDK.
  653. *
  654. * @param {string} dataDir The base launcher directory.
  655. * @returns {Promise.<string>} A Promise which resolves to the executable path of a valid
  656. * x64 Java installation. If none are found, null is returned.
  657. */
  658. async _win32JavaValidate(dataDir){
  659. // Get possible paths from the registry.
  660. let pathSet1 = await JavaGuard._scanRegistry()
  661. if(pathSet1.length === 0){
  662. // Do a manual file system scan of program files.
  663. pathSet1 = JavaGuard._scanFileSystem('C:\\Program Files\\Java')
  664. }
  665. // Get possible paths from the data directory.
  666. const pathSet2 = await JavaGuard._scanFileSystem(path.join(dataDir, 'runtime', 'x64'))
  667. // Merge the results.
  668. const uberSet = new Set([...pathSet1, ...pathSet2])
  669. // Validate JAVA_HOME.
  670. const jHome = JavaGuard._scanJavaHome()
  671. if(jHome != null && jHome.indexOf('(x86)') === -1){
  672. uberSet.add(jHome)
  673. }
  674. let pathArr = await this._validateJavaRootSet(uberSet)
  675. pathArr = JavaGuard._sortValidJavaArray(pathArr)
  676. if(pathArr.length > 0){
  677. return pathArr[0].execPath
  678. } else {
  679. return null
  680. }
  681. }
  682. /**
  683. * Attempts to find a valid x64 installation of Java on MacOS.
  684. * The system JVM directory is scanned for possible installations.
  685. * The JAVA_HOME enviroment variable and internet plugins directory
  686. * are also scanned and validated.
  687. *
  688. * Higher versions > Lower versions
  689. * If versions are equal, JRE > JDK.
  690. *
  691. * @param {string} dataDir The base launcher directory.
  692. * @returns {Promise.<string>} A Promise which resolves to the executable path of a valid
  693. * x64 Java installation. If none are found, null is returned.
  694. */
  695. async _darwinJavaValidate(dataDir){
  696. const pathSet1 = await JavaGuard._scanFileSystem('/Library/Java/JavaVirtualMachines')
  697. const pathSet2 = await JavaGuard._scanFileSystem(path.join(dataDir, 'runtime', 'x64'))
  698. const uberSet = new Set([...pathSet1, ...pathSet2])
  699. // Check Internet Plugins folder.
  700. const iPPath = JavaGuard._scanInternetPlugins()
  701. if(iPPath != null){
  702. uberSet.add(iPPath)
  703. }
  704. // Check the JAVA_HOME environment variable.
  705. let jHome = JavaGuard._scanJavaHome()
  706. if(jHome != null){
  707. // Ensure we are at the absolute root.
  708. if(jHome.contains('/Contents/Home')){
  709. jHome = jHome.substring(0, jHome.indexOf('/Contents/Home'))
  710. }
  711. uberSet.add(jHome)
  712. }
  713. let pathArr = await this._validateJavaRootSet(uberSet)
  714. pathArr = JavaGuard._sortValidJavaArray(pathArr)
  715. if(pathArr.length > 0){
  716. return pathArr[0].execPath
  717. } else {
  718. return null
  719. }
  720. }
  721. /**
  722. * Attempts to find a valid x64 installation of Java on Linux.
  723. * The system JVM directory is scanned for possible installations.
  724. * The JAVA_HOME enviroment variable is also scanned and validated.
  725. *
  726. * Higher versions > Lower versions
  727. * If versions are equal, JRE > JDK.
  728. *
  729. * @param {string} dataDir The base launcher directory.
  730. * @returns {Promise.<string>} A Promise which resolves to the executable path of a valid
  731. * x64 Java installation. If none are found, null is returned.
  732. */
  733. async _linuxJavaValidate(dataDir){
  734. const pathSet1 = await JavaGuard._scanFileSystem('/usr/lib/jvm')
  735. const pathSet2 = await JavaGuard._scanFileSystem(path.join(dataDir, 'runtime', 'x64'))
  736. const uberSet = new Set([...pathSet1, ...pathSet2])
  737. // Validate JAVA_HOME
  738. const jHome = JavaGuard._scanJavaHome()
  739. if(jHome != null){
  740. uberSet.add(jHome)
  741. }
  742. let pathArr = await this._validateJavaRootSet(uberSet)
  743. pathArr = JavaGuard._sortValidJavaArray(pathArr)
  744. if(pathArr.length > 0){
  745. return pathArr[0].execPath
  746. } else {
  747. return null
  748. }
  749. }
  750. /**
  751. * Retrieve the path of a valid x64 Java installation.
  752. *
  753. * @param {string} dataDir The base launcher directory.
  754. * @returns {string} A path to a valid x64 Java installation, null if none found.
  755. */
  756. async validateJava(dataDir){
  757. return await this['_' + process.platform + 'JavaValidate'](dataDir)
  758. }
  759. }
  760. /**
  761. * Central object class used for control flow. This object stores data about
  762. * categories of downloads. Each category is assigned an identifier with a
  763. * DLTracker object as its value. Combined information is also stored, such as
  764. * the total size of all the queued files in each category. This event is used
  765. * to emit events so that external modules can listen into processing done in
  766. * this module.
  767. */
  768. class AssetGuard extends EventEmitter {
  769. /**
  770. * Create an instance of AssetGuard.
  771. * On creation the object's properties are never-null default
  772. * values. Each identifier is resolved to an empty DLTracker.
  773. *
  774. * @param {string} commonPath The common path for shared game files.
  775. * @param {string} javaexec The path to a java executable which will be used
  776. * to finalize installation.
  777. */
  778. constructor(commonPath, javaexec){
  779. super()
  780. this.totaldlsize = 0
  781. this.progress = 0
  782. this.assets = new DLTracker([], 0)
  783. this.libraries = new DLTracker([], 0)
  784. this.files = new DLTracker([], 0)
  785. this.forge = new DLTracker([], 0)
  786. this.java = new DLTracker([], 0)
  787. this.extractQueue = []
  788. this.commonPath = commonPath
  789. this.javaexec = javaexec
  790. }
  791. // Static Utility Functions
  792. // #region
  793. // Static Hash Validation Functions
  794. // #region
  795. /**
  796. * Calculates the hash for a file using the specified algorithm.
  797. *
  798. * @param {Buffer} buf The buffer containing file data.
  799. * @param {string} algo The hash algorithm.
  800. * @returns {string} The calculated hash in hex.
  801. */
  802. static _calculateHash(buf, algo){
  803. return crypto.createHash(algo).update(buf).digest('hex')
  804. }
  805. /**
  806. * Used to parse a checksums file. This is specifically designed for
  807. * the checksums.sha1 files found inside the forge scala dependencies.
  808. *
  809. * @param {string} content The string content of the checksums file.
  810. * @returns {Object} An object with keys being the file names, and values being the hashes.
  811. */
  812. static _parseChecksumsFile(content){
  813. let finalContent = {}
  814. let lines = content.split('\n')
  815. for(let i=0; i<lines.length; i++){
  816. let bits = lines[i].split(' ')
  817. if(bits[1] == null) {
  818. continue
  819. }
  820. finalContent[bits[1]] = bits[0]
  821. }
  822. return finalContent
  823. }
  824. /**
  825. * Validate that a file exists and matches a given hash value.
  826. *
  827. * @param {string} filePath The path of the file to validate.
  828. * @param {string} algo The hash algorithm to check against.
  829. * @param {string} hash The existing hash to check against.
  830. * @returns {boolean} True if the file exists and calculated hash matches the given hash, otherwise false.
  831. */
  832. static _validateLocal(filePath, algo, hash){
  833. if(fs.existsSync(filePath)){
  834. //No hash provided, have to assume it's good.
  835. if(hash == null){
  836. return true
  837. }
  838. let buf = fs.readFileSync(filePath)
  839. let calcdhash = AssetGuard._calculateHash(buf, algo)
  840. return calcdhash === hash
  841. }
  842. return false
  843. }
  844. /**
  845. * Validates a file in the style used by forge's version index.
  846. *
  847. * @param {string} filePath The path of the file to validate.
  848. * @param {Array.<string>} checksums The checksums listed in the forge version index.
  849. * @returns {boolean} True if the file exists and the hashes match, otherwise false.
  850. */
  851. static _validateForgeChecksum(filePath, checksums){
  852. if(fs.existsSync(filePath)){
  853. if(checksums == null || checksums.length === 0){
  854. return true
  855. }
  856. let buf = fs.readFileSync(filePath)
  857. let calcdhash = AssetGuard._calculateHash(buf, 'sha1')
  858. let valid = checksums.includes(calcdhash)
  859. if(!valid && filePath.endsWith('.jar')){
  860. valid = AssetGuard._validateForgeJar(filePath, checksums)
  861. }
  862. return valid
  863. }
  864. return false
  865. }
  866. /**
  867. * Validates a forge jar file dependency who declares a checksums.sha1 file.
  868. * This can be an expensive task as it usually requires that we calculate thousands
  869. * of hashes.
  870. *
  871. * @param {Buffer} buf The buffer of the jar file.
  872. * @param {Array.<string>} checksums The checksums listed in the forge version index.
  873. * @returns {boolean} True if all hashes declared in the checksums.sha1 file match the actual hashes.
  874. */
  875. static _validateForgeJar(buf, checksums){
  876. // Double pass method was the quickest I found. I tried a version where we store data
  877. // to only require a single pass, plus some quick cleanup but that seemed to take slightly more time.
  878. const hashes = {}
  879. let expected = {}
  880. const zip = new AdmZip(buf)
  881. const zipEntries = zip.getEntries()
  882. //First pass
  883. for(let i=0; i<zipEntries.length; i++){
  884. let entry = zipEntries[i]
  885. if(entry.entryName === 'checksums.sha1'){
  886. expected = AssetGuard._parseChecksumsFile(zip.readAsText(entry))
  887. }
  888. hashes[entry.entryName] = AssetGuard._calculateHash(entry.getData(), 'sha1')
  889. }
  890. if(!checksums.includes(hashes['checksums.sha1'])){
  891. return false
  892. }
  893. //Check against expected
  894. const expectedEntries = Object.keys(expected)
  895. for(let i=0; i<expectedEntries.length; i++){
  896. if(expected[expectedEntries[i]] !== hashes[expectedEntries[i]]){
  897. return false
  898. }
  899. }
  900. return true
  901. }
  902. // #endregion
  903. // Miscellaneous Static Functions
  904. // #region
  905. /**
  906. * Extracts and unpacks a file from .pack.xz format.
  907. *
  908. * @param {Array.<string>} filePaths The paths of the files to be extracted and unpacked.
  909. * @returns {Promise.<void>} An empty promise to indicate the extraction has completed.
  910. */
  911. static _extractPackXZ(filePaths, javaExecutable){
  912. console.log('[PackXZExtract] Starting')
  913. return new Promise((resolve, reject) => {
  914. let libPath
  915. if(isDev){
  916. libPath = path.join(process.cwd(), 'libraries', 'java', 'PackXZExtract.jar')
  917. } else {
  918. if(process.platform === 'darwin'){
  919. libPath = path.join(process.cwd(),'Contents', 'Resources', 'libraries', 'java', 'PackXZExtract.jar')
  920. } else {
  921. libPath = path.join(process.cwd(), 'resources', 'libraries', 'java', 'PackXZExtract.jar')
  922. }
  923. }
  924. const filePath = filePaths.join(',')
  925. const child = child_process.spawn(javaExecutable, ['-jar', libPath, '-packxz', filePath])
  926. child.stdout.on('data', (data) => {
  927. console.log('[PackXZExtract]', data.toString('utf8'))
  928. })
  929. child.stderr.on('data', (data) => {
  930. console.log('[PackXZExtract]', data.toString('utf8'))
  931. })
  932. child.on('close', (code, signal) => {
  933. console.log('[PackXZExtract]', 'Exited with code', code)
  934. resolve()
  935. })
  936. })
  937. }
  938. /**
  939. * Function which finalizes the forge installation process. This creates a 'version'
  940. * instance for forge and saves its version.json file into that instance. If that
  941. * instance already exists, the contents of the version.json file are read and returned
  942. * in a promise.
  943. *
  944. * @param {Asset} asset The Asset object representing Forge.
  945. * @param {string} commonPath The common path for shared game files.
  946. * @returns {Promise.<Object>} A promise which resolves to the contents of forge's version.json.
  947. */
  948. static _finalizeForgeAsset(asset, commonPath){
  949. return new Promise((resolve, reject) => {
  950. fs.readFile(asset.to, (err, data) => {
  951. const zip = new AdmZip(data)
  952. const zipEntries = zip.getEntries()
  953. for(let i=0; i<zipEntries.length; i++){
  954. if(zipEntries[i].entryName === 'version.json'){
  955. const forgeVersion = JSON.parse(zip.readAsText(zipEntries[i]))
  956. const versionPath = path.join(commonPath, 'versions', forgeVersion.id)
  957. const versionFile = path.join(versionPath, forgeVersion.id + '.json')
  958. if(!fs.existsSync(versionFile)){
  959. fs.ensureDirSync(versionPath)
  960. fs.writeFileSync(path.join(versionPath, forgeVersion.id + '.json'), zipEntries[i].getData())
  961. resolve(forgeVersion)
  962. } else {
  963. //Read the saved file to allow for user modifications.
  964. resolve(JSON.parse(fs.readFileSync(versionFile, 'utf-8')))
  965. }
  966. return
  967. }
  968. }
  969. //We didn't find forge's version.json.
  970. reject('Unable to finalize Forge processing, version.json not found! Has forge changed their format?')
  971. })
  972. })
  973. }
  974. // #endregion
  975. // #endregion
  976. // Validation Functions
  977. // #region
  978. /**
  979. * Loads the version data for a given minecraft version.
  980. *
  981. * @param {string} version The game version for which to load the index data.
  982. * @param {boolean} force Optional. If true, the version index will be downloaded even if it exists locally. Defaults to false.
  983. * @returns {Promise.<Object>} Promise which resolves to the version data object.
  984. */
  985. loadVersionData(version, force = false){
  986. const self = this
  987. return new Promise(async (resolve, reject) => {
  988. const versionPath = path.join(self.commonPath, 'versions', version)
  989. const versionFile = path.join(versionPath, version + '.json')
  990. if(!fs.existsSync(versionFile) || force){
  991. const url = await self._getVersionDataUrl(version)
  992. //This download will never be tracked as it's essential and trivial.
  993. console.log('Preparing download of ' + version + ' assets.')
  994. fs.ensureDirSync(versionPath)
  995. const stream = request(url).pipe(fs.createWriteStream(versionFile))
  996. stream.on('finish', () => {
  997. resolve(JSON.parse(fs.readFileSync(versionFile)))
  998. })
  999. } else {
  1000. resolve(JSON.parse(fs.readFileSync(versionFile)))
  1001. }
  1002. })
  1003. }
  1004. /**
  1005. * Parses Mojang's version manifest and retrieves the url of the version
  1006. * data index.
  1007. *
  1008. * @param {string} version The version to lookup.
  1009. * @returns {Promise.<string>} Promise which resolves to the url of the version data index.
  1010. * If the version could not be found, resolves to null.
  1011. */
  1012. _getVersionDataUrl(version){
  1013. return new Promise((resolve, reject) => {
  1014. request('https://launchermeta.mojang.com/mc/game/version_manifest.json', (error, resp, body) => {
  1015. if(error){
  1016. reject(error)
  1017. } else {
  1018. const manifest = JSON.parse(body)
  1019. for(let v of manifest.versions){
  1020. if(v.id === version){
  1021. resolve(v.url)
  1022. }
  1023. }
  1024. resolve(null)
  1025. }
  1026. })
  1027. })
  1028. }
  1029. // Asset (Category=''') Validation Functions
  1030. // #region
  1031. /**
  1032. * Public asset validation function. This function will handle the validation of assets.
  1033. * It will parse the asset index specified in the version data, analyzing each
  1034. * asset entry. In this analysis it will check to see if the local file exists and is valid.
  1035. * If not, it will be added to the download queue for the 'assets' identifier.
  1036. *
  1037. * @param {Object} versionData The version data for the assets.
  1038. * @param {boolean} force Optional. If true, the asset index will be downloaded even if it exists locally. Defaults to false.
  1039. * @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
  1040. */
  1041. validateAssets(versionData, force = false){
  1042. const self = this
  1043. return new Promise((resolve, reject) => {
  1044. self._assetChainIndexData(versionData, force).then(() => {
  1045. resolve()
  1046. })
  1047. })
  1048. }
  1049. //Chain the asset tasks to provide full async. The below functions are private.
  1050. /**
  1051. * Private function used to chain the asset validation process. This function retrieves
  1052. * the index data.
  1053. * @param {Object} versionData
  1054. * @param {boolean} force
  1055. * @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
  1056. */
  1057. _assetChainIndexData(versionData, force = false){
  1058. const self = this
  1059. return new Promise((resolve, reject) => {
  1060. //Asset index constants.
  1061. const assetIndex = versionData.assetIndex
  1062. const name = assetIndex.id + '.json'
  1063. const indexPath = path.join(self.commonPath, 'assets', 'indexes')
  1064. const assetIndexLoc = path.join(indexPath, name)
  1065. let data = null
  1066. if(!fs.existsSync(assetIndexLoc) || force){
  1067. console.log('Downloading ' + versionData.id + ' asset index.')
  1068. fs.ensureDirSync(indexPath)
  1069. const stream = request(assetIndex.url).pipe(fs.createWriteStream(assetIndexLoc))
  1070. stream.on('finish', () => {
  1071. data = JSON.parse(fs.readFileSync(assetIndexLoc, 'utf-8'))
  1072. self._assetChainValidateAssets(versionData, data).then(() => {
  1073. resolve()
  1074. })
  1075. })
  1076. } else {
  1077. data = JSON.parse(fs.readFileSync(assetIndexLoc, 'utf-8'))
  1078. self._assetChainValidateAssets(versionData, data).then(() => {
  1079. resolve()
  1080. })
  1081. }
  1082. })
  1083. }
  1084. /**
  1085. * Private function used to chain the asset validation process. This function processes
  1086. * the assets and enqueues missing or invalid files.
  1087. * @param {Object} versionData
  1088. * @param {boolean} force
  1089. * @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
  1090. */
  1091. _assetChainValidateAssets(versionData, indexData){
  1092. const self = this
  1093. return new Promise((resolve, reject) => {
  1094. //Asset constants
  1095. const resourceURL = 'http://resources.download.minecraft.net/'
  1096. const localPath = path.join(self.commonPath, 'assets')
  1097. const objectPath = path.join(localPath, 'objects')
  1098. const assetDlQueue = []
  1099. let dlSize = 0
  1100. let acc = 0
  1101. const total = Object.keys(indexData.objects).length
  1102. //const objKeys = Object.keys(data.objects)
  1103. async.forEachOfLimit(indexData.objects, 10, (value, key, cb) => {
  1104. acc++
  1105. self.emit('progress', 'assets', acc, total)
  1106. const hash = value.hash
  1107. const assetName = path.join(hash.substring(0, 2), hash)
  1108. const urlName = hash.substring(0, 2) + '/' + hash
  1109. const ast = new Asset(key, hash, value.size, resourceURL + urlName, path.join(objectPath, assetName))
  1110. if(!AssetGuard._validateLocal(ast.to, 'sha1', ast.hash)){
  1111. dlSize += (ast.size*1)
  1112. assetDlQueue.push(ast)
  1113. }
  1114. cb()
  1115. }, (err) => {
  1116. self.assets = new DLTracker(assetDlQueue, dlSize)
  1117. resolve()
  1118. })
  1119. })
  1120. }
  1121. // #endregion
  1122. // Library (Category=''') Validation Functions
  1123. // #region
  1124. /**
  1125. * Public library validation function. This function will handle the validation of libraries.
  1126. * It will parse the version data, analyzing each library entry. In this analysis, it will
  1127. * check to see if the local file exists and is valid. If not, it will be added to the download
  1128. * queue for the 'libraries' identifier.
  1129. *
  1130. * @param {Object} versionData The version data for the assets.
  1131. * @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
  1132. */
  1133. validateLibraries(versionData){
  1134. const self = this
  1135. return new Promise((resolve, reject) => {
  1136. const libArr = versionData.libraries
  1137. const libPath = path.join(self.commonPath, 'libraries')
  1138. const libDlQueue = []
  1139. let dlSize = 0
  1140. //Check validity of each library. If the hashs don't match, download the library.
  1141. async.eachLimit(libArr, 5, (lib, cb) => {
  1142. if(Library.validateRules(lib.rules, lib.natives)){
  1143. let artifact = (lib.natives == null) ? lib.downloads.artifact : lib.downloads.classifiers[lib.natives[Library.mojangFriendlyOS()].replace('${arch}', process.arch.replace('x', ''))]
  1144. const libItm = new Library(lib.name, artifact.sha1, artifact.size, artifact.url, path.join(libPath, artifact.path))
  1145. if(!AssetGuard._validateLocal(libItm.to, 'sha1', libItm.hash)){
  1146. dlSize += (libItm.size*1)
  1147. libDlQueue.push(libItm)
  1148. }
  1149. }
  1150. cb()
  1151. }, (err) => {
  1152. self.libraries = new DLTracker(libDlQueue, dlSize)
  1153. resolve()
  1154. })
  1155. })
  1156. }
  1157. // #endregion
  1158. // Miscellaneous (Category=files) Validation Functions
  1159. // #region
  1160. /**
  1161. * Public miscellaneous mojang file validation function. These files will be enqueued under
  1162. * the 'files' identifier.
  1163. *
  1164. * @param {Object} versionData The version data for the assets.
  1165. * @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
  1166. */
  1167. validateMiscellaneous(versionData){
  1168. const self = this
  1169. return new Promise(async (resolve, reject) => {
  1170. await self.validateClient(versionData)
  1171. await self.validateLogConfig(versionData)
  1172. resolve()
  1173. })
  1174. }
  1175. /**
  1176. * Validate client file - artifact renamed from client.jar to '{version}'.jar.
  1177. *
  1178. * @param {Object} versionData The version data for the assets.
  1179. * @param {boolean} force Optional. If true, the asset index will be downloaded even if it exists locally. Defaults to false.
  1180. * @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
  1181. */
  1182. validateClient(versionData, force = false){
  1183. const self = this
  1184. return new Promise((resolve, reject) => {
  1185. const clientData = versionData.downloads.client
  1186. const version = versionData.id
  1187. const targetPath = path.join(self.commonPath, 'versions', version)
  1188. const targetFile = version + '.jar'
  1189. let client = new Asset(version + ' client', clientData.sha1, clientData.size, clientData.url, path.join(targetPath, targetFile))
  1190. if(!AssetGuard._validateLocal(client.to, 'sha1', client.hash) || force){
  1191. self.files.dlqueue.push(client)
  1192. self.files.dlsize += client.size*1
  1193. resolve()
  1194. } else {
  1195. resolve()
  1196. }
  1197. })
  1198. }
  1199. /**
  1200. * Validate log config.
  1201. *
  1202. * @param {Object} versionData The version data for the assets.
  1203. * @param {boolean} force Optional. If true, the asset index will be downloaded even if it exists locally. Defaults to false.
  1204. * @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
  1205. */
  1206. validateLogConfig(versionData){
  1207. const self = this
  1208. return new Promise((resolve, reject) => {
  1209. const client = versionData.logging.client
  1210. const file = client.file
  1211. const targetPath = path.join(self.commonPath, 'assets', 'log_configs')
  1212. let logConfig = new Asset(file.id, file.sha1, file.size, file.url, path.join(targetPath, file.id))
  1213. if(!AssetGuard._validateLocal(logConfig.to, 'sha1', logConfig.hash)){
  1214. self.files.dlqueue.push(logConfig)
  1215. self.files.dlsize += logConfig.size*1
  1216. resolve()
  1217. } else {
  1218. resolve()
  1219. }
  1220. })
  1221. }
  1222. // #endregion
  1223. // Distribution (Category=forge) Validation Functions
  1224. // #region
  1225. /**
  1226. * Validate the distribution.
  1227. *
  1228. * @param {Server} server The Server to validate.
  1229. * @returns {Promise.<Object>} A promise which resolves to the server distribution object.
  1230. */
  1231. validateDistribution(server){
  1232. const self = this
  1233. return new Promise((resolve, reject) => {
  1234. self.forge = self._parseDistroModules(server.getModules(), server.getMinecraftVersion(), server.getID())
  1235. resolve(server)
  1236. })
  1237. }
  1238. _parseDistroModules(modules, version, servid){
  1239. let alist = []
  1240. let asize = 0
  1241. for(let ob of modules){
  1242. let obArtifact = ob.getArtifact()
  1243. let obPath = obArtifact.getPath()
  1244. let artifact = new DistroModule(ob.getIdentifier(), obArtifact.getHash(), obArtifact.getSize(), obArtifact.getURL(), obPath, ob.getType())
  1245. const validationPath = obPath.toLowerCase().endsWith('.pack.xz') ? obPath.substring(0, obPath.toLowerCase().lastIndexOf('.pack.xz')) : obPath
  1246. if(!AssetGuard._validateLocal(validationPath, 'MD5', artifact.hash)){
  1247. asize += artifact.size*1
  1248. alist.push(artifact)
  1249. if(validationPath !== obPath) this.extractQueue.push(obPath)
  1250. }
  1251. //Recursively process the submodules then combine the results.
  1252. if(ob.getSubModules() != null){
  1253. let dltrack = this._parseDistroModules(ob.getSubModules(), version, servid)
  1254. asize += dltrack.dlsize*1
  1255. alist = alist.concat(dltrack.dlqueue)
  1256. }
  1257. }
  1258. return new DLTracker(alist, asize)
  1259. }
  1260. /**
  1261. * Loads Forge's version.json data into memory for the specified server id.
  1262. *
  1263. * @param {string} server The Server to load Forge data for.
  1264. * @returns {Promise.<Object>} A promise which resolves to Forge's version.json data.
  1265. */
  1266. loadForgeData(server){
  1267. const self = this
  1268. return new Promise(async (resolve, reject) => {
  1269. const modules = server.getModules()
  1270. for(let ob of modules){
  1271. const type = ob.getType()
  1272. if(type === DistroManager.Types.ForgeHosted || type === DistroManager.Types.Forge){
  1273. if(Util.mcVersionAtLeast('1.13', server.getMinecraftVersion())){
  1274. // Read Manifest
  1275. for(let sub of ob.getSubModules()){
  1276. if(sub.getType() === DistroManager.Types.VersionManifest){
  1277. resolve(JSON.parse(fs.readFileSync(sub.getArtifact().getPath(), 'utf-8')))
  1278. return
  1279. }
  1280. }
  1281. reject('No forge version manifest found!')
  1282. return
  1283. } else {
  1284. let obArtifact = ob.getArtifact()
  1285. let obPath = obArtifact.getPath()
  1286. let asset = new DistroModule(ob.getIdentifier(), obArtifact.getHash(), obArtifact.getSize(), obArtifact.getURL(), obPath, type)
  1287. try {
  1288. let forgeData = await AssetGuard._finalizeForgeAsset(asset, self.commonPath)
  1289. resolve(forgeData)
  1290. } catch (err){
  1291. reject(err)
  1292. }
  1293. return
  1294. }
  1295. }
  1296. }
  1297. reject('No forge module found!')
  1298. })
  1299. }
  1300. _parseForgeLibraries(){
  1301. /* TODO
  1302. * Forge asset validations are already implemented. When there's nothing much
  1303. * to work on, implement forge downloads using forge's version.json. This is to
  1304. * have the code on standby if we ever need it (since it's half implemented already).
  1305. */
  1306. }
  1307. // #endregion
  1308. // Java (Category=''') Validation (download) Functions
  1309. // #region
  1310. _enqueueOpenJDK(dataDir){
  1311. return new Promise((resolve, reject) => {
  1312. JavaGuard._latestOpenJDK('8').then(verData => {
  1313. if(verData != null){
  1314. dataDir = path.join(dataDir, 'runtime', 'x64')
  1315. const fDir = path.join(dataDir, verData.name)
  1316. const jre = new Asset(verData.name, null, verData.size, verData.uri, fDir)
  1317. this.java = new DLTracker([jre], jre.size, (a, self) => {
  1318. if(verData.name.endsWith('zip')){
  1319. const zip = new AdmZip(a.to)
  1320. const pos = path.join(dataDir, zip.getEntries()[0].entryName)
  1321. zip.extractAllToAsync(dataDir, true, (err) => {
  1322. if(err){
  1323. console.log(err)
  1324. self.emit('complete', 'java', JavaGuard.javaExecFromRoot(pos))
  1325. } else {
  1326. fs.unlink(a.to, err => {
  1327. if(err){
  1328. console.log(err)
  1329. }
  1330. self.emit('complete', 'java', JavaGuard.javaExecFromRoot(pos))
  1331. })
  1332. }
  1333. })
  1334. } else {
  1335. // Tar.gz
  1336. let h = null
  1337. fs.createReadStream(a.to)
  1338. .on('error', err => console.log(err))
  1339. .pipe(zlib.createGunzip())
  1340. .on('error', err => console.log(err))
  1341. .pipe(tar.extract(dataDir, {
  1342. map: (header) => {
  1343. if(h == null){
  1344. h = header.name
  1345. }
  1346. }
  1347. }))
  1348. .on('error', err => console.log(err))
  1349. .on('finish', () => {
  1350. fs.unlink(a.to, err => {
  1351. if(err){
  1352. console.log(err)
  1353. }
  1354. if(h.indexOf('/') > -1){
  1355. h = h.substring(0, h.indexOf('/'))
  1356. }
  1357. const pos = path.join(dataDir, h)
  1358. self.emit('complete', 'java', JavaGuard.javaExecFromRoot(pos))
  1359. })
  1360. })
  1361. }
  1362. })
  1363. resolve(true)
  1364. } else {
  1365. resolve(false)
  1366. }
  1367. })
  1368. })
  1369. }
  1370. // _enqueueOracleJRE(dataDir){
  1371. // return new Promise((resolve, reject) => {
  1372. // JavaGuard._latestJREOracle().then(verData => {
  1373. // if(verData != null){
  1374. // const combined = verData.uri + PLATFORM_MAP[process.platform]
  1375. // const opts = {
  1376. // url: combined,
  1377. // headers: {
  1378. // 'Cookie': 'oraclelicense=accept-securebackup-cookie'
  1379. // }
  1380. // }
  1381. // request.head(opts, (err, resp, body) => {
  1382. // if(err){
  1383. // resolve(false)
  1384. // } else {
  1385. // dataDir = path.join(dataDir, 'runtime', 'x64')
  1386. // const name = combined.substring(combined.lastIndexOf('/')+1)
  1387. // const fDir = path.join(dataDir, name)
  1388. // const jre = new Asset(name, null, parseInt(resp.headers['content-length']), opts, fDir)
  1389. // this.java = new DLTracker([jre], jre.size, (a, self) => {
  1390. // let h = null
  1391. // fs.createReadStream(a.to)
  1392. // .on('error', err => console.log(err))
  1393. // .pipe(zlib.createGunzip())
  1394. // .on('error', err => console.log(err))
  1395. // .pipe(tar.extract(dataDir, {
  1396. // map: (header) => {
  1397. // if(h == null){
  1398. // h = header.name
  1399. // }
  1400. // }
  1401. // }))
  1402. // .on('error', err => console.log(err))
  1403. // .on('finish', () => {
  1404. // fs.unlink(a.to, err => {
  1405. // if(err){
  1406. // console.log(err)
  1407. // }
  1408. // if(h.indexOf('/') > -1){
  1409. // h = h.substring(0, h.indexOf('/'))
  1410. // }
  1411. // const pos = path.join(dataDir, h)
  1412. // self.emit('complete', 'java', JavaGuard.javaExecFromRoot(pos))
  1413. // })
  1414. // })
  1415. // })
  1416. // resolve(true)
  1417. // }
  1418. // })
  1419. // } else {
  1420. // resolve(false)
  1421. // }
  1422. // })
  1423. // })
  1424. // }
  1425. // _enqueueMojangJRE(dir){
  1426. // return new Promise((resolve, reject) => {
  1427. // // Mojang does not host the JRE for linux.
  1428. // if(process.platform === 'linux'){
  1429. // resolve(false)
  1430. // }
  1431. // AssetGuard.loadMojangLauncherData().then(data => {
  1432. // if(data != null) {
  1433. // try {
  1434. // const mJRE = data[Library.mojangFriendlyOS()]['64'].jre
  1435. // const url = mJRE.url
  1436. // request.head(url, (err, resp, body) => {
  1437. // if(err){
  1438. // resolve(false)
  1439. // } else {
  1440. // const name = url.substring(url.lastIndexOf('/')+1)
  1441. // const fDir = path.join(dir, name)
  1442. // const jre = new Asset('jre' + mJRE.version, mJRE.sha1, resp.headers['content-length'], url, fDir)
  1443. // this.java = new DLTracker([jre], jre.size, a => {
  1444. // fs.readFile(a.to, (err, data) => {
  1445. // // Data buffer needs to be decompressed from lzma,
  1446. // // not really possible using node.js
  1447. // })
  1448. // })
  1449. // }
  1450. // })
  1451. // } catch (err){
  1452. // resolve(false)
  1453. // }
  1454. // }
  1455. // })
  1456. // })
  1457. // }
  1458. // #endregion
  1459. // #endregion
  1460. // Control Flow Functions
  1461. // #region
  1462. /**
  1463. * Initiate an async download process for an AssetGuard DLTracker.
  1464. *
  1465. * @param {string} identifier The identifier of the AssetGuard DLTracker.
  1466. * @param {number} limit Optional. The number of async processes to run in parallel.
  1467. * @returns {boolean} True if the process began, otherwise false.
  1468. */
  1469. startAsyncProcess(identifier, limit = 5){
  1470. const self = this
  1471. const dlTracker = this[identifier]
  1472. const dlQueue = dlTracker.dlqueue
  1473. if(dlQueue.length > 0){
  1474. console.log('DLQueue', dlQueue)
  1475. async.eachLimit(dlQueue, limit, (asset, cb) => {
  1476. fs.ensureDirSync(path.join(asset.to, '..'))
  1477. let req = request(asset.from)
  1478. req.pause()
  1479. req.on('response', (resp) => {
  1480. if(resp.statusCode === 200){
  1481. let doHashCheck = false
  1482. const contentLength = parseInt(resp.headers['content-length'])
  1483. if(contentLength !== asset.size){
  1484. console.log(`WARN: Got ${contentLength} bytes for ${asset.id}: Expected ${asset.size}`)
  1485. doHashCheck = true
  1486. // Adjust download
  1487. this.totaldlsize -= asset.size
  1488. this.totaldlsize += contentLength
  1489. }
  1490. let writeStream = fs.createWriteStream(asset.to)
  1491. writeStream.on('close', () => {
  1492. if(dlTracker.callback != null){
  1493. dlTracker.callback.apply(dlTracker, [asset, self])
  1494. }
  1495. if(doHashCheck){
  1496. const v = AssetGuard._validateLocal(asset.to, asset.type != null ? 'md5' : 'sha1', asset.hash)
  1497. if(v){
  1498. console.log(`Hashes match for ${asset.id}, byte mismatch is an issue in the distro index.`)
  1499. } else {
  1500. console.error(`Hashes do not match, ${asset.id} may be corrupted.`)
  1501. }
  1502. }
  1503. cb()
  1504. })
  1505. req.pipe(writeStream)
  1506. req.resume()
  1507. } else {
  1508. req.abort()
  1509. console.log(`Failed to download ${asset.id}(${typeof asset.from === 'object' ? asset.from.url : asset.from}). Response code ${resp.statusCode}`)
  1510. self.progress += asset.size*1
  1511. self.emit('progress', 'download', self.progress, self.totaldlsize)
  1512. cb()
  1513. }
  1514. })
  1515. req.on('error', (err) => {
  1516. self.emit('error', 'download', err)
  1517. })
  1518. req.on('data', (chunk) => {
  1519. self.progress += chunk.length
  1520. self.emit('progress', 'download', self.progress, self.totaldlsize)
  1521. })
  1522. }, (err) => {
  1523. if(err){
  1524. console.log('An item in ' + identifier + ' failed to process')
  1525. } else {
  1526. console.log('All ' + identifier + ' have been processed successfully')
  1527. }
  1528. //self.totaldlsize -= dlTracker.dlsize
  1529. //self.progress -= dlTracker.dlsize
  1530. self[identifier] = new DLTracker([], 0)
  1531. if(self.progress >= self.totaldlsize) {
  1532. if(self.extractQueue.length > 0){
  1533. self.emit('progress', 'extract', 1, 1)
  1534. //self.emit('extracting')
  1535. AssetGuard._extractPackXZ(self.extractQueue, self.javaexec).then(() => {
  1536. self.extractQueue = []
  1537. self.emit('complete', 'download')
  1538. })
  1539. } else {
  1540. self.emit('complete', 'download')
  1541. }
  1542. }
  1543. })
  1544. return true
  1545. } else {
  1546. return false
  1547. }
  1548. }
  1549. /**
  1550. * This function will initiate the download processed for the specified identifiers. If no argument is
  1551. * given, all identifiers will be initiated. Note that in order for files to be processed you need to run
  1552. * the processing function corresponding to that identifier. If you run this function without processing
  1553. * the files, it is likely nothing will be enqueued in the object and processing will complete
  1554. * immediately. Once all downloads are complete, this function will fire the 'complete' event on the
  1555. * global object instance.
  1556. *
  1557. * @param {Array.<{id: string, limit: number}>} identifiers Optional. The identifiers to process and corresponding parallel async task limit.
  1558. */
  1559. processDlQueues(identifiers = [{id:'assets', limit:20}, {id:'libraries', limit:5}, {id:'files', limit:5}, {id:'forge', limit:5}]){
  1560. return new Promise((resolve, reject) => {
  1561. let shouldFire = true
  1562. // Assign dltracking variables.
  1563. this.totaldlsize = 0
  1564. this.progress = 0
  1565. for(let iden of identifiers){
  1566. this.totaldlsize += this[iden.id].dlsize
  1567. }
  1568. this.once('complete', (data) => {
  1569. resolve()
  1570. })
  1571. for(let iden of identifiers){
  1572. let r = this.startAsyncProcess(iden.id, iden.limit)
  1573. if(r) shouldFire = false
  1574. }
  1575. if(shouldFire){
  1576. this.emit('complete', 'download')
  1577. }
  1578. })
  1579. }
  1580. async validateEverything(serverid, dev = false){
  1581. try {
  1582. if(!ConfigManager.isLoaded()){
  1583. ConfigManager.load()
  1584. }
  1585. DistroManager.setDevMode(dev)
  1586. const dI = await DistroManager.pullLocal()
  1587. const server = dI.getServer(serverid)
  1588. // Validate Everything
  1589. await this.validateDistribution(server)
  1590. this.emit('validate', 'distribution')
  1591. const versionData = await this.loadVersionData(server.getMinecraftVersion())
  1592. this.emit('validate', 'version')
  1593. await this.validateAssets(versionData)
  1594. this.emit('validate', 'assets')
  1595. await this.validateLibraries(versionData)
  1596. this.emit('validate', 'libraries')
  1597. await this.validateMiscellaneous(versionData)
  1598. this.emit('validate', 'files')
  1599. await this.processDlQueues()
  1600. //this.emit('complete', 'download')
  1601. const forgeData = await this.loadForgeData(server)
  1602. return {
  1603. versionData,
  1604. forgeData
  1605. }
  1606. } catch (err){
  1607. return {
  1608. versionData: null,
  1609. forgeData: null,
  1610. error: err
  1611. }
  1612. }
  1613. }
  1614. // #endregion
  1615. }
  1616. module.exports = {
  1617. Util,
  1618. AssetGuard,
  1619. JavaGuard,
  1620. Asset,
  1621. Library
  1622. }