processbuilder.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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. let isAutoconnectBroken
  425. try {
  426. isAutoconnectBroken = Util.isAutoconnectBroken(this.forgeData.id.split('-')[2])
  427. } catch(err) {
  428. logger.error('Forge version format changed.. assuming autoconnect works.')
  429. }
  430. if(isAutoconnectBroken) {
  431. logger.error('Server autoconnect disabled on Forge 1.15.2 for builds earlier than 31.2.15 due to OpenGL Stack Overflow issue.')
  432. logger.error('Please upgrade your Forge version to at least 31.2.15!')
  433. } else {
  434. this._processAutoConnectArg(args)
  435. }
  436. // Forge Specific Arguments
  437. args = args.concat(this.forgeData.arguments.game)
  438. // Filter null values
  439. args = args.filter(arg => {
  440. return arg != null
  441. })
  442. return args
  443. }
  444. /**
  445. * Resolve the arguments required by forge.
  446. *
  447. * @returns {Array.<string>} An array containing the arguments required by forge.
  448. */
  449. _resolveForgeArgs(){
  450. const mcArgs = this.forgeData.minecraftArguments.split(' ')
  451. const argDiscovery = /\${*(.*)}/
  452. // Replace the declared variables with their proper values.
  453. for(let i=0; i<mcArgs.length; ++i){
  454. if(argDiscovery.test(mcArgs[i])){
  455. const identifier = mcArgs[i].match(argDiscovery)[1]
  456. let val = null
  457. switch(identifier){
  458. case 'auth_player_name':
  459. val = this.authUser.displayName.trim()
  460. break
  461. case 'version_name':
  462. //val = versionData.id
  463. val = this.server.getID()
  464. break
  465. case 'game_directory':
  466. val = this.gameDir
  467. break
  468. case 'assets_root':
  469. val = path.join(this.commonDir, 'assets')
  470. break
  471. case 'assets_index_name':
  472. val = this.versionData.assets
  473. break
  474. case 'auth_uuid':
  475. val = this.authUser.uuid.trim()
  476. break
  477. case 'auth_access_token':
  478. val = this.authUser.accessToken
  479. break
  480. case 'user_type':
  481. val = 'mojang'
  482. break
  483. case 'user_properties': // 1.8.9 and below.
  484. val = '{}'
  485. break
  486. case 'version_type':
  487. val = this.versionData.type
  488. break
  489. }
  490. if(val != null){
  491. mcArgs[i] = val
  492. }
  493. }
  494. }
  495. // Autoconnect to the selected server.
  496. this._processAutoConnectArg(mcArgs)
  497. // Prepare game resolution
  498. if(ConfigManager.getFullscreen()){
  499. mcArgs.push('--fullscreen')
  500. mcArgs.push(true)
  501. } else {
  502. mcArgs.push('--width')
  503. mcArgs.push(ConfigManager.getGameWidth())
  504. mcArgs.push('--height')
  505. mcArgs.push(ConfigManager.getGameHeight())
  506. }
  507. // Mod List File Argument
  508. mcArgs.push('--modListFile')
  509. if(this._isBelowOneDotSeven()) {
  510. mcArgs.push(path.basename(this.fmlDir))
  511. } else {
  512. mcArgs.push('absolute:' + this.fmlDir)
  513. }
  514. // LiteLoader
  515. if(this.usingLiteLoader){
  516. mcArgs.push('--modRepo')
  517. mcArgs.push(this.llDir)
  518. // Set first arg to liteloader tweak class
  519. mcArgs.unshift('com.mumfrey.liteloader.launch.LiteLoaderTweaker')
  520. mcArgs.unshift('--tweakClass')
  521. }
  522. return mcArgs
  523. }
  524. /**
  525. * Ensure that the classpath entries all point to jar files.
  526. *
  527. * @param {Array.<String>} list Array of classpath entries.
  528. */
  529. _processClassPathList(list) {
  530. const ext = '.jar'
  531. const extLen = ext.length
  532. for(let i=0; i<list.length; i++) {
  533. const extIndex = list[i].indexOf(ext)
  534. if(extIndex > -1 && extIndex !== list[i].length - extLen) {
  535. list[i] = list[i].substring(0, extIndex + extLen)
  536. }
  537. }
  538. }
  539. /**
  540. * Resolve the full classpath argument list for this process. This method will resolve all Mojang-declared
  541. * libraries as well as the libraries declared by the server. Since mods are permitted to declare libraries,
  542. * this method requires all enabled mods as an input
  543. *
  544. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  545. * @param {string} tempNativePath The path to store the native libraries.
  546. * @returns {Array.<string>} An array containing the paths of each library required by this process.
  547. */
  548. classpathArg(mods, tempNativePath){
  549. let cpArgs = []
  550. // Add the version.jar to the classpath.
  551. const version = this.versionData.id
  552. cpArgs.push(path.join(this.commonDir, 'versions', version, version + '.jar'))
  553. if(this.usingLiteLoader){
  554. cpArgs.push(this.llPath)
  555. }
  556. // Resolve the Mojang declared libraries.
  557. const mojangLibs = this._resolveMojangLibraries(tempNativePath)
  558. // Resolve the server declared libraries.
  559. const servLibs = this._resolveServerLibraries(mods)
  560. // Merge libraries, server libs with the same
  561. // maven identifier will override the mojang ones.
  562. // Ex. 1.7.10 forge overrides mojang's guava with newer version.
  563. const finalLibs = {...mojangLibs, ...servLibs}
  564. cpArgs = cpArgs.concat(Object.values(finalLibs))
  565. this._processClassPathList(cpArgs)
  566. return cpArgs
  567. }
  568. /**
  569. * Resolve the libraries defined by Mojang's version data. This method will also extract
  570. * native libraries and point to the correct location for its classpath.
  571. *
  572. * TODO - clean up function
  573. *
  574. * @param {string} tempNativePath The path to store the native libraries.
  575. * @returns {{[id: string]: string}} An object containing the paths of each library mojang declares.
  576. */
  577. _resolveMojangLibraries(tempNativePath){
  578. const libs = {}
  579. const libArr = this.versionData.libraries
  580. fs.ensureDirSync(tempNativePath)
  581. for(let i=0; i<libArr.length; i++){
  582. const lib = libArr[i]
  583. if(Library.validateRules(lib.rules, lib.natives)){
  584. if(lib.natives == null){
  585. const dlInfo = lib.downloads
  586. const artifact = dlInfo.artifact
  587. const to = path.join(this.libPath, artifact.path)
  588. const versionIndependentId = lib.name.substring(0, lib.name.lastIndexOf(':'))
  589. libs[versionIndependentId] = to
  590. } else {
  591. // Extract the native library.
  592. const exclusionArr = lib.extract != null ? lib.extract.exclude : ['META-INF/']
  593. const artifact = lib.downloads.classifiers[lib.natives[Library.mojangFriendlyOS()].replace('${arch}', process.arch.replace('x', ''))]
  594. // Location of native zip.
  595. const to = path.join(this.libPath, artifact.path)
  596. let zip = new AdmZip(to)
  597. let zipEntries = zip.getEntries()
  598. // Unzip the native zip.
  599. for(let i=0; i<zipEntries.length; i++){
  600. const fileName = zipEntries[i].entryName
  601. let shouldExclude = false
  602. // Exclude noted files.
  603. exclusionArr.forEach(function(exclusion){
  604. if(fileName.indexOf(exclusion) > -1){
  605. shouldExclude = true
  606. }
  607. })
  608. // Extract the file.
  609. if(!shouldExclude){
  610. fs.writeFile(path.join(tempNativePath, fileName), zipEntries[i].getData(), (err) => {
  611. if(err){
  612. logger.error('Error while extracting native library:', err)
  613. }
  614. })
  615. }
  616. }
  617. }
  618. }
  619. }
  620. return libs
  621. }
  622. /**
  623. * Resolve the libraries declared by this server in order to add them to the classpath.
  624. * This method will also check each enabled mod for libraries, as mods are permitted to
  625. * declare libraries.
  626. *
  627. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  628. * @returns {{[id: string]: string}} An object containing the paths of each library this server requires.
  629. */
  630. _resolveServerLibraries(mods){
  631. const mdls = this.server.getModules()
  632. let libs = {}
  633. // Locate Forge/Libraries
  634. for(let mdl of mdls){
  635. const type = mdl.getType()
  636. if(type === DistroManager.Types.ForgeHosted || type === DistroManager.Types.Library){
  637. libs[mdl.getVersionlessID()] = mdl.getArtifact().getPath()
  638. if(mdl.hasSubModules()){
  639. const res = this._resolveModuleLibraries(mdl)
  640. if(res.length > 0){
  641. libs = {...libs, ...res}
  642. }
  643. }
  644. }
  645. }
  646. //Check for any libraries in our mod list.
  647. for(let i=0; i<mods.length; i++){
  648. if(mods.sub_modules != null){
  649. const res = this._resolveModuleLibraries(mods[i])
  650. if(res.length > 0){
  651. libs = {...libs, ...res}
  652. }
  653. }
  654. }
  655. return libs
  656. }
  657. /**
  658. * Recursively resolve the path of each library required by this module.
  659. *
  660. * @param {Object} mdl A module object from the server distro index.
  661. * @returns {Array.<string>} An array containing the paths of each library this module requires.
  662. */
  663. _resolveModuleLibraries(mdl){
  664. if(!mdl.hasSubModules()){
  665. return []
  666. }
  667. let libs = []
  668. for(let sm of mdl.getSubModules()){
  669. if(sm.getType() === DistroManager.Types.Library){
  670. libs.push(sm.getArtifact().getPath())
  671. }
  672. // If this module has submodules, we need to resolve the libraries for those.
  673. // To avoid unnecessary recursive calls, base case is checked here.
  674. if(mdl.hasSubModules()){
  675. const res = this._resolveModuleLibraries(sm)
  676. if(res.length > 0){
  677. libs = libs.concat(res)
  678. }
  679. }
  680. }
  681. return libs
  682. }
  683. }
  684. module.exports = ProcessBuilder