assetguard.js 67 KB

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