download assets a tad faster!

This commit is contained in:
Pierce 2019-04-12 19:26:39 -04:00
parent dd8b80d69d
commit 0e9195e5b1

View file

@ -13,7 +13,6 @@ function downloadAsync (url, directory, name) {
const _request = request(url); const _request = request(url);
_request.on('error', function(error) { _request.on('error', function(error) {
console.log(error.message);
resolve({ resolve({
failed: true, failed: true,
asset: { asset: {
@ -88,7 +87,7 @@ module.exports.getAssets = function (directory, version) {
const index = require(path.join(directory, 'assets', 'indexes',`${version.assetIndex.id}.json`)); 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 hash = index.objects[asset].hash;
const subhash = hash.substring(0,2); const subhash = hash.substring(0,2);
const assetDirectory = path.join(directory, 'assets', 'objects', subhash); 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); 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) { if(failed) {
for (const fail of failed) await downloadAsync(fail.url, fail.directory, fail.name); 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") { 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 hash = index.objects[asset].hash;
const subhash = hash.substring(0,2); const subhash = hash.substring(0,2);
const assetDirectory = path.join(directory, 'assets', 'objects', subhash); 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))) { if (!fs.existsSync(path.join(directory, 'assets', 'legacy', asset))) {
fs.copyFileSync(path.join(assetDirectory, hash), path.join(directory, 'assets', 'legacy', asset)) fs.copyFileSync(path.join(assetDirectory, hash), path.join(directory, 'assets', 'legacy', asset))
} }
} });
await Promise.all(legacyCopy);
} }
resolve(); resolve();