processbuilder.js 14 KB

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