processbuilder.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. const AdmZip = require('adm-zip')
  2. const child_process = require('child_process')
  3. const crypto = require('crypto')
  4. const fs = require('fs-extra')
  5. const os = require('os')
  6. const path = require('path')
  7. const { URL } = require('url')
  8. const { Util, Library } = require('./assetguard')
  9. const ConfigManager = require('./configmanager')
  10. const DistroManager = require('./distromanager')
  11. const LoggerUtil = require('./loggerutil')
  12. const logger = LoggerUtil('%c[ProcessBuilder]', 'color: #003996; font-weight: bold')
  13. class ProcessBuilder {
  14. constructor(distroServer, versionData, forgeData, authUser, launcherVersion){
  15. this.gameDir = path.join(ConfigManager.getInstanceDirectory(), distroServer.getID())
  16. this.commonDir = ConfigManager.getCommonDirectory()
  17. this.server = distroServer
  18. this.versionData = versionData
  19. this.forgeData = forgeData
  20. this.authUser = authUser
  21. this.launcherVersion = launcherVersion
  22. this.fmlDir = path.join(this.gameDir, 'forgeModList.json')
  23. this.llDir = path.join(this.gameDir, 'liteloaderModList.json')
  24. this.libPath = path.join(this.commonDir, 'libraries')
  25. this.usingLiteLoader = false
  26. this.llPath = null
  27. }
  28. /**
  29. * Convienence method to run the functions typically used to build a process.
  30. */
  31. build(){
  32. fs.ensureDirSync(this.gameDir)
  33. const tempNativePath = path.join(os.tmpdir(), ConfigManager.getTempNativeFolder(), crypto.pseudoRandomBytes(16).toString('hex'))
  34. process.throwDeprecation = true
  35. this.setupLiteLoader()
  36. logger.log('Using liteloader:', this.usingLiteLoader)
  37. const modObj = this.resolveModConfiguration(ConfigManager.getModConfiguration(this.server.getID()).mods, this.server.getModules())
  38. // Mod list below 1.13
  39. if(!Util.mcVersionAtLeast('1.13', this.server.getMinecraftVersion())){
  40. this.constructModList('forge', modObj.fMods, true)
  41. if(this.usingLiteLoader){
  42. this.constructModList('liteloader', modObj.lMods, true)
  43. }
  44. }
  45. const uberModArr = modObj.fMods.concat(modObj.lMods)
  46. let args = this.constructJVMArguments(uberModArr, tempNativePath)
  47. if(Util.mcVersionAtLeast('1.13', this.server.getMinecraftVersion())){
  48. args = args.concat(this.constructModArguments(modObj.fMods))
  49. }
  50. logger.log('Launch Arguments:', args)
  51. const child = child_process.spawn(ConfigManager.getJavaExecutable(), args, {
  52. cwd: this.gameDir,
  53. detached: ConfigManager.getLaunchDetached()
  54. })
  55. if(ConfigManager.getLaunchDetached()){
  56. child.unref()
  57. }
  58. child.stdout.setEncoding('utf8')
  59. child.stderr.setEncoding('utf8')
  60. const loggerMCstdout = LoggerUtil('%c[Minecraft]', 'color: #36b030; font-weight: bold')
  61. const loggerMCstderr = LoggerUtil('%c[Minecraft]', 'color: #b03030; font-weight: bold')
  62. child.stdout.on('data', (data) => {
  63. loggerMCstdout.log(data)
  64. })
  65. child.stderr.on('data', (data) => {
  66. loggerMCstderr.log(data)
  67. })
  68. child.on('close', (code, signal) => {
  69. logger.log('Exited with code', code)
  70. fs.remove(tempNativePath, (err) => {
  71. if(err){
  72. logger.warn('Error while deleting temp dir', err)
  73. } else {
  74. logger.log('Temp dir deleted successfully.')
  75. }
  76. })
  77. })
  78. return child
  79. }
  80. /**
  81. * Determine if an optional mod is enabled from its configuration value. If the
  82. * configuration value is null, the required object will be used to
  83. * determine if it is enabled.
  84. *
  85. * A mod is enabled if:
  86. * * The configuration is not null and one of the following:
  87. * * The configuration is a boolean and true.
  88. * * The configuration is an object and its 'value' property is true.
  89. * * The configuration is null and one of the following:
  90. * * The required object is null.
  91. * * The required object's 'def' property is null or true.
  92. *
  93. * @param {Object | boolean} modCfg The mod configuration object.
  94. * @param {Object} required Optional. The required object from the mod's distro declaration.
  95. * @returns {boolean} True if the mod is enabled, false otherwise.
  96. */
  97. static isModEnabled(modCfg, required = null){
  98. return modCfg != null ? ((typeof modCfg === 'boolean' && modCfg) || (typeof modCfg === 'object' && (typeof modCfg.value !== 'undefined' ? modCfg.value : true))) : required != null ? required.isDefault() : true
  99. }
  100. /**
  101. * Function which performs a preliminary scan of the top level
  102. * mods. If liteloader is present here, we setup the special liteloader
  103. * launch options. Note that liteloader is only allowed as a top level
  104. * mod. It must not be declared as a submodule.
  105. */
  106. setupLiteLoader(){
  107. for(let ll of this.server.getModules()){
  108. if(ll.getType() === DistroManager.Types.LiteLoader){
  109. if(!ll.getRequired().isRequired()){
  110. const modCfg = ConfigManager.getModConfiguration(this.server.getID()).mods
  111. if(ProcessBuilder.isModEnabled(modCfg[ll.getVersionlessID()], ll.getRequired())){
  112. if(fs.existsSync(ll.getArtifact().getPath())){
  113. this.usingLiteLoader = true
  114. this.llPath = ll.getArtifact().getPath()
  115. }
  116. }
  117. } else {
  118. if(fs.existsSync(ll.getArtifact().getPath())){
  119. this.usingLiteLoader = true
  120. this.llPath = ll.getArtifact().getPath()
  121. }
  122. }
  123. }
  124. }
  125. }
  126. /**
  127. * Resolve an array of all enabled mods. These mods will be constructed into
  128. * a mod list format and enabled at launch.
  129. *
  130. * @param {Object} modCfg The mod configuration object.
  131. * @param {Array.<Object>} mdls An array of modules to parse.
  132. * @returns {{fMods: Array.<Object>, lMods: Array.<Object>}} An object which contains
  133. * a list of enabled forge mods and litemods.
  134. */
  135. resolveModConfiguration(modCfg, mdls){
  136. let fMods = []
  137. let lMods = []
  138. for(let mdl of mdls){
  139. const type = mdl.getType()
  140. if(type === DistroManager.Types.ForgeMod || type === DistroManager.Types.LiteMod || type === DistroManager.Types.LiteLoader){
  141. const o = !mdl.getRequired().isRequired()
  142. const e = ProcessBuilder.isModEnabled(modCfg[mdl.getVersionlessID()], mdl.getRequired())
  143. if(!o || (o && e)){
  144. if(mdl.hasSubModules()){
  145. const v = this.resolveModConfiguration(modCfg[mdl.getVersionlessID()].mods, mdl.getSubModules())
  146. fMods = fMods.concat(v.fMods)
  147. lMods = lMods.concat(v.lMods)
  148. if(mdl.type === DistroManager.Types.LiteLoader){
  149. continue
  150. }
  151. }
  152. if(mdl.type === DistroManager.Types.ForgeMod){
  153. fMods.push(mdl)
  154. } else {
  155. lMods.push(mdl)
  156. }
  157. }
  158. }
  159. }
  160. return {
  161. fMods,
  162. lMods
  163. }
  164. }
  165. /**
  166. * Test to see if this version of forge requires the absolute: prefix
  167. * on the modListFile repository field.
  168. */
  169. _requiresAbsolute(){
  170. try {
  171. const ver = this.forgeData.id.split('-')[2]
  172. const pts = ver.split('.')
  173. const min = [14, 23, 3, 2655]
  174. for(let i=0; i<pts.length; i++){
  175. const parsed = Number.parseInt(pts[i])
  176. if(parsed < min[i]){
  177. return false
  178. } else if(parsed > min[i]){
  179. return true
  180. }
  181. }
  182. } catch (err) {
  183. // We know old forge versions follow this format.
  184. // Error must be caused by newer version.
  185. }
  186. // Equal or errored
  187. return true
  188. }
  189. /**
  190. * Construct a mod list json object.
  191. *
  192. * @param {'forge' | 'liteloader'} type The mod list type to construct.
  193. * @param {Array.<Object>} mods An array of mods to add to the mod list.
  194. * @param {boolean} save Optional. Whether or not we should save the mod list file.
  195. */
  196. constructModList(type, mods, save = false){
  197. const modList = {
  198. repositoryRoot: ((type === 'forge' && this._requiresAbsolute()) ? 'absolute:' : '') + path.join(this.commonDir, 'modstore')
  199. }
  200. const ids = []
  201. if(type === 'forge'){
  202. for(let mod of mods){
  203. ids.push(mod.getExtensionlessID())
  204. }
  205. } else {
  206. for(let mod of mods){
  207. ids.push(mod.getExtensionlessID() + '@' + mod.getExtension())
  208. }
  209. }
  210. modList.modRef = ids
  211. if(save){
  212. const json = JSON.stringify(modList, null, 4)
  213. fs.writeFileSync(type === 'forge' ? this.fmlDir : this.llDir, json, 'UTF-8')
  214. }
  215. return modList
  216. }
  217. /**
  218. * Construct the mod argument list for forge 1.13
  219. *
  220. * @param {Array.<Object>} mods An array of mods to add to the mod list.
  221. */
  222. constructModArguments(mods){
  223. const argStr = mods.map(mod => {
  224. return mod.getExtensionlessID()
  225. }).join(',')
  226. if(argStr){
  227. return [
  228. '--fml.mavenRoots',
  229. path.join('..', '..', 'common', 'modstore'),
  230. '--fml.mods',
  231. argStr
  232. ]
  233. } else {
  234. return []
  235. }
  236. }
  237. /**
  238. * Construct the argument array that will be passed to the JVM process.
  239. *
  240. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  241. * @param {string} tempNativePath The path to store the native libraries.
  242. * @returns {Array.<string>} An array containing the full JVM arguments for this process.
  243. */
  244. constructJVMArguments(mods, tempNativePath){
  245. if(Util.mcVersionAtLeast('1.13', this.server.getMinecraftVersion())){
  246. return this._constructJVMArguments113(mods, tempNativePath)
  247. } else {
  248. return this._constructJVMArguments112(mods, tempNativePath)
  249. }
  250. }
  251. /**
  252. * Construct the argument array that will be passed to the JVM process.
  253. * This function is for 1.12 and below.
  254. *
  255. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  256. * @param {string} tempNativePath The path to store the native libraries.
  257. * @returns {Array.<string>} An array containing the full JVM arguments for this process.
  258. */
  259. _constructJVMArguments112(mods, tempNativePath){
  260. let args = []
  261. // Classpath Argument
  262. args.push('-cp')
  263. args.push(this.classpathArg(mods, tempNativePath).join(process.platform === 'win32' ? ';' : ':'))
  264. // Java Arguments
  265. if(process.platform === 'darwin'){
  266. args.push('-Xdock:name=WesterosCraft')
  267. args.push('-Xdock:icon=' + path.join(__dirname, '..', 'images', 'minecraft.icns'))
  268. }
  269. args.push('-Xmx' + ConfigManager.getMaxRAM())
  270. args.push('-Xms' + ConfigManager.getMinRAM())
  271. args = args.concat(ConfigManager.getJVMOptions())
  272. args.push('-Djava.library.path=' + tempNativePath)
  273. // Main Java Class
  274. args.push(this.forgeData.mainClass)
  275. // Forge Arguments
  276. args = args.concat(this._resolveForgeArgs())
  277. return args
  278. }
  279. /**
  280. * Construct the argument array that will be passed to the JVM process.
  281. * This function is for 1.13+
  282. *
  283. * Note: Required Libs https://github.com/MinecraftForge/MinecraftForge/blob/af98088d04186452cb364280340124dfd4766a5c/src/fmllauncher/java/net/minecraftforge/fml/loading/LibraryFinder.java#L82
  284. *
  285. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  286. * @param {string} tempNativePath The path to store the native libraries.
  287. * @returns {Array.<string>} An array containing the full JVM arguments for this process.
  288. */
  289. _constructJVMArguments113(mods, tempNativePath){
  290. const argDiscovery = /\${*(.*)}/
  291. // JVM Arguments First
  292. let args = this.versionData.arguments.jvm
  293. //args.push('-Dlog4j.configurationFile=D:\\WesterosCraft\\game\\common\\assets\\log_configs\\client-1.12.xml')
  294. // Java Arguments
  295. if(process.platform === 'darwin'){
  296. args.push('-Xdock:name=WesterosCraft')
  297. args.push('-Xdock:icon=' + path.join(__dirname, '..', 'images', 'minecraft.icns'))
  298. }
  299. args.push('-Xmx' + ConfigManager.getMaxRAM())
  300. args.push('-Xms' + ConfigManager.getMinRAM())
  301. args = args.concat(ConfigManager.getJVMOptions())
  302. // Main Java Class
  303. args.push(this.forgeData.mainClass)
  304. // Vanilla Arguments
  305. args = args.concat(this.versionData.arguments.game)
  306. for(let i=0; i<args.length; i++){
  307. if(typeof args[i] === 'object' && args[i].rules != null){
  308. let checksum = 0
  309. for(let rule of args[i].rules){
  310. if(rule.os != null){
  311. if(rule.os.name === Library.mojangFriendlyOS()
  312. && (rule.os.version == null || new RegExp(rule.os.version).test(os.release))){
  313. if(rule.action === 'allow'){
  314. checksum++
  315. }
  316. } else {
  317. if(rule.action === 'disallow'){
  318. checksum++
  319. }
  320. }
  321. } else if(rule.features != null){
  322. // We don't have many 'features' in the index at the moment.
  323. // This should be fine for a while.
  324. if(rule.features.has_custom_resolution != null && rule.features.has_custom_resolution === true){
  325. if(ConfigManager.getFullscreen()){
  326. rule.values = [
  327. '--fullscreen',
  328. 'true'
  329. ]
  330. }
  331. checksum++
  332. }
  333. }
  334. }
  335. // TODO splice not push
  336. if(checksum === args[i].rules.length){
  337. if(typeof args[i].value === 'string'){
  338. args[i] = args[i].value
  339. } else if(typeof args[i].value === 'object'){
  340. //args = args.concat(args[i].value)
  341. args.splice(i, 1, ...args[i].value)
  342. }
  343. // Decrement i to reprocess the resolved value
  344. i--
  345. } else {
  346. args[i] = null
  347. }
  348. } else if(typeof args[i] === 'string'){
  349. if(argDiscovery.test(args[i])){
  350. const identifier = args[i].match(argDiscovery)[1]
  351. let val = null
  352. switch(identifier){
  353. case 'auth_player_name':
  354. val = this.authUser.displayName.trim()
  355. break
  356. case 'version_name':
  357. //val = versionData.id
  358. val = this.server.getID()
  359. break
  360. case 'game_directory':
  361. val = this.gameDir
  362. break
  363. case 'assets_root':
  364. val = path.join(this.commonDir, 'assets')
  365. break
  366. case 'assets_index_name':
  367. val = this.versionData.assets
  368. break
  369. case 'auth_uuid':
  370. val = this.authUser.uuid.trim()
  371. break
  372. case 'auth_access_token':
  373. val = this.authUser.accessToken
  374. break
  375. case 'user_type':
  376. val = 'mojang'
  377. break
  378. case 'version_type':
  379. val = this.versionData.type
  380. break
  381. case 'resolution_width':
  382. val = ConfigManager.getGameWidth()
  383. break
  384. case 'resolution_height':
  385. val = ConfigManager.getGameHeight()
  386. break
  387. case 'natives_directory':
  388. val = args[i].replace(argDiscovery, tempNativePath)
  389. break
  390. case 'launcher_name':
  391. val = args[i].replace(argDiscovery, 'WesterosCraft-Launcher')
  392. break
  393. case 'launcher_version':
  394. val = args[i].replace(argDiscovery, this.launcherVersion)
  395. break
  396. case 'classpath':
  397. val = this.classpathArg(mods, tempNativePath).join(process.platform === 'win32' ? ';' : ':')
  398. break
  399. }
  400. if(val != null){
  401. args[i] = val
  402. }
  403. }
  404. }
  405. }
  406. // Forge Specific Arguments
  407. args = args.concat(this.forgeData.arguments.game)
  408. // Filter null values
  409. args = args.filter(arg => {
  410. return arg != null
  411. })
  412. return args
  413. }
  414. /**
  415. * Resolve the arguments required by forge.
  416. *
  417. * @returns {Array.<string>} An array containing the arguments required by forge.
  418. */
  419. _resolveForgeArgs(){
  420. const mcArgs = this.forgeData.minecraftArguments.split(' ')
  421. const argDiscovery = /\${*(.*)}/
  422. // Replace the declared variables with their proper values.
  423. for(let i=0; i<mcArgs.length; ++i){
  424. if(argDiscovery.test(mcArgs[i])){
  425. const identifier = mcArgs[i].match(argDiscovery)[1]
  426. let val = null
  427. switch(identifier){
  428. case 'auth_player_name':
  429. val = this.authUser.displayName.trim()
  430. break
  431. case 'version_name':
  432. //val = versionData.id
  433. val = this.server.getID()
  434. break
  435. case 'game_directory':
  436. val = this.gameDir
  437. break
  438. case 'assets_root':
  439. val = path.join(this.commonDir, 'assets')
  440. break
  441. case 'assets_index_name':
  442. val = this.versionData.assets
  443. break
  444. case 'auth_uuid':
  445. val = this.authUser.uuid.trim()
  446. break
  447. case 'auth_access_token':
  448. val = this.authUser.accessToken
  449. break
  450. case 'user_type':
  451. val = 'mojang'
  452. break
  453. case 'version_type':
  454. val = this.versionData.type
  455. break
  456. }
  457. if(val != null){
  458. mcArgs[i] = val
  459. }
  460. }
  461. }
  462. // Autoconnect to the selected server.
  463. if(ConfigManager.getAutoConnect() && this.server.isAutoConnect()){
  464. const serverURL = new URL('my://' + this.server.getAddress())
  465. mcArgs.push('--server')
  466. mcArgs.push(serverURL.hostname)
  467. if(serverURL.port){
  468. mcArgs.push('--port')
  469. mcArgs.push(serverURL.port)
  470. }
  471. }
  472. // Prepare game resolution
  473. if(ConfigManager.getFullscreen()){
  474. mcArgs.push('--fullscreen')
  475. mcArgs.push(true)
  476. } else {
  477. mcArgs.push('--width')
  478. mcArgs.push(ConfigManager.getGameWidth())
  479. mcArgs.push('--height')
  480. mcArgs.push(ConfigManager.getGameHeight())
  481. }
  482. // Mod List File Argument
  483. mcArgs.push('--modListFile')
  484. mcArgs.push('absolute:' + this.fmlDir)
  485. // LiteLoader
  486. if(this.usingLiteLoader){
  487. mcArgs.push('--modRepo')
  488. mcArgs.push(this.llDir)
  489. // Set first arg to liteloader tweak class
  490. mcArgs.unshift('com.mumfrey.liteloader.launch.LiteLoaderTweaker')
  491. mcArgs.unshift('--tweakClass')
  492. }
  493. return mcArgs
  494. }
  495. /**
  496. * Resolve the full classpath argument list for this process. This method will resolve all Mojang-declared
  497. * libraries as well as the libraries declared by the server. Since mods are permitted to declare libraries,
  498. * this method requires all enabled mods as an input
  499. *
  500. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  501. * @param {string} tempNativePath The path to store the native libraries.
  502. * @returns {Array.<string>} An array containing the paths of each library required by this process.
  503. */
  504. classpathArg(mods, tempNativePath){
  505. let cpArgs = []
  506. // Add the version.jar to the classpath.
  507. const version = this.versionData.id
  508. cpArgs.push(path.join(this.commonDir, 'versions', version, version + '.jar'))
  509. if(this.usingLiteLoader){
  510. cpArgs.push(this.llPath)
  511. }
  512. // Resolve the Mojang declared libraries.
  513. const mojangLibs = this._resolveMojangLibraries(tempNativePath)
  514. cpArgs = cpArgs.concat(mojangLibs)
  515. // Resolve the server declared libraries.
  516. const servLibs = this._resolveServerLibraries(mods)
  517. cpArgs = cpArgs.concat(servLibs)
  518. return cpArgs
  519. }
  520. /**
  521. * Resolve the libraries defined by Mojang's version data. This method will also extract
  522. * native libraries and point to the correct location for its classpath.
  523. *
  524. * TODO - clean up function
  525. *
  526. * @param {string} tempNativePath The path to store the native libraries.
  527. * @returns {Array.<string>} An array containing the paths of each library mojang declares.
  528. */
  529. _resolveMojangLibraries(tempNativePath){
  530. const libs = []
  531. const libArr = this.versionData.libraries
  532. fs.ensureDirSync(tempNativePath)
  533. for(let i=0; i<libArr.length; i++){
  534. const lib = libArr[i]
  535. if(Library.validateRules(lib.rules, lib.natives)){
  536. if(lib.natives == null){
  537. const dlInfo = lib.downloads
  538. const artifact = dlInfo.artifact
  539. const to = path.join(this.libPath, artifact.path)
  540. libs.push(to)
  541. } else {
  542. // Extract the native library.
  543. const exclusionArr = lib.extract != null ? lib.extract.exclude : ['META-INF/']
  544. const artifact = lib.downloads.classifiers[lib.natives[Library.mojangFriendlyOS()].replace('${arch}', process.arch.replace('x', ''))]
  545. // Location of native zip.
  546. const to = path.join(this.libPath, artifact.path)
  547. let zip = new AdmZip(to)
  548. let zipEntries = zip.getEntries()
  549. // Unzip the native zip.
  550. for(let i=0; i<zipEntries.length; i++){
  551. const fileName = zipEntries[i].entryName
  552. let shouldExclude = false
  553. // Exclude noted files.
  554. exclusionArr.forEach(function(exclusion){
  555. if(fileName.indexOf(exclusion) > -1){
  556. shouldExclude = true
  557. }
  558. })
  559. // Extract the file.
  560. if(!shouldExclude){
  561. fs.writeFile(path.join(tempNativePath, fileName), zipEntries[i].getData(), (err) => {
  562. if(err){
  563. logger.error('Error while extracting native library:', err)
  564. }
  565. })
  566. }
  567. }
  568. }
  569. }
  570. }
  571. return libs
  572. }
  573. /**
  574. * Resolve the libraries declared by this server in order to add them to the classpath.
  575. * This method will also check each enabled mod for libraries, as mods are permitted to
  576. * declare libraries.
  577. *
  578. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  579. * @returns {Array.<string>} An array containing the paths of each library this server requires.
  580. */
  581. _resolveServerLibraries(mods){
  582. const mdls = this.server.getModules()
  583. let libs = []
  584. // Locate Forge/Libraries
  585. for(let mdl of mdls){
  586. const type = mdl.getType()
  587. if(type === DistroManager.Types.ForgeHosted || type === DistroManager.Types.Library){
  588. libs.push(mdl.getArtifact().getPath())
  589. if(mdl.hasSubModules()){
  590. const res = this._resolveModuleLibraries(mdl)
  591. if(res.length > 0){
  592. libs = libs.concat(res)
  593. }
  594. }
  595. }
  596. }
  597. //Check for any libraries in our mod list.
  598. for(let i=0; i<mods.length; i++){
  599. if(mods.sub_modules != null){
  600. const res = this._resolveModuleLibraries(mods[i])
  601. if(res.length > 0){
  602. libs = libs.concat(res)
  603. }
  604. }
  605. }
  606. return libs
  607. }
  608. /**
  609. * Recursively resolve the path of each library required by this module.
  610. *
  611. * @param {Object} mdl A module object from the server distro index.
  612. * @returns {Array.<string>} An array containing the paths of each library this module requires.
  613. */
  614. _resolveModuleLibraries(mdl){
  615. if(!mdl.hasSubModules()){
  616. return []
  617. }
  618. let libs = []
  619. for(let sm of mdl.getSubModules()){
  620. if(sm.getType() === DistroManager.Types.Library){
  621. libs.push(sm.getArtifact().getPath())
  622. }
  623. // If this module has submodules, we need to resolve the libraries for those.
  624. // To avoid unnecessary recursive calls, base case is checked here.
  625. if(mdl.hasSubModules()){
  626. const res = this._resolveModuleLibraries(sm)
  627. if(res.length > 0){
  628. libs = libs.concat(res)
  629. }
  630. }
  631. }
  632. return libs
  633. }
  634. }
  635. module.exports = ProcessBuilder