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 {
|
2019-06-06 21:49:18 +03:00
|
|
|
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
|
|
|
|
2019-06-06 21:49:18 +03:00
|
|
|
async launch(options) {
|
|
|
|
this.options = options;
|
2019-05-23 01:28:48 +03:00
|
|
|
this.options.root = path.resolve(this.options.root);
|
2019-08-22 18:40:38 +03:00
|
|
|
|
|
|
|
// Simplified overrides so launcher devs can set the paths to what ever they want. see docs for variable names.
|
2019-08-13 04:06:54 +03:00
|
|
|
if(!this.options.overrides) this.options.overrides = { url: {} };
|
2019-08-22 18:40:38 +03:00
|
|
|
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/"
|
2019-07-23 02:29:36 +03:00
|
|
|
};
|
2019-06-06 21:49:18 +03:00
|
|
|
this.handler = new handler(this);
|
2019-08-20 21:28:56 +03:00
|
|
|
// Lets the events register. our magic switch!
|
|
|
|
await void(0);
|
|
|
|
|
|
|
|
this.emit('debug', `[MCLC]: MCLC version ${require(path.join(__dirname,'..', 'package.json')).version}`);
|
2019-08-26 23:32:32 +03:00
|
|
|
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-06-06 21:49:18 +03:00
|
|
|
|
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);
|
|
|
|
}
|
2018-12-01 21:26:15 +03:00
|
|
|
|
2019-05-23 01:28:48 +03:00
|
|
|
if(this.options.clientPackage) {
|
|
|
|
this.emit('debug', `[MCLC]: Extracting client package to ${this.options.root}`);
|
2019-10-10 20:21:23 +03:00
|
|
|
await this.handler.extractPackage();
|
2019-05-23 01:28:48 +03:00
|
|
|
}
|
2018-10-30 01:13:58 +03:00
|
|
|
|
2019-07-19 13:30:34 +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));
|
2019-07-19 13:30:34 +03:00
|
|
|
await this.handler.runInstaller(this.options.installer)
|
|
|
|
}
|
|
|
|
|
2019-08-20 21:28:56 +03:00
|
|
|
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();
|
2019-08-20 21:28:56 +03:00
|
|
|
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`):
|
2019-07-23 02:29:36 +03:00
|
|
|
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');
|
2019-07-19 13:30:34 +03:00
|
|
|
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
|
|
|
|
2019-08-05 17:30:06 +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'];
|
2019-06-08 21:21:15 +03:00
|
|
|
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');
|
2019-06-25 18:01:35 +03:00
|
|
|
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;
|
2019-07-19 13:30:34 +03:00
|
|
|
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);
|
2019-05-24 02:40:34 +03:00
|
|
|
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});
|
2019-08-20 21:28:56 +03: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')));
|
2019-05-23 01:28:48 +03:00
|
|
|
minecraft.on('close', (code) => this.emit('close', code));
|
2019-05-24 02:40:34 +03:00
|
|
|
|
2019-06-08 21:21:15 +03:00
|
|
|
return minecraft;
|
2019-05-23 01:28:48 +03:00
|
|
|
}
|
|
|
|
}
|
2018-10-30 01:13:58 +03:00
|
|
|
|
2019-06-06 21:49:18 +03:00
|
|
|
module.exports = MCLCore;
|