Merge pull request #23 from Coding-Kiwi/master

Fixed wrong total value in downloadAsync
This commit is contained in:
Pierce 2019-08-28 10:00:55 -04:00 committed by GitHub
commit b1f59bf3ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -45,6 +45,13 @@ class Handler {
const _request = this.baseRequest(url); const _request = this.baseRequest(url);
let received_bytes = 0;
let total_bytes = 0;
_request.on('response', (data) => {
total_bytes = parseInt(data.headers['content-length']);
});
_request.on('error', async (error) => { _request.on('error', async (error) => {
this.client.emit('debug', `[MCLC]: Failed to download asset to ${path.join(directory, name)} due to\n${error}.` + this.client.emit('debug', `[MCLC]: Failed to download asset to ${path.join(directory, name)} due to\n${error}.` +
` Retrying... ${retry}`); ` Retrying... ${retry}`);
@ -53,12 +60,11 @@ class Handler {
}); });
_request.on('data', (data) => { _request.on('data', (data) => {
let size = 0; received_bytes += data.length;
if (fs.existsSync(path.join(directory, name))) size = fs.statSync(path.join(directory, name))["size"];
this.client.emit('download-status', { this.client.emit('download-status', {
"name": name, "name": name,
"current": Math.round(size / 10000), "current": received_bytes,
"total": data.length "total": total_bytes
}) })
}); });
@ -139,6 +145,12 @@ class Handler {
const index = require(path.join(this.options.root, 'assets', 'indexes',`${this.version.assetIndex.id}.json`)); 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 => { await Promise.all(Object.keys(index.objects).map(async asset => {
const hash = index.objects[asset].hash; const hash = index.objects[asset].hash;
const subhash = hash.substring(0,2); const subhash = hash.substring(0,2);
@ -161,6 +173,13 @@ class Handler {
if(this.version.assets === "legacy" || this.version.assets === "pre-1.6") { if(this.version.assets === "legacy" || this.version.assets === "pre-1.6") {
const assetDirectory = this.options.overrides.assetRoot || path.join(this.options.root, 'assets'); 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('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 => { await Promise.all(Object.keys(index.objects).map(async asset => {
const hash = index.objects[asset].hash; const hash = index.objects[asset].hash;
const subhash = hash.substring(0,2); const subhash = hash.substring(0,2);
@ -233,6 +252,13 @@ class Handler {
}) })
}; };
const stat = await natives(); const stat = await natives();
this.client.emit('progress', {
type: 'natives',
task: 0,
total: stat.length
});
await Promise.all(stat.map(async (native) => { await Promise.all(stat.map(async (native) => {
const name = native.path.split('/').pop(); const name = native.path.split('/').pop();
await this.downloadAsync(native.url, nativeDirectory, name); await this.downloadAsync(native.url, nativeDirectory, name);
@ -273,6 +299,12 @@ class Handler {
const forge = require(path.join(this.options.root, 'forge', `${this.version.id}`, 'version.json')); const forge = require(path.join(this.options.root, 'forge', `${this.version.id}`, 'version.json'));
const paths = []; const paths = [];
this.client.emit('progress', {
type: 'forge',
task: 0,
total: forge.libraries.length
});
await Promise.all(forge.libraries.map(async library => { await Promise.all(forge.libraries.map(async library => {
const lib = library.name.split(':'); const lib = library.name.split(':');
@ -305,7 +337,7 @@ class Handler {
paths.push(`${jarPath}${path.sep}${name}`); paths.push(`${jarPath}${path.sep}${name}`);
counter = counter + 1; counter = counter + 1;
this.client.emit('progress', { this.client.emit('progress', {
type: 'natives-forge', type: 'forge',
task: counter, task: counter,
total: forge.libraries.length total: forge.libraries.length
}) })
@ -330,6 +362,13 @@ class Handler {
if(this.options.version.custom) { if(this.options.version.custom) {
const customJarJson = require(path.join(this.options.root, 'versions', this.options.version.custom, `${this.options.version.custom}.json`)); 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 => { await Promise.all(customJarJson.libraries.map(async library => {
const lib = library.name.split(':'); const lib = library.name.split(':');
@ -366,6 +405,13 @@ class Handler {
}) })
}; };
const parsed = await parsedClasses(); const parsed = await parsedClasses();
this.client.emit('progress', {
type: 'classes',
task: 0,
total: parsed.length
});
await Promise.all(parsed.map(async (_lib) => { await Promise.all(parsed.map(async (_lib) => {
const libraryPath = _lib.downloads.artifact.path; const libraryPath = _lib.downloads.artifact.path;
const libraryUrl = _lib.downloads.artifact.url; const libraryUrl = _lib.downloads.artifact.url;