assetguard.js 65 KB

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