processbuilder.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 { 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){
  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.fmlDir = path.join(this.gameDir, 'forgeModList.json')
  22. this.llDir = path.join(this.gameDir, 'liteloaderModList.json')
  23. this.libPath = path.join(this.commonDir, 'libraries')
  24. this.usingLiteLoader = false
  25. this.llPath = null
  26. }
  27. /**
  28. * Convienence method to run the functions typically used to build a process.
  29. */
  30. build(){
  31. fs.ensureDirSync(this.gameDir)
  32. const tempNativePath = path.join(os.tmpdir(), ConfigManager.getTempNativeFolder(), crypto.pseudoRandomBytes(16).toString('hex'))
  33. process.throwDeprecation = true
  34. this.setupLiteLoader()
  35. logger.log('Using liteloader:', this.usingLiteLoader)
  36. const modObj = this.resolveModConfiguration(ConfigManager.getModConfiguration(this.server.getID()).mods, this.server.getModules())
  37. this.constructModList('forge', modObj.fMods, true)
  38. if(this.usingLiteLoader){
  39. this.constructModList('liteloader', modObj.lMods, true)
  40. }
  41. const uberModArr = modObj.fMods.concat(modObj.lMods)
  42. const args = this.constructJVMArguments(uberModArr, tempNativePath)
  43. logger.log('Launch Arguments:', args)
  44. const child = child_process.spawn(ConfigManager.getJavaExecutable(), args, {
  45. cwd: this.gameDir,
  46. detached: ConfigManager.getLaunchDetached()
  47. })
  48. if(ConfigManager.getLaunchDetached()){
  49. child.unref()
  50. }
  51. child.stdout.setEncoding('utf8')
  52. child.stderr.setEncoding('utf8')
  53. const loggerMCstdout = LoggerUtil('%c[Minecraft]', 'color: #36b030; font-weight: bold')
  54. const loggerMCstderr = LoggerUtil('%c[Minecraft]', 'color: #b03030; font-weight: bold')
  55. child.stdout.on('data', (data) => {
  56. loggerMCstdout.log(data)
  57. })
  58. child.stderr.on('data', (data) => {
  59. loggerMCstderr.log(data)
  60. })
  61. child.on('close', (code, signal) => {
  62. logger.log('Exited with code', code)
  63. fs.remove(tempNativePath, (err) => {
  64. if(err){
  65. logger.warn('Error while deleting temp dir', err)
  66. } else {
  67. logger.log('Temp dir deleted successfully.')
  68. }
  69. })
  70. })
  71. return child
  72. }
  73. /**
  74. * Determine if an optional mod is enabled from its configuration value. If the
  75. * configuration value is null, the required object will be used to
  76. * determine if it is enabled.
  77. *
  78. * A mod is enabled if:
  79. * * The configuration is not null and one of the following:
  80. * * The configuration is a boolean and true.
  81. * * The configuration is an object and its 'value' property is true.
  82. * * The configuration is null and one of the following:
  83. * * The required object is null.
  84. * * The required object's 'def' property is null or true.
  85. *
  86. * @param {Object | boolean} modCfg The mod configuration object.
  87. * @param {Object} required Optional. The required object from the mod's distro declaration.
  88. * @returns {boolean} True if the mod is enabled, false otherwise.
  89. */
  90. static isModEnabled(modCfg, required = null){
  91. return modCfg != null ? ((typeof modCfg === 'boolean' && modCfg) || (typeof modCfg === 'object' && (typeof modCfg.value !== 'undefined' ? modCfg.value : true))) : required != null ? required.isDefault() : true
  92. }
  93. /**
  94. * Function which performs a preliminary scan of the top level
  95. * mods. If liteloader is present here, we setup the special liteloader
  96. * launch options. Note that liteloader is only allowed as a top level
  97. * mod. It must not be declared as a submodule.
  98. */
  99. setupLiteLoader(){
  100. for(let ll of this.server.getModules()){
  101. if(ll.getType() === DistroManager.Types.LiteLoader){
  102. if(!ll.getRequired().isRequired()){
  103. const modCfg = ConfigManager.getModConfiguration(this.server.getID()).mods
  104. if(ProcessBuilder.isModEnabled(modCfg[ll.getVersionlessID()], ll.getRequired())){
  105. if(fs.existsSync(ll.getArtifact().getPath())){
  106. this.usingLiteLoader = true
  107. this.llPath = ll.getArtifact().getPath()
  108. }
  109. }
  110. } else {
  111. if(fs.existsSync(ll.getArtifact().getPath())){
  112. this.usingLiteLoader = true
  113. this.llPath = ll.getArtifact().getPath()
  114. }
  115. }
  116. }
  117. }
  118. }
  119. /**
  120. * Resolve an array of all enabled mods. These mods will be constructed into
  121. * a mod list format and enabled at launch.
  122. *
  123. * @param {Object} modCfg The mod configuration object.
  124. * @param {Array.<Object>} mdls An array of modules to parse.
  125. * @returns {{fMods: Array.<Object>, lMods: Array.<Object>}} An object which contains
  126. * a list of enabled forge mods and litemods.
  127. */
  128. resolveModConfiguration(modCfg, mdls){
  129. let fMods = []
  130. let lMods = []
  131. for(let mdl of mdls){
  132. const type = mdl.getType()
  133. if(type === DistroManager.Types.ForgeMod || type === DistroManager.Types.LiteMod || type === DistroManager.Types.LiteLoader){
  134. const o = !mdl.getRequired().isRequired()
  135. const e = ProcessBuilder.isModEnabled(modCfg[mdl.getVersionlessID()], mdl.getRequired())
  136. if(!o || (o && e)){
  137. if(mdl.hasSubModules()){
  138. const v = this.resolveModConfiguration(modCfg[mdl.getVersionlessID()].mods, mdl.getSubModules())
  139. fMods = fMods.concat(v.fMods)
  140. lMods = lMods.concat(v.lMods)
  141. if(mdl.type === DistroManager.Types.LiteLoader){
  142. continue
  143. }
  144. }
  145. if(mdl.type === DistroManager.Types.ForgeMod){
  146. fMods.push(mdl)
  147. } else {
  148. lMods.push(mdl)
  149. }
  150. }
  151. }
  152. }
  153. return {
  154. fMods,
  155. lMods
  156. }
  157. }
  158. /**
  159. * Test to see if this version of forge requires the absolute: prefix
  160. * on the modListFile repository field.
  161. */
  162. _requiresAbsolute(){
  163. try {
  164. const ver = this.forgeData.id.split('-')[2]
  165. const pts = ver.split('.')
  166. const min = [14, 23, 3, 2655]
  167. for(let i=0; i<pts.length; i++){
  168. const parsed = Number.parseInt(pts[i])
  169. if(parsed < min[i]){
  170. return false
  171. } else if(parsed > min[i]){
  172. return true
  173. }
  174. }
  175. } catch (err) {
  176. // We know old forge versions follow this format.
  177. // Error must be caused by newer version.
  178. }
  179. // Equal or errored
  180. return true
  181. }
  182. /**
  183. * Construct a mod list json object.
  184. *
  185. * @param {'forge' | 'liteloader'} type The mod list type to construct.
  186. * @param {Array.<Object>} mods An array of mods to add to the mod list.
  187. * @param {boolean} save Optional. Whether or not we should save the mod list file.
  188. */
  189. constructModList(type, mods, save = false){
  190. const modList = {
  191. repositoryRoot: ((type === 'forge' && this._requiresAbsolute()) ? 'absolute:' : '') + path.join(this.commonDir, 'modstore')
  192. }
  193. const ids = []
  194. if(type === 'forge'){
  195. for(let mod of mods){
  196. ids.push(mod.getExtensionlessID())
  197. }
  198. } else {
  199. for(let mod of mods){
  200. ids.push(mod.getExtensionlessID() + '@' + mod.getExtension())
  201. }
  202. }
  203. modList.modRef = ids
  204. if(save){
  205. const json = JSON.stringify(modList, null, 4)
  206. fs.writeFileSync(type === 'forge' ? this.fmlDir : this.llDir, json, 'UTF-8')
  207. }
  208. return modList
  209. }
  210. /**
  211. * Construct the argument array that will be passed to the JVM process.
  212. *
  213. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  214. * @param {string} tempNativePath The path to store the native libraries.
  215. * @returns {Array.<string>} An array containing the full JVM arguments for this process.
  216. */
  217. constructJVMArguments(mods, tempNativePath){
  218. let args = []
  219. // Classpath Argument
  220. args.push('-cp')
  221. args.push(this.classpathArg(mods, tempNativePath).join(process.platform === 'win32' ? ';' : ':'))
  222. // Java Arguments
  223. if(process.platform === 'darwin'){
  224. args.push('-Xdock:name=WesterosCraft')
  225. args.push('-Xdock:icon=' + path.join(__dirname, '..', 'images', 'minecraft.icns'))
  226. }
  227. args.push('-Xmx' + ConfigManager.getMaxRAM())
  228. args.push('-Xms' + ConfigManager.getMinRAM())
  229. args = args.concat(ConfigManager.getJVMOptions())
  230. args.push('-Djava.library.path=' + tempNativePath)
  231. // Main Java Class
  232. args.push(this.forgeData.mainClass)
  233. // Forge Arguments
  234. args = args.concat(this._resolveForgeArgs())
  235. return args
  236. }
  237. /**
  238. * Resolve the arguments required by forge.
  239. *
  240. * @returns {Array.<string>} An array containing the arguments required by forge.
  241. */
  242. _resolveForgeArgs(){
  243. const mcArgs = this.forgeData.minecraftArguments.split(' ')
  244. const argDiscovery = /\${*(.*)}/
  245. // Replace the declared variables with their proper values.
  246. for(let i=0; i<mcArgs.length; ++i){
  247. if(argDiscovery.test(mcArgs[i])){
  248. const identifier = mcArgs[i].match(argDiscovery)[1]
  249. let val = null
  250. switch(identifier){
  251. case 'auth_player_name':
  252. val = this.authUser.displayName.trim()
  253. break
  254. case 'version_name':
  255. //val = versionData.id
  256. val = this.server.getID()
  257. break
  258. case 'game_directory':
  259. val = this.gameDir
  260. break
  261. case 'assets_root':
  262. val = path.join(this.commonDir, 'assets')
  263. break
  264. case 'assets_index_name':
  265. val = this.versionData.assets
  266. break
  267. case 'auth_uuid':
  268. val = this.authUser.uuid.trim()
  269. break
  270. case 'auth_access_token':
  271. val = this.authUser.accessToken
  272. break
  273. case 'user_type':
  274. val = 'mojang'
  275. break
  276. case 'version_type':
  277. val = this.versionData.type
  278. break
  279. }
  280. if(val != null){
  281. mcArgs[i] = val
  282. }
  283. }
  284. }
  285. // Autoconnect to the selected server.
  286. if(ConfigManager.getAutoConnect() && this.server.isAutoConnect()){
  287. const serverURL = new URL('my://' + this.server.getAddress())
  288. mcArgs.push('--server')
  289. mcArgs.push(serverURL.hostname)
  290. if(serverURL.port){
  291. mcArgs.push('--port')
  292. mcArgs.push(serverURL.port)
  293. }
  294. }
  295. // Prepare game resolution
  296. if(ConfigManager.getFullscreen()){
  297. mcArgs.push('--fullscreen')
  298. mcArgs.push(true)
  299. } else {
  300. mcArgs.push('--width')
  301. mcArgs.push(ConfigManager.getGameWidth())
  302. mcArgs.push('--height')
  303. mcArgs.push(ConfigManager.getGameHeight())
  304. }
  305. // Mod List File Argument
  306. mcArgs.push('--modListFile')
  307. mcArgs.push('absolute:' + this.fmlDir)
  308. // LiteLoader
  309. if(this.usingLiteLoader){
  310. mcArgs.push('--modRepo')
  311. mcArgs.push(this.llDir)
  312. // Set first arg to liteloader tweak class
  313. mcArgs.unshift('com.mumfrey.liteloader.launch.LiteLoaderTweaker')
  314. mcArgs.unshift('--tweakClass')
  315. }
  316. return mcArgs
  317. }
  318. /**
  319. * Resolve the full classpath argument list for this process. This method will resolve all Mojang-declared
  320. * libraries as well as the libraries declared by the server. Since mods are permitted to declare libraries,
  321. * this method requires all enabled mods as an input
  322. *
  323. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  324. * @param {string} tempNativePath The path to store the native libraries.
  325. * @returns {Array.<string>} An array containing the paths of each library required by this process.
  326. */
  327. classpathArg(mods, tempNativePath){
  328. let cpArgs = []
  329. // Add the version.jar to the classpath.
  330. const version = this.versionData.id
  331. cpArgs.push(path.join(this.commonDir, 'versions', version, version + '.jar'))
  332. if(this.usingLiteLoader){
  333. cpArgs.push(this.llPath)
  334. }
  335. // Resolve the Mojang declared libraries.
  336. const mojangLibs = this._resolveMojangLibraries(tempNativePath)
  337. cpArgs = cpArgs.concat(mojangLibs)
  338. // Resolve the server declared libraries.
  339. const servLibs = this._resolveServerLibraries(mods)
  340. cpArgs = cpArgs.concat(servLibs)
  341. return cpArgs
  342. }
  343. /**
  344. * Resolve the libraries defined by Mojang's version data. This method will also extract
  345. * native libraries and point to the correct location for its classpath.
  346. *
  347. * TODO - clean up function
  348. *
  349. * @param {string} tempNativePath The path to store the native libraries.
  350. * @returns {Array.<string>} An array containing the paths of each library mojang declares.
  351. */
  352. _resolveMojangLibraries(tempNativePath){
  353. const libs = []
  354. const libArr = this.versionData.libraries
  355. fs.ensureDirSync(tempNativePath)
  356. for(let i=0; i<libArr.length; i++){
  357. const lib = libArr[i]
  358. if(Library.validateRules(lib.rules, lib.natives)){
  359. if(lib.natives == null){
  360. const dlInfo = lib.downloads
  361. const artifact = dlInfo.artifact
  362. const to = path.join(this.libPath, artifact.path)
  363. libs.push(to)
  364. } else {
  365. // Extract the native library.
  366. const extractInst = lib.extract
  367. const exclusionArr = extractInst.exclude
  368. const artifact = lib.downloads.classifiers[lib.natives[Library.mojangFriendlyOS()].replace('${arch}', process.arch.replace('x', ''))]
  369. // Location of native zip.
  370. const to = path.join(this.libPath, artifact.path)
  371. let zip = new AdmZip(to)
  372. let zipEntries = zip.getEntries()
  373. // Unzip the native zip.
  374. for(let i=0; i<zipEntries.length; i++){
  375. const fileName = zipEntries[i].entryName
  376. let shouldExclude = false
  377. // Exclude noted files.
  378. exclusionArr.forEach(function(exclusion){
  379. if(fileName.indexOf(exclusion) > -1){
  380. shouldExclude = true
  381. }
  382. })
  383. // Extract the file.
  384. if(!shouldExclude){
  385. fs.writeFile(path.join(tempNativePath, fileName), zipEntries[i].getData(), (err) => {
  386. if(err){
  387. logger.error('Error while extracting native library:', err)
  388. }
  389. })
  390. }
  391. }
  392. }
  393. }
  394. }
  395. return libs
  396. }
  397. /**
  398. * Resolve the libraries declared by this server in order to add them to the classpath.
  399. * This method will also check each enabled mod for libraries, as mods are permitted to
  400. * declare libraries.
  401. *
  402. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  403. * @returns {Array.<string>} An array containing the paths of each library this server requires.
  404. */
  405. _resolveServerLibraries(mods){
  406. const mdls = this.server.getModules()
  407. let libs = []
  408. // Locate Forge/Libraries
  409. for(let mdl of mdls){
  410. const type = mdl.getType()
  411. if(type === DistroManager.Types.ForgeHosted || type === DistroManager.Types.Library){
  412. libs.push(mdl.getArtifact().getPath())
  413. if(mdl.hasSubModules()){
  414. const res = this._resolveModuleLibraries(mdl)
  415. if(res.length > 0){
  416. libs = libs.concat(res)
  417. }
  418. }
  419. }
  420. }
  421. //Check for any libraries in our mod list.
  422. for(let i=0; i<mods.length; i++){
  423. if(mods.sub_modules != null){
  424. const res = this._resolveModuleLibraries(mods[i])
  425. if(res.length > 0){
  426. libs = libs.concat(res)
  427. }
  428. }
  429. }
  430. return libs
  431. }
  432. /**
  433. * Recursively resolve the path of each library required by this module.
  434. *
  435. * @param {Object} mdl A module object from the server distro index.
  436. * @returns {Array.<string>} An array containing the paths of each library this module requires.
  437. */
  438. _resolveModuleLibraries(mdl){
  439. if(!mdl.hasSubModules()){
  440. return []
  441. }
  442. let libs = []
  443. for(let sm of mdl.getSubModules()){
  444. if(sm.getType() === DistroManager.Types.Library){
  445. libs.push(sm.getArtifact().getPath())
  446. }
  447. // If this module has submodules, we need to resolve the libraries for those.
  448. // To avoid unnecessary recursive calls, base case is checked here.
  449. if(mdl.hasSubModules()){
  450. const res = this._resolveModuleLibraries(sm)
  451. if(res.length > 0){
  452. libs = libs.concat(res)
  453. }
  454. }
  455. }
  456. return libs
  457. }
  458. }
  459. module.exports = ProcessBuilder