pimi-launcher-core/components/launcher.js

77 lines
2.8 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) {
2018-12-09 18:13:20 -05:00
if(!fs.existsSync(options.root)) fs.mkdirSync(options.root);
2018-10-29 18:13:58 -04:00
if(options.clientPackage) {
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);
2018-10-29 18:13:58 -04:00
const mcPath = path.join(directory, `${options.version.number}.jar`);
const nativePath = await handler.getNatives(options.root, versionFile, options.os);
if (!fs.existsSync(mcPath)) {
await handler.getJar(versionFile, options.version.number, directory);
}
2019-02-08 12:19:54 -05:00
let forge = null;
if(options.forge) {
forge = await handler.getForgeDependencies(options.root, versionFile, options.forge.path);
}
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`,
'-Xincgc'
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
2019-02-08 12:19:54 -05:00
let mainClass;
const classes = await handler.getClasses(options.root, versionFile);
2019-02-08 12:19:54 -05:00
const classPaths = [];
if(forge) {
classPaths.push(`${options.forge.path};${forge.paths.join(';')};${classes.join(';')};${mcPath}`);
mainClass = forge.forge.mainClass
} else {
classPaths.push('-cp');
classPaths.push(`${mcPath};${classes.join(";")}`);
classPaths.push(versionFile.mainClass);
}
2018-10-29 18:13:58 -04:00
// Download version's assets
await handler.getAssets(options.root, versionFile);
// Launch options
let launchOptions;
if(forge) {
launchOptions = await handler.getLaunchOptions(versionFile, forge.forge, options);
} else {
launchOptions = await handler.getLaunchOptions(versionFile, null, options);
}
2018-10-29 18:13:58 -04:00
2019-02-08 12:19:54 -05:00
// NOTE: Hacky way of setting up launch options, will rework this next update.
let launchArguments = args.concat(jvm, classPaths, launchOptions);
if(forge) launchArguments = `${jvm.join(' ')} -cp ${classPaths} ${mainClass} ${launchOptions.join(' ')}`.split(' ');
2018-10-29 18:13:58 -04:00
const minecraft = child.spawn(`java`, launchArguments);
2019-01-12 21:39:22 -05:00
event.emit('start', null);
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
};