From ffcfe93482a6323a1cb74816370147d2fe247872 Mon Sep 17 00:00:00 2001 From: CodingKiwi Date: Wed, 28 Aug 2019 15:08:59 +0200 Subject: [PATCH 1/3] Fixed wrong total value in downloadAsync --- components/handler.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/components/handler.js b/components/handler.js index 3977d29..0bc51b6 100644 --- a/components/handler.js +++ b/components/handler.js @@ -45,6 +45,13 @@ class Handler { const _request = this.baseRequest(url); + var received_bytes = 0; + var total_bytes = 0; + + _request.on('response', function (data) { + total_bytes = parseInt(data.headers['content-length']); + }); + _request.on('error', async (error) => { this.client.emit('debug', `[MCLC]: Failed to download asset to ${path.join(directory, name)} due to\n${error}.` + ` Retrying... ${retry}`); @@ -53,12 +60,11 @@ class Handler { }); _request.on('data', (data) => { - let size = 0; - if (fs.existsSync(path.join(directory, name))) size = fs.statSync(path.join(directory, name))["size"]; + received_bytes += data.length; this.client.emit('download-status', { "name": name, - "current": Math.round(size / 10000), - "total": data.length + "current": received_bytes, + "total": total_bytes }) }); @@ -417,7 +423,9 @@ class Handler { const minArgs = this.options.overrides.minArgs || 5; if(args.length < minArgs) args = args.concat(this.version.minecraftArguments ? this.version.minecraftArguments.split(' ') : this.version.arguments.game); - this.options.authorization = await Promise.resolve(this.options.authorization); + if({}.toString.call(this.options.authorization) === "[object Promise]") { + this.options.authorization = await this.options.authorization; + } const fields = { '${auth_access_token}': this.options.authorization.access_token, From 874ed6eedc87574bdf4a8df5eefae2869190ecd7 Mon Sep 17 00:00:00 2001 From: CodingKiwi Date: Wed, 28 Aug 2019 15:47:04 +0200 Subject: [PATCH 2/3] Added progress events for 0% --- components/handler.js | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/components/handler.js b/components/handler.js index 0bc51b6..842dd34 100644 --- a/components/handler.js +++ b/components/handler.js @@ -145,6 +145,12 @@ class Handler { const index = require(path.join(this.options.root, 'assets', 'indexes',`${this.version.assetIndex.id}.json`)); + this.client.emit('progress', { + type: 'assets', + task: 0, + total: Object.keys(index.objects).length + }); + await Promise.all(Object.keys(index.objects).map(async asset => { const hash = index.objects[asset].hash; const subhash = hash.substring(0,2); @@ -167,6 +173,13 @@ class Handler { if(this.version.assets === "legacy" || this.version.assets === "pre-1.6") { const assetDirectory = this.options.overrides.assetRoot || path.join(this.options.root, 'assets'); this.client.emit('debug', `[MCLC]: Copying assets over to ${path.join(assetDirectory, 'legacy')}`); + + this.client.emit('progress', { + type: 'assets-copy', + task: 0, + total: Object.keys(index.objects).length + }); + await Promise.all(Object.keys(index.objects).map(async asset => { const hash = index.objects[asset].hash; const subhash = hash.substring(0,2); @@ -239,6 +252,13 @@ class Handler { }) }; const stat = await natives(); + + this.client.emit('progress', { + type: 'natives', + task: 0, + total: stat.length + }); + await Promise.all(stat.map(async (native) => { const name = native.path.split('/').pop(); await this.downloadAsync(native.url, nativeDirectory, name); @@ -279,6 +299,12 @@ class Handler { const forge = require(path.join(this.options.root, 'forge', `${this.version.id}`, 'version.json')); const paths = []; + this.client.emit('progress', { + type: 'forge', + task: 0, + total: forge.libraries.length + }); + await Promise.all(forge.libraries.map(async library => { const lib = library.name.split(':'); @@ -311,7 +337,7 @@ class Handler { paths.push(`${jarPath}${path.sep}${name}`); counter = counter + 1; this.client.emit('progress', { - type: 'natives-forge', + type: 'forge', task: counter, total: forge.libraries.length }) @@ -336,6 +362,13 @@ class Handler { if(this.options.version.custom) { const customJarJson = require(path.join(this.options.root, 'versions', this.options.version.custom, `${this.options.version.custom}.json`)); + + this.client.emit('progress', { + type: 'classes-custom', + task: 0, + total: customJarJson.libraries.length + }); + await Promise.all(customJarJson.libraries.map(async library => { const lib = library.name.split(':'); @@ -372,6 +405,13 @@ class Handler { }) }; const parsed = await parsedClasses(); + + this.client.emit('progress', { + type: 'classes', + task: 0, + total: parsed.length + }); + await Promise.all(parsed.map(async (_lib) => { const libraryPath = _lib.downloads.artifact.path; const libraryUrl = _lib.downloads.artifact.url; From 4815924279680f0e5d84ef0c6e5b5e13793df3a2 Mon Sep 17 00:00:00 2001 From: CodingKiwi Date: Wed, 28 Aug 2019 15:50:31 +0200 Subject: [PATCH 3/3] Changed var to let, updated promise check --- components/handler.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/components/handler.js b/components/handler.js index 842dd34..248e963 100644 --- a/components/handler.js +++ b/components/handler.js @@ -45,10 +45,10 @@ class Handler { const _request = this.baseRequest(url); - var received_bytes = 0; - var total_bytes = 0; + let received_bytes = 0; + let total_bytes = 0; - _request.on('response', function (data) { + _request.on('response', (data) => { total_bytes = parseInt(data.headers['content-length']); }); @@ -463,9 +463,7 @@ class Handler { const minArgs = this.options.overrides.minArgs || 5; if(args.length < minArgs) args = args.concat(this.version.minecraftArguments ? this.version.minecraftArguments.split(' ') : this.version.arguments.game); - if({}.toString.call(this.options.authorization) === "[object Promise]") { - this.options.authorization = await this.options.authorization; - } + this.options.authorization = await Promise.resolve(this.options.authorization); const fields = { '${auth_access_token}': this.options.authorization.access_token,