processbuilder.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. * The initial iteration of this file will not support optional submodules.
  3. * Support will be added down the line, only top-level modules will recieve optional support.
  4. */
  5. const AdmZip = require('adm-zip')
  6. const {AssetGuard, Library} = require('./assetguard.js')
  7. const child_process = require('child_process')
  8. const ConfigManager = require('./configmanager.js')
  9. const fs = require('fs')
  10. const mkpath = require('mkdirp')
  11. const path = require('path')
  12. const {URL} = require('url')
  13. class ProcessBuilder {
  14. constructor(gameDirectory, distroServer, versionData, forgeData, authUser){
  15. this.dir = gameDirectory
  16. this.server = distroServer
  17. this.versionData = versionData
  18. this.forgeData = forgeData
  19. this.authUser = authUser
  20. this.fmlDir = path.join(this.dir, 'versions', this.server.id + '.json')
  21. this.libPath = path.join(this.dir, 'libraries')
  22. }
  23. static shouldInclude(mdle){
  24. //If the module should be included by default
  25. return mdle.required == null || mdle.required.value == null || mdle.required.value === true || (mdle.required.value === false && (mdle.required.def == null || mdle.required.def === true))
  26. }
  27. /**
  28. * Convienence method to run the functions typically used to build a process.
  29. */
  30. build(){
  31. process.throwDeprecation = true
  32. const mods = this.resolveDefaultMods()
  33. this.constructFMLModList(mods, true)
  34. const args = this.constructJVMArguments(mods)
  35. console.log(args)
  36. const child = child_process.spawn(ConfigManager.getJavaExecutable(), args, {
  37. cwd: ConfigManager.getGameDirectory()
  38. })
  39. child.stdout.on('data', (data) => {
  40. console.log('Minecraft:', data.toString('utf8'))
  41. })
  42. child.stderr.on('data', (data) => {
  43. console.log('Minecraft:', data.toString('utf8'))
  44. })
  45. child.on('close', (code, signal) => {
  46. console.log('Exited with code', code)
  47. })
  48. return child
  49. }
  50. resolveDefaultMods(options = {type: 'forgemod'}){
  51. //Returns array of default forge mods to load.
  52. const mods = []
  53. const mdles = this.server.modules
  54. for(let i=0; i<mdles.length; ++i){
  55. if(mdles[i].type != null && mdles[i].type === options.type){
  56. if(ProcessBuilder.shouldInclude(mdles[i])){
  57. mods.push(mdles[i])
  58. }
  59. }
  60. }
  61. return mods
  62. }
  63. constructFMLModList(mods, save = false){
  64. const modList = {}
  65. modList.repositoryRoot = path.join(this.dir, 'modstore')
  66. const ids = []
  67. for(let i=0; i<mods.length; ++i){
  68. ids.push(mods[i].id)
  69. }
  70. modList.modRef = ids
  71. if(save){
  72. const json = JSON.stringify(modList, null, 4)
  73. fs.writeFileSync(this.fmlDir, json, 'UTF-8')
  74. }
  75. return modList
  76. }
  77. /**
  78. * Construct the argument array that will be passed to the JVM process.
  79. *
  80. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  81. * @returns {Array.<string>} An array containing the full JVM arguments for this process.
  82. */
  83. constructJVMArguments(mods){
  84. let args = ['-Xmx' + ConfigManager.getMaxRAM(),
  85. '-Xms' + ConfigManager.getMinRAM(),,
  86. '-Djava.library.path=' + path.join(this.dir, 'natives'),
  87. '-cp',
  88. this.classpathArg(mods).join(';'),
  89. this.forgeData.mainClass]
  90. // For some reason this will add an undefined value unless
  91. // the delete count is 1. I suspect this is unintended behavior
  92. // by the function.. need to keep an eye on this.
  93. args.splice(2, 1, ...ConfigManager.getJVMOptions())
  94. args = args.concat(this._resolveForgeArgs())
  95. return args
  96. }
  97. /**
  98. * Resolve the arguments required by forge.
  99. *
  100. * @returns {Array.<string>} An array containing the arguments required by forge.
  101. */
  102. _resolveForgeArgs(){
  103. const mcArgs = this.forgeData.minecraftArguments.split(' ')
  104. const argDiscovery = /\${*(.*)}/
  105. // Replace the declared variables with their proper values.
  106. for(let i=0; i<mcArgs.length; ++i){
  107. if(argDiscovery.test(mcArgs[i])){
  108. const identifier = mcArgs[i].match(argDiscovery)[1]
  109. let val = null;
  110. switch(identifier){
  111. case 'auth_player_name':
  112. val = this.authUser.displayName
  113. break
  114. case 'version_name':
  115. //val = versionData.id
  116. val = this.server.id
  117. break
  118. case 'game_directory':
  119. val = this.dir
  120. break
  121. case 'assets_root':
  122. val = path.join(this.dir, 'assets')
  123. break
  124. case 'assets_index_name':
  125. val = this.versionData.assets
  126. break
  127. case 'auth_uuid':
  128. val = this.authUser.uuid
  129. break
  130. case 'auth_access_token':
  131. val = this.authUser.accessToken
  132. break
  133. case 'user_type':
  134. val = 'MOJANG'
  135. break
  136. case 'version_type':
  137. val = this.versionData.type
  138. break
  139. }
  140. if(val != null){
  141. mcArgs[i] = val;
  142. }
  143. }
  144. }
  145. mcArgs.push('--modListFile')
  146. mcArgs.push('absolute:' + this.fmlDir)
  147. // Prepare game resolution
  148. if(ConfigManager.isFullscreen()){
  149. mcArgs.unshift('--fullscreen')
  150. } else {
  151. mcArgs.unshift(ConfigManager.getGameWidth())
  152. mcArgs.unshift('--width')
  153. mcArgs.unshift(ConfigManager.getGameHeight())
  154. mcArgs.unshift('--height')
  155. }
  156. // Prepare autoconnect
  157. if(ConfigManager.isAutoConnect() && this.server.autoconnect){
  158. const serverURL = new URL('my://' + this.server.server_ip)
  159. mcArgs.unshift(serverURL.hostname)
  160. mcArgs.unshift('--server')
  161. if(serverURL.port){
  162. mcArgs.unshift(serverURL.port)
  163. mcArgs.unshift('--port')
  164. }
  165. }
  166. return mcArgs
  167. }
  168. /**
  169. * Resolve the full classpath argument list for this process. This method will resolve all Mojang-declared
  170. * libraries as well as the libraries declared by the server. Since mods are permitted to declare libraries,
  171. * this method requires all enabled mods as an input
  172. *
  173. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  174. * @returns {Array.<string>} An array containing the paths of each library required by this process.
  175. */
  176. classpathArg(mods){
  177. let cpArgs = []
  178. // Add the version.jar to the classpath.
  179. const version = this.versionData.id
  180. cpArgs.push(path.join(this.dir, 'versions', version, version + '.jar'))
  181. // Resolve the Mojang declared libraries.
  182. const mojangLibs = this._resolveMojangLibraries()
  183. cpArgs = cpArgs.concat(mojangLibs)
  184. // Resolve the server declared libraries.
  185. const servLibs = this._resolveServerLibraries(mods)
  186. cpArgs = cpArgs.concat(servLibs)
  187. return cpArgs
  188. }
  189. /**
  190. * Resolve the libraries defined by Mojang's version data. This method will also extract
  191. * native libraries and point to the correct location for its classpath.
  192. *
  193. * TODO - clean up function
  194. *
  195. * @returns {Array.<string>} An array containing the paths of each library mojang declares.
  196. */
  197. _resolveMojangLibraries(){
  198. const libs = []
  199. const libArr = this.versionData.libraries
  200. const nativePath = path.join(this.dir, 'natives')
  201. for(let i=0; i<libArr.length; i++){
  202. const lib = libArr[i]
  203. if(Library.validateRules(lib.rules)){
  204. if(lib.natives == null){
  205. const dlInfo = lib.downloads
  206. const artifact = dlInfo.artifact
  207. const to = path.join(this.libPath, artifact.path)
  208. libs.push(to)
  209. } else {
  210. // Extract the native library.
  211. const natives = lib.natives
  212. const extractInst = lib.extract
  213. const exclusionArr = extractInst.exclude
  214. const opSys = Library.mojangFriendlyOS()
  215. const indexId = natives[opSys]
  216. const dlInfo = lib.downloads
  217. const classifiers = dlInfo.classifiers
  218. const artifact = classifiers[indexId]
  219. // Location of native zip.
  220. const to = path.join(this.libPath, artifact.path)
  221. let zip = new AdmZip(to)
  222. let zipEntries = zip.getEntries()
  223. // Unzip the native zip.
  224. for(let i=0; i<zipEntries.length; i++){
  225. const fileName = zipEntries[i].entryName
  226. let shouldExclude = false
  227. // Exclude noted files.
  228. exclusionArr.forEach(function(exclusion){
  229. if(fileName.indexOf(exclusion) > -1){
  230. shouldExclude = true
  231. }
  232. })
  233. // Extract the file.
  234. if(!shouldExclude){
  235. mkpath.sync(path.join(nativePath, fileName, '..'))
  236. fs.writeFile(path.join(nativePath, fileName), zipEntries[i].getData(), (err) => {
  237. if(err){
  238. console.error('Error while extracting native library:', err)
  239. }
  240. })
  241. }
  242. }
  243. libs.push(to)
  244. }
  245. }
  246. }
  247. return libs
  248. }
  249. /**
  250. * Resolve the libraries declared by this server in order to add them to the classpath.
  251. * This method will also check each enabled mod for libraries, as mods are permitted to
  252. * declare libraries.
  253. *
  254. * @param {Array.<Object>} mods An array of enabled mods which will be launched with this process.
  255. * @returns {Array.<string>} An array containing the paths of each library this server requires.
  256. */
  257. _resolveServerLibraries(mods){
  258. const mdles = this.server.modules
  259. let libs = []
  260. // Locate Forge/Libraries
  261. for(let i=0; i<mdles.length; i++){
  262. if(mdles[i].type != null && (mdles[i].type === 'forge-hosted' || mdles[i].type === 'library')){
  263. let lib = mdles[i]
  264. libs.push(path.join(this.libPath, lib.artifact.path == null ? AssetGuard._resolvePath(lib.id, lib.artifact.extension) : lib.artifact.path))
  265. if(lib.sub_modules != null){
  266. const res = this._resolveModuleLibraries(lib)
  267. if(res.length > 0){
  268. libs = libs.concat(res)
  269. }
  270. }
  271. }
  272. }
  273. //Check for any libraries in our mod list.
  274. for(let i=0; i<mods.length; i++){
  275. if(mods.sub_modules != null){
  276. const res = this._resolveModuleLibraries(mods[i])
  277. if(res.length > 0){
  278. libs = libs.concat(res)
  279. }
  280. }
  281. }
  282. return libs
  283. }
  284. /**
  285. * Recursively resolve the path of each library required by this module.
  286. *
  287. * @param {Object} mdle A module object from the server distro index.
  288. * @returns {Array.<string>} An array containing the paths of each library this module requires.
  289. */
  290. _resolveModuleLibraries(mdle){
  291. if(mdle.sub_modules == null){
  292. return []
  293. }
  294. let libs = []
  295. for(let i=0; i<mdle.sub_modules.length; i++){
  296. const sm = mdle.sub_modules[i]
  297. if(sm.type != null && sm.type == 'library'){
  298. libs.push(path.join(this.libPath, sm.artifact.path == null ? AssetGuard._resolvePath(sm.id, sm.artifact.extension) : sm.artifact.path))
  299. }
  300. // If this module has submodules, we need to resolve the libraries for those.
  301. // To avoid unnecessary recursive calls, base case is checked here.
  302. if(mdle.sub_modules != null){
  303. const res = this._resolveModuleLibraries(sm)
  304. if(res.length > 0){
  305. libs = libs.concat(res)
  306. }
  307. }
  308. }
  309. return libs
  310. }
  311. }
  312. module.exports = ProcessBuilder