From 926060e79c68d45377decf02d5282a8a3091c1dc Mon Sep 17 00:00:00 2001 From: Kev Date: Sun, 31 Jan 2021 23:09:37 +0100 Subject: [PATCH 1/2] Split large MCLCore#launch into multiple methods --- components/launcher.js | 83 ++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 28 deletions(-) diff --git a/components/launcher.js b/components/launcher.js index 50cb727..b2f7fd6 100644 --- a/components/launcher.js +++ b/components/launcher.js @@ -31,9 +31,8 @@ class MCLCore extends EventEmitter { 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.') } + this.printVersion() + 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}`) @@ -41,22 +40,10 @@ class MCLCore extends EventEmitter { return null } - if (!fs.existsSync(this.options.root)) { - this.emit('debug', '[MCLC]: Attempting to create root folder') - fs.mkdirSync(this.options.root) - } + this.createRootDirectory() + this.createGameDirectory() - 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 }) - } - } - - if (this.options.clientPackage) { - this.emit('debug', `[MCLC]: Extracting client package to ${this.options.root}`) - await this.handler.extractPackage() - } + await this.extractPackage() if (this.options.installer) { // So installers that create a profile in launcher_profiles.json can run without breaking. @@ -80,15 +67,7 @@ class MCLCore extends EventEmitter { await this.handler.getJar() } - let modifyJson = null - if (this.options.forge) { - this.options.forge = path.resolve(this.options.forge) - this.emit('debug', '[MCLC]: Detected Forge in options, getting dependencies') - modifyJson = await this.handler.getForgedWrapped() - } else if (this.options.version.custom) { - this.emit('debug', '[MCLC]: Detected custom in options, setting custom version file') - modifyJson = modifyJson || JSON.parse(fs.readFileSync(path.join(this.options.root, 'versions', this.options.version.custom, `${this.options.version.custom}.json`), { encoding: 'utf8' })) - } + const modifyJson = await this.getModifyJson() const args = [] @@ -130,12 +109,60 @@ class MCLCore extends EventEmitter { this.emit('arguments', launchArguments) this.emit('debug', `[MCLC]: Launching with arguments ${launchArguments.join(' ')}`) + return this.startMinecraft(launchArguments) + } + + printVersion () { + if (fs.existsSync(path.join(__dirname, '..', 'package.json'))) { + const { version } = require('../package.json') + this.emit('debug', `[MCLC]: MCLC version ${version}`) + } else { this.emit('debug', '[MCLC]: Package JSON not found, skipping MCLC version check.') } + } + + createRootDirectory () { + if (!fs.existsSync(this.options.root)) { + this.emit('debug', '[MCLC]: Attempting to create root folder') + fs.mkdirSync(this.options.root) + } + } + + createGameDirectory () { + 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 }) + } + } + } + + async extractPackage () { + if (this.options.clientPackage) { + this.emit('debug', `[MCLC]: Extracting client package to ${this.options.root}`) + await this.handler.extractPackage() + } + } + + async getModifyJson () { + let modifyJson = null + + if (this.options.forge) { + this.options.forge = path.resolve(this.options.forge) + this.emit('debug', '[MCLC]: Detected Forge in options, getting dependencies') + modifyJson = await this.handler.getForgedWrapped() + } else if (this.options.version.custom) { + this.emit('debug', '[MCLC]: Detected custom in options, setting custom version file') + modifyJson = modifyJson || JSON.parse(fs.readFileSync(path.join(this.options.root, 'versions', this.options.version.custom, `${this.options.version.custom}.json`), { encoding: 'utf8' })) + } + + return modifyJson + } + + startMinecraft (launchArguments) { const minecraft = child.spawn(this.options.javaPath ? this.options.javaPath : 'java', launchArguments, { cwd: this.options.overrides.cwd || this.options.root, detached: this.options.overrides.detached }) 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 } } From afe4c4d7a42b98a8cbf7e6c0290695a0f39eabba Mon Sep 17 00:00:00 2001 From: Kev Date: Wed, 10 Feb 2021 15:04:25 +0100 Subject: [PATCH 2/2] Add launcher methods to index.d.ts --- index.d.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/index.d.ts b/index.d.ts index 652e293..3353a8e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -206,6 +206,12 @@ declare module "minecraft-launcher-core" { export class Client extends EventEmitter { launch(options: ILauncherOptions): ChildProcessWithoutNullStreams | null; + protected printVersion(): void; + protected createRootDirectory(): void; + protected createGameDirectory(): void; + protected async extractPackage(): void; + protected async getModifyJson(): any; + protected startMinecraft(launchArguments: string[]): ChildProcessWithoutNullStreams; } export const Authenticator: IAuthenticator;