pimi-launcher-core/components/launcher.js

89 lines
4 KiB
JavaScript
Raw Normal View History

2018-10-29 18:13:58 -04:00
const child = require('child_process');
2019-01-12 21:39:22 -05:00
const event = require('./events');
2018-10-29 18:13:58 -04:00
const path = require('path');
const handler = require('./handler');
const fs = require('fs');
module.exports = async function (options) {
options.root = path.resolve(options.root);
2019-04-28 17:52:37 -04:00
if(!fs.existsSync(options.root)) {
event.emit('debug', '[MCLC]: Attempting to create root folder');
fs.mkdirSync(options.root);
}
2018-10-29 18:13:58 -04:00
if(options.clientPackage) {
2019-04-28 17:52:37 -04:00
event.emit('debug', `[MCLC]: Extracting client package to ${options.root}`);
await handler.extractPackage(options.root, options.clientPackage);
}
2018-10-29 18:13:58 -04:00
const directory = path.join(options.root, 'versions', options.version.number);
options.directory = directory;
const versionFile = await handler.getVersion(options.version.number, options.directory);
const mcPath = options.version.custom ? path.join(options.root, 'versions', options.version.custom , `${options.version.custom}.jar`):
path.join(directory, `${options.version.number}.jar`);
2018-10-29 18:13:58 -04:00
const nativePath = await handler.getNatives(options.root, versionFile, options.os);
if (!fs.existsSync(mcPath)) {
2019-04-28 17:52:37 -04:00
event.emit('debug', '[MCLC]: Attempting to download Minecraft version jar');
2018-10-29 18:13:58 -04:00
await handler.getJar(versionFile, options.version.number, directory);
}
2019-02-08 12:19:54 -05:00
let forge = null;
let custom = null;
2019-04-28 17:52:37 -04:00
if(options.forge) {
if(options.forge.path) process.emitWarning('\'options.forge.path\' is deprecated and will be removed. Use \'options.forge\' instead');
2019-04-28 17:52:37 -04:00
event.emit('debug', '[MCLC]: Detected Forge in options, getting dependencies');
forge = await handler.getForgeDependencies(options.root, versionFile, options.forge.path || options.forge);
2019-04-28 17:52:37 -04:00
}
if(options.version.custom) {
event.emit('debug', '[MCLC]: Detected custom in options, setting custom version file');
custom = require(path.join(options.root, 'versions', options.version.custom, `${options.version.custom}.json`));
}
2018-10-29 18:13:58 -04:00
const args = [];
2018-10-29 18:13:58 -04:00
// Jvm
let jvm = [
'-XX:-UseAdaptiveSizePolicy',
'-XX:-OmitStackTraceInFastThrow',
'-Dfml.ignorePatchDiscrepancies=true',
'-Dfml.ignoreInvalidMinecraftCertificates=true',
2019-02-08 12:19:54 -05:00
`-Djava.library.path=${nativePath}`,
`-Xmx${options.memory.max}M`,
`-Xms${options.memory.min}M`
2018-10-29 18:13:58 -04:00
];
jvm.push(await handler.getJVM(versionFile, options));
if(options.customArgs) jvm = jvm.concat(options.customArgs);
2018-10-29 18:13:58 -04:00
const classes = await handler.getClasses(options, versionFile);
let classPaths = ['-cp'];
const separator = options.os === "windows" ? ";" : ":";
2019-04-28 17:52:37 -04:00
event.emit('debug', `[MCLC]: Using ${separator} to separate class paths`);
2019-02-08 12:19:54 -05:00
if(forge) {
2019-04-28 17:52:37 -04:00
event.emit('debug', '[MCLC]: Setting Forge class paths');
classPaths.push(`${options.forge.path || options.forge}${separator}${forge.paths.join(separator)}${separator}${classes.join(separator)};${mcPath}`);
2019-02-08 18:37:05 -05:00
classPaths.push(forge.forge.mainClass)
2019-02-08 12:19:54 -05:00
} else {
classPaths.push(`${mcPath}${separator}${classes.join(separator)}`);
classPaths.push(versionFile.mainClass || custom.mainClass);
2019-02-08 12:19:54 -05:00
}
classPaths = await handler.cleanUp(classPaths);
2018-10-29 18:13:58 -04:00
// Download version's assets
2019-04-28 17:52:37 -04:00
event.emit('debug', '[MCLC]: Attempting to download assets');
2018-10-29 18:13:58 -04:00
await handler.getAssets(options.root, versionFile);
// Launch options. Thank you Lyrus for the reformat <3
const modification = forge ? forge.forge : null || custom ? custom : null;
const launchOptions = await handler.getLaunchOptions(versionFile, modification, options);
2018-10-29 18:13:58 -04:00
2019-02-08 18:37:05 -05:00
const launchArguments = args.concat(jvm, classPaths, launchOptions);
event.emit('arguments', launchArguments);
2019-04-28 17:52:37 -04:00
event.emit('debug', launchArguments.join(' '));
2018-10-29 18:13:58 -04:00
const minecraft = child.spawn(options.javaPath ? options.javaPath : 'java', launchArguments);
2019-01-12 21:39:22 -05:00
minecraft.stdout.on('data', (data) => event.emit('data', data));
minecraft.stderr.on('data', (data) => event.emit('error', data));
minecraft.on('close', (code) => event.emit('close', code));
2018-10-29 18:13:58 -04:00
};