processbuilder.js 12 KB

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