processbuilder.ts 30 KB

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