pimi-launcher-core/components/launcher.js

151 lines
6.4 KiB
JavaScript
Raw Normal View History

2020-05-28 22:39:13 -04:00
const child = require('child_process')
const path = require('path')
const Handler = require('./handler')
const fs = require('fs')
const EventEmitter = require('events').EventEmitter
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
class MCLCore extends EventEmitter {
2020-06-19 12:26:58 -04:00
async launch (options) {
2020-05-28 22:39:13 -04:00
this.options = options
this.options.root = path.resolve(this.options.root)
this.options.overrides = {
2020-06-19 22:21:00 -04:00
detached: true,
2020-05-28 22:39:13 -04:00
...this.options.overrides,
url: {
meta: 'https://launchermeta.mojang.com',
resource: 'https://resources.download.minecraft.net',
mavenForge: 'http://files.minecraftforge.net/maven/',
defaultRepoForge: 'https://libraries.minecraft.net/',
fallbackMaven: 'https://search.maven.org/remotecontent?filepath=',
...this.options.overrides
? this.options.overrides.url
: undefined
}
2019-04-28 17:52:37 -04:00
}
2020-05-28 22:39:13 -04:00
// ForgeWrapper fork that is maintained on a side repo (https://github.com/Pierce01/ForgeWrapper)
this.options.forgeWrapper = {
jar: path.join(__dirname, 'fw.jar'),
version: '1.4.1-mclc'
}
this.handler = new Handler(this)
if (fs.existsSync(path.join(__dirname, '..', 'package.json'))) {
this.emit('debug', `[MCLC]: MCLC version ${JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json'), { encoding: 'utf8' })).version}`)
} else { this.emit('debug', '[MCLC]: Package JSON not found, skipping MCLC version check.') }
const java = await this.handler.checkJava(this.options.javaPath || 'java')
if (!java.run) {
this.emit('debug', `[MCLC]: Couldn't start Minecraft due to: ${java.message}`)
this.emit('close', 1)
return null
}
if (!fs.existsSync(this.options.root)) {
this.emit('debug', '[MCLC]: Attempting to create root folder')
fs.mkdirSync(this.options.root)
}
if (this.options.overrides.gameDirectory) {
this.options.overrides.gameDirectory = path.resolve(this.options.overrides.gameDirectory)
if (!fs.existsSync(this.options.overrides.gameDirectory)) {
fs.mkdirSync(this.options.overrides.gameDirectory, { recursive: true })
}
}
2020-05-28 22:39:13 -04:00
if (this.options.clientPackage) {
this.emit('debug', `[MCLC]: Extracting client package to ${this.options.root}`)
await this.handler.extractPackage()
}
if (this.options.installer) {
// So the forge installer can run without breaking :)
const profilePath = path.join(this.options.root, 'launcher_profiles.json')
if (!fs.existsSync(profilePath)) { fs.writeFileSync(profilePath, JSON.stringify({}, null, 4)) }
await this.handler.runInstaller(this.options.installer)
}
const directory = this.options.overrides.directory || path.join(this.options.root, 'versions', this.options.version.number)
this.options.directory = directory
const versionFile = await this.handler.getVersion()
const mcPath = this.options.overrides.minecraftJar || (this.options.version.custom
? path.join(this.options.root, 'versions', this.options.version.custom, `${this.options.version.custom}.jar`)
: path.join(directory, `${this.options.version.number}.jar`))
const nativePath = await this.handler.getNatives()
2018-10-29 18:13:58 -04:00
2020-05-28 22:39:13 -04:00
if (!fs.existsSync(mcPath)) {
this.emit('debug', '[MCLC]: Attempting to download Minecraft version jar')
await this.handler.getJar()
}
let forge = null
let custom = null
if (this.options.forge) {
this.options.forge = path.resolve(this.options.forge)
this.emit('debug', '[MCLC]: Detected Forge in options, getting dependencies')
forge = await this.handler.getForgeDependenciesLegacy()
if (forge === false) custom = await this.handler.getForgedWrapped()
}
if (this.options.version.custom || custom) {
2020-06-19 12:26:58 -04:00
if (!custom) this.emit('debug', '[MCLC]: Detected custom in options, setting custom version file')
2020-05-28 22:39:13 -04:00
custom = custom || JSON.parse(fs.readFileSync(path.join(this.options.root, 'versions', this.options.version.custom, `${this.options.version.custom}.json`), { encoding: 'utf8' }))
}
const args = []
let jvm = [
'-XX:-UseAdaptiveSizePolicy',
'-XX:-OmitStackTraceInFastThrow',
'-Dfml.ignorePatchDiscrepancies=true',
'-Dfml.ignoreInvalidMinecraftCertificates=true',
2020-06-19 12:26:58 -04:00
`-Djava.library.path=${nativePath}`,
2020-08-23 23:57:28 -04:00
`-Xmx${this.handler.getMemory()[0]}`,
`-Xms${this.handler.getMemory()[1]}`
2020-05-28 22:39:13 -04:00
]
if (this.handler.getOS() === 'osx') {
if (parseInt(versionFile.id.split('.')[1]) > 12) jvm.push(await this.handler.getJVM())
} else jvm.push(await this.handler.getJVM())
if (this.options.customArgs) jvm = jvm.concat(this.options.customArgs)
2020-07-07 23:36:31 -04:00
const classes = this.options.overrides.classes || this.handler.cleanUp(await this.handler.getClasses(custom))
2020-05-28 22:39:13 -04:00
const classPaths = ['-cp']
const separator = this.handler.getOS() === 'windows' ? ';' : ':'
this.emit('debug', `[MCLC]: Using ${separator} to separate class paths`)
if (forge) {
this.emit('debug', '[MCLC]: Setting Forge class paths')
classPaths.push(`${path.resolve(this.options.forge)}${separator}${forge.paths.join(separator)}${separator}${classes.join(separator)}${separator}${mcPath}`)
classPaths.push(forge.forge.mainClass)
} else {
const file = custom || versionFile
// So mods like fabric work.
const jar = fs.existsSync(mcPath)
? `${separator}${mcPath}`
: `${separator}${path.join(directory, `${this.options.version.number}.jar`)}`
classPaths.push(`${classes.join(separator)}${jar}`)
classPaths.push(file.mainClass)
2019-05-22 18:28:48 -04:00
}
2020-05-28 22:39:13 -04:00
this.emit('debug', '[MCLC]: Attempting to download assets')
await this.handler.getAssets()
2020-07-07 23:36:31 -04:00
// Forge -> Custom -> Vanilla
2020-05-28 22:39:13 -04:00
const modification = forge ? forge.forge : null || custom ? custom : null
const launchOptions = await this.handler.getLaunchOptions(modification)
const launchArguments = args.concat(jvm, classPaths, launchOptions)
this.emit('arguments', launchArguments)
this.emit('debug', `[MCLC]: Launching with arguments ${launchArguments.join(' ')}`)
const minecraft = child.spawn(this.options.javaPath ? this.options.javaPath : 'java', launchArguments,
2020-06-19 22:21:00 -04:00
{ cwd: this.options.overrides.cwd || this.options.root, detached: this.options.overrides.detached })
2020-05-28 22:39:13 -04:00
minecraft.stdout.on('data', (data) => this.emit('data', data.toString('utf-8')))
minecraft.stderr.on('data', (data) => this.emit('data', data.toString('utf-8')))
minecraft.on('close', (code) => this.emit('close', code))
return minecraft
}
2019-05-22 18:28:48 -04:00
}
2018-10-29 18:13:58 -04:00
2020-05-28 22:39:13 -04:00
module.exports = MCLCore