processbuilder.js 18 KB

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