processbuilder.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. _isBelowOneDotSeven() {
  166. return Number(this.forgeData.id.split('-')[0].split('.')[1]) <= 7
  167. }
  168. /**
  169. * Test to see if this version of forge requires the absolute: prefix
  170. * on the modListFile repository field.
  171. */
  172. _requiresAbsolute(){
  173. try {
  174. if(this._isBelowOneDotSeven()) {
  175. return false
  176. }
  177. const ver = this.forgeData.id.split('-')[2]
  178. const pts = ver.split('.')
  179. const min = [14, 23, 3, 2655]
  180. for(let i=0; i<pts.length; i++){
  181. const parsed = Number.parseInt(pts[i])
  182. if(parsed < min[i]){
  183. return false
  184. } else if(parsed > min[i]){
  185. return true
  186. }
  187. }
  188. } catch (err) {
  189. // We know old forge versions follow this format.
  190. // Error must be caused by newer version.
  191. }
  192. // Equal or errored
  193. return true
  194. }
  195. /**
  196. * Construct a mod list json object.
  197. *
  198. * @param {'forge' | 'liteloader'} type The mod list type to construct.
  199. * @param {Array.<Object>} mods An array of mods to add to the mod list.
  200. * @param {boolean} save Optional. Whether or not we should save the mod list file.
  201. */
  202. constructModList(type, mods, save = false){
  203. const modList = {
  204. repositoryRoot: ((type === 'forge' && this._requiresAbsolute()) ? 'absolute:' : '') + path.join(this.commonDir, 'modstore')
  205. }
  206. const ids = []
  207. if(type === 'forge'){
  208. for(let mod of mods){
  209. ids.push(mod.getExtensionlessID())
  210. }
  211. } else {
  212. for(let mod of mods){
  213. ids.push(mod.getExtensionlessID() + '@' + mod.getExtension())
  214. }
  215. }
  216. modList.modRef = ids
  217. if(save){
  218. const json = JSON.stringify(modList, null, 4)
  219. fs.writeFileSync(type === 'forge' ? this.fmlDir : this.llDir, json, 'UTF-8')
  220. }
  221. return modList
  222. }
  223. /**
  224. * Construct the mod argument list for forge 1.13
  225. *
  226. * @param {Array.<Object>} mods An array of mods to add to the mod list.
  227. */
  228. constructModArguments(mods){
  229. const argStr = mods.map(mod => {
  230. return mod.getExtensionlessID()
  231. }).join(',')
  232. if(argStr){
  233. return [
  234. '--fml.mavenRoots',
  235. path.join('..', '..', 'common', 'modstore'),
  236. '--fml.mods',
  237. argStr
  238. ]
  239. } else {
  240. return []
  241. }
  242. }
  243. /**
  244. * Construct the argument array that will be passed to the JVM process.
  245. *
  246. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  247. * @param {string} tempNativePath The path to store the native libraries.
  248. * @returns {Array.<string>} An array containing the full JVM arguments for this process.
  249. */
  250. constructJVMArguments(mods, tempNativePath){
  251. if(Util.mcVersionAtLeast('1.13', this.server.getMinecraftVersion())){
  252. return this._constructJVMArguments113(mods, tempNativePath)
  253. } else {
  254. return this._constructJVMArguments112(mods, tempNativePath)
  255. }
  256. }
  257. /**
  258. * Construct the argument array that will be passed to the JVM process.
  259. * This function is for 1.12 and below.
  260. *
  261. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  262. * @param {string} tempNativePath The path to store the native libraries.
  263. * @returns {Array.<string>} An array containing the full JVM arguments for this process.
  264. */
  265. _constructJVMArguments112(mods, tempNativePath){
  266. let args = []
  267. // Classpath Argument
  268. args.push('-cp')
  269. args.push(this.classpathArg(mods, tempNativePath).join(process.platform === 'win32' ? ';' : ':'))
  270. // Java Arguments
  271. if(process.platform === 'darwin'){
  272. args.push('-Xdock:name=HeliosLauncher')
  273. args.push('-Xdock:icon=' + path.join(__dirname, '..', 'images', 'minecraft.icns'))
  274. }
  275. args.push('-Xmx' + ConfigManager.getMaxRAM())
  276. args.push('-Xms' + ConfigManager.getMinRAM())
  277. args = args.concat(ConfigManager.getJVMOptions())
  278. args.push('-Djava.library.path=' + tempNativePath)
  279. // Main Java Class
  280. args.push(this.forgeData.mainClass)
  281. // Forge Arguments
  282. args = args.concat(this._resolveForgeArgs())
  283. return args
  284. }
  285. /**
  286. * Construct the argument array that will be passed to the JVM process.
  287. * This function is for 1.13+
  288. *
  289. * Note: Required Libs https://github.com/MinecraftForge/MinecraftForge/blob/af98088d04186452cb364280340124dfd4766a5c/src/fmllauncher/java/net/minecraftforge/fml/loading/LibraryFinder.java#L82
  290. *
  291. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  292. * @param {string} tempNativePath The path to store the native libraries.
  293. * @returns {Array.<string>} An array containing the full JVM arguments for this process.
  294. */
  295. _constructJVMArguments113(mods, tempNativePath){
  296. const argDiscovery = /\${*(.*)}/
  297. // JVM Arguments First
  298. let args = this.versionData.arguments.jvm
  299. //args.push('-Dlog4j.configurationFile=D:\\WesterosCraft\\game\\common\\assets\\log_configs\\client-1.12.xml')
  300. // Java Arguments
  301. if(process.platform === 'darwin'){
  302. args.push('-Xdock:name=HeliosLauncher')
  303. args.push('-Xdock:icon=' + path.join(__dirname, '..', 'images', 'minecraft.icns'))
  304. }
  305. args.push('-Xmx' + ConfigManager.getMaxRAM())
  306. args.push('-Xms' + ConfigManager.getMinRAM())
  307. args = args.concat(ConfigManager.getJVMOptions())
  308. // Main Java Class
  309. args.push(this.forgeData.mainClass)
  310. // Vanilla Arguments
  311. args = args.concat(this.versionData.arguments.game)
  312. for(let i=0; i<args.length; i++){
  313. if(typeof args[i] === 'object' && args[i].rules != null){
  314. let checksum = 0
  315. for(let rule of args[i].rules){
  316. if(rule.os != null){
  317. if(rule.os.name === Library.mojangFriendlyOS()
  318. && (rule.os.version == null || new RegExp(rule.os.version).test(os.release))){
  319. if(rule.action === 'allow'){
  320. checksum++
  321. }
  322. } else {
  323. if(rule.action === 'disallow'){
  324. checksum++
  325. }
  326. }
  327. } else if(rule.features != null){
  328. // We don't have many 'features' in the index at the moment.
  329. // This should be fine for a while.
  330. if(rule.features.has_custom_resolution != null && rule.features.has_custom_resolution === true){
  331. if(ConfigManager.getFullscreen()){
  332. rule.values = [
  333. '--fullscreen',
  334. 'true'
  335. ]
  336. }
  337. checksum++
  338. }
  339. }
  340. }
  341. // TODO splice not push
  342. if(checksum === args[i].rules.length){
  343. if(typeof args[i].value === 'string'){
  344. args[i] = args[i].value
  345. } else if(typeof args[i].value === 'object'){
  346. //args = args.concat(args[i].value)
  347. args.splice(i, 1, ...args[i].value)
  348. }
  349. // Decrement i to reprocess the resolved value
  350. i--
  351. } else {
  352. args[i] = null
  353. }
  354. } else if(typeof args[i] === 'string'){
  355. if(argDiscovery.test(args[i])){
  356. const identifier = args[i].match(argDiscovery)[1]
  357. let val = null
  358. switch(identifier){
  359. case 'auth_player_name':
  360. val = this.authUser.displayName.trim()
  361. break
  362. case 'version_name':
  363. //val = versionData.id
  364. val = this.server.getID()
  365. break
  366. case 'game_directory':
  367. val = this.gameDir
  368. break
  369. case 'assets_root':
  370. val = path.join(this.commonDir, 'assets')
  371. break
  372. case 'assets_index_name':
  373. val = this.versionData.assets
  374. break
  375. case 'auth_uuid':
  376. val = this.authUser.uuid.trim()
  377. break
  378. case 'auth_access_token':
  379. val = this.authUser.accessToken
  380. break
  381. case 'user_type':
  382. val = 'mojang'
  383. break
  384. case 'version_type':
  385. val = this.versionData.type
  386. break
  387. case 'resolution_width':
  388. val = ConfigManager.getGameWidth()
  389. break
  390. case 'resolution_height':
  391. val = ConfigManager.getGameHeight()
  392. break
  393. case 'natives_directory':
  394. val = args[i].replace(argDiscovery, tempNativePath)
  395. break
  396. case 'launcher_name':
  397. val = args[i].replace(argDiscovery, 'Helios-Launcher')
  398. break
  399. case 'launcher_version':
  400. val = args[i].replace(argDiscovery, this.launcherVersion)
  401. break
  402. case 'classpath':
  403. val = this.classpathArg(mods, tempNativePath).join(process.platform === 'win32' ? ';' : ':')
  404. break
  405. }
  406. if(val != null){
  407. args[i] = val
  408. }
  409. }
  410. }
  411. }
  412. // Forge Specific Arguments
  413. args = args.concat(this.forgeData.arguments.game)
  414. // Filter null values
  415. args = args.filter(arg => {
  416. return arg != null
  417. })
  418. return args
  419. }
  420. /**
  421. * Resolve the arguments required by forge.
  422. *
  423. * @returns {Array.<string>} An array containing the arguments required by forge.
  424. */
  425. _resolveForgeArgs(){
  426. const mcArgs = this.forgeData.minecraftArguments.split(' ')
  427. const argDiscovery = /\${*(.*)}/
  428. // Replace the declared variables with their proper values.
  429. for(let i=0; i<mcArgs.length; ++i){
  430. if(argDiscovery.test(mcArgs[i])){
  431. const identifier = mcArgs[i].match(argDiscovery)[1]
  432. let val = null
  433. switch(identifier){
  434. case 'auth_player_name':
  435. val = this.authUser.displayName.trim()
  436. break
  437. case 'version_name':
  438. //val = versionData.id
  439. val = this.server.getID()
  440. break
  441. case 'game_directory':
  442. val = this.gameDir
  443. break
  444. case 'assets_root':
  445. val = path.join(this.commonDir, 'assets')
  446. break
  447. case 'assets_index_name':
  448. val = this.versionData.assets
  449. break
  450. case 'auth_uuid':
  451. val = this.authUser.uuid.trim()
  452. break
  453. case 'auth_access_token':
  454. val = this.authUser.accessToken
  455. break
  456. case 'user_type':
  457. val = 'mojang'
  458. break
  459. case 'user_properties': // 1.8.9 and below.
  460. val = '{}'
  461. break
  462. case 'version_type':
  463. val = this.versionData.type
  464. break
  465. }
  466. if(val != null){
  467. mcArgs[i] = val
  468. }
  469. }
  470. }
  471. // Autoconnect to the selected server.
  472. if(ConfigManager.getAutoConnect() && this.server.isAutoConnect()){
  473. const serverURL = new URL('my://' + this.server.getAddress())
  474. mcArgs.push('--server')
  475. mcArgs.push(serverURL.hostname)
  476. if(serverURL.port){
  477. mcArgs.push('--port')
  478. mcArgs.push(serverURL.port)
  479. }
  480. }
  481. // Prepare game resolution
  482. if(ConfigManager.getFullscreen()){
  483. mcArgs.push('--fullscreen')
  484. mcArgs.push(true)
  485. } else {
  486. mcArgs.push('--width')
  487. mcArgs.push(ConfigManager.getGameWidth())
  488. mcArgs.push('--height')
  489. mcArgs.push(ConfigManager.getGameHeight())
  490. }
  491. // Mod List File Argument
  492. mcArgs.push('--modListFile')
  493. if(this._isBelowOneDotSeven()) {
  494. mcArgs.push(path.basename(this.fmlDir))
  495. } else {
  496. mcArgs.push('absolute:' + this.fmlDir)
  497. }
  498. // LiteLoader
  499. if(this.usingLiteLoader){
  500. mcArgs.push('--modRepo')
  501. mcArgs.push(this.llDir)
  502. // Set first arg to liteloader tweak class
  503. mcArgs.unshift('com.mumfrey.liteloader.launch.LiteLoaderTweaker')
  504. mcArgs.unshift('--tweakClass')
  505. }
  506. return mcArgs
  507. }
  508. /**
  509. * Resolve the full classpath argument list for this process. This method will resolve all Mojang-declared
  510. * libraries as well as the libraries declared by the server. Since mods are permitted to declare libraries,
  511. * this method requires all enabled mods as an input
  512. *
  513. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  514. * @param {string} tempNativePath The path to store the native libraries.
  515. * @returns {Array.<string>} An array containing the paths of each library required by this process.
  516. */
  517. classpathArg(mods, tempNativePath){
  518. let cpArgs = []
  519. // Add the version.jar to the classpath.
  520. const version = this.versionData.id
  521. cpArgs.push(path.join(this.commonDir, 'versions', version, version + '.jar'))
  522. if(this.usingLiteLoader){
  523. cpArgs.push(this.llPath)
  524. }
  525. // Resolve the Mojang declared libraries.
  526. const mojangLibs = this._resolveMojangLibraries(tempNativePath)
  527. // Resolve the server declared libraries.
  528. const servLibs = this._resolveServerLibraries(mods)
  529. // Merge libraries, server libs with the same
  530. // maven identifier will override the mojang ones.
  531. // Ex. 1.7.10 forge overrides mojang's guava with newer version.
  532. const finalLibs = {...mojangLibs, ...servLibs}
  533. cpArgs = cpArgs.concat(Object.values(finalLibs))
  534. return cpArgs
  535. }
  536. /**
  537. * Resolve the libraries defined by Mojang's version data. This method will also extract
  538. * native libraries and point to the correct location for its classpath.
  539. *
  540. * TODO - clean up function
  541. *
  542. * @param {string} tempNativePath The path to store the native libraries.
  543. * @returns {{[id: string]: string}} An object containing the paths of each library mojang declares.
  544. */
  545. _resolveMojangLibraries(tempNativePath){
  546. const libs = {}
  547. const libArr = this.versionData.libraries
  548. fs.ensureDirSync(tempNativePath)
  549. for(let i=0; i<libArr.length; i++){
  550. const lib = libArr[i]
  551. if(Library.validateRules(lib.rules, lib.natives)){
  552. if(lib.natives == null){
  553. const dlInfo = lib.downloads
  554. const artifact = dlInfo.artifact
  555. const to = path.join(this.libPath, artifact.path)
  556. const versionIndependentId = lib.name.substring(0, lib.name.lastIndexOf(':'))
  557. libs[versionIndependentId] = to
  558. } else {
  559. // Extract the native library.
  560. const exclusionArr = lib.extract != null ? lib.extract.exclude : ['META-INF/']
  561. const artifact = lib.downloads.classifiers[lib.natives[Library.mojangFriendlyOS()].replace('${arch}', process.arch.replace('x', ''))]
  562. // Location of native zip.
  563. const to = path.join(this.libPath, artifact.path)
  564. let zip = new AdmZip(to)
  565. let zipEntries = zip.getEntries()
  566. // Unzip the native zip.
  567. for(let i=0; i<zipEntries.length; i++){
  568. const fileName = zipEntries[i].entryName
  569. let shouldExclude = false
  570. // Exclude noted files.
  571. exclusionArr.forEach(function(exclusion){
  572. if(fileName.indexOf(exclusion) > -1){
  573. shouldExclude = true
  574. }
  575. })
  576. // Extract the file.
  577. if(!shouldExclude){
  578. fs.writeFile(path.join(tempNativePath, fileName), zipEntries[i].getData(), (err) => {
  579. if(err){
  580. logger.error('Error while extracting native library:', err)
  581. }
  582. })
  583. }
  584. }
  585. }
  586. }
  587. }
  588. return libs
  589. }
  590. /**
  591. * Resolve the libraries declared by this server in order to add them to the classpath.
  592. * This method will also check each enabled mod for libraries, as mods are permitted to
  593. * declare libraries.
  594. *
  595. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  596. * @returns {{[id: string]: string}} An object containing the paths of each library this server requires.
  597. */
  598. _resolveServerLibraries(mods){
  599. const mdls = this.server.getModules()
  600. let libs = {}
  601. // Locate Forge/Libraries
  602. for(let mdl of mdls){
  603. const type = mdl.getType()
  604. if(type === DistroManager.Types.ForgeHosted || type === DistroManager.Types.Library){
  605. libs[mdl.getVersionlessID()] = mdl.getArtifact().getPath()
  606. if(mdl.hasSubModules()){
  607. const res = this._resolveModuleLibraries(mdl)
  608. if(res.length > 0){
  609. libs = {...libs, ...res}
  610. }
  611. }
  612. }
  613. }
  614. //Check for any libraries in our mod list.
  615. for(let i=0; i<mods.length; i++){
  616. if(mods.sub_modules != null){
  617. const res = this._resolveModuleLibraries(mods[i])
  618. if(res.length > 0){
  619. libs = {...libs, ...res}
  620. }
  621. }
  622. }
  623. return libs
  624. }
  625. /**
  626. * Recursively resolve the path of each library required by this module.
  627. *
  628. * @param {Object} mdl A module object from the server distro index.
  629. * @returns {Array.<string>} An array containing the paths of each library this module requires.
  630. */
  631. _resolveModuleLibraries(mdl){
  632. if(!mdl.hasSubModules()){
  633. return []
  634. }
  635. let libs = []
  636. for(let sm of mdl.getSubModules()){
  637. if(sm.getType() === DistroManager.Types.Library){
  638. libs.push(sm.getArtifact().getPath())
  639. }
  640. // If this module has submodules, we need to resolve the libraries for those.
  641. // To avoid unnecessary recursive calls, base case is checked here.
  642. if(mdl.hasSubModules()){
  643. const res = this._resolveModuleLibraries(sm)
  644. if(res.length > 0){
  645. libs = libs.concat(res)
  646. }
  647. }
  648. }
  649. return libs
  650. }
  651. }
  652. module.exports = ProcessBuilder