From 0e9195e5b1a80eb626fe4123f178cbbfa915a8d3 Mon Sep 17 00:00:00 2001 From: Pierce Date: Fri, 12 Apr 2019 19:26:39 -0400 Subject: [PATCH] download assets a tad faster! --- components/handler.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/components/handler.js b/components/handler.js index f913084..56e96dd 100644 --- a/components/handler.js +++ b/components/handler.js @@ -13,7 +13,6 @@ function downloadAsync (url, directory, name) { const _request = request(url); _request.on('error', function(error) { - console.log(error.message); resolve({ failed: true, asset: { @@ -88,7 +87,7 @@ module.exports.getAssets = function (directory, version) { const index = require(path.join(directory, 'assets', 'indexes',`${version.assetIndex.id}.json`)); - for(const asset in index.objects) { + const mainAssetsDownload = Object.keys(index.objects).map(async asset => { const hash = index.objects[asset].hash; const subhash = hash.substring(0,2); const assetDirectory = path.join(directory, 'assets', 'objects', subhash); @@ -98,16 +97,18 @@ module.exports.getAssets = function (directory, version) { if(download.failed) failed.push(download.asset); } - } + }); - // why do we have this? B/c sometimes minecraft's resource site times out! + await Promise.all(mainAssetsDownload); + + // why do we have this? B/c sometimes Minecraft's resource site times out! if(failed) { for (const fail of failed) await downloadAsync(fail.url, fail.directory, fail.name); } - // Seems taking it out of the initial download loop allows everything to be copied... + // Copy assets to legacy if it's an older Minecarft version. if(version.assets === "legacy" || version.assets === "pre-1.6") { - for(const asset in index.objects) { + const legacyCopy = Object.keys(index.objects).map(async asset => { const hash = index.objects[asset].hash; const subhash = hash.substring(0,2); const assetDirectory = path.join(directory, 'assets', 'objects', subhash); @@ -122,7 +123,9 @@ module.exports.getAssets = function (directory, version) { if (!fs.existsSync(path.join(directory, 'assets', 'legacy', asset))) { fs.copyFileSync(path.join(assetDirectory, hash), path.join(directory, 'assets', 'legacy', asset)) } - } + }); + + await Promise.all(legacyCopy); } resolve();