assetguard.js 62 KB

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