processbuilder.js 33 KB

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