assetguard.js 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  1. /**
  2. * AssetGuard
  3. *
  4. * This module aims to provide a comprehensive and stable method for processing
  5. * and downloading game assets for the WesterosCraft server. Download meta is
  6. * for several identifiers (categories) is stored inside of an AssetGuard object.
  7. * This meta data is initially empty until one of the module's processing functions
  8. * are called. That function will process the corresponding asset index and validate
  9. * any exisitng local files. If a file is missing or fails validation, it will be
  10. * placed into a download queue (array). This queue is wrapped in a download tracker object
  11. * so that essential information can be cached. The download tracker object is then
  12. * assigned as the value of the identifier in the AssetGuard object. These download
  13. * trackers will remain idle until an async process is started to process them.
  14. *
  15. * Once the async process is started, any enqueued assets will be downloaded. The AssetGuard
  16. * object will emit events throughout the download whose name correspond to the identifier
  17. * being processed. For example, if the 'assets' identifier was being processed, whenever
  18. * the download stream recieves data, the event 'assetsdlprogress' will be emitted off of
  19. * the AssetGuard instance. This can be listened to by external modules allowing for
  20. * categorical tracking of the downloading process.
  21. *
  22. * @module assetguard
  23. */
  24. // Requirements
  25. const AdmZip = require('adm-zip')
  26. const async = require('async')
  27. const child_process = require('child_process')
  28. const crypto = require('crypto')
  29. const EventEmitter = require('events')
  30. const fs = require('fs')
  31. const mkpath = require('mkdirp');
  32. const path = require('path')
  33. const Registry = require('winreg')
  34. const request = require('request')
  35. const tar = require('tar-fs')
  36. const zlib = require('zlib')
  37. // Constants
  38. const PLATFORM_MAP = {
  39. win32: '-windows-x64.tar.gz',
  40. darwin: '-macosx-x64.tar.gz',
  41. linux: '-linux-x64.tar.gz'
  42. }
  43. // Classes
  44. /** Class representing a base asset. */
  45. class Asset {
  46. /**
  47. * Create an asset.
  48. *
  49. * @param {any} id The id of the asset.
  50. * @param {string} hash The hash value of the asset.
  51. * @param {number} size The size in bytes of the asset.
  52. * @param {string} from The url where the asset can be found.
  53. * @param {string} to The absolute local file path of the asset.
  54. */
  55. constructor(id, hash, size, from, to){
  56. this.id = id
  57. this.hash = hash
  58. this.size = size
  59. this.from = from
  60. this.to = to
  61. }
  62. }
  63. /** Class representing a mojang library. */
  64. class Library extends Asset {
  65. /**
  66. * Converts the process.platform OS names to match mojang's OS names.
  67. */
  68. static mojangFriendlyOS(){
  69. const opSys = process.platform
  70. if (opSys === 'darwin') {
  71. return 'osx';
  72. } else if (opSys === 'win32'){
  73. return 'windows';
  74. } else if (opSys === 'linux'){
  75. return 'linux';
  76. } else {
  77. return 'unknown_os';
  78. }
  79. }
  80. /**
  81. * Checks whether or not a library is valid for download on a particular OS, following
  82. * the rule format specified in the mojang version data index. If the allow property has
  83. * an OS specified, then the library can ONLY be downloaded on that OS. If the disallow
  84. * property has instead specified an OS, the library can be downloaded on any OS EXCLUDING
  85. * the one specified.
  86. *
  87. * @param {Object} rules The Library's download rules.
  88. * @returns {boolean} True if the Library follows the specified rules, otherwise false.
  89. */
  90. static validateRules(rules){
  91. if(rules == null) return true
  92. let result = true
  93. rules.forEach(function(rule){
  94. const action = rule['action']
  95. const osProp = rule['os']
  96. if(action != null){
  97. if(osProp != null){
  98. const osName = osProp['name']
  99. const osMoj = Library.mojangFriendlyOS()
  100. if(action === 'allow'){
  101. result = osName === osMoj
  102. return
  103. } else if(action === 'disallow'){
  104. result = osName !== osMoj
  105. return
  106. }
  107. }
  108. }
  109. })
  110. return result
  111. }
  112. }
  113. class DistroModule extends Asset {
  114. /**
  115. * Create a DistroModule. This is for processing,
  116. * not equivalent to the module objects in the
  117. * distro index.
  118. *
  119. * @param {any} id The id of the asset.
  120. * @param {string} hash The hash value of the asset.
  121. * @param {number} size The size in bytes of the asset.
  122. * @param {string} from The url where the asset can be found.
  123. * @param {string} to The absolute local file path of the asset.
  124. * @param {string} type The the module type.
  125. */
  126. constructor(id, hash, size, from, to, type){
  127. super(id, hash, size, from, to)
  128. this.type = type
  129. }
  130. }
  131. /**
  132. * Class representing a download tracker. This is used to store meta data
  133. * about a download queue, including the queue itself.
  134. */
  135. class DLTracker {
  136. /**
  137. * Create a DLTracker
  138. *
  139. * @param {Array.<Asset>} dlqueue An array containing assets queued for download.
  140. * @param {number} dlsize The combined size of each asset in the download queue array.
  141. * @param {function(Asset)} callback Optional callback which is called when an asset finishes downloading.
  142. */
  143. constructor(dlqueue, dlsize, callback = null){
  144. this.dlqueue = dlqueue
  145. this.dlsize = dlsize
  146. this.callback = callback
  147. }
  148. }
  149. let distributionData = null
  150. /**
  151. * Central object class used for control flow. This object stores data about
  152. * categories of downloads. Each category is assigned an identifier with a
  153. * DLTracker object as its value. Combined information is also stored, such as
  154. * the total size of all the queued files in each category. This event is used
  155. * to emit events so that external modules can listen into processing done in
  156. * this module.
  157. */
  158. class AssetGuard extends EventEmitter {
  159. /**
  160. * Create an instance of AssetGuard.
  161. * On creation the object's properties are never-null default
  162. * values. Each identifier is resolved to an empty DLTracker.
  163. *
  164. * @param {string} basePath The base path for asset validation (game root).
  165. * @param {string} javaexec The path to a java executable which will be used
  166. * to finalize installation.
  167. */
  168. constructor(basePath, javaexec){
  169. super()
  170. this.totaldlsize = 0
  171. this.progress = 0
  172. this.assets = new DLTracker([], 0)
  173. this.libraries = new DLTracker([], 0)
  174. this.files = new DLTracker([], 0)
  175. this.forge = new DLTracker([], 0)
  176. this.java = new DLTracker([], 0)
  177. this.basePath = basePath
  178. this.javaexec = javaexec
  179. }
  180. // Static Utility Functions
  181. // #region
  182. // Static General Resolve Functions
  183. // #region
  184. /**
  185. * Resolve an artifact id into a path. For example, on windows
  186. * 'net.minecraftforge:forge:1.11.2-13.20.0.2282', '.jar' becomes
  187. * net\minecraftforge\forge\1.11.2-13.20.0.2282\forge-1.11.2-13.20.0.2282.jar
  188. *
  189. * @param {string} artifactid The artifact id string.
  190. * @param {string} extension The extension of the file at the resolved path.
  191. * @returns {string} The resolved relative path from the artifact id.
  192. */
  193. static _resolvePath(artifactid, extension){
  194. let ps = artifactid.split(':')
  195. let cs = ps[0].split('.')
  196. cs.push(ps[1])
  197. cs.push(ps[2])
  198. cs.push(ps[1].concat('-').concat(ps[2]).concat(extension))
  199. return path.join.apply(path, cs)
  200. }
  201. /**
  202. * Resolve an artifact id into a URL. For example,
  203. * 'net.minecraftforge:forge:1.11.2-13.20.0.2282', '.jar' becomes
  204. * net/minecraftforge/forge/1.11.2-13.20.0.2282/forge-1.11.2-13.20.0.2282.jar
  205. *
  206. * @param {string} artifactid The artifact id string.
  207. * @param {string} extension The extension of the file at the resolved url.
  208. * @returns {string} The resolved relative URL from the artifact id.
  209. */
  210. static _resolveURL(artifactid, extension){
  211. let ps = artifactid.split(':')
  212. let cs = ps[0].split('.')
  213. cs.push(ps[1])
  214. cs.push(ps[2])
  215. cs.push(ps[1].concat('-').concat(ps[2]).concat(extension))
  216. return cs.join('/')
  217. }
  218. // #endregion
  219. // Static Hash Validation Functions
  220. // #region
  221. /**
  222. * Calculates the hash for a file using the specified algorithm.
  223. *
  224. * @param {Buffer} buf The buffer containing file data.
  225. * @param {string} algo The hash algorithm.
  226. * @returns {string} The calculated hash in hex.
  227. */
  228. static _calculateHash(buf, algo){
  229. return crypto.createHash(algo).update(buf).digest('hex')
  230. }
  231. /**
  232. * Used to parse a checksums file. This is specifically designed for
  233. * the checksums.sha1 files found inside the forge scala dependencies.
  234. *
  235. * @param {string} content The string content of the checksums file.
  236. * @returns {Object} An object with keys being the file names, and values being the hashes.
  237. */
  238. static _parseChecksumsFile(content){
  239. let finalContent = {}
  240. let lines = content.split('\n')
  241. for(let i=0; i<lines.length; i++){
  242. let bits = lines[i].split(' ')
  243. if(bits[1] == null) {
  244. continue
  245. }
  246. finalContent[bits[1]] = bits[0]
  247. }
  248. return finalContent
  249. }
  250. /**
  251. * Validate that a file exists and matches a given hash value.
  252. *
  253. * @param {string} filePath The path of the file to validate.
  254. * @param {string} algo The hash algorithm to check against.
  255. * @param {string} hash The existing hash to check against.
  256. * @returns {boolean} True if the file exists and calculated hash matches the given hash, otherwise false.
  257. */
  258. static _validateLocal(filePath, algo, hash){
  259. if(fs.existsSync(filePath)){
  260. //No hash provided, have to assume it's good.
  261. if(hash == null){
  262. return true
  263. }
  264. let fileName = path.basename(filePath)
  265. let buf = fs.readFileSync(filePath)
  266. let calcdhash = AssetGuard._calculateHash(buf, algo)
  267. return calcdhash === hash
  268. }
  269. return false;
  270. }
  271. /**
  272. * Validates a file in the style used by forge's version index.
  273. *
  274. * @param {string} filePath The path of the file to validate.
  275. * @param {Array.<string>} checksums The checksums listed in the forge version index.
  276. * @returns {boolean} True if the file exists and the hashes match, otherwise false.
  277. */
  278. static _validateForgeChecksum(filePath, checksums){
  279. if(fs.existsSync(filePath)){
  280. if(checksums == null || checksums.length === 0){
  281. return true
  282. }
  283. let buf = fs.readFileSync(filePath)
  284. let calcdhash = AssetGuard._calculateHash(buf, 'sha1')
  285. let valid = checksums.includes(calcdhash)
  286. if(!valid && filePath.endsWith('.jar')){
  287. valid = AssetGuard._validateForgeJar(filePath, checksums)
  288. }
  289. return valid
  290. }
  291. return false
  292. }
  293. /**
  294. * Validates a forge jar file dependency who declares a checksums.sha1 file.
  295. * This can be an expensive task as it usually requires that we calculate thousands
  296. * of hashes.
  297. *
  298. * @param {Buffer} buf The buffer of the jar file.
  299. * @param {Array.<string>} checksums The checksums listed in the forge version index.
  300. * @returns {boolean} True if all hashes declared in the checksums.sha1 file match the actual hashes.
  301. */
  302. static _validateForgeJar(buf, checksums){
  303. // Double pass method was the quickest I found. I tried a version where we store data
  304. // to only require a single pass, plus some quick cleanup but that seemed to take slightly more time.
  305. const hashes = {}
  306. let expected = {}
  307. const zip = new AdmZip(buf)
  308. const zipEntries = zip.getEntries()
  309. //First pass
  310. for(let i=0; i<zipEntries.length; i++){
  311. let entry = zipEntries[i]
  312. if(entry.entryName === 'checksums.sha1'){
  313. expected = AssetGuard._parseChecksumsFile(zip.readAsText(entry))
  314. }
  315. hashes[entry.entryName] = AssetGuard._calculateHash(entry.getData(), 'sha1')
  316. }
  317. if(!checksums.includes(hashes['checksums.sha1'])){
  318. return false
  319. }
  320. //Check against expected
  321. const expectedEntries = Object.keys(expected)
  322. for(let i=0; i<expectedEntries.length; i++){
  323. if(expected[expectedEntries[i]] !== hashes[expectedEntries[i]]){
  324. return false
  325. }
  326. }
  327. return true
  328. }
  329. // #endregion
  330. // Static Distribution Index Functions
  331. // #region
  332. /**
  333. * Statically retrieve the distribution data.
  334. *
  335. * @param {string} basePath The base path for asset validation (game root).
  336. * @param {boolean} cached Optional. False if the distro should be freshly downloaded, else
  337. * a cached copy will be returned.
  338. * @returns {Promise.<Object>} A promise which resolves to the distribution data object.
  339. */
  340. static retrieveDistributionData(basePath, cached = true){
  341. return new Promise(function(fulfill, reject){
  342. if(!cached || distributionData == null){
  343. // TODO Download file from upstream.
  344. //const distroURL = 'http://mc.westeroscraft.com/WesterosCraftLauncher/westeroscraft.json'
  345. // TODO Save file to path.join(basePath, 'westeroscraft.json')
  346. // TODO Fulfill with JSON.parse()
  347. // Workaround while file is not hosted.
  348. fs.readFile(path.join(__dirname, '..', 'westeroscraft.json'), 'utf-8', (err, data) => {
  349. distributionData = JSON.parse(data)
  350. fulfill(distributionData)
  351. })
  352. } else {
  353. fulfill(distributionData)
  354. }
  355. })
  356. }
  357. /**
  358. * Statically retrieve the distribution data.
  359. *
  360. * @param {string} basePath The base path for asset validation (game root).
  361. * @param {boolean} cached Optional. False if the distro should be freshly downloaded, else
  362. * a cached copy will be returned.
  363. * @returns {Object} The distribution data object.
  364. */
  365. static retrieveDistributionDataSync(basePath, cached = true){
  366. if(!cached || distributionData == null){
  367. distributionData = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'westeroscraft.json'), 'utf-8'))
  368. }
  369. return distributionData
  370. }
  371. /**
  372. * Resolve the default selected server from the distribution index.
  373. *
  374. * @param {string} basePath The base path for asset validation (game root).
  375. * @returns {Object} An object resolving to the default selected server.
  376. */
  377. static resolveSelectedServer(basePath){
  378. const distro = AssetGuard.retrieveDistributionDataSync(basePath)
  379. const servers = distro.servers
  380. for(let i=0; i<servers.length; i++){
  381. if(servers[i].default_selected){
  382. return servers[i].id
  383. }
  384. }
  385. // If no server declares default_selected, default to the first one declared.
  386. return (servers.length > 0) ? servers[0].id : null
  387. }
  388. /**
  389. * Gets a server from the distro index which maches the provided ID.
  390. * Returns null if the ID could not be found or the distro index has
  391. * not yet been loaded.
  392. *
  393. * @param {string} basePath The base path for asset validation (game root).
  394. * @param {string} serverID The id of the server to retrieve.
  395. * @returns {Object} The server object whose id matches the parameter.
  396. */
  397. static getServerById(basePath, serverID){
  398. if(distributionData == null){
  399. AssetGuard.retrieveDistributionDataSync(basePath, false)
  400. }
  401. const servers = distributionData.servers
  402. let serv = null
  403. for(let i=0; i<servers.length; i++){
  404. if(servers[i].id === serverID){
  405. serv = servers[i]
  406. }
  407. }
  408. return serv
  409. }
  410. // #endregion
  411. // Miscellaneous Static Functions
  412. // #region
  413. /**
  414. * Extracts and unpacks a file from .pack.xz format.
  415. *
  416. * @param {Array.<string>} filePaths The paths of the files to be extracted and unpacked.
  417. * @returns {Promise.<void>} An empty promise to indicate the extraction has completed.
  418. */
  419. static _extractPackXZ(filePaths, javaExecutable){
  420. return new Promise(function(fulfill, reject){
  421. const libPath = path.join(__dirname, '..', 'libraries', 'java', 'PackXZExtract.jar')
  422. const filePath = filePaths.join(',')
  423. const child = child_process.spawn(javaExecutable, ['-jar', libPath, '-packxz', filePath])
  424. child.stdout.on('data', (data) => {
  425. //console.log('PackXZExtract:', data.toString('utf8'))
  426. })
  427. child.stderr.on('data', (data) => {
  428. //console.log('PackXZExtract:', data.toString('utf8'))
  429. })
  430. child.on('close', (code, signal) => {
  431. //console.log('PackXZExtract: Exited with code', code)
  432. fulfill()
  433. })
  434. })
  435. }
  436. /**
  437. * Function which finalizes the forge installation process. This creates a 'version'
  438. * instance for forge and saves its version.json file into that instance. If that
  439. * instance already exists, the contents of the version.json file are read and returned
  440. * in a promise.
  441. *
  442. * @param {Asset} asset The Asset object representing Forge.
  443. * @param {string} basePath Base path for asset validation (game root).
  444. * @returns {Promise.<Object>} A promise which resolves to the contents of forge's version.json.
  445. */
  446. static _finalizeForgeAsset(asset, basePath){
  447. return new Promise(function(fulfill, reject){
  448. fs.readFile(asset.to, (err, data) => {
  449. const zip = new AdmZip(data)
  450. const zipEntries = zip.getEntries()
  451. for(let i=0; i<zipEntries.length; i++){
  452. if(zipEntries[i].entryName === 'version.json'){
  453. const forgeVersion = JSON.parse(zip.readAsText(zipEntries[i]))
  454. const versionPath = path.join(basePath, 'versions', forgeVersion.id)
  455. const versionFile = path.join(versionPath, forgeVersion.id + '.json')
  456. if(!fs.existsSync(versionFile)){
  457. mkpath.sync(versionPath)
  458. fs.writeFileSync(path.join(versionPath, forgeVersion.id + '.json'), zipEntries[i].getData())
  459. fulfill(forgeVersion)
  460. } else {
  461. //Read the saved file to allow for user modifications.
  462. fulfill(JSON.parse(fs.readFileSync(versionFile, 'utf-8')))
  463. }
  464. return
  465. }
  466. }
  467. //We didn't find forge's version.json.
  468. reject('Unable to finalize Forge processing, version.json not found! Has forge changed their format?')
  469. })
  470. })
  471. }
  472. // #endregion
  473. // Static Java Utility
  474. // #region
  475. /**
  476. * @typedef OracleJREData
  477. * @property {string} uri The base uri of the JRE.
  478. * @property {{major: string, update: string, build: string}} version Object containing version information.
  479. */
  480. /**
  481. * Resolves the latest version of Oracle's JRE and parses its download link.
  482. *
  483. * @returns {Promise.<OracleJREData>} Promise which resolved to an object containing the JRE download data.
  484. */
  485. static _latestJREOracle(){
  486. const url = 'http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html'
  487. const regex = /http:\/\/.+?(?=\/java)\/java\/jdk\/([0-9]+u[0-9]+)-(b[0-9]+)\/([a-f0-9]{32})?\/jre-\1/
  488. return new Promise((resolve, reject) => {
  489. request(url, (err, resp, body) => {
  490. if(!err){
  491. const arr = body.match(regex)
  492. const verSplit = arr[1].split('u')
  493. resolve({
  494. uri: arr[0],
  495. version: {
  496. major: verSplit[0],
  497. update: verSplit[1],
  498. build: arr[2]
  499. }
  500. })
  501. } else {
  502. resolve(null)
  503. }
  504. })
  505. })
  506. }
  507. /**
  508. * Returns the path of the OS-specific executable for the given Java
  509. * installation. Supported OS's are win32, darwin, linux.
  510. *
  511. * @param {string} rootDir The root directory of the Java installation.
  512. */
  513. static javaExecFromRoot(rootDir){
  514. if(process.platform === 'win32'){
  515. return path.join(rootDir, 'bin', 'javaw.exe')
  516. } else if(process.platform === 'darwin'){
  517. return path.join(rootDir, 'Contents', 'Home', 'bin', 'java')
  518. } else if(process.platform === 'linux'){
  519. return path.join(rootDir, 'bin', 'java')
  520. }
  521. return rootDir
  522. }
  523. /**
  524. * Load Mojang's launcher.json file.
  525. *
  526. * @returns {Promise.<Object>} Promise which resolves to Mojang's launcher.json object.
  527. */
  528. static loadMojangLauncherData(){
  529. return new Promise((resolve, reject) => {
  530. request.get('https://launchermeta.mojang.com/mc/launcher.json', (err, resp, body) => {
  531. if(err){
  532. resolve(null)
  533. } else {
  534. resolve(JSON.parse(body))
  535. }
  536. })
  537. })
  538. }
  539. /**
  540. * Validates that a Java binary is at least 64 bit. This makes use of the non-standard
  541. * command line option -XshowSettings:properties. The output of this contains a property,
  542. * sun.arch.data.model = ARCH, in which ARCH is either 32 or 64. This option is supported
  543. * in Java 8 and 9. Since this is a non-standard option. This will resolve to true if
  544. * the function's code throws errors. That would indicate that the option is changed or
  545. * removed.
  546. *
  547. * @param {string} binaryExecPath Path to the java executable we wish to validate.
  548. *
  549. * @returns {Promise.<boolean>} Resolves to false only if the test is successful and the result
  550. * is less than 64.
  551. */
  552. static _validateJavaBinary(binaryExecPath){
  553. return new Promise((resolve, reject) => {
  554. if(fs.existsSync(binaryExecPath)){
  555. child_process.exec('"' + binaryExecPath + '" -XshowSettings:properties', (err, stdout, stderr) => {
  556. try {
  557. // Output is stored in stderr?
  558. const res = stderr
  559. const props = res.split('\n')
  560. for(let i=0; i<props.length; i++){
  561. if(props[i].indexOf('sun.arch.data.model') > -1){
  562. let arch = props[i].split('=')[1].trim()
  563. console.log(props[i].trim() + ' for ' + binaryExecPath)
  564. resolve(parseInt(arch) >= 64)
  565. }
  566. }
  567. // sun.arch.data.model not found?
  568. // Disregard this test.
  569. resolve(true)
  570. } catch (err){
  571. // Output format might have changed, validation cannot be completed.
  572. // Disregard this test in that case.
  573. resolve(true)
  574. }
  575. })
  576. } else {
  577. resolve(false)
  578. }
  579. })
  580. }
  581. /**
  582. * Checks for the presence of the environment variable JAVA_HOME. If it exits, we will check
  583. * to see if the value points to a path which exists. If the path exits, the path is returned.
  584. *
  585. * @returns {string} The path defined by JAVA_HOME, if it exists. Otherwise null.
  586. */
  587. static _scanJavaHome(){
  588. const jHome = process.env.JAVA_HOME
  589. try {
  590. let res = fs.existsSync(jHome)
  591. return res ? jHome : null
  592. } catch (err) {
  593. // Malformed JAVA_HOME property.
  594. return null
  595. }
  596. }
  597. /**
  598. * Scans the data folder's runtime directory for suitable JRE candidates.
  599. *
  600. * @param {string} dataDir The base launcher directory.
  601. * @returns {Promise.<Set.<string>>} A set containing suitable JRE candidates found
  602. * in the runtime directory.
  603. */
  604. static _scanDataFolder(dataDir){
  605. return new Promise((resolve, reject) => {
  606. const x64RuntimeDir = path.join(dataDir, 'runtime', 'x64')
  607. fs.exists(x64RuntimeDir, (e) => {
  608. let res = new Set()
  609. if(e){
  610. fs.readdir(x64RuntimeDir, (err, files) => {
  611. if(err){
  612. resolve(res)
  613. console.log(err)
  614. } else {
  615. for(let i=0; i<files.length; i++){
  616. res.add(path.join(x64RuntimeDir, files[i]))
  617. }
  618. resolve(res)
  619. }
  620. })
  621. } else {
  622. resolve(res)
  623. }
  624. })
  625. })
  626. }
  627. /**
  628. * Scans the registry for 64-bit Java entries. The paths of each entry are added to
  629. * a set and returned. Currently, only Java 8 (1.8) is supported.
  630. *
  631. * @returns {Promise.<Set.<string>>} A promise which resolves to a set of 64-bit Java root
  632. * paths found in the registry.
  633. */
  634. static _scanRegistry(){
  635. return new Promise((resolve, reject) => {
  636. // Keys for Java v9.0.0 and later:
  637. // 'SOFTWARE\\JavaSoft\\JRE'
  638. // 'SOFTWARE\\JavaSoft\\JDK'
  639. // Forge does not yet support Java 9, therefore we do not.
  640. let cbTracker = 0
  641. let cbAcc = 0
  642. // Keys for Java 1.8 and prior:
  643. const regKeys = [
  644. '\\SOFTWARE\\JavaSoft\\Java Runtime Environment',
  645. '\\SOFTWARE\\JavaSoft\\Java Development Kit'
  646. ]
  647. const candidates = new Set()
  648. for(let i=0; i<regKeys.length; i++){
  649. const key = new Registry({
  650. hive: Registry.HKLM,
  651. key: regKeys[i],
  652. arch: 'x64'
  653. })
  654. key.keyExists((err, exists) => {
  655. if(exists) {
  656. key.keys((err, javaVers) => {
  657. if(err){
  658. console.error(err)
  659. if(i === regKeys.length-1 && cbAcc === cbTracker){
  660. resolve(candidates)
  661. }
  662. } else {
  663. cbTracker += javaVers.length
  664. if(i === regKeys.length-1 && cbTracker === cbAcc){
  665. resolve(candidates)
  666. } else {
  667. for(let j=0; j<javaVers.length; j++){
  668. const javaVer = javaVers[j]
  669. const vKey = javaVer.key.substring(javaVer.key.lastIndexOf('\\')+1)
  670. // Only Java 8 is supported currently.
  671. if(parseFloat(vKey) === 1.8){
  672. javaVer.get('JavaHome', (err, res) => {
  673. const jHome = res.value
  674. if(jHome.indexOf('(x86)') === -1){
  675. candidates.add(jHome)
  676. }
  677. cbAcc++
  678. if(i === regKeys.length-1 && cbAcc === cbTracker){
  679. resolve(candidates)
  680. }
  681. })
  682. } else {
  683. cbAcc++
  684. if(i === regKeys.length-1 && cbAcc === cbTracker){
  685. resolve(candidates)
  686. }
  687. }
  688. }
  689. }
  690. }
  691. })
  692. } else {
  693. if(i === regKeys.length-1 && cbAcc === cbTracker){
  694. resolve(candidates)
  695. }
  696. }
  697. })
  698. }
  699. })
  700. }
  701. /**
  702. * Attempts to find a valid x64 installation of Java on Windows machines.
  703. * Possible paths will be pulled from the registry and the JAVA_HOME environment
  704. * variable. The paths will be sorted with higher versions preceeding lower, and
  705. * JREs preceeding JDKs. The binaries at the sorted paths will then be validated.
  706. * The first validated is returned.
  707. *
  708. * Higher versions > Lower versions
  709. * If versions are equal, JRE > JDK.
  710. *
  711. * @param {string} dataDir The base launcher directory.
  712. * @returns {Promise.<string>} A Promise which resolves to the executable path of a valid
  713. * x64 Java installation. If none are found, null is returned.
  714. */
  715. static async _win32JavaValidate(dataDir){
  716. // Get possible paths from the registry.
  717. const pathSet = await AssetGuard._scanRegistry()
  718. //console.log(Array.from(pathSet)) // DEBUGGING
  719. // Get possible paths from the data directory.
  720. const pathSet2 = await AssetGuard._scanDataFolder(dataDir)
  721. // Validate JAVA_HOME
  722. const jHome = AssetGuard._scanJavaHome()
  723. if(jHome != null && jHome.indexOf('(x86)') === -1){
  724. pathSet.add(jHome)
  725. }
  726. const mergedSet = new Set([...pathSet, ...pathSet2])
  727. // Convert path set to an array for processing.
  728. let pathArr = Array.from(mergedSet)
  729. //console.log(pathArr) // DEBUGGING
  730. // Sorts array. Higher version numbers preceed lower. JRE preceeds JDK.
  731. pathArr = pathArr.sort((a, b) => {
  732. // Note that Java 9+ uses semver and that will need to be accounted for in
  733. // the future.
  734. const aVer = parseInt(a.split('_')[1])
  735. const bVer = parseInt(b.split('_')[1])
  736. if(bVer === aVer){
  737. return a.indexOf('jdk') > -1 ? 1 : 0
  738. } else {
  739. return bVer - aVer
  740. }
  741. })
  742. //console.log(pathArr) // DEBUGGING
  743. // Validate that the binary is actually x64.
  744. for(let i=0; i<pathArr.length; i++) {
  745. const execPath = AssetGuard.javaExecFromRoot(pathArr[i])
  746. let res = await AssetGuard._validateJavaBinary(execPath)
  747. if(res){
  748. return execPath
  749. }
  750. }
  751. // No suitable candidates found.
  752. return null;
  753. }
  754. /**
  755. * WIP -> get a valid x64 Java path on macOS.
  756. */
  757. static async _darwinJavaValidate(dataDir){
  758. return null
  759. }
  760. /**
  761. * WIP -> get a valid x64 Java path on linux.
  762. */
  763. static async _linuxJavaValidate(dataDir){
  764. return null
  765. }
  766. /**
  767. * Retrieve the path of a valid x64 Java installation.
  768. *
  769. * @param {string} dataDir The base launcher directory.
  770. * @returns {string} A path to a valid x64 Java installation, null if none found.
  771. */
  772. static async validateJava(dataDir){
  773. return await AssetGuard['_' + process.platform + 'JavaValidate'](dataDir)
  774. }
  775. // #endregion
  776. // #endregion
  777. // Validation Functions
  778. // #region
  779. /**
  780. * Loads the version data for a given minecraft version.
  781. *
  782. * @param {string} version The game version for which to load the index data.
  783. * @param {boolean} force Optional. If true, the version index will be downloaded even if it exists locally. Defaults to false.
  784. * @returns {Promise.<Object>} Promise which resolves to the version data object.
  785. */
  786. loadVersionData(version, force = false){
  787. const self = this
  788. return new Promise((resolve, reject) => {
  789. const name = version + '.json'
  790. const url = 'https://s3.amazonaws.com/Minecraft.Download/versions/' + version + '/' + name
  791. const versionPath = path.join(self.basePath, 'versions', version)
  792. const versionFile = path.join(versionPath, name)
  793. if(!fs.existsSync(versionFile) || force){
  794. //This download will never be tracked as it's essential and trivial.
  795. console.log('Preparing download of ' + version + ' assets.')
  796. mkpath.sync(versionPath)
  797. const stream = request(url).pipe(fs.createWriteStream(versionFile))
  798. stream.on('finish', () => {
  799. resolve(JSON.parse(fs.readFileSync(versionFile)))
  800. })
  801. } else {
  802. resolve(JSON.parse(fs.readFileSync(versionFile)))
  803. }
  804. })
  805. }
  806. // Asset (Category=''') Validation Functions
  807. // #region
  808. /**
  809. * Public asset validation function. This function will handle the validation of assets.
  810. * It will parse the asset index specified in the version data, analyzing each
  811. * asset entry. In this analysis it will check to see if the local file exists and is valid.
  812. * If not, it will be added to the download queue for the 'assets' identifier.
  813. *
  814. * @param {Object} versionData The version data for the assets.
  815. * @param {boolean} force Optional. If true, the asset index will be downloaded even if it exists locally. Defaults to false.
  816. * @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
  817. */
  818. validateAssets(versionData, force = false){
  819. const self = this
  820. return new Promise(function(fulfill, reject){
  821. self._assetChainIndexData(versionData, force).then(() => {
  822. fulfill()
  823. })
  824. })
  825. }
  826. //Chain the asset tasks to provide full async. The below functions are private.
  827. /**
  828. * Private function used to chain the asset validation process. This function retrieves
  829. * the index data.
  830. * @param {Object} versionData
  831. * @param {boolean} force
  832. * @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
  833. */
  834. _assetChainIndexData(versionData, force = false){
  835. const self = this
  836. return new Promise(function(fulfill, reject){
  837. //Asset index constants.
  838. const assetIndex = versionData.assetIndex
  839. const name = assetIndex.id + '.json'
  840. const indexPath = path.join(self.basePath, 'assets', 'indexes')
  841. const assetIndexLoc = path.join(indexPath, name)
  842. let data = null
  843. if(!fs.existsSync(assetIndexLoc) || force){
  844. console.log('Downloading ' + versionData.id + ' asset index.')
  845. mkpath.sync(indexPath)
  846. const stream = request(assetIndex.url).pipe(fs.createWriteStream(assetIndexLoc))
  847. stream.on('finish', function() {
  848. data = JSON.parse(fs.readFileSync(assetIndexLoc, 'utf-8'))
  849. self._assetChainValidateAssets(versionData, data).then(() => {
  850. fulfill()
  851. })
  852. })
  853. } else {
  854. data = JSON.parse(fs.readFileSync(assetIndexLoc, 'utf-8'))
  855. self._assetChainValidateAssets(versionData, data).then(() => {
  856. fulfill()
  857. })
  858. }
  859. })
  860. }
  861. /**
  862. * Private function used to chain the asset validation process. This function processes
  863. * the assets and enqueues missing or invalid files.
  864. * @param {Object} versionData
  865. * @param {boolean} force
  866. * @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
  867. */
  868. _assetChainValidateAssets(versionData, indexData){
  869. const self = this
  870. return new Promise(function(fulfill, reject){
  871. //Asset constants
  872. const resourceURL = 'http://resources.download.minecraft.net/'
  873. const localPath = path.join(self.basePath, 'assets')
  874. const indexPath = path.join(localPath, 'indexes')
  875. const objectPath = path.join(localPath, 'objects')
  876. const assetDlQueue = []
  877. let dlSize = 0
  878. let acc = 0
  879. const total = Object.keys(indexData.objects).length
  880. //const objKeys = Object.keys(data.objects)
  881. async.forEachOfLimit(indexData.objects, 10, function(value, key, cb){
  882. acc++
  883. self.emit('assetVal', {acc, total})
  884. const hash = value.hash
  885. const assetName = path.join(hash.substring(0, 2), hash)
  886. const urlName = hash.substring(0, 2) + "/" + hash
  887. const ast = new Asset(key, hash, String(value.size), resourceURL + urlName, path.join(objectPath, assetName))
  888. if(!AssetGuard._validateLocal(ast.to, 'sha1', ast.hash)){
  889. dlSize += (ast.size*1)
  890. assetDlQueue.push(ast)
  891. }
  892. cb()
  893. }, function(err){
  894. self.assets = new DLTracker(assetDlQueue, dlSize)
  895. fulfill()
  896. })
  897. })
  898. }
  899. // #endregion
  900. // Library (Category=''') Validation Functions
  901. // #region
  902. /**
  903. * Public library validation function. This function will handle the validation of libraries.
  904. * It will parse the version data, analyzing each library entry. In this analysis, it will
  905. * check to see if the local file exists and is valid. If not, it will be added to the download
  906. * queue for the 'libraries' identifier.
  907. *
  908. * @param {Object} versionData The version data for the assets.
  909. * @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
  910. */
  911. validateLibraries(versionData){
  912. const self = this
  913. return new Promise(function(fulfill, reject){
  914. const libArr = versionData.libraries
  915. const libPath = path.join(self.basePath, 'libraries')
  916. const libDlQueue = []
  917. let dlSize = 0
  918. //Check validity of each library. If the hashs don't match, download the library.
  919. async.eachLimit(libArr, 5, function(lib, cb){
  920. if(Library.validateRules(lib.rules)){
  921. let artifact = (lib.natives == null) ? lib.downloads.artifact : lib.downloads.classifiers[lib.natives[Library.mojangFriendlyOS()]]
  922. const libItm = new Library(lib.name, artifact.sha1, artifact.size, artifact.url, path.join(libPath, artifact.path))
  923. if(!AssetGuard._validateLocal(libItm.to, 'sha1', libItm.hash)){
  924. dlSize += (libItm.size*1)
  925. libDlQueue.push(libItm)
  926. }
  927. }
  928. cb()
  929. }, function(err){
  930. self.libraries = new DLTracker(libDlQueue, dlSize)
  931. fulfill()
  932. })
  933. })
  934. }
  935. // #endregion
  936. // Miscellaneous (Category=files) Validation Functions
  937. // #region
  938. /**
  939. * Public miscellaneous mojang file validation function. These files will be enqueued under
  940. * the 'files' identifier.
  941. *
  942. * @param {Object} versionData The version data for the assets.
  943. * @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
  944. */
  945. validateMiscellaneous(versionData){
  946. const self = this
  947. return new Promise(async function(fulfill, reject){
  948. await self.validateClient(versionData)
  949. await self.validateLogConfig(versionData)
  950. fulfill()
  951. })
  952. }
  953. /**
  954. * Validate client file - artifact renamed from client.jar to '{version}'.jar.
  955. *
  956. * @param {Object} versionData The version data for the assets.
  957. * @param {boolean} force Optional. If true, the asset index will be downloaded even if it exists locally. Defaults to false.
  958. * @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
  959. */
  960. validateClient(versionData, force = false){
  961. const self = this
  962. return new Promise(function(fulfill, reject){
  963. const clientData = versionData.downloads.client
  964. const version = versionData.id
  965. const targetPath = path.join(self.basePath, 'versions', version)
  966. const targetFile = version + '.jar'
  967. let client = new Asset(version + ' client', clientData.sha1, clientData.size, clientData.url, path.join(targetPath, targetFile))
  968. if(!AssetGuard._validateLocal(client.to, 'sha1', client.hash) || force){
  969. self.files.dlqueue.push(client)
  970. self.files.dlsize += client.size*1
  971. fulfill()
  972. } else {
  973. fulfill()
  974. }
  975. })
  976. }
  977. /**
  978. * Validate log config.
  979. *
  980. * @param {Object} versionData The version data for the assets.
  981. * @param {boolean} force Optional. If true, the asset index will be downloaded even if it exists locally. Defaults to false.
  982. * @returns {Promise.<void>} An empty promise to indicate the async processing has completed.
  983. */
  984. validateLogConfig(versionData){
  985. const self = this
  986. return new Promise(function(fulfill, reject){
  987. const client = versionData.logging.client
  988. const file = client.file
  989. const targetPath = path.join(self.basePath, 'assets', 'log_configs')
  990. let logConfig = new Asset(file.id, file.sha1, file.size, file.url, path.join(targetPath, file.id))
  991. if(!AssetGuard._validateLocal(logConfig.to, 'sha1', logConfig.hash)){
  992. self.files.dlqueue.push(logConfig)
  993. self.files.dlsize += logConfig.size*1
  994. fulfill()
  995. } else {
  996. fulfill()
  997. }
  998. })
  999. }
  1000. // #endregion
  1001. // Distribution (Category=forge) Validation Functions
  1002. // #region
  1003. /**
  1004. * Validate the distribution.
  1005. *
  1006. * @param {string} serverpackid The id of the server to validate.
  1007. * @returns {Promise.<Object>} A promise which resolves to the server distribution object.
  1008. */
  1009. validateDistribution(serverpackid){
  1010. const self = this
  1011. return new Promise(function(fulfill, reject){
  1012. AssetGuard.retrieveDistributionData(self.basePath, false).then((value) => {
  1013. /*const servers = value.servers
  1014. let serv = null
  1015. for(let i=0; i<servers.length; i++){
  1016. if(servers[i].id === serverpackid){
  1017. serv = servers[i]
  1018. break
  1019. }
  1020. }*/
  1021. const serv = AssetGuard.getServerById(self.basePath, serverpackid)
  1022. if(serv == null) {
  1023. console.error('Invalid server pack id:', serverpackid)
  1024. }
  1025. self.forge = self._parseDistroModules(serv.modules, serv.mc_version)
  1026. //Correct our workaround here.
  1027. let decompressqueue = self.forge.callback
  1028. self.forge.callback = function(asset, self){
  1029. if(asset.to.toLowerCase().endsWith('.pack.xz')){
  1030. AssetGuard._extractPackXZ([asset.to], self.javaexec)
  1031. }
  1032. if(asset.type === 'forge-hosted' || asset.type === 'forge'){
  1033. AssetGuard._finalizeForgeAsset(asset, self.basePath)
  1034. }
  1035. }
  1036. fulfill(serv)
  1037. })
  1038. })
  1039. }
  1040. /*//TODO The file should be hosted, the following code is for local testing.
  1041. _chainValidateDistributionIndex(basePath){
  1042. return new Promise(function(fulfill, reject){
  1043. //const distroURL = 'http://mc.westeroscraft.com/WesterosCraftLauncher/westeroscraft.json'
  1044. //const targetFile = path.join(basePath, 'westeroscraft.json')
  1045. //TEMP WORKAROUND TO TEST WHILE THIS IS NOT HOSTED
  1046. fs.readFile(path.join(__dirname, '..', 'westeroscraft.json'), 'utf-8', (err, data) => {
  1047. fulfill(JSON.parse(data))
  1048. })
  1049. })
  1050. }*/
  1051. _parseDistroModules(modules, version){
  1052. let alist = []
  1053. let asize = 0;
  1054. //This may be removed soon, considering the most efficient way to extract.
  1055. let decompressqueue = []
  1056. for(let i=0; i<modules.length; i++){
  1057. let ob = modules[i]
  1058. let obType = ob.type
  1059. let obArtifact = ob.artifact
  1060. let obPath = obArtifact.path == null ? AssetGuard._resolvePath(ob.id, obArtifact.extension) : obArtifact.path
  1061. switch(obType){
  1062. case 'forge-hosted':
  1063. case 'forge':
  1064. case 'library':
  1065. obPath = path.join(this.basePath, 'libraries', obPath)
  1066. break
  1067. case 'forgemod':
  1068. //obPath = path.join(this.basePath, 'mods', obPath)
  1069. obPath = path.join(this.basePath, 'modstore', obPath)
  1070. break
  1071. case 'litemod':
  1072. //obPath = path.join(this.basePath, 'mods', version, obPath)
  1073. obPath = path.join(this.basePath, 'modstore', obPath)
  1074. break
  1075. case 'file':
  1076. default:
  1077. obPath = path.join(this.basePath, obPath)
  1078. }
  1079. let artifact = new DistroModule(ob.id, obArtifact.MD5, obArtifact.size, obArtifact.url, obPath, obType)
  1080. const validationPath = obPath.toLowerCase().endsWith('.pack.xz') ? obPath.substring(0, obPath.toLowerCase().lastIndexOf('.pack.xz')) : obPath
  1081. if(!AssetGuard._validateLocal(validationPath, 'MD5', artifact.hash)){
  1082. asize += artifact.size*1
  1083. alist.push(artifact)
  1084. if(validationPath !== obPath) decompressqueue.push(obPath)
  1085. }
  1086. //Recursively process the submodules then combine the results.
  1087. if(ob.sub_modules != null){
  1088. let dltrack = this._parseDistroModules(ob.sub_modules, version)
  1089. asize += dltrack.dlsize*1
  1090. alist = alist.concat(dltrack.dlqueue)
  1091. decompressqueue = decompressqueue.concat(dltrack.callback)
  1092. }
  1093. }
  1094. //Since we have no callback at this point, we use this value to store the decompressqueue.
  1095. return new DLTracker(alist, asize, decompressqueue)
  1096. }
  1097. /**
  1098. * Loads Forge's version.json data into memory for the specified server id.
  1099. *
  1100. * @param {string} serverpack The id of the server to load Forge data for.
  1101. * @returns {Promise.<Object>} A promise which resolves to Forge's version.json data.
  1102. */
  1103. loadForgeData(serverpack){
  1104. const self = this
  1105. return new Promise(async function(fulfill, reject){
  1106. let distro = AssetGuard.retrieveDistributionDataSync(self.basePath)
  1107. const servers = distro.servers
  1108. let serv = null
  1109. for(let i=0; i<servers.length; i++){
  1110. if(servers[i].id === serverpack){
  1111. serv = servers[i]
  1112. break
  1113. }
  1114. }
  1115. const modules = serv.modules
  1116. for(let i=0; i<modules.length; i++){
  1117. const ob = modules[i]
  1118. if(ob.type === 'forge-hosted' || ob.type === 'forge'){
  1119. let obArtifact = ob.artifact
  1120. let obPath = obArtifact.path == null ? path.join(self.basePath, 'libraries', AssetGuard._resolvePath(ob.id, obArtifact.extension)) : obArtifact.path
  1121. let asset = new DistroModule(ob.id, obArtifact.MD5, obArtifact.size, obArtifact.url, obPath, ob.type)
  1122. let forgeData = await AssetGuard._finalizeForgeAsset(asset, self.basePath)
  1123. fulfill(forgeData)
  1124. return
  1125. }
  1126. }
  1127. reject('No forge module found!')
  1128. })
  1129. }
  1130. _parseForgeLibraries(){
  1131. /* TODO
  1132. * Forge asset validations are already implemented. When there's nothing much
  1133. * to work on, implement forge downloads using forge's version.json. This is to
  1134. * have the code on standby if we ever need it (since it's half implemented already).
  1135. */
  1136. }
  1137. // #endregion
  1138. // Java (Category=''') Validation (download) Functions
  1139. // #region
  1140. _enqueueOracleJRE(dataDir){
  1141. return new Promise((resolve, reject) => {
  1142. AssetGuard._latestJREOracle().then(verData => {
  1143. if(verData != null){
  1144. const combined = verData.uri + PLATFORM_MAP[process.platform]
  1145. const opts = {
  1146. url: combined,
  1147. headers: {
  1148. 'Cookie': 'oraclelicense=accept-securebackup-cookie'
  1149. }
  1150. }
  1151. request.head(opts, (err, resp, body) => {
  1152. if(err){
  1153. resolve(false)
  1154. } else {
  1155. dataDir = path.join(dataDir, 'runtime', 'x64')
  1156. const name = combined.substring(combined.lastIndexOf('/')+1)
  1157. const fDir = path.join(dataDir, name)
  1158. const jre = new Asset(name, null, resp.headers['content-length'], opts, fDir)
  1159. this.java = new DLTracker([jre], jre.size, (a, self) => {
  1160. let h = null
  1161. fs.createReadStream(a.to)
  1162. .on('error', err => console.log(err))
  1163. .pipe(zlib.createGunzip())
  1164. .on('error', err => console.log(err))
  1165. .pipe(tar.extract(dataDir, {
  1166. map: (header) => {
  1167. if(h == null){
  1168. h = header.name
  1169. }
  1170. }
  1171. }))
  1172. .on('error', err => console.log(err))
  1173. .on('finish', () => {
  1174. fs.unlink(a.to, err => {
  1175. if(err){
  1176. console.log(err)
  1177. }
  1178. if(h.indexOf('/') > -1){
  1179. h = h.substring(0, h.indexOf('/'))
  1180. }
  1181. const pos = path.join(dataDir, h)
  1182. self.emit('jExtracted', AssetGuard.javaExecFromRoot(pos))
  1183. })
  1184. })
  1185. })
  1186. resolve(true)
  1187. }
  1188. })
  1189. } else {
  1190. resolve(false)
  1191. }
  1192. })
  1193. })
  1194. }
  1195. /*_enqueueMojangJRE(dir){
  1196. return new Promise((resolve, reject) => {
  1197. // Mojang does not host the JRE for linux.
  1198. if(process.platform === 'linux'){
  1199. resolve(false)
  1200. }
  1201. AssetGuard.loadMojangLauncherData().then(data => {
  1202. if(data != null) {
  1203. try {
  1204. const mJRE = data[Library.mojangFriendlyOS()]['64'].jre
  1205. const url = mJRE.url
  1206. request.head(url, (err, resp, body) => {
  1207. if(err){
  1208. resolve(false)
  1209. } else {
  1210. const name = url.substring(url.lastIndexOf('/')+1)
  1211. const fDir = path.join(dir, name)
  1212. const jre = new Asset('jre' + mJRE.version, mJRE.sha1, resp.headers['content-length'], url, fDir)
  1213. this.java = new DLTracker([jre], jre.size, a => {
  1214. fs.readFile(a.to, (err, data) => {
  1215. // Data buffer needs to be decompressed from lzma,
  1216. // not really possible using node.js
  1217. })
  1218. })
  1219. }
  1220. })
  1221. } catch (err){
  1222. resolve(false)
  1223. }
  1224. }
  1225. })
  1226. })
  1227. }*/
  1228. // #endregion
  1229. // #endregion
  1230. // Control Flow Functions
  1231. // #region
  1232. /**
  1233. * Initiate an async download process for an AssetGuard DLTracker.
  1234. *
  1235. * @param {string} identifier The identifier of the AssetGuard DLTracker.
  1236. * @param {number} limit Optional. The number of async processes to run in parallel.
  1237. * @returns {boolean} True if the process began, otherwise false.
  1238. */
  1239. startAsyncProcess(identifier, limit = 5){
  1240. const self = this
  1241. let acc = 0
  1242. const concurrentDlTracker = this[identifier]
  1243. const concurrentDlQueue = concurrentDlTracker.dlqueue.slice(0)
  1244. if(concurrentDlQueue.length === 0){
  1245. return false
  1246. } else {
  1247. async.eachLimit(concurrentDlQueue, limit, function(asset, cb){
  1248. let count = 0;
  1249. mkpath.sync(path.join(asset.to, ".."))
  1250. let req = request(asset.from)
  1251. req.pause()
  1252. req.on('response', (resp) => {
  1253. if(resp.statusCode === 200){
  1254. let writeStream = fs.createWriteStream(asset.to)
  1255. writeStream.on('close', () => {
  1256. //console.log('DLResults ' + asset.size + ' ' + count + ' ', asset.size === count)
  1257. if(concurrentDlTracker.callback != null){
  1258. concurrentDlTracker.callback.apply(concurrentDlTracker, [asset, self])
  1259. }
  1260. cb()
  1261. })
  1262. req.pipe(writeStream)
  1263. req.resume()
  1264. } else {
  1265. req.abort()
  1266. const realFrom = typeof asset.from === 'object' ? asset.from.url : asset.from
  1267. console.log('Failed to download ' + realFrom + '. Response code', resp.statusCode)
  1268. self.progress += asset.size*1
  1269. self.emit('totaldlprogress', {acc: self.progress, total: self.totaldlsize})
  1270. cb()
  1271. }
  1272. })
  1273. req.on('data', function(chunk){
  1274. count += chunk.length
  1275. self.progress += chunk.length
  1276. acc += chunk.length
  1277. self.emit(identifier + 'dlprogress', acc)
  1278. self.emit('totaldlprogress', {acc: self.progress, total: self.totaldlsize})
  1279. })
  1280. }, function(err){
  1281. if(err){
  1282. self.emit(identifier + 'dlerror')
  1283. console.log('An item in ' + identifier + ' failed to process');
  1284. } else {
  1285. self.emit(identifier + 'dlcomplete')
  1286. console.log('All ' + identifier + ' have been processed successfully')
  1287. }
  1288. self.totaldlsize -= self[identifier].dlsize
  1289. self.progress -= self[identifier].dlsize
  1290. self[identifier] = new DLTracker([], 0)
  1291. if(self.totaldlsize === 0) {
  1292. self.emit('dlcomplete')
  1293. }
  1294. })
  1295. return true
  1296. }
  1297. }
  1298. /**
  1299. * This function will initiate the download processed for the specified identifiers. If no argument is
  1300. * given, all identifiers will be initiated. Note that in order for files to be processed you need to run
  1301. * the processing function corresponding to that identifier. If you run this function without processing
  1302. * the files, it is likely nothing will be enqueued in the object and processing will complete
  1303. * immediately. Once all downloads are complete, this function will fire the 'dlcomplete' event on the
  1304. * global object instance.
  1305. *
  1306. * @param {Array.<{id: string, limit: number}>} identifiers Optional. The identifiers to process and corresponding parallel async task limit.
  1307. */
  1308. processDlQueues(identifiers = [{id:'assets', limit:20}, {id:'libraries', limit:5}, {id:'files', limit:5}, {id:'forge', limit:5}]){
  1309. this.progress = 0;
  1310. let shouldFire = true
  1311. // Assign dltracking variables.
  1312. this.totaldlsize = 0
  1313. this.progress = 0
  1314. for(let i=0; i<identifiers.length; i++){
  1315. this.totaldlsize += this[identifiers[i].id].dlsize
  1316. }
  1317. for(let i=0; i<identifiers.length; i++){
  1318. let iden = identifiers[i]
  1319. let r = this.startAsyncProcess(iden.id, iden.limit)
  1320. if(r) shouldFire = false
  1321. }
  1322. if(shouldFire){
  1323. this.emit('dlcomplete')
  1324. }
  1325. }
  1326. // #endregion
  1327. }
  1328. module.exports = {
  1329. AssetGuard,
  1330. Asset,
  1331. Library
  1332. }