processbuilder.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 = ['-Xmx' + ConfigManager.getMaxRAM(),
  219. '-Xms' + ConfigManager.getMinRAM(),
  220. '-Djava.library.path=' + tempNativePath,
  221. '-cp',
  222. this.classpathArg(mods, tempNativePath).join(process.platform === 'win32' ? ';' : ':'),
  223. this.forgeData.mainClass]
  224. if(process.platform === 'darwin'){
  225. args.unshift('-Xdock:name=WesterosCraft')
  226. args.unshift('-Xdock:icon=' + path.join(__dirname, '..', 'images', 'minecraft.icns'))
  227. }
  228. args.splice(2, 0, ...ConfigManager.getJVMOptions())
  229. args = args.concat(this._resolveForgeArgs())
  230. return args
  231. }
  232. /**
  233. * Resolve the arguments required by forge.
  234. *
  235. * @returns {Array.<string>} An array containing the arguments required by forge.
  236. */
  237. _resolveForgeArgs(){
  238. const mcArgs = this.forgeData.minecraftArguments.split(' ')
  239. const argDiscovery = /\${*(.*)}/
  240. // Replace the declared variables with their proper values.
  241. for(let i=0; i<mcArgs.length; ++i){
  242. if(argDiscovery.test(mcArgs[i])){
  243. const identifier = mcArgs[i].match(argDiscovery)[1]
  244. let val = null
  245. switch(identifier){
  246. case 'auth_player_name':
  247. val = this.authUser.displayName.trim()
  248. break
  249. case 'version_name':
  250. //val = versionData.id
  251. val = this.server.getID()
  252. break
  253. case 'game_directory':
  254. val = this.gameDir
  255. break
  256. case 'assets_root':
  257. val = path.join(this.commonDir, 'assets')
  258. break
  259. case 'assets_index_name':
  260. val = this.versionData.assets
  261. break
  262. case 'auth_uuid':
  263. val = this.authUser.uuid.trim()
  264. break
  265. case 'auth_access_token':
  266. val = this.authUser.accessToken
  267. break
  268. case 'user_type':
  269. val = 'MOJANG'
  270. break
  271. case 'version_type':
  272. val = this.versionData.type
  273. break
  274. }
  275. if(val != null){
  276. mcArgs[i] = val
  277. }
  278. }
  279. }
  280. mcArgs.push('--modListFile')
  281. mcArgs.push('absolute:' + this.fmlDir)
  282. if(this.usingLiteLoader){
  283. mcArgs.push('--modRepo')
  284. mcArgs.push(this.llDir)
  285. mcArgs.unshift('com.mumfrey.liteloader.launch.LiteLoaderTweaker')
  286. mcArgs.unshift('--tweakClass')
  287. }
  288. // Prepare game resolution
  289. if(ConfigManager.getFullscreen()){
  290. mcArgs.unshift('--fullscreen')
  291. } else {
  292. mcArgs.unshift(ConfigManager.getGameWidth())
  293. mcArgs.unshift('--width')
  294. mcArgs.unshift(ConfigManager.getGameHeight())
  295. mcArgs.unshift('--height')
  296. }
  297. // Prepare autoconnect
  298. if(ConfigManager.getAutoConnect() && this.server.isAutoConnect()){
  299. const serverURL = new URL('my://' + this.server.getAddress())
  300. mcArgs.unshift(serverURL.hostname)
  301. mcArgs.unshift('--server')
  302. if(serverURL.port){
  303. mcArgs.unshift(serverURL.port)
  304. mcArgs.unshift('--port')
  305. }
  306. }
  307. return mcArgs
  308. }
  309. /**
  310. * Resolve the full classpath argument list for this process. This method will resolve all Mojang-declared
  311. * libraries as well as the libraries declared by the server. Since mods are permitted to declare libraries,
  312. * this method requires all enabled mods as an input
  313. *
  314. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  315. * @param {string} tempNativePath The path to store the native libraries.
  316. * @returns {Array.<string>} An array containing the paths of each library required by this process.
  317. */
  318. classpathArg(mods, tempNativePath){
  319. let cpArgs = []
  320. // Add the version.jar to the classpath.
  321. const version = this.versionData.id
  322. cpArgs.push(path.join(this.commonDir, 'versions', version, version + '.jar'))
  323. if(this.usingLiteLoader){
  324. cpArgs.push(this.llPath)
  325. }
  326. // Resolve the Mojang declared libraries.
  327. const mojangLibs = this._resolveMojangLibraries(tempNativePath)
  328. cpArgs = cpArgs.concat(mojangLibs)
  329. // Resolve the server declared libraries.
  330. const servLibs = this._resolveServerLibraries(mods)
  331. cpArgs = cpArgs.concat(servLibs)
  332. return cpArgs
  333. }
  334. /**
  335. * Resolve the libraries defined by Mojang's version data. This method will also extract
  336. * native libraries and point to the correct location for its classpath.
  337. *
  338. * TODO - clean up function
  339. *
  340. * @param {string} tempNativePath The path to store the native libraries.
  341. * @returns {Array.<string>} An array containing the paths of each library mojang declares.
  342. */
  343. _resolveMojangLibraries(tempNativePath){
  344. const libs = []
  345. const libArr = this.versionData.libraries
  346. fs.ensureDirSync(tempNativePath)
  347. for(let i=0; i<libArr.length; i++){
  348. const lib = libArr[i]
  349. if(Library.validateRules(lib.rules, lib.natives)){
  350. if(lib.natives == null){
  351. const dlInfo = lib.downloads
  352. const artifact = dlInfo.artifact
  353. const to = path.join(this.libPath, artifact.path)
  354. libs.push(to)
  355. } else {
  356. // Extract the native library.
  357. const extractInst = lib.extract
  358. const exclusionArr = extractInst.exclude
  359. const artifact = lib.downloads.classifiers[lib.natives[Library.mojangFriendlyOS()].replace('${arch}', process.arch.replace('x', ''))]
  360. // Location of native zip.
  361. const to = path.join(this.libPath, artifact.path)
  362. let zip = new AdmZip(to)
  363. let zipEntries = zip.getEntries()
  364. // Unzip the native zip.
  365. for(let i=0; i<zipEntries.length; i++){
  366. const fileName = zipEntries[i].entryName
  367. let shouldExclude = false
  368. // Exclude noted files.
  369. exclusionArr.forEach(function(exclusion){
  370. if(fileName.indexOf(exclusion) > -1){
  371. shouldExclude = true
  372. }
  373. })
  374. // Extract the file.
  375. if(!shouldExclude){
  376. fs.writeFile(path.join(tempNativePath, fileName), zipEntries[i].getData(), (err) => {
  377. if(err){
  378. logger.error('Error while extracting native library:', err)
  379. }
  380. })
  381. }
  382. }
  383. }
  384. }
  385. }
  386. return libs
  387. }
  388. /**
  389. * Resolve the libraries declared by this server in order to add them to the classpath.
  390. * This method will also check each enabled mod for libraries, as mods are permitted to
  391. * declare libraries.
  392. *
  393. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  394. * @returns {Array.<string>} An array containing the paths of each library this server requires.
  395. */
  396. _resolveServerLibraries(mods){
  397. const mdls = this.server.getModules()
  398. let libs = []
  399. // Locate Forge/Libraries
  400. for(let mdl of mdls){
  401. const type = mdl.getType()
  402. if(type === DistroManager.Types.ForgeHosted || type === DistroManager.Types.Library){
  403. libs.push(mdl.getArtifact().getPath())
  404. if(mdl.hasSubModules()){
  405. const res = this._resolveModuleLibraries(mdl)
  406. if(res.length > 0){
  407. libs = libs.concat(res)
  408. }
  409. }
  410. }
  411. }
  412. //Check for any libraries in our mod list.
  413. for(let i=0; i<mods.length; i++){
  414. if(mods.sub_modules != null){
  415. const res = this._resolveModuleLibraries(mods[i])
  416. if(res.length > 0){
  417. libs = libs.concat(res)
  418. }
  419. }
  420. }
  421. return libs
  422. }
  423. /**
  424. * Recursively resolve the path of each library required by this module.
  425. *
  426. * @param {Object} mdl A module object from the server distro index.
  427. * @returns {Array.<string>} An array containing the paths of each library this module requires.
  428. */
  429. _resolveModuleLibraries(mdl){
  430. if(!mdl.hasSubModules()){
  431. return []
  432. }
  433. let libs = []
  434. for(let sm of mdl.getSubModules()){
  435. if(sm.getType() === DistroManager.Types.Library){
  436. libs.push(sm.getArtifact().getPath())
  437. }
  438. // If this module has submodules, we need to resolve the libraries for those.
  439. // To avoid unnecessary recursive calls, base case is checked here.
  440. if(mdl.hasSubModules()){
  441. const res = this._resolveModuleLibraries(sm)
  442. if(res.length > 0){
  443. libs = libs.concat(res)
  444. }
  445. }
  446. }
  447. return libs
  448. }
  449. }
  450. module.exports = ProcessBuilder