processbuilder.js 14 KB

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