pimi-launcher-core/components/launcher.js

136 lines
6.2 KiB
JavaScript
Raw Normal View History

2018-10-30 01:13:58 +03:00
const child = require('child_process');
const path = require('path');
const handler = require('./handler');
const fs = require('fs');
2019-05-23 01:28:48 +03:00
const EventEmitter = require('events').EventEmitter;
2018-10-30 01:13:58 +03:00
2019-05-23 01:28:48 +03:00
class MCLCore extends EventEmitter {
constructor() {
2019-05-23 01:28:48 +03:00
super();
2019-04-29 00:52:37 +03:00
}
2018-10-30 01:13:58 +03:00
async launch(options) {
this.options = options;
2019-05-23 01:28:48 +03:00
this.options.root = path.resolve(this.options.root);
// Simplified overrides so launcher devs can set the paths to what ever they want. see docs for variable names.
if(!this.options.overrides) this.options.overrides = { url: {} };
if(!this.options.overrides.url) this.options.overrides.url = {};
this.options.overrides.url = {
meta: this.options.overrides.url.meta || "https://launchermeta.mojang.com",
resource: this.options.overrides.url.resource || "https://resources.download.minecraft.net",
mavenForge: this.options.overrides.url.mavenForge || "http://files.minecraftforge.net/maven/",
defaultRepoForge: this.options.overrides.url.defaultRepoForge || "https://libraries.minecraft.net/"
};
this.handler = new handler(this);
// Lets the events register. our magic switch!
await void(0);
this.emit('debug', `[MCLC]: MCLC version ${require(path.join(__dirname,'..', 'package.json')).version}`);
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;
}
2019-05-23 01:28:48 +03:00
if(!fs.existsSync(this.options.root)) {
this.emit('debug', '[MCLC]: Attempting to create root folder');
fs.mkdirSync(this.options.root);
}
2019-05-23 01:28:48 +03:00
if(this.options.clientPackage) {
this.emit('debug', `[MCLC]: Extracting client package to ${this.options.root}`);
await this.handler.extractPackage();
2019-05-23 01:28:48 +03:00
}
2018-10-30 01:13:58 +03:00
if(this.options.installer) {
// So the forge installer can run without breaking :)
2019-07-28 19:52:53 +03:00
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);
2019-05-23 01:28:48 +03:00
this.options.directory = directory;
2018-10-30 01:13:58 +03:00
2019-05-23 01:28:48 +03:00
// Version JSON for the main launcher folder
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`));
2019-05-23 01:28:48 +03:00
const nativePath = await this.handler.getNatives();
2018-10-30 01:13:58 +03:00
2019-05-23 01:28:48 +03: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.emit('debug', '[MCLC]: Detected Forge in options, getting dependencies');
forge = await this.handler.getForgeDependenciesLegacy();
2019-05-23 01:28:48 +03:00
}
if(this.options.version.custom) {
this.emit('debug', '[MCLC]: Detected custom in options, setting custom version file');
custom = require(path.join(this.options.root, 'versions', this.options.version.custom, `${this.options.version.custom}.json`));
}
2018-10-30 01:13:58 +03:00
2019-05-23 01:28:48 +03:00
const args = [];
2018-10-30 01:13:58 +03:00
2019-05-23 01:28:48 +03:00
// Jvm
let jvm = [
'-XX:-UseAdaptiveSizePolicy',
'-XX:-OmitStackTraceInFastThrow',
'-Dfml.ignorePatchDiscrepancies=true',
'-Dfml.ignoreInvalidMinecraftCertificates=true',
`-Djava.library.path=${nativePath}`,
`-Xmx${this.options.memory.max}M`,
`-Xms${this.options.memory.min}M`
];
2019-07-23 23:27:33 +03: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());
2019-05-23 01:28:48 +03:00
if(this.options.customArgs) jvm = jvm.concat(this.options.customArgs);
2018-10-30 01:13:58 +03:00
const classes = this.options.overrides.classes || await handler.cleanUp(await this.handler.getClasses());
2019-05-23 01:28:48 +03:00
let classPaths = ['-cp'];
const separator = this.handler.getOS() === "windows" ? ";" : ":";
2019-05-23 01:28:48 +03:00
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}`);
2019-05-23 01:28:48 +03:00
classPaths.push(forge.forge.mainClass)
} else {
const file = custom || versionFile;
const jar = fs.existsSync(mcPath) ? `${mcPath}${separator}` : '';
classPaths.push(`${jar}${classes.join(separator)}`);
2019-05-23 01:28:48 +03:00
classPaths.push(file.mainClass);
}
// Download version's assets
this.emit('debug', '[MCLC]: Attempting to download assets');
await this.handler.getAssets();
// Launch options. Thank you Lyrus for the reformat <3
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', launchArguments.join(' '));
2019-05-23 01:28:48 +03:00
2019-08-05 19:54:57 +03:00
const minecraft = child.spawn(this.options.javaPath ? this.options.javaPath : 'java', launchArguments,
{cwd: this.options.overrides.cwd || this.options.root});
minecraft.stdout.on('data', (data) => this.emit('data', data.toString('utf-8')));
minecraft.stderr.on('data', (data) => this.emit('data', data.toString('utf-8')));
2019-05-23 01:28:48 +03:00
minecraft.on('close', (code) => this.emit('close', code));
return minecraft;
2019-05-23 01:28:48 +03:00
}
}
2018-10-30 01:13:58 +03:00
module.exports = MCLCore;