processbuilder.js 30 KB

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